疯狂的狮子li
2022-01-13 6ca853516671882e8e54d9630a4299f12fbcf9aa
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
package com.ruoyi.common.utils;
 
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.DeviceType;
import com.ruoyi.common.enums.UserType;
import com.ruoyi.common.exception.UtilException;
 
/**
 * 登录鉴权工具
 * 为适配多端登录而封装
 *
 * @author Lion Li
 */
public class LoginUtils {
 
    private final static String LOGIN_USER_KEY = "loginUser";
 
    /**
     * 登录系统
     * 针对两套用户体系
     * @param loginUser 登录用户信息
     */
    public static void login(LoginUser loginUser, UserType userType) {
        StpUtil.login(userType.getUserType() + loginUser.getUserId());
        setLoginUser(loginUser);
    }
 
    /**
     * 登录系统 基于 设备类型
     * 针对一套用户体系
     * @param loginUser 登录用户信息
     */
    public static void loginByDevice(LoginUser loginUser, UserType userType, DeviceType deviceType) {
        StpUtil.login(userType.getUserType() + loginUser.getUserId(), deviceType.getDevice());
        setLoginUser(loginUser);
    }
 
    /**
     * 设置用户数据
     */
    public static void setLoginUser(LoginUser loginUser) {
        StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser);
    }
 
    /**
     * 获取用户
     **/
    public static LoginUser getLoginUser() {
        return (LoginUser) StpUtil.getTokenSession().get(LOGIN_USER_KEY);
    }
 
    /**
     * 获取用户id
     */
    public static Long getUserId() {
        LoginUser loginUser = getLoginUser();
        if (ObjectUtil.isNull(loginUser)) {
            String loginId = StpUtil.getLoginIdAsString();
            String userId;
            String replace = "";
            if (StringUtils.contains(loginId, UserType.SYS_USER.getUserType())) {
                userId = StringUtils.replace(loginId, UserType.SYS_USER.getUserType(), replace);
            } else if (StringUtils.contains(loginId, UserType.APP_USER.getUserType())){
                userId = StringUtils.replace(loginId, UserType.APP_USER.getUserType(), replace);
            } else {
                throw new UtilException("登录用户: LoginId异常 => " + loginId);
            }
            return Long.parseLong(userId);
        }
        return loginUser.getUserId();
    }
 
    /**
     * 获取部门ID
     **/
    public static Long getDeptId() {
        return getLoginUser().getDeptId();
    }
 
    /**
     * 获取用户账户
     **/
    public static String getUsername() {
        return getLoginUser().getUsername();
    }
 
    /**
     * 获取用户类型
     */
    public static UserType getUserType() {
        String loginId = StpUtil.getLoginIdAsString();
        return getUserType(loginId);
    }
 
    public static UserType getUserType(Object loginId) {
        if (StringUtils.contains(loginId.toString(), UserType.SYS_USER.getUserType())) {
            return UserType.SYS_USER;
        } else if (StringUtils.contains(loginId.toString(), UserType.APP_USER.getUserType())){
            return UserType.APP_USER;
        } else {
            throw new UtilException("登录用户: LoginId异常 => " + loginId);
        }
    }
 
}