“mitao”
2026-03-24 e7f9345c528ab4d6f18d8e450d8178845989d5e1
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
package com.tcm.training.admin.auth.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tcm.training.admin.auth.dto.LoginRequest;
import com.tcm.training.admin.auth.vo.LoginResponse;
import com.tcm.training.admin.auth.vo.UserVO;
import com.tcm.training.admin.user.entity.SysUser;
import com.tcm.training.admin.user.service.RbacService;
import com.tcm.training.admin.user.service.SysUserService;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
 
import java.time.LocalDateTime;
import java.util.List;
 
@Service
public class AuthService {
 
    private final SysUserService sysUserService;
    private final BCryptPasswordEncoder passwordEncoder;
    private final CaptchaService captchaService;
    private final RbacService rbacService;
 
    public AuthService(
            SysUserService sysUserService,
            BCryptPasswordEncoder passwordEncoder,
            CaptchaService captchaService,
            RbacService rbacService
    ) {
        this.sysUserService = sysUserService;
        this.passwordEncoder = passwordEncoder;
        this.captchaService = captchaService;
        this.rbacService = rbacService;
    }
 
    public LoginResponse login(LoginRequest req) {
        captchaService.validate(req.getCaptchaId(), req.getCaptchaCode());
 
        SysUser user = sysUserService.getOne(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getUsername, req.getUsername())
                .eq(SysUser::getDeleted, 0)
                .last("limit 1"));
        if (user == null) {
            throw new IllegalArgumentException("账号或密码错误");
        }
        if (user.getStatus() != null && user.getStatus() != 1) {
            throw new IllegalArgumentException("账号已被禁用");
        }
        if (!passwordEncoder.matches(req.getPassword(), user.getPassword())) {
            throw new IllegalArgumentException("账号或密码错误");
        }
 
        StpUtil.login(user.getId());
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        user.setLastLoginAt(LocalDateTime.now());
        sysUserService.updateById(user);
 
        UserVO userVO = buildUserVO(user);
        return new LoginResponse(tokenInfo.getTokenValue(), "Bearer", tokenInfo.getTokenTimeout(), userVO);
    }
 
    public void logout() {
        StpUtil.logout();
    }
 
    public UserVO me() {
        Long userId = StpUtil.getLoginIdAsLong();
        SysUser user = sysUserService.getById(userId);
        if (user == null || (user.getDeleted() != null && user.getDeleted() == 1)) {
            throw new IllegalArgumentException("用户不存在");
        }
        return buildUserVO(user);
    }
 
    private UserVO buildUserVO(SysUser user) {
        List<String> roleCodes = rbacService.getRoleCodesByUserId(user.getId());
        List<String> permissionKeys = rbacService.getPermissionKeysByUserId(user.getId());
        return new UserVO(
                user.getId(),
                user.getUsername(),
                user.getNickname(),
                user.getRealName(),
                user.getPhone(),
                roleCodes,
                permissionKeys
        );
    }
}