SysProfileController.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. currentUser.setAvatar(user.getAvatar());
  80. if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(currentUser)) {
  81. return error("修改用户'" + loginUser.getUsername() + "'失败,手机号码已存在");
  82. }
  83. if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(currentUser)) {
  84. return error("修改用户'" + loginUser.getUsername() + "'失败,邮箱账号已存在");
  85. }
  86. //查询用户是否还有修改性别的次数
  87. SysUser sysUsers = userMapper.selectOne(
  88. new QueryWrapper<SysUser>()
  89. .select("sex", "is_sex") // 选择 sex 和 is_sex 字段
  90. .eq("user_id", currentUser.getUserId())
  91. );
  92. if (sysUsers.getIsSex() && !currentUser.getSex().equals(sysUsers.getSex())) {
  93. return error("不允许修改用户'" + loginUser.getUsername() + "'性别");
  94. } else if (currentUser.getSex().equals(sysUsers.getSex())) {
  95. currentUser.setSex(null);
  96. }
  97. if (userService.updateUserProfile(currentUser) > 0) {
  98. CommunityUserInfo communityUserInfo = communityUserInfoMapper.selectOne(new QueryWrapper<CommunityUserInfo>().eq("user_id", currentUser.getUserId()));
  99. communityUserInfo.setProfile(currentUser.getProfile());
  100. communityUserInfo.setBirthday(user.getBirthday());
  101. communityUserInfo.setTags(user.getTags());
  102. communityUserInfo.setUpdateBy(currentUser.getUserId());
  103. communityUserInfo.setUpdateTime(DateUtils.parseDate(DateUtils.getTime()));
  104. communityUserInfo.setBackImage(user.getBackImage());
  105. //更新前端用户的个人简介信息
  106. communityUserInfoMapper.updateById(communityUserInfo);
  107. // 更新缓存用户信息
  108. tokenService.setLoginUser(loginUser);
  109. return success();
  110. }
  111. return error("修改个人信息异常,请联系管理员");
  112. }
  113. /**
  114. * 重置密码
  115. */
  116. @Log(title = "个人信息", businessType = BusinessType.UPDATE)
  117. @PutMapping("/updatePwd")
  118. public AjaxResult updatePwd(String oldPassword, String newPassword) {
  119. LoginUser loginUser = getLoginUser();
  120. String userName = loginUser.getUsername();
  121. String password = loginUser.getPassword();
  122. if (!SecurityUtils.matchesPassword(oldPassword, password)) {
  123. return error("修改密码失败,旧密码错误");
  124. }
  125. if (SecurityUtils.matchesPassword(newPassword, password)) {
  126. return error("新密码不能与旧密码相同");
  127. }
  128. newPassword = SecurityUtils.encryptPassword(newPassword);
  129. if (userService.resetUserPwd(userName, newPassword) > 0) {
  130. // 更新缓存用户密码
  131. loginUser.getUser().setPassword(newPassword);
  132. tokenService.setLoginUser(loginUser);
  133. return success();
  134. }
  135. return error("修改密码异常,请联系管理员");
  136. }
  137. /**
  138. * 重置密码(验证码)
  139. */
  140. @Log(title = "个人信息", businessType = BusinessType.UPDATE)
  141. @PutMapping("/updatePwdBySmsCode")
  142. public AjaxResult updatePwdBySmsCode(String username, String smsCode, String newPassword) {
  143. if (StringUtils.isEmpty(newPassword) || StringUtils.isEmpty(smsCode) || StringUtils.isEmpty(username)) {
  144. return error("参数异常!");
  145. }
  146. validateSmsCaptcha(username, smsCode);
  147. newPassword = SecurityUtils.encryptPassword(newPassword);
  148. int result = userService.resetUserPwd(username, newPassword);
  149. if (result > 0) {
  150. return success();
  151. }
  152. return error("修改密码异常,请联系管理员");
  153. }
  154. /**
  155. * 头像上传
  156. */
  157. @Log(title = "用户头像", businessType = BusinessType.UPDATE)
  158. @PostMapping("/avatar")
  159. public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws Exception {
  160. if (!file.isEmpty()) {
  161. LoginUser loginUser = getLoginUser();
  162. String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
  163. if (userService.updateUserAvatar(loginUser.getUsername(), avatar)) {
  164. AjaxResult ajax = AjaxResult.success();
  165. ajax.put("imgUrl", avatar);
  166. // 更新缓存用户头像
  167. loginUser.getUser().setAvatar(avatar);
  168. tokenService.setLoginUser(loginUser);
  169. return ajax;
  170. }
  171. }
  172. return error("上传图片异常,请联系管理员");
  173. }
  174. /**
  175. * 校验验证码
  176. *
  177. * @param username 用户名
  178. * @param code 验证码
  179. * @return 结果
  180. */
  181. public void validateSmsCaptcha(String username, String code) {
  182. if (StringUtils.isEmpty(code)) {
  183. throw new CaptchaNullException();
  184. }
  185. String verifyKey = CacheConstants.SMS_UPDATE_PASSWORD_CODE_KEY + StringUtils.nvl(username, "");
  186. try {
  187. String captcha = redisCache.getCacheObject(verifyKey).toString();
  188. if (captcha == null) {
  189. throw new CaptchaExpireException();
  190. }
  191. if (!code.equalsIgnoreCase(captcha)) {
  192. throw new CaptchaException();
  193. }
  194. redisCache.deleteObject(verifyKey);
  195. } catch (NullPointerException e) {
  196. throw new CaptchaExpireException();
  197. }
  198. }
  199. /**
  200. * 拉黑用户
  201. */
  202. @ApiOperation("拉黑用户")
  203. @PostMapping("/blockUser")
  204. @Transactional
  205. //@Anonymous
  206. public AjaxResult blockTag(@RequestBody CommunityUserBlock communityUserBlock) {
  207. if (Objects.isNull(communityUserBlock.getUserId()) || Objects.isNull(communityUserBlock.getPeerId())) {
  208. return AjaxResult.error("参数异常!");
  209. }
  210. CommunityUserBlock communityTagBlock = null;
  211. try {
  212. communityTagBlock = communityUserBlockService.blockUser(communityUserBlock);
  213. } catch (Exception e) {
  214. throw new ProjectException();
  215. }
  216. return AjaxResult.success(communityTagBlock);
  217. }
  218. }