SysProfileController.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package com.ruoyi.web.controller.system;
  2. import org.apache.shiro.crypto.hash.Md5Hash;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.ui.ModelMap;
  8. import org.springframework.web.bind.annotation.GetMapping;
  9. import org.springframework.web.bind.annotation.PathVariable;
  10. import org.springframework.web.bind.annotation.PostMapping;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RequestParam;
  13. import org.springframework.web.bind.annotation.ResponseBody;
  14. import org.springframework.web.multipart.MultipartFile;
  15. import com.ruoyi.common.annotation.Log;
  16. import com.ruoyi.common.base.AjaxResult;
  17. import com.ruoyi.common.config.Global;
  18. import com.ruoyi.common.enums.BusinessType;
  19. import com.ruoyi.framework.util.FileUploadUtils;
  20. import com.ruoyi.system.domain.SysUser;
  21. import com.ruoyi.system.service.ISysDictDataService;
  22. import com.ruoyi.system.service.ISysUserService;
  23. import com.ruoyi.web.core.base.BaseController;
  24. /**
  25. * 个人信息 业务处理
  26. *
  27. * @author ruoyi
  28. */
  29. @Controller
  30. @RequestMapping("/system/user/profile")
  31. public class SysProfileController extends BaseController
  32. {
  33. private static final Logger log = LoggerFactory.getLogger(SysProfileController.class);
  34. private String prefix = "system/user/profile";
  35. @Autowired
  36. private ISysUserService userService;
  37. @Autowired
  38. private ISysDictDataService dictDataService;
  39. /**
  40. * 个人信息
  41. */
  42. @GetMapping()
  43. public String profile(ModelMap mmap)
  44. {
  45. SysUser user = getUser();
  46. user.setSex(dictDataService.selectDictLabel("sys_user_sex", user.getSex()));
  47. mmap.put("user", user);
  48. mmap.put("roleGroup", userService.selectUserRoleGroup(user.getUserId()));
  49. mmap.put("postGroup", userService.selectUserPostGroup(user.getUserId()));
  50. return prefix + "/profile";
  51. }
  52. @GetMapping("/checkPassword")
  53. @ResponseBody
  54. public boolean checkPassword(String password)
  55. {
  56. SysUser user = getUser();
  57. String encrypt = new Md5Hash(user.getLoginName() + password + user.getSalt()).toHex().toString();
  58. if (user.getPassword().equals(encrypt))
  59. {
  60. return true;
  61. }
  62. return false;
  63. }
  64. @GetMapping("/resetPwd/{userId}")
  65. public String resetPwd(@PathVariable("userId") Long userId, ModelMap mmap)
  66. {
  67. mmap.put("user", userService.selectUserById(userId));
  68. return prefix + "/resetPwd";
  69. }
  70. @Log(title = "重置密码", businessType = BusinessType.UPDATE)
  71. @PostMapping("/resetPwd")
  72. @ResponseBody
  73. public AjaxResult resetPwd(SysUser user)
  74. {
  75. int rows = userService.resetUserPwd(user);
  76. if (rows > 0)
  77. {
  78. setUser(userService.selectUserById(user.getUserId()));
  79. return success();
  80. }
  81. return error();
  82. }
  83. /**
  84. * 修改用户
  85. */
  86. @GetMapping("/edit/{userId}")
  87. public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
  88. {
  89. mmap.put("user", userService.selectUserById(userId));
  90. return prefix + "/edit";
  91. }
  92. /**
  93. * 修改头像
  94. */
  95. @GetMapping("/avatar/{userId}")
  96. public String avatar(@PathVariable("userId") Long userId, ModelMap mmap)
  97. {
  98. mmap.put("user", userService.selectUserById(userId));
  99. return prefix + "/avatar";
  100. }
  101. /**
  102. * 修改用户
  103. */
  104. @Log(title = "个人信息", businessType = BusinessType.UPDATE)
  105. @PostMapping("/update")
  106. @ResponseBody
  107. public AjaxResult update(SysUser user)
  108. {
  109. if (userService.updateUserInfo(user) > 0)
  110. {
  111. setUser(userService.selectUserById(user.getUserId()));
  112. return success();
  113. }
  114. return error();
  115. }
  116. /**
  117. * 保存头像
  118. */
  119. @Log(title = "个人信息", businessType = BusinessType.UPDATE)
  120. @PostMapping("/updateAvatar")
  121. @ResponseBody
  122. public AjaxResult updateAvatar(SysUser user, @RequestParam("avatarfile") MultipartFile file)
  123. {
  124. try
  125. {
  126. if (!file.isEmpty())
  127. {
  128. String avatar = FileUploadUtils.upload(Global.getAvatarPath(), file);
  129. user.setAvatar(avatar);
  130. if (userService.updateUserInfo(user) > 0)
  131. {
  132. setUser(userService.selectUserById(user.getUserId()));
  133. return success();
  134. }
  135. }
  136. return error();
  137. }
  138. catch (Exception e)
  139. {
  140. log.error("修改头像失败!", e);
  141. return error(e.getMessage());
  142. }
  143. }
  144. }