935090232@qq.com
2022-04-20 281dc31c4a4d4e2cf266dd65a470c7d29e4e7332
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
/**
 * projectName: zq-erp
 * fileName: UserCacheManager.java
 * packageName: com.matrix.system.common.init
 * date: 2021-10-25 16:58
 * copyright(c) 2021 http://www.hydee.cn/ Inc. All rights reserved.
 */
package com.matrix.system.common.init;
 
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.core.tools.WebUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import java.util.Objects;
 
/**
 * @version: V1.0
 * @author: JiangYouYao
 * @className: UserCacheManager
 * @packageName: com.matrix.system.common.init
 * @description: 用户缓存管理
 * @data: 2021-10-25 16:58
 **/
@Component
public class UserCacheManager {
 
    @Value("${debug}")
    private String isDebug;
 
 
    /**
     * 判断用户是否已经登录过
     *
     * @return
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2018年1月19日
     * 会从http请求投中获取token进行验证
     */
    public boolean isUserLogin() {
        String token = getUserToken();
        if (StringUtils.isNotBlank(token)) {
            return LocalCache.get(token) != null;
        } else {
            return false;
        }
    }
 
    /**
     * 在本地缓存中保存用户信息,并且返回保存的token
     *
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2018年1月19日
     * @return 返回保存用户信息的token
     */
    public String saveUserInfo(Object obj) {
        String token = UUIDUtil.getRandomID();
        LocalCache.save(token, obj);
       return token;
    }
 
    /**
     * 用户退出
     *
     * @author:吕敬瑛
     * @date:2018年1月19日下午3:17:14
     */
    public void loginOut() {
        LocalCache.remove(getUserToken());
    }
 
    /**
     * 更新用户缓存
     */
    public void updateUserInfo(Object user) {
        LocalCache.save(getUserToken(),user);
    }
 
    /**
     * 获取登录用户信息,如果 获取失败将会抛出异常
     *
     * @return
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2018年1月19日
     */
    public <T> T getLoginUser() {
        String userToken = getUserToken();
        if (userToken != null) {
            T user = LocalCache.get(userToken);
            if (Objects.nonNull(user)) {
                //重新设置key过期时间
                LocalCache.resetExpire(userToken);
                return user;
            } else {
                throw new GlobleException(SystemErrorCode.REQUEST_INVALID);
            }
        } else {
            throw new GlobleException(SystemErrorCode.REQUEST_INVALID);
        }
    }
 
    public String getUserToken() {
        String token = WebUtil.getRequest().getHeader("token");
        // debug模式可以从url参数中获取token
        if (StringUtils.isBlank(token) && "true".equals(isDebug)) {
            if (token == null) {
                token = WebUtil.getRequest().getParameter("token");
            }
        }
        return token;
    }
 
 
}