SysProfileController.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package com.ruoyi.web.controller.system;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.ruoyi.common.annotation.Log;
  4. import com.ruoyi.common.config.RuoYiConfig;
  5. import com.ruoyi.common.constant.CacheConstants;
  6. import com.ruoyi.common.core.controller.BaseController;
  7. import com.ruoyi.common.core.domain.AjaxResult;
  8. import com.ruoyi.common.core.domain.entity.SysUser;
  9. import com.ruoyi.common.core.domain.model.LoginUser;
  10. import com.ruoyi.common.core.redis.RedisCache;
  11. import com.ruoyi.common.enums.BusinessType;
  12. import com.ruoyi.common.exception.user.CaptchaException;
  13. import com.ruoyi.common.exception.user.CaptchaExpireException;
  14. import com.ruoyi.common.exception.user.CaptchaNullException;
  15. import com.ruoyi.common.exception.user.ProjectException;
  16. import com.ruoyi.common.utils.DateUtils;
  17. import com.ruoyi.common.utils.SecurityUtils;
  18. import com.ruoyi.common.utils.StringUtils;
  19. import com.ruoyi.common.utils.file.FileUploadUtils;
  20. import com.ruoyi.common.utils.file.MimeTypeUtils;
  21. import com.ruoyi.framework.web.service.TokenService;
  22. import com.ruoyi.generator.domain.Community.CommunityUserBlock;
  23. import com.ruoyi.generator.domain.Community.CommunityUserInfo;
  24. import com.ruoyi.generator.mapper.community.CommunityUserInfoMapper;
  25. import com.ruoyi.generator.service.ICommunityUserBlockService;
  26. import com.ruoyi.system.mapper.SysUserMapper;
  27. import com.ruoyi.system.service.ISysUserService;
  28. import io.swagger.annotations.ApiOperation;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.transaction.annotation.Transactional;
  31. import org.springframework.web.bind.annotation.*;
  32. import org.springframework.web.multipart.MultipartFile;
  33. import java.util.Objects;
  34. /**
  35. * 个人信息 业务处理
  36. *
  37. * @author ruoyi
  38. */
  39. @RestController
  40. @RequestMapping("/system/user/profile")
  41. public class SysProfileController extends BaseController {
  42. @Autowired
  43. private ISysUserService userService;
  44. @Autowired
  45. private TokenService tokenService;
  46. @Autowired
  47. private CommunityUserInfoMapper communityUserInfoMapper;
  48. @Autowired
  49. private ICommunityUserBlockService communityUserBlockService;
  50. @Autowired
  51. private SysUserMapper userMapper;
  52. @Autowired
  53. private RedisCache redisCache;
  54. /**
  55. * 个人信息
  56. */
  57. @GetMapping
  58. public AjaxResult profile() {
  59. LoginUser loginUser = getLoginUser();
  60. SysUser user = loginUser.getUser();
  61. AjaxResult ajax = AjaxResult.success(user);
  62. ajax.put("roleGroup", userService.selectUserRoleGroup(loginUser.getUsername()));
  63. ajax.put("postGroup", userService.selectUserPostGroup(loginUser.getUsername()));
  64. return ajax;
  65. }
  66. /**
  67. * 修改用户
  68. */
  69. @Log(title = "个人信息", businessType = BusinessType.UPDATE)
  70. @PutMapping
  71. public AjaxResult updateProfile(@RequestBody SysUser user) {
  72. LoginUser loginUser = getLoginUser();
  73. SysUser currentUser = loginUser.getUser();
  74. currentUser.setNickName(user.getNickName());
  75. currentUser.setEmail(user.getEmail());
  76. currentUser.setPhonenumber(user.getPhonenumber());
  77. currentUser.setSex(user.getSex());
  78. currentUser.setProfile(user.getProfile());
  79. if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(currentUser)) {
  80. return error("修改用户'" + loginUser.getUsername() + "'失败,手机号码已存在");
  81. }
  82. if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(currentUser)) {
  83. return error("修改用户'" + loginUser.getUsername() + "'失败,邮箱账号已存在");
  84. }
  85. //查询用户是否还有修改性别的次数
  86. SysUser sysUsers = userMapper.selectOne(
  87. new QueryWrapper<SysUser>()
  88. .select("sex", "is_sex") // 选择 sex 和 is_sex 字段
  89. .eq("user_id", currentUser.getUserId())
  90. );
  91. if (sysUsers.getIsSex() && !currentUser.getSex().equals(sysUsers.getSex())) {
  92. return error("不允许修改用户'" + loginUser.getUsername() + "'性别");
  93. } else if (currentUser.getSex().equals(sysUsers.getSex())) {
  94. currentUser.setSex(null);
  95. }
  96. if (userService.updateUserProfile(currentUser) > 0) {
  97. CommunityUserInfo communityUserInfo = communityUserInfoMapper.selectOne(new QueryWrapper<CommunityUserInfo>().eq("user_id", currentUser.getUserId()));
  98. communityUserInfo.setProfile(currentUser.getProfile());
  99. communityUserInfo.setBirthday(user.getBirthday());
  100. communityUserInfo.setTags(user.getTags());
  101. communityUserInfo.setUpdateBy(currentUser.getUserId());
  102. communityUserInfo.setUpdateTime(DateUtils.parseDate(DateUtils.getTime()));
  103. communityUserInfo.setBackImage(user.getBackImage());
  104. //更新前端用户的个人简介信息
  105. communityUserInfoMapper.updateById(communityUserInfo);
  106. // 更新缓存用户信息
  107. tokenService.setLoginUser(loginUser);
  108. return success();
  109. }
  110. return error("修改个人信息异常,请联系管理员");
  111. }
  112. /**
  113. * 重置密码
  114. */
  115. @Log(title = "个人信息", businessType = BusinessType.UPDATE)
  116. @PutMapping("/updatePwd")
  117. public AjaxResult updatePwd(String oldPassword, String newPassword) {
  118. LoginUser loginUser = getLoginUser();
  119. String userName = loginUser.getUsername();
  120. String password = loginUser.getPassword();
  121. if (!SecurityUtils.matchesPassword(oldPassword, password)) {
  122. return error("修改密码失败,旧密码错误");
  123. }
  124. if (SecurityUtils.matchesPassword(newPassword, password)) {
  125. return error("新密码不能与旧密码相同");
  126. }
  127. newPassword = SecurityUtils.encryptPassword(newPassword);
  128. if (userService.resetUserPwd(userName, newPassword) > 0) {
  129. // 更新缓存用户密码
  130. loginUser.getUser().setPassword(newPassword);
  131. tokenService.setLoginUser(loginUser);
  132. return success();
  133. }
  134. return error("修改密码异常,请联系管理员");
  135. }
  136. /**
  137. * 重置密码(验证码)
  138. */
  139. @Log(title = "个人信息", businessType = BusinessType.UPDATE)
  140. @PutMapping("/updatePwdBySmsCode")
  141. public AjaxResult updatePwdBySmsCode(String username, String smsCode, String newPassword) {
  142. if (StringUtils.isEmpty(newPassword) || StringUtils.isEmpty(smsCode) || StringUtils.isEmpty(username)) {
  143. return error("参数异常!");
  144. }
  145. validateSmsCaptcha(username, smsCode);
  146. newPassword = SecurityUtils.encryptPassword(newPassword);
  147. int result = userService.resetUserPwd(username, newPassword);
  148. if (result > 0) {
  149. return success();
  150. }
  151. return error("修改密码异常,请联系管理员");
  152. }
  153. /**
  154. * 头像上传
  155. */
  156. @Log(title = "用户头像", businessType = BusinessType.UPDATE)
  157. @PostMapping("/avatar")
  158. public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws Exception {
  159. if (!file.isEmpty()) {
  160. LoginUser loginUser = getLoginUser();
  161. String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
  162. if (userService.updateUserAvatar(loginUser.getUsername(), avatar)) {
  163. AjaxResult ajax = AjaxResult.success();
  164. ajax.put("imgUrl", avatar);
  165. // 更新缓存用户头像
  166. loginUser.getUser().setAvatar(avatar);
  167. tokenService.setLoginUser(loginUser);
  168. return ajax;
  169. }
  170. }
  171. return error("上传图片异常,请联系管理员");
  172. }
  173. /**
  174. * 校验验证码
  175. *
  176. * @param username 用户名
  177. * @param code 验证码
  178. * @return 结果
  179. */
  180. public void validateSmsCaptcha(String username, String code) {
  181. if (StringUtils.isEmpty(code)) {
  182. throw new CaptchaNullException();
  183. }
  184. String verifyKey = CacheConstants.SMS_UPDATE_PASSWORD_CODE_KEY + StringUtils.nvl(username, "");
  185. try {
  186. String captcha = redisCache.getCacheObject(verifyKey).toString();
  187. if (captcha == null) {
  188. throw new CaptchaExpireException();
  189. }
  190. if (!code.equalsIgnoreCase(captcha)) {
  191. throw new CaptchaException();
  192. }
  193. redisCache.deleteObject(verifyKey);
  194. } catch (NullPointerException e) {
  195. throw new CaptchaExpireException();
  196. }
  197. }
  198. /**
  199. * 拉黑用户
  200. */
  201. @ApiOperation("拉黑用户")
  202. @PostMapping("/blockUser")
  203. @Transactional
  204. //@Anonymous
  205. public AjaxResult blockTag(@RequestBody CommunityUserBlock communityUserBlock) {
  206. if (Objects.isNull(communityUserBlock.getUserId()) || Objects.isNull(communityUserBlock.getPeerId())) {
  207. return AjaxResult.error("参数异常!");
  208. }
  209. CommunityUserBlock communityTagBlock = null;
  210. try {
  211. communityTagBlock = communityUserBlockService.blockUser(communityUserBlock);
  212. } catch (Exception e) {
  213. throw new ProjectException();
  214. }
  215. return AjaxResult.success(communityTagBlock);
  216. }
  217. }