AuthController.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package com.sckw.auth.controller;
  2. import com.sckw.auth.model.vo.req.*;
  3. import com.sckw.auth.service.IAuthService;
  4. import com.sckw.core.common.enums.enums.DictEnum;
  5. import com.sckw.core.exception.SystemException;
  6. import com.sckw.core.model.enums.LoginMethodEnum;
  7. import com.sckw.core.model.enums.SystemTypeEnum;
  8. import com.sckw.core.utils.RegularUtils;
  9. import com.sckw.core.utils.StringUtils;
  10. import com.sckw.core.web.constant.HttpStatus;
  11. import com.sckw.core.web.response.HttpResult;
  12. import com.sckw.redis.constant.RedisConstant;
  13. import com.sckw.redis.utils.RedissonUtils;
  14. import jakarta.validation.Valid;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.web.bind.annotation.*;
  17. /**
  18. * @desc: 用户权限
  19. * @author: czh
  20. * @date: 2023/6/19
  21. */
  22. @RestController
  23. @RequestMapping("/auth")
  24. public class AuthController {
  25. @Autowired
  26. private IAuthService authService;
  27. @PostMapping("/auth")
  28. public HttpResult auth(@RequestHeader(name = "System-Type") int systemType,
  29. @RequestHeader(name = "Client-Type") String clientType,
  30. @RequestBody @Valid LoginBase loginBase) {
  31. loginBase.setSystemType(systemType);
  32. loginBase.setClientType(clientType);
  33. loginBase.setLoginMethod(LoginMethodEnum.ORDINARY.getValue());
  34. /**参数校验**/
  35. HttpResult result = checkParams(loginBase);
  36. if (result.getCode() != HttpStatus.SUCCESS_CODE) {
  37. return result;
  38. }
  39. /**运营端/企业端登录(PC/APP)**/
  40. if (loginBase.getSystemType() == SystemTypeEnum.MANAGE.getCode()
  41. || loginBase.getSystemType() == SystemTypeEnum.COMPANY.getCode()) {
  42. return authService.commonAuth(loginBase);
  43. }
  44. /**司机端**/
  45. if (loginBase.getSystemType() == SystemTypeEnum.DRIVER.getCode()) {
  46. return authService.driverAuth(loginBase);
  47. }
  48. return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, HttpStatus.GLOBAL_EXCEPTION_MESSAGE);
  49. }
  50. @PostMapping("/smsAuth")
  51. public HttpResult smsAuth(@RequestHeader(name = "System-Type") int systemType,
  52. @RequestHeader(name = "Client-Type") String clientType,
  53. @RequestBody @Valid LoginBase loginBase) {
  54. loginBase.setSystemType(systemType);
  55. loginBase.setClientType(clientType);
  56. loginBase.setLoginMethod(LoginMethodEnum.SMS.getValue());
  57. /**参数校验**/
  58. HttpResult result = checkParams(loginBase);
  59. if (result.getCode() != HttpStatus.SUCCESS_CODE) {
  60. return result;
  61. }
  62. /**运营端/企业端登录(PC/APP)**/
  63. if (loginBase.getSystemType() == SystemTypeEnum.MANAGE.getCode()
  64. || loginBase.getSystemType() == SystemTypeEnum.COMPANY.getCode()) {
  65. return authService.commonAuth(loginBase);
  66. }
  67. /**司机端**/
  68. if (loginBase.getSystemType() == SystemTypeEnum.DRIVER.getCode()) {
  69. return authService.driverAuth(loginBase);
  70. }
  71. return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, HttpStatus.GLOBAL_EXCEPTION_MESSAGE);
  72. }
  73. /**
  74. * @param
  75. * @return
  76. * @description 登录参数校验
  77. * @author zk
  78. * @date 2020/6/14 18:14
  79. **/
  80. public HttpResult checkParams(LoginBase params) {
  81. if (StringUtils.isBlank(params.getSystemType())) {
  82. return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "应用服务类型不能为空!");
  83. }
  84. if (StringUtils.isBlank(params.getClientType())) {
  85. return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "客户端类型不能为空!");
  86. }
  87. if (StringUtils.isBlank(params.getAccount())) {
  88. return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "请输入您的账号!");
  89. }
  90. if (params.getLoginMethod() == LoginMethodEnum.ORDINARY.getValue() && StringUtils.isBlank(params.getPassword())) {
  91. return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "请输入您的密码!");
  92. }
  93. if (params.getLoginMethod() == LoginMethodEnum.SMS.getValue() && !RegularUtils.matchs(RegularUtils.PHONE_REG, params.getAccount())) {
  94. return HttpResult.error(HttpStatus.PARAMETERS_PATTERN_ERROR_CODE, "手机号格式不正确,请检查并重新输入!");
  95. }
  96. if (params.getLoginMethod() == LoginMethodEnum.SMS.getValue() && StringUtils.isBlank(params.getCaptcha())) {
  97. return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "请输入您的验证码!");
  98. }
  99. String key = StringUtils.format(RedisConstant.MESSAGE_SMS_VERIFY_CODE_VALUE_KEY, DictEnum.SMS_LOGIN.getValue(), params.getAccount());
  100. RedissonUtils.putString(key, params.getCaptcha(), RedisConstant.SMS_VERIFY_CODE_VALID_TIME);
  101. String smsCaptcha = RedissonUtils.getString(key);
  102. if (params.getLoginMethod() == LoginMethodEnum.SMS.getValue() && StringUtils.isBlank(smsCaptcha)) {
  103. return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "验证码已过期,请重新获取!");
  104. }
  105. if (params.getLoginMethod() == LoginMethodEnum.SMS.getValue() && StringUtils.isBlank(smsCaptcha)) {
  106. return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "验证码已过期,请重新获取!");
  107. }
  108. if (params.getLoginMethod() == LoginMethodEnum.SMS.getValue() && !smsCaptcha.equals(params.getCaptcha())) {
  109. return HttpResult.error(HttpStatus.UN_LOGIN_CODE, "验证码不正确,请检查并重新输入!");
  110. }
  111. return HttpResult.ok();
  112. }
  113. /**--------------------------------------------------------------------------------------------------------------**/
  114. /**
  115. * @param reqVo 登录入参
  116. * @return HttpResult
  117. * @desc: 用户登录
  118. * @author: czh
  119. * @date: 2023/6/16
  120. */
  121. @PostMapping("/login")
  122. public HttpResult login(@Valid @RequestBody LoginReqVo reqVo,
  123. @RequestHeader(name = "Client-Type", required = true) String clientType,
  124. @RequestHeader(name = "System-Type", required = true) int systemType) throws SystemException {
  125. // reqVo.setSystemType(systemType);
  126. // reqVo.setClientType(clientType);
  127. // LoginBase loginBase = new LoginBase();
  128. // loginBase.setAccount(reqVo.getAccount());
  129. // loginBase.setPassword(reqVo.getPassword());
  130. // loginBase.setCaptcha(reqVo.getCaptcha());
  131. // loginBase.setSystemType(systemType);
  132. // loginBase.setClientType(clientType);
  133. //
  134. // loginBase.setLoginMethod(LoginMethodEnum.ORDINARY.getValue());
  135. // if (StringUtils.isNotBlank(loginBase.getCaptcha())) {
  136. // loginBase.setLoginMethod(LoginMethodEnum.SMS.getValue());
  137. // }
  138. //
  139. // /**参数校验**/
  140. // HttpResult result = checkParams(loginBase);
  141. // if (result.getCode() != HttpStatus.SUCCESS_CODE) {
  142. // return result;
  143. // }
  144. //
  145. // /**运营端/企业端登录(PC/APP)**/
  146. // if (loginBase.getSystemType() == SystemTypeEnum.MANAGE.getCode()
  147. // || loginBase.getSystemType() == SystemTypeEnum.COMPANY.getCode()) {
  148. // return authService.commonAuth(loginBase);
  149. // }
  150. //
  151. // /**司机端**/
  152. // if (loginBase.getSystemType() == SystemTypeEnum.DRIVER.getCode()) {
  153. // return authService.driverAuth(loginBase);
  154. // }
  155. //
  156. // return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, HttpStatus.GLOBAL_EXCEPTION_MESSAGE);
  157. reqVo.setSystemType(systemType);
  158. reqVo.setClientType(clientType);
  159. return HttpResult.ok(authService.login(reqVo));
  160. }
  161. /**
  162. * @param id 主键ID
  163. * @description 重置密码
  164. * @author zk
  165. * @date 2023/06/02
  166. **/
  167. @PostMapping("/resetPassword")
  168. public HttpResult resetPassword(@RequestParam Long id) {
  169. authService.resetPassword(id);
  170. return HttpResult.ok(HttpStatus.MSG_001);
  171. }
  172. }