SysProfileController.java 5.2 KB

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