liujie
2026-06-09 c96fbe6b0aeb413a17c429133ee6d81d138144b9
xp-robotaxi-account-boot/src/main/java/com/xiaopeng/account/controller/TokenController.java
@@ -1,6 +1,7 @@
package com.xiaopeng.account.controller;
import com.xiaopeng.account.constant.I18nKeyConstant;
import com.xiaopeng.account.service.ISysGroupService;
import com.xiaopeng.account.service.ISysUserService;
import com.xiaopeng.account.service.IXpOperationsUserService;
import com.xiaopeng.account.service.impl.SysLoginService;
@@ -18,6 +19,7 @@
import com.xiaopeng.common.core.domain.R;
import com.xiaopeng.common.core.exception.ServiceException;
import com.xiaopeng.common.core.utils.JwtUtils;
import com.xiaopeng.common.core.utils.SensitiveDataCryptoUtil;
import com.xiaopeng.common.core.utils.StringUtils;
import com.xiaopeng.common.redis.service.RedisService;
import com.xiaopeng.common.security.auth.AuthUtil;
@@ -27,7 +29,10 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@@ -51,8 +56,12 @@
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private IXpOperationsUserService xpOperationsUserService;
    @Autowired
    private ISysGroupService sysGroupService;
    @Autowired
    private RedisService redisService;
@@ -65,22 +74,25 @@
    // @Autowired
    // private EncryptionDecryptionUtil encryptionDecryptionUtil;
    @ApiOperation(value = "用户登录", tags = "管理后台-用户登录")
    @PostMapping("/base/v1/login")
    public R<?> login(@RequestBody LoginBody form, HttpServletRequest request) {
        // 用户登录
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword(), request);
        HashMap<String, Object> map = new HashMap<>();
        map.put("token", tokenService.createToken(userInfo));
        List<SysRole> roles = userInfo.getSysUser().getRoles();
        // if (CollectionUtils.isEmpty(roles)) {
        //     throw new ServiceException("请关联角色!", I18nKeyConstant.LOGIN_ROLE_NOT_BIND);
        // }
        // map.put("roleName",roles.get(0).getRoleName());
      map.put("info", userInfo);
      // 修改用户最后登录时间
        return R.ok(map);
    }
//    @ApiOperation(value = "用户登录", tags = "管理后台-用户登录")
//    @PostMapping("/base/v1/login")
//    public R<?> login(@RequestBody LoginBody form, HttpServletRequest request) {
//        // 用户登录
//        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword(), request);
//        boolean phoneDesensitization = sysGroupService.getPhoneDesensitization();
//        userInfo.setPhoneDesensitizationExempt(phoneDesensitization);
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("token", tokenService.createToken(userInfo));
//        List<SysRole> roles = userInfo.getSysUser().getRoles();
//        // if (CollectionUtils.isEmpty(roles)) {
//        //     throw new ServiceException("请关联角色!", I18nKeyConstant.LOGIN_ROLE_NOT_BIND);
//        // }
//        // map.put("roleName",roles.get(0).getRoleName());
//      map.put("info", userInfo);
//      // 修改用户最后登录时间
//        return R.ok(map);
//    }
   /* @ApiOperation(value = "解密测试", tags = "管理后台-用户登录")
    @PostMapping("/base/v1/decrypted")
    public R<?> decrypted(@RequestBody LoginBody form, HttpServletRequest request) {
@@ -111,90 +123,96 @@
        response.setHeader("xps", encrypt.getString("aes_key"));
        return R.ok(encrypt.getString("content"));
    }*/
    @ApiOperation(value = "账号密码登录", tags = {"运维APP登录"})
    @PostMapping("/maintenance-app/base/v1/operationPasswordLogin")
    public R<?> operationPasswordLogin(@RequestBody AppLoginPasswordDTO appLoginPasswordDTO, HttpServletRequest request) {
        // 用户登录
        LoginOperationUser userInfo = sysLoginService.operationPasswordLogin(appLoginPasswordDTO.getPhoneNumber(), appLoginPasswordDTO.getPassword(), appLoginPasswordDTO.getDeviceType(), request);
        HashMap<String, Object> map = new HashMap<>();
        map.put("token", tokenService.createOperationUserToken(userInfo));
//        List<SysRole> roles = userInfo.getXpOperationsUser().getRoles();
//        if (CollectionUtils.isEmpty(roles)) {
//            return R.fail("请关联角色!");
//    @ApiOperation(value = "账号密码登录", tags = {"运维APP登录"})
//    @PostMapping("/maintenance-app/base/v1/operationPasswordLogin")
//    public R<?> operationPasswordLogin(@RequestBody AppLoginPasswordDTO appLoginPasswordDTO, HttpServletRequest request) {
//        // 用户登录
//        LoginOperationUser userInfo = sysLoginService.operationPasswordLogin(appLoginPasswordDTO.getPhoneNumber(), appLoginPasswordDTO.getPassword(), appLoginPasswordDTO.getDeviceType(), request);
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("token", tokenService.createOperationUserToken(userInfo));
////        List<SysRole> roles = userInfo.getXpOperationsUser().getRoles();
////        if (CollectionUtils.isEmpty(roles)) {
////            return R.fail("请关联角色!");
////        }
////        map.put("roleName", roles.get(0).getRoleName());
//        map.put("info", userInfo);
//        return R.ok(map);
//    }
//    @ApiOperation(value = "手机验证码登录", tags = {"运维APP登录"})
//    @PostMapping("/maintenance-app/base/v1/verificationCodeLogin")
//    public R<?> verificationCodeLogin(@RequestBody AppLoginCodeDTO appLoginCodeDTO, HttpServletRequest request) {
//        // 用户登录
//        LoginOperationUser userInfo = sysLoginService.verificationCodeLogin(appLoginCodeDTO.getPhoneNumber(), appLoginCodeDTO.getVerificationCode(),appLoginCodeDTO.getDeviceType(), request);
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("token", tokenService.createOperationUserToken(userInfo));
////        List<SysRole> roles = userInfo.getXpOperationsUser().getRoles();
////        if (CollectionUtils.isEmpty(roles)) {
////            return R.fail("请关联角色!");
////        }
////        map.put("roleName", roles.get(0).getRoleName());
//        map.put("info", userInfo);
//        return R.ok(map);
//    }
//    @ApiOperation(value = "获取手机验证码", tags = {"运维APP登录"})
//    @PostMapping("/maintenance-app/base/v1/getVerificationCode")
//    public R<?> verificationCodeLogin(@RequestBody AppLoginGetCodeDTO appLoginPasswordDTO) {
//        if (StringUtils.isBlank(appLoginPasswordDTO.getPhoneNumber())) {
//            return R.fail("手机号不能为空!");
//        }
//        map.put("roleName", roles.get(0).getRoleName());
        map.put("info", userInfo);
        return R.ok(map);
    }
    @ApiOperation(value = "手机验证码登录", tags = {"运维APP登录"})
    @PostMapping("/maintenance-app/base/v1/verificationCodeLogin")
    public R<?> verificationCodeLogin(@RequestBody AppLoginCodeDTO appLoginCodeDTO, HttpServletRequest request) {
        // 用户登录
        LoginOperationUser userInfo = sysLoginService.verificationCodeLogin(appLoginCodeDTO.getPhoneNumber(), appLoginCodeDTO.getVerificationCode(),appLoginCodeDTO.getDeviceType(), request);
        HashMap<String, Object> map = new HashMap<>();
        map.put("token", tokenService.createOperationUserToken(userInfo));
//        List<SysRole> roles = userInfo.getXpOperationsUser().getRoles();
//        if (CollectionUtils.isEmpty(roles)) {
//            return R.fail("请关联角色!");
//        XpOperationsUser xpOperationsUser = xpOperationsUserService.lambdaQuery()
//                .in(XpOperationsUser::getPhoneNumber, SensitiveDataCryptoUtil.queryValues(appLoginPasswordDTO.getPhoneNumber()))
//                .last("limit 1").one();
//        if (xpOperationsUser == null) {
//            return R.fail("账号不存在!");
//        }
//        map.put("roleName", roles.get(0).getRoleName());
        map.put("info", userInfo);
        return R.ok(map);
    }
//        if (!xpOperationsUser.getStatus()) {
//            return R.fail("账号已禁用!");
//        }
//        // 生成6位数短信验证码
//        String code = String.valueOf((int) (Math.random() * 1000000));
//        String codeType = "";
//        switch (appLoginPasswordDTO.getCodeType()) {
//            case 1:
//            codeType = VerificationCodeConstants.LOGIN_CODE;
//                break;
//            case 2:
//            codeType = VerificationCodeConstants.FORGET_PASSWORD_CODE;
//
//            break;
//         case 3:
//            codeType = VerificationCodeConstants.UPDATE_PASSWORD_CODE;
//            break;
//        }
//        SmsVo vo = new SmsVo();
//        vo.setAppId(sms_appid);
//        vo.setPushType(1);
//        vo.setReceivers(appLoginPasswordDTO.getPhoneNumber());
//        vo.setTemplateCode("RobotaxiVerifyCodeSmsTpl");
//        vo.setAppPushConfigCode("RobotaxiVerifyCodeSms");
//        vo.setParams(new HashMap<>());
//        vo.getParams().put("code", code);
//        SmsDto smsDto = smsClient.pushSms(vo);
//        if(null == smsDto || smsDto.getCode() != 200){
//         throw new ServiceException("短信发送失败", I18nKeyConstant.SMS_PUSH_FAILED);
//        }
//        redisService.setCacheObject(codeType+appLoginPasswordDTO.getPhoneNumber(), code, 60L, TimeUnit.SECONDS);
//        return R.ok();
//    }
    @ApiOperation(value = "获取手机验证码", tags = {"运维APP登录"})
    @PostMapping("/maintenance-app/base/v1/getVerificationCode")
    public R<?> verificationCodeLogin(@RequestBody AppLoginGetCodeDTO appLoginPasswordDTO) {
        XpOperationsUser xpOperationsUser = xpOperationsUserService.lambdaQuery()
                .eq(XpOperationsUser::getPhoneNumber, appLoginPasswordDTO.getPhoneNumber())
                .last("limit 1").one();
        if (xpOperationsUser == null) {
            return R.fail("账号不存在!");
        }
        if (!xpOperationsUser.getStatus()) {
            return R.fail("账号已禁用!");
        }
        // 生成6位数短信验证码
        String code = String.valueOf((int) (Math.random() * 1000000));
        String codeType = "";
        switch (appLoginPasswordDTO.getCodeType()) {
            case 1:
            codeType = VerificationCodeConstants.LOGIN_CODE;
                break;
            case 2:
            codeType = VerificationCodeConstants.FORGET_PASSWORD_CODE;
            break;
         case 3:
            codeType = VerificationCodeConstants.UPDATE_PASSWORD_CODE;
            break;
        }
        SmsVo vo = new SmsVo();
        vo.setAppId(sms_appid);
        vo.setPushType(1);
        vo.setReceivers(appLoginPasswordDTO.getPhoneNumber());
        vo.setTemplateCode("RobotaxiVerifyCodeSmsTpl");
        vo.setAppPushConfigCode("RobotaxiVerifyCodeSms");
        vo.setParams(new HashMap<>());
        vo.getParams().put("code", code);
        SmsDto smsDto = smsClient.pushSms(vo);
        if(null == smsDto || smsDto.getCode() != 200){
         throw new ServiceException("短信发送失败", I18nKeyConstant.SMS_PUSH_FAILED);
        }
        redisService.setCacheObject(codeType+appLoginPasswordDTO.getPhoneNumber(), code, 60L, TimeUnit.SECONDS);
        return R.ok();
    }
   @ApiOperation(value = "修改密码", tags = {"运维APP登录"})
   @PostMapping("/maintenance-app/base/v1/updatePassword")
   public R<?> updatePassword(@RequestBody UpdatePasswordDTO updatePasswordDTO, HttpServletRequest request) {
      // 用户登录
      if (sysLoginService.verificationCodeUpdate(updatePasswordDTO, request)){
         return R.ok("修改成功,请重新登录");
      }else{
         return R.fail("修改失败");
      }
   }
//   @ApiOperation(value = "修改密码", tags = {"运维APP登录"})
//   @PostMapping("/maintenance-app/base/v1/updatePassword")
//   public R<?> updatePassword(@RequestBody UpdatePasswordDTO updatePasswordDTO, HttpServletRequest request) {
//      // 用户登录
//      if (sysLoginService.verificationCodeUpdate(updatePasswordDTO, request)){
//         return R.ok("修改成功,请重新登录");
//      }else{
//         return R.fail("修改失败");
//      }
//   }
    @DeleteMapping("/api/v1/logout")
    public R<?> logout(HttpServletRequest request) {