puzhibing
2026-02-26 493b0377ea1d14ffd5e8f11d2af200c8a5c22dd5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package com.xiaopeng.account.controller;
 
import com.xiaopeng.account.constant.I18nKeyConstant;
import com.xiaopeng.account.service.ISysUserService;
import com.xiaopeng.account.service.IXpOperationsUserService;
import com.xiaopeng.account.service.impl.SysLoginService;
import com.xiaopeng.account.vo.LoginBody;
import com.xiaopeng.account.vo.RegisterBody;
import com.xiaopeng.api.dto.operation.*;
import com.xiaopeng.api.feignClient.operation.SmsClient;
import com.xiaopeng.api.model.account.SysRole;
import com.xiaopeng.api.model.account.XpOperationsUser;
import com.xiaopeng.api.vo.account.LoginOperationUser;
import com.xiaopeng.api.vo.account.LoginUser;
import com.xiaopeng.api.vo.operation.SmsVo;
import com.xiaopeng.common.core.constant.VerificationCodeConstants;
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.StringUtils;
import com.xiaopeng.common.redis.service.RedisService;
import com.xiaopeng.common.security.auth.AuthUtil;
import com.xiaopeng.common.security.service.TokenService;
import com.xiaopeng.common.security.utils.SecurityUtils;
import io.swagger.annotations.ApiOperation;
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.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;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
/**
 * token 控制
 *
 * @author xiaopeng
 */
@Slf4j
@RestController
public class TokenController {
    @Autowired
    private TokenService tokenService;
    
    @Autowired
    private SysLoginService sysLoginService;
 
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private IXpOperationsUserService xpOperationsUserService;
 
    @Autowired
    private RedisService redisService;
 
    @Resource
    private SmsClient smsClient;
 
    @Value("${sms.appid}")
    private String sms_appid;
 
    @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 = {"运维APP登录"})
    @PostMapping("base/v1/operationPasswordLogin")
    public R<?> operationPasswordLogin(@RequestBody AppLoginPasswordDTO appLoginPasswordDTO, HttpServletRequest request) {
        // 用户登录
        LoginOperationUser userInfo = sysLoginService.operationPasswordLogin(appLoginPasswordDTO.getPhoneNumber(), appLoginPasswordDTO.getPassword(), 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("base/v1/verificationCodeLogin")
    public R<?> verificationCodeLogin(@RequestBody AppLoginCodeDTO appLoginCodeDTO, HttpServletRequest request) {
        // 用户登录
        LoginOperationUser userInfo = sysLoginService.verificationCodeLogin(appLoginCodeDTO.getPhoneNumber(), appLoginCodeDTO.getVerificationCode(), 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("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("账号已禁用!");
        }
        // todo 后续补充验证码
        // 生成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("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) {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token)) {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username, request);
        }
        return R.ok();
    }
 
 
    @PostMapping("refresh")
    public R<?> refresh(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser)) {
            // 刷新令牌有效期
            tokenService.refreshToken(loginUser);
            return R.ok();
        }
        return R.ok();
    }
 
    @PostMapping("register")
    public R<?> register(@RequestBody RegisterBody registerBody, HttpServletRequest request) {
        // 用户注册
        sysLoginService.register(registerBody.getUsername(), registerBody.getPassword(), request);
        return R.ok();
    }
 
}