LoginController.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package com.yunzhi.questions.controller;
  2. import com.yunzhi.questions.common.*;
  3. import com.yunzhi.questions.entity.SysLogin;
  4. import com.yunzhi.questions.entity.SysUser;
  5. import com.yunzhi.questions.entity.TaApp;
  6. import com.yunzhi.questions.entity.TaPerson;
  7. import com.yunzhi.questions.service.ISysLoginService;
  8. import com.yunzhi.questions.service.ISysUserService;
  9. import com.yunzhi.questions.service.ITaAppService;
  10. import com.yunzhi.questions.service.ITaPersonService;
  11. import com.yunzhi.questions.vo.ChangePassword;
  12. import com.yunzhi.questions.vo.AdminLoginParam;
  13. import com.yunzhi.questions.vo.LoginParam;
  14. import com.yunzhi.questions.vo.TokenParam;
  15. import io.swagger.annotations.Api;
  16. import io.swagger.annotations.ApiOperation;
  17. import io.swagger.annotations.ApiParam;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.web.bind.annotation.*;
  20. import javax.servlet.http.HttpServletRequest;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. @Api(tags = "登入/登出")
  24. @RestController
  25. public class LoginController extends BaseController {
  26. @Autowired
  27. SMSCaptcha smsCaptcha;
  28. @Autowired
  29. ISysLoginService iSysLoginService;
  30. @Autowired
  31. ISysUserService iSysUserService;
  32. @Autowired
  33. ITaPersonService iTaPersonService;
  34. @Autowired
  35. ITaAppService iTaAppService;
  36. @PostMapping("/admin/login")
  37. @ApiOperation(value="登录", notes = "登录", httpMethod = "POST", response = ResponseBean.class)
  38. public ResponseBean login(@ApiParam("登录参数") @RequestBody AdminLoginParam loginParam) throws Exception {
  39. if (null == loginParam) {
  40. return ResponseBean.error("账户或密码不能为空", ResponseBean.ERROR_ILLEGAL_PARAMS);
  41. }
  42. String userName = loginParam.getUserName();
  43. String password = loginParam.getPassword();
  44. if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
  45. return ResponseBean.error("账户或密码不能为空", ResponseBean.ERROR_ILLEGAL_PARAMS);
  46. }
  47. SysLogin sysLogin = iSysLoginService.getByLoginName(userName);
  48. if (null == sysLogin) {
  49. return ResponseBean.error("账户或密码不正确", ResponseBean.ERROR_ILLEGAL_PARAMS);
  50. }
  51. SysUser sysUser = iSysUserService.getById(sysLogin.getUserId());
  52. if (null == sysUser) {
  53. return ResponseBean.error("账户或密码不正确", ResponseBean.ERROR_ILLEGAL_PARAMS);
  54. }
  55. if (!checkPassword(password, sysLogin.getPassword(), sysLogin.getUserId())) {
  56. return ResponseBean.error("账户或密码不正确", ResponseBean.ERROR_ILLEGAL_PARAMS);
  57. }
  58. if (Constants.STATUS_NORMAL != sysUser.getStatus()) {
  59. return ResponseBean.error("用户状态不正确, 请联系管理人员", ResponseBean.ERROR_UNAVAILABLE);
  60. }
  61. // 生成 token
  62. TokenParam tokenParam = new TokenParam()
  63. .setUserId(sysUser.getUserId())
  64. .setOrgId("0");
  65. Map<String, Object> tokenMap = tokenParam.toMap();
  66. String token = JWTUtils.encode(tokenMap);
  67. // CurrentContext.setTokenParam(tokenMap);
  68. Map<String, Object> res = new HashMap<String, Object>() {{
  69. put("user", sysUser);
  70. put("token", token);
  71. }};
  72. return ResponseBean.success(res);
  73. }
  74. @PutMapping("/admin/change-password")
  75. @ApiOperation(value="修改密码", notes = "修改密码", httpMethod = "PUT", response = ResponseBean.class)
  76. public ResponseBean changePassword(@ApiParam("修改密码参数") @RequestBody ChangePassword param,
  77. HttpServletRequest request) throws Exception {
  78. if (StringUtils.isEmpty(param.getOriginPassword()) || StringUtils.isEmpty(param.getNewPassword())) {
  79. return ResponseBean.error("原始密码或新密码不能为空", ResponseBean.ERROR_ILLEGAL_PARAMS);
  80. }
  81. SysUser currentUser = getCurrentUser(request);
  82. SysLogin sysLogin = iSysLoginService.getByUser(currentUser.getUserId());
  83. if (!checkPassword(param.getOriginPassword(), sysLogin.getPassword(), currentUser.getUserId())) {
  84. return ResponseBean.error("原始密码不正确", ResponseBean.ERROR_ILLEGAL_PARAMS);
  85. }
  86. sysLogin.setPassword(EncryptUtils.md5(param.getNewPassword(), currentUser.getUserId()));
  87. iSysLoginService.updateById(sysLogin);
  88. return ResponseBean.success("密码修改成功");
  89. }
  90. @ApiParam("客户端登录")
  91. @PostMapping("/{appid}/login")
  92. public ResponseBean clientLogin(@ApiParam("APPID") @PathVariable String appid,
  93. @ApiParam("登录参数") @RequestBody LoginParam loginParam) throws Exception {
  94. if (null == loginParam) {
  95. return ResponseBean.error("登录参数不存在", ResponseBean.ERROR_ILLEGAL_PARAMS);
  96. }
  97. if (StringUtils.isEmpty(loginParam.getName())
  98. || StringUtils.isEmpty(loginParam.getPhone())
  99. || StringUtils.isEmpty(loginParam.getCaptcha())) {
  100. return ResponseBean.error("登录参数不合法", ResponseBean.ERROR_ILLEGAL_PARAMS);
  101. }
  102. boolean isCapatchRight = smsCaptcha.validate(loginParam.getPhone(), loginParam.getCaptcha());
  103. if (!isCapatchRight) {
  104. return ResponseBean.error("验证码不正确", ResponseBean.ERROR_ILLEGAL_PARAMS);
  105. }
  106. //
  107. TaApp taApp = iTaAppService.getById(appid);
  108. if (null == taApp) {
  109. return ResponseBean.error("APPID不正确", ResponseBean.ERROR_ILLEGAL_PARAMS);
  110. }
  111. TaPerson taPerson = iTaPersonService.getByPhone(loginParam.getPhone());
  112. if (null != taPerson) {
  113. return ResponseBean.success(taPerson);
  114. }
  115. taPerson = new TaPerson();
  116. taPerson.setNickName(loginParam.getName());
  117. taPerson.setPhone(loginParam.getPhone());
  118. if (iTaPersonService.save(taPerson)) {
  119. Map<String, Object> result = new HashMap<>();
  120. result.put("person", taPerson);
  121. return ResponseBean.success(taPerson);
  122. } else {
  123. return ResponseBean.error("登录失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
  124. }
  125. }
  126. // @PutMapping("/admin/reset-password/{userId}")
  127. // @ApiOperation(value="重置密码", notes = "重置密码", httpMethod = "PUT", response = ResponseBean.class)
  128. // public ResponseBean resetPassword(@ApiParam("用户ID") @PathVariable String userId) throws Exception {
  129. // SysLogin sysLogin = iSysLoginService.getByUser(userId);
  130. //
  131. // String newPassword = EncryptUtils.md5(EncryptUtils.md5(Constants.DEFAULT_PASSWORD), userId);
  132. // sysLogin.setPassword(newPassword);
  133. // iSysLoginService.updateById(sysLogin);
  134. //
  135. // return ResponseBean.success("重置密码成功");
  136. // }
  137. private boolean checkPassword(String src, String targ, String salt) {
  138. return EncryptUtils.md5(src, salt).equals(targ);
  139. }
  140. }