Pu Zhibing
2025-02-19 8f45afced0c6a4085560c62dbd58e6ef0f4cecf4
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
package com.ruoyi.sange.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.sange.domain.SystemUser;
import com.ruoyi.sange.service.ISystemUserService;
import com.ruoyi.sange.util.JwtUtils;
import com.ruoyi.sange.util.SecurityConstants;
import com.ruoyi.sange.util.ServletUtils;
import com.ruoyi.sange.util.TokenService;
import com.ruoyi.sange.warpper.LoginVo;
import com.ruoyi.sange.warpper.TokenVo;
import com.ruoyi.web.tool.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
 
/**
 * @author zhibing.pu
 * @Date 2025/2/18 22:30
 */
@Api
@RestController
@RequestMapping("/api/systemUser")
public class SystemUserController {
    
    @Resource
    private ISystemUserService systemUserService;
    
    @Resource
    private RedisTemplate redisTemplate;
    
    private final Integer max_err = 5;
    
    @Resource
    private TokenService tokenService;
    
    
    @PostMapping("/login")
    @ApiOperation(value = "登录", tags = {"管理后台-登录"})
    public R<TokenVo> login(@RequestBody LoginVo vo){
        String key = "login:" + vo.getPhone();
        Integer size = (Integer) redisTemplate.opsForValue().get(key);
        if(null != size && max_err.equals(size)){
            return R.fail("连续登录失败,请稍后重试。");
        }
        if(null == size){
            size = 0;
        }
        SystemUser systemUser = systemUserService.getOne(new LambdaQueryWrapper<SystemUser>()
                .eq(SystemUser::getPhone, vo.getPhone()).ne(SystemUser::getStatus, 3));
        if(null == systemUser){
            size++;
            redisTemplate.opsForValue().set(key, size, 5, TimeUnit.MINUTES);
            return R.fail("登录失败,手机号/密码错误。");
        }
        if(!vo.getPassword().equals(systemUser.getPassword())){
            size++;
            redisTemplate.opsForValue().set(key, size, 5, TimeUnit.MINUTES);
            return R.fail("登录失败,手机号/密码错误。");
        }
        if(2 == systemUser.getStatus()){
            return R.fail("当前账号已冻结。");
        }
        //创建token
        String userKey = UUID.randomUUID().toString();
        Map<String, Object> claims = new HashMap<>();
        claims.put(SecurityConstants.USER_KEY, userKey);
        String token = JwtUtils.createToken(claims);
        //token有效期2小时
        redisTemplate.opsForValue().set("token:" + userKey, token, SecurityConstants.expireTime, TimeUnit.MILLISECONDS);
        redisTemplate.delete(key);
        TokenVo tokenVo = new TokenVo();
        tokenVo.setToken(token);
        tokenVo.setExpireTime(SecurityConstants.expireTime);
        return R.ok(tokenVo);
    }
    
    
    @PostMapping("/logout")
    @ApiOperation(value = "退出登录", tags = {"管理后台-登录"})
    public R logout(){
        String token = tokenService.getToken(ServletUtils.getRequest());
        if(StringUtils.isEmpty(token)){
            return R.fail("请先登录");
        }
        String userKey = JwtUtils.getUserKey(token);
        redisTemplate.delete("token:" + userKey);
        return R.ok();
    }
}