LoginController.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package com.template.controller;
  2. import com.template.annotation.DESRespondSecret;
  3. import com.template.annotation.PassToken;
  4. import com.template.api.LoginControllerAPI;
  5. import com.template.common.utils.DingTalkInterface;
  6. import com.template.model.enumModel.eStatu;
  7. import com.template.model.pojo.RepairAdmin;
  8. import com.template.model.pojo.SmartUser;
  9. import com.template.model.request.changePasswordRequest;
  10. import com.template.model.request.loginRequest;
  11. import com.template.model.result.CommonResult;
  12. import com.template.model.vo.LoginVO;
  13. import com.template.services.RepairAdminService;
  14. import com.template.common.utils.AesUtils;
  15. import com.template.common.utils.paramUtils;
  16. import com.template.common.utils.JWTUtil;
  17. import com.template.services.SmartUserService;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.validation.BindingResult;
  22. import org.springframework.web.bind.annotation.RequestBody;
  23. import org.springframework.web.bind.annotation.RequestHeader;
  24. import org.springframework.web.bind.annotation.RestController;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. /**
  28. * @Author: binguo
  29. * @Date: 2023/7/5 星期三 9:28
  30. * @Description: com.template.controller
  31. * @Version: 1.0
  32. */
  33. @RestController
  34. //返回参数加密注解
  35. @DESRespondSecret
  36. public class LoginController implements LoginControllerAPI {
  37. @Autowired
  38. private RepairAdminService repairAdminService;
  39. @Autowired
  40. private SmartUserService smartUserService;
  41. private static Logger logger = LoggerFactory.getLogger(LoginController.class);
  42. /**
  43. * 查看系统版本号
  44. *
  45. * @return
  46. */
  47. @Override
  48. @PassToken
  49. @DESRespondSecret(validated = true)
  50. public CommonResult queryReduce() {
  51. logger.info("test success");
  52. logger.error("test error");
  53. List<String> params = new ArrayList<>();
  54. params.add("24");
  55. params.add("25");
  56. List<SmartUser> result = smartUserService.getSmartUserIds(params);
  57. return CommonResult.ok("200", "测试返回参数加密", result);
  58. }
  59. /**
  60. * 注册接口
  61. *
  62. * @param registerdo account 账号
  63. * password 密码
  64. * username 昵称
  65. * phone 手机号
  66. * @return
  67. */
  68. @Override
  69. @PassToken
  70. @DESRespondSecret(validated = true)
  71. public CommonResult Register(@RequestBody RepairAdmin registerdo, BindingResult bindingResult) {
  72. if (registerdo == null) {
  73. return CommonResult.fail("请传递参数");
  74. }
  75. if (bindingResult.hasErrors()) {
  76. String st = paramUtils.getParamError(bindingResult);
  77. return CommonResult.fail(st);
  78. }
  79. int result = 0;
  80. try {
  81. result = repairAdminService.insertRepairAdmin(registerdo);
  82. } catch (Exception e) {
  83. if (e.getCause().getMessage().contains("'repair_admin.account_un'")) {
  84. return CommonResult.fail("该账号已存在!");
  85. }
  86. return CommonResult.fail("系统异常,注册失败!");
  87. }
  88. if (result > 0) {
  89. return CommonResult.ok("注册成功!");
  90. }
  91. return CommonResult.fail("注册失败!");
  92. }
  93. /**
  94. * 登录接口
  95. *
  96. * @param loginRequest account 账号
  97. * password 密码
  98. * @return
  99. */
  100. @Override
  101. @PassToken
  102. @DESRespondSecret(validated = true)
  103. public CommonResult Login(@RequestBody loginRequest loginRequest, BindingResult bindingResult) {
  104. if (loginRequest == null) {
  105. return CommonResult.fail("请传递参数");
  106. }
  107. if (bindingResult.hasErrors()) {
  108. String st = paramUtils.getParamError(bindingResult);
  109. return CommonResult.fail(st);
  110. }
  111. RepairAdmin result = repairAdminService.getRepairByAccount(loginRequest.getAccount());
  112. if (result == null) {
  113. return CommonResult.fail("账号或密码错误");
  114. }
  115. if (result.getStatu() == eStatu.Freeze.getValue()) {
  116. return CommonResult.fail("该账号已被冻结");
  117. }
  118. String encPassword = AesUtils.encrypt(loginRequest.getPassword());
  119. if (!encPassword.equals(result.getPassword())) {
  120. return CommonResult.fail("密码错误");
  121. }
  122. SmartUser user = new SmartUser();
  123. String token = JWTUtil.getToken(user, null);
  124. LoginVO login = new LoginVO();
  125. login.setToken(token);
  126. login.setTokenTtl(JWTUtil.getExpired());
  127. login.setUserName(result.getUsername());
  128. login.setUserhead(AesUtils.encrypt(result.getId()));
  129. return CommonResult.ok("登录成功", login);
  130. }
  131. /**
  132. * 修改密码
  133. *
  134. * @param cpr oldPassword 旧密码
  135. * newPassword 新密码
  136. * confirmPassword 确认密码
  137. * @param userhead
  138. * @param bindingResult
  139. * @return
  140. */
  141. @Override
  142. @DESRespondSecret(validated = true)
  143. public CommonResult ChangePassword(changePasswordRequest cpr, @RequestHeader("user_head") String userhead, BindingResult bindingResult) {
  144. if (bindingResult.hasErrors()) {
  145. String st = paramUtils.getParamError(bindingResult);
  146. return CommonResult.fail(st);
  147. }
  148. if (!cpr.getNewPassword().equals(cpr.getConfirmPassword())) {
  149. return CommonResult.fail("确认密码和新密码不一致!");
  150. }
  151. String userID = AesUtils.decrypt(userhead);
  152. RepairAdmin operateData = repairAdminService.getRepairById(userID);
  153. if (operateData == null) {
  154. return CommonResult.fail("当前账号不合法!");
  155. }
  156. if (operateData.getStatu() == eStatu.Freeze.getValue()) {
  157. return CommonResult.fail("该账号已被冻结");
  158. }
  159. if (!AesUtils.encrypt(cpr.getOldPassword()).equals(operateData.getPassword())) {
  160. return CommonResult.fail("原密码错误!");
  161. }
  162. RepairAdmin ra = new RepairAdmin();
  163. ra.setId(userID);
  164. ra.setPassword(AesUtils.encrypt(cpr.getNewPassword()));
  165. int result = repairAdminService.updateRepairAdmin(ra);
  166. return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
  167. }
  168. }