CompanyProfileController.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package com.fs.company.controller;
  2. import com.fs.common.annotation.Log;
  3. import com.fs.common.config.FSConfig;
  4. import com.fs.common.core.controller.BaseController;
  5. import com.fs.common.core.domain.AjaxResult;
  6. import com.fs.common.enums.BusinessType;
  7. import com.fs.common.utils.PatternUtils;
  8. import com.fs.common.utils.ServletUtils;
  9. import com.fs.common.utils.file.FileUploadUtils;
  10. import com.fs.company.domain.CompanyUser;
  11. import com.fs.company.domain.CompanyVoiceCaller;
  12. import com.fs.company.param.CompanyUserEditParam;
  13. import com.fs.company.service.ICompanyUserService;
  14. import com.fs.company.service.ICompanyVoiceCallerService;
  15. import com.fs.core.security.LoginUser;
  16. import com.fs.core.security.SecurityUtils;
  17. import com.fs.core.web.service.TokenService;
  18. import org.springframework.beans.BeanUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.web.bind.annotation.*;
  21. import org.springframework.web.multipart.MultipartFile;
  22. import java.io.IOException;
  23. /**
  24. * 个人信息
  25. */
  26. @RestController
  27. @RequestMapping("company/user/profile")
  28. public class CompanyProfileController extends BaseController
  29. {
  30. @Autowired
  31. private ICompanyUserService userService;
  32. @Autowired
  33. private TokenService tokenService;
  34. @Autowired
  35. private ICompanyVoiceCallerService callerService;
  36. /**
  37. * 个人信息
  38. */
  39. @GetMapping
  40. public AjaxResult profile()
  41. {
  42. LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  43. CompanyUser user = loginUser.getUser();
  44. CompanyVoiceCaller companyVoiceCaller = callerService.selectCompanyVoiceCallerByUserId(user.getCompanyId(), user.getUserId());
  45. if (companyVoiceCaller != null) {
  46. user.setCallerNo(companyVoiceCaller.getCallerNo());
  47. }
  48. AjaxResult ajax = AjaxResult.success(user);
  49. ajax.put("roleGroup", userService.selectUserRoleGroup(loginUser.getUsername()));
  50. ajax.put("postGroup", userService.selectUserPostGroup(loginUser.getUsername()));
  51. return ajax;
  52. }
  53. /**
  54. * 修改用户
  55. */
  56. @Log(title = "个人信息", businessType = BusinessType.UPDATE)
  57. @PutMapping
  58. public AjaxResult updateProfile(@RequestBody CompanyUserEditParam param)
  59. {
  60. CompanyUser user=new CompanyUser();
  61. BeanUtils.copyProperties(param,user);
  62. if (userService.updateUserProfile(user) > 0)
  63. {
  64. LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  65. // 更新缓存用户信息
  66. loginUser.getUser().setNickName(user.getNickName());
  67. loginUser.getUser().setPhonenumber(user.getPhonenumber());
  68. loginUser.getUser().setEmail(user.getEmail());
  69. loginUser.getUser().setSex(user.getSex());
  70. tokenService.setLoginUser(loginUser);
  71. return AjaxResult.success();
  72. }
  73. return AjaxResult.error("修改个人信息异常,请联系管理员");
  74. }
  75. /**
  76. * 重置密码
  77. */
  78. @Log(title = "个人信息", businessType = BusinessType.UPDATE)
  79. @PutMapping("/updatePwd")
  80. public AjaxResult updatePwd(String oldPassword, String newPassword)
  81. {
  82. LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  83. String userName = loginUser.getUsername();
  84. String password = loginUser.getPassword();
  85. if (!SecurityUtils.matchesPassword(oldPassword, password))
  86. {
  87. return AjaxResult.error("修改密码失败,旧密码错误");
  88. }
  89. if (SecurityUtils.matchesPassword(newPassword, password))
  90. {
  91. return AjaxResult.error("新密码不能与旧密码相同");
  92. }
  93. if (!PatternUtils.checkPassword(newPassword)) {
  94. return AjaxResult.error("新密码格式不正确,需包含字母、数字和特殊字符,长度为 8-20 位");
  95. }
  96. if (userService.resetUserPwd(userName, SecurityUtils.encryptPassword(newPassword)) > 0)
  97. {
  98. // 更新缓存用户密码
  99. loginUser.getUser().setPassword(SecurityUtils.encryptPassword(newPassword));
  100. tokenService.setLoginUser(loginUser);
  101. return AjaxResult.success();
  102. }
  103. return AjaxResult.error("修改密码异常,请联系管理员");
  104. }
  105. /**
  106. * 头像上传
  107. */
  108. @Log(title = "用户头像", businessType = BusinessType.UPDATE)
  109. @PostMapping("/avatar")
  110. public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException
  111. {
  112. if (!file.isEmpty())
  113. {
  114. LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  115. String avatar = FileUploadUtils.upload(FSConfig.getAvatarPath(), file);
  116. if (userService.updateUserAvatar(loginUser.getUsername(), avatar)>0)
  117. {
  118. AjaxResult ajax = AjaxResult.success();
  119. ajax.put("imgUrl", avatar);
  120. // 更新缓存用户头像
  121. loginUser.getUser().setAvatar(avatar);
  122. tokenService.setLoginUser(loginUser);
  123. return ajax;
  124. }
  125. }
  126. return AjaxResult.error("上传图片异常,请联系管理员");
  127. }
  128. }