¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.common.utils; |
| | | |
| | | import cn.dev33.satoken.stp.StpUtil; |
| | | import com.ruoyi.common.enums.DeviceType; |
| | | import com.ruoyi.common.enums.UserType; |
| | | import com.ruoyi.common.exception.UtilException; |
| | | |
| | | /** |
| | | * ç»å½é´æå·¥å
· |
| | | * 为éé
å¤ç«¯ç»å½èå°è£
|
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class LoginUtils { |
| | | |
| | | /** |
| | | * ç»å½ç³»ç» |
| | | * é对两å¥ç¨æ·ä½ç³» |
| | | * @param userId ç¨æ·id |
| | | */ |
| | | public static void login(Long userId, UserType userType) { |
| | | StpUtil.login(userType.getUserType() + userId); |
| | | } |
| | | |
| | | /** |
| | | * ç»å½ç³»ç» åºäº 设å¤ç±»å |
| | | * é对ä¸å¥ç¨æ·ä½ç³» |
| | | * @param userId ç¨æ·id |
| | | */ |
| | | public static void loginByDevice(Long userId, UserType userType, DeviceType deviceType) { |
| | | StpUtil.login(userType.getUserType() + userId, deviceType.getDevice()); |
| | | } |
| | | |
| | | /** |
| | | * è·åç¨æ·id |
| | | */ |
| | | public static Long getUserId() { |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | | * è·åç¨æ·ç±»å |
| | | */ |
| | | 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); |
| | | } |
| | | } |
| | | |
| | | } |