AuthController.java 8.6 KB

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