| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- package com.sckw.auth.controller;
- import com.sckw.auth.model.vo.req.*;
- import com.sckw.auth.service.IAuthService;
- import com.sckw.core.common.enums.enums.DictEnum;
- import com.sckw.core.exception.SystemException;
- import com.sckw.core.model.constant.Global;
- import com.sckw.core.model.enums.LoginMethodEnum;
- import com.sckw.core.model.enums.SystemTypeEnum;
- import com.sckw.core.utils.RegularUtils;
- import com.sckw.core.utils.StringUtils;
- import com.sckw.core.web.constant.HttpStatus;
- import com.sckw.core.web.response.HttpResult;
- import com.sckw.redis.constant.RedisConstant;
- import com.sckw.redis.utils.RedissonUtils;
- import jakarta.validation.Valid;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- /**
- * @desc: 用户权限
- * @author: czh
- * @date: 2023/6/19
- */
- @RestController
- @RequestMapping("/auth")
- public class AuthController {
- @Autowired
- private IAuthService authService;
- @PostMapping("/login1")
- public HttpResult login(@RequestHeader(name = "System-Type") int systemType,
- @RequestHeader(name = "Client-Type") String clientType,
- @RequestBody @Valid LoginBase loginBase) {
- loginBase.setSystemType(systemType);
- loginBase.setClientType(clientType);
- loginBase.setLoginMethod(LoginMethodEnum.ORDINARY.getValue());
- /**参数校验**/
- HttpResult result = checkParams(loginBase);
- if (result.getCode() != HttpStatus.SUCCESS_CODE) {
- return result;
- }
- /**运营端/企业端登录(PC/APP)**/
- if (loginBase.getSystemType() == SystemTypeEnum.MANAGE.getCode()
- || loginBase.getSystemType() == SystemTypeEnum.COMPANY.getCode()) {
- return authService.commonAuth(loginBase);
- }
- /**司机端**/
- if (loginBase.getSystemType() == SystemTypeEnum.DRIVER.getCode()) {
- return authService.driverAuth(loginBase);
- }
- return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, HttpStatus.GLOBAL_EXCEPTION_MESSAGE);
- }
- @PostMapping("/smsLogin")
- public HttpResult smsAuth(@RequestHeader(name = "System-Type") int systemType,
- @RequestHeader(name = "Client-Type") String clientType,
- @RequestBody @Valid LoginBase loginBase) {
- loginBase.setSystemType(systemType);
- loginBase.setClientType(clientType);
- loginBase.setLoginMethod(LoginMethodEnum.SMS.getValue());
- /**参数校验**/
- HttpResult result = checkParams(loginBase);
- if (result.getCode() != HttpStatus.SUCCESS_CODE) {
- return result;
- }
- /**运营端/企业端登录(PC/APP)**/
- if (loginBase.getSystemType() == SystemTypeEnum.MANAGE.getCode()
- || loginBase.getSystemType() == SystemTypeEnum.COMPANY.getCode()) {
- return authService.commonAuth(loginBase);
- }
- /**司机端**/
- if (loginBase.getSystemType() == SystemTypeEnum.DRIVER.getCode()) {
- return authService.driverAuth(loginBase);
- }
- return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, HttpStatus.GLOBAL_EXCEPTION_MESSAGE);
- }
- /**
- * @param
- * @return
- * @description 登录参数校验
- * @author zk
- * @date 2020/6/14 18:14
- **/
- public HttpResult checkParams(LoginBase params) {
- if (StringUtils.isBlank(params.getSystemType())) {
- return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "应用服务类型不能为空!");
- }
- if (StringUtils.isBlank(params.getClientType())) {
- return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "客户端类型不能为空!");
- }
- if (StringUtils.isBlank(params.getAccount())) {
- return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "请输入您的账号!");
- }
- if (params.getLoginMethod() == LoginMethodEnum.ORDINARY.getValue() && StringUtils.isBlank(params.getPassword())) {
- return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "请输入您的密码!");
- }
- if (params.getLoginMethod() == LoginMethodEnum.SMS.getValue() && !RegularUtils.matchs(RegularUtils.PHONE_REG, params.getAccount())) {
- return HttpResult.error(HttpStatus.PARAMETERS_PATTERN_ERROR_CODE, "手机号格式不正确,请检查并重新输入!");
- }
- if (params.getLoginMethod() == LoginMethodEnum.SMS.getValue() && StringUtils.isBlank(params.getCaptcha())) {
- return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "请输入您的验证码!");
- }
- String key = StringUtils.format(RedisConstant.MESSAGE_SMS_VERIFY_CODE_VALUE_KEY, DictEnum.SMS_LOGIN.getValue(), params.getAccount());
- RedissonUtils.putString(key, params.getCaptcha(), RedisConstant.SMS_VERIFY_CODE_VALID_TIME);
- String captcha = RedissonUtils.getString(key);
- if (params.getLoginMethod() == LoginMethodEnum.SMS.getValue() && StringUtils.isBlank(captcha)) {
- return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "验证码已过期,请重新获取!");
- }
- if (params.getLoginMethod() == LoginMethodEnum.SMS.getValue() && StringUtils.isBlank(captcha)) {
- return HttpResult.error(HttpStatus.PARAMETERS_MISSING_CODE, "验证码已过期,请重新获取!");
- }
- if (params.getLoginMethod() == LoginMethodEnum.SMS.getValue() && !captcha.equals(params.getCaptcha())) {
- return HttpResult.error(HttpStatus.UN_LOGIN_CODE, "验证码不正确,请检查并重新输入!");
- }
- return HttpResult.ok();
- }
- /**--------------------------------------------------------------------------------------------------------------**/
- /**
- * @param reqVo 登录入参
- * @return HttpResult
- * @desc: 用户登录
- * @author: czh
- * @date: 2023/6/16
- */
- @PostMapping("/login")
- public HttpResult login(@Valid @RequestBody LoginReqVo reqVo,
- @RequestHeader(name = "Client-Type", required = true) String clientType,
- @RequestHeader(name = "System-Type", required = true) int systemType) throws SystemException {
- reqVo.setSystemType(systemType);
- reqVo.setClientType(clientType);
- return HttpResult.ok(authService.login(reqVo));
- }
- /**
- * @param reqVo 注册
- * @return HttpResult
- * @desc: 用户注册
- * @author: czh
- * @date: 2023/6/16
- */
- @PostMapping("/register")
- public HttpResult register(@Valid @RequestBody RegisterReqVo reqVo,
- @RequestHeader(name = "System-Type", required = true) int systemType) throws SystemException {
- reqVo.setSystemType(systemType);
- authService.register(reqVo);
- return HttpResult.ok(HttpStatus.MSG_007);
- }
- /**
- * @param reqVo 忘记密码入参
- * @return HttpResult
- * @desc: 忘记密码
- * @author: czh
- * @date: 2023/6/19
- */
- @PostMapping("/forgetPassword")
- public HttpResult forgetPassword(@Valid @RequestBody ForgetPasswordReqVo reqVo,
- @RequestHeader(name = "System-Type", required = true) int systemType) throws SystemException {
- reqVo.setSystemType(systemType);
- authService.forgetPassword(reqVo);
- return HttpResult.ok(HttpStatus.MSG_002);
- }
- /**
- * @param {password 旧密码、newPassword 新密码、account 账号}
- * @description 修改密码
- * @author zk
- * @date 2023/06/02
- **/
- @PostMapping("/updatePassword")
- public HttpResult updatePassword(@Valid @RequestBody UpdatePasswordReqVo reqVo,
- @RequestHeader(name = "System-Type", required = true) int systemType) {
- reqVo.setSystemType(systemType);
- authService.updatePassword(reqVo);
- return HttpResult.ok(HttpStatus.MSG_002);
- }
- /**
- * @param id 主键ID
- * @description 重置密码
- * @author zk
- * @date 2023/06/02
- **/
- @PostMapping("/resetPassword")
- public HttpResult resetPassword(@RequestParam Long id) {
- authService.resetPassword(id);
- return HttpResult.ok(HttpStatus.MSG_001);
- }
- /**
- * @desc: 获取验证码
- * @param: reqVo 入参
- * @author: czh
- * @date 2023/7/23
- * @return HttpResult
- */
- @PostMapping("getRegisterSms")
- public HttpResult getRegisterSms(@Valid @RequestBody GetRegisterSmsReqVo reqVo,
- @RequestHeader(name = "System-Type", required = true) int systemType) {
- reqVo.setSystemType(systemType);
- return HttpResult.ok(authService.getRegisterSms(reqVo));
- }
- }
|