| | |
| | | package com.ruoyi.common.helper; |
| | | |
| | | import cn.dev33.satoken.context.SaHolder; |
| | | import cn.dev33.satoken.stp.StpUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.ruoyi.common.constant.UserConstants; |
| | | import com.ruoyi.common.core.domain.model.LoginUser; |
| | | import com.ruoyi.common.enums.DeviceType; |
| | | import com.ruoyi.common.enums.UserType; |
| | |
| | | |
| | | /** |
| | | * 登录鉴权助手 |
| | | * 为适配多端登录而封装 |
| | | * |
| | | * user_type 为 用户类型 同一个用户表 可以有多种用户类型 例如 pc,app |
| | | * deivce 为 设备类型 同一个用户类型 可以有 多种设备类型 例如 web,ios |
| | | * 可以组成 用户类型与设备类型多对多的 权限灵活控制 |
| | | * |
| | | * 多用户体系 针对 多种用户类型 但权限控制不一致 |
| | | * 可以组成 多用户类型表与多设备类型 分别控制权限 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @NoArgsConstructor(access = AccessLevel.PRIVATE) |
| | | public class LoginHelper { |
| | | |
| | | private static final String LOGIN_USER_KEY = "loginUser"; |
| | | private static final ThreadLocal<LoginUser> LOGIN_CACHE = new ThreadLocal<>(); |
| | | public static final String JOIN_CODE = ":"; |
| | | public static final String LOGIN_USER_KEY = "loginUser"; |
| | | |
| | | /** |
| | | * 登录系统 |
| | | * 针对两套用户体系 |
| | | * |
| | | * @param loginUser 登录用户信息 |
| | | */ |
| | | public static void login(LoginUser loginUser) { |
| | | SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser); |
| | | StpUtil.login(loginUser.getLoginId()); |
| | | setLoginUser(loginUser); |
| | | } |
| | | |
| | | /** |
| | | * 登录系统 基于 设备类型 |
| | | * 针对一套用户体系 |
| | | * 针对相同用户体系不同设备 |
| | | * |
| | | * @param loginUser 登录用户信息 |
| | | */ |
| | | public static void loginByDevice(LoginUser loginUser, DeviceType deviceType) { |
| | | SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser); |
| | | StpUtil.login(loginUser.getLoginId(), deviceType.getDevice()); |
| | | setLoginUser(loginUser); |
| | | } |
| | |
| | | */ |
| | | public static void setLoginUser(LoginUser loginUser) { |
| | | StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser); |
| | | LOGIN_CACHE.set(loginUser); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户(多级缓存) |
| | | */ |
| | | public static LoginUser getLoginUser() { |
| | | LoginUser loginUser = LOGIN_CACHE.get(); |
| | | LoginUser loginUser = (LoginUser) SaHolder.getStorage().get(LOGIN_USER_KEY); |
| | | if (loginUser != null) { |
| | | return loginUser; |
| | | } |
| | | return (LoginUser) StpUtil.getTokenSession().get(LOGIN_USER_KEY); |
| | | } |
| | | |
| | | /** |
| | | * 清除一级缓存 防止内存问题 |
| | | */ |
| | | public static void clearCache() { |
| | | LOGIN_CACHE.remove(); |
| | | loginUser = (LoginUser) StpUtil.getTokenSession().get(LOGIN_USER_KEY); |
| | | SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser); |
| | | return loginUser; |
| | | } |
| | | |
| | | /** |
| | |
| | | String userId = null; |
| | | for (UserType value : UserType.values()) { |
| | | if (StringUtils.contains(loginId, value.getUserType())) { |
| | | userId = StringUtils.replace(loginId, value.getUserType(), StringUtils.EMPTY); |
| | | String[] strs = StringUtils.split(loginId, JOIN_CODE); |
| | | // 用户id在总是在最后 |
| | | userId = strs[strs.length - 1]; |
| | | } |
| | | } |
| | | if (StringUtils.isBlank(userId)) { |
| | |
| | | return UserType.getUserType(loginId); |
| | | } |
| | | |
| | | /** |
| | | * 是否为管理员 |
| | | * |
| | | * @param userId 用户ID |
| | | * @return 结果 |
| | | */ |
| | | public static boolean isAdmin(Long userId) { |
| | | return UserConstants.ADMIN_ID.equals(userId); |
| | | } |
| | | |
| | | public static boolean isAdmin() { |
| | | return isAdmin(getUserId()); |
| | | } |
| | | |
| | | } |