From 8b45cb19638e359bec4e35fa20038aa0efbc776e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Tue, 10 Oct 2023 12:49:20 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20=E5=B0=86?= =?UTF-8?q?=E9=83=A8=E9=97=A8id=E5=AD=98=E5=85=A5token=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E8=BF=87=E5=BA=A6=E6=9F=A5=E8=AF=A2redis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/satoken/utils/LoginHelper.java | 45 +++++++++---------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/ruoyi-common/ruoyi-common-satoken/src/main/java/org/dromara/common/satoken/utils/LoginHelper.java b/ruoyi-common/ruoyi-common-satoken/src/main/java/org/dromara/common/satoken/utils/LoginHelper.java index 2bbcd0d9..97b7041a 100644 --- a/ruoyi-common/ruoyi-common-satoken/src/main/java/org/dromara/common/satoken/utils/LoginHelper.java +++ b/ruoyi-common/ruoyi-common-satoken/src/main/java/org/dromara/common/satoken/utils/LoginHelper.java @@ -34,6 +34,7 @@ public class LoginHelper { public static final String LOGIN_USER_KEY = "loginUser"; public static final String TENANT_KEY = "tenantId"; public static final String USER_KEY = "userId"; + public static final String DEPT_KEY = "deptId"; public static final String CLIENT_KEY = "clientid"; /** @@ -48,10 +49,12 @@ public class LoginHelper { storage.set(LOGIN_USER_KEY, loginUser); storage.set(TENANT_KEY, loginUser.getTenantId()); storage.set(USER_KEY, loginUser.getUserId()); + storage.set(DEPT_KEY, loginUser.getDeptId()); model = ObjectUtil.defaultIfNull(model, new SaLoginModel()); StpUtil.login(loginUser.getLoginId(), model.setExtra(TENANT_KEY, loginUser.getTenantId()) - .setExtra(USER_KEY, loginUser.getUserId())); + .setExtra(USER_KEY, loginUser.getUserId()) + .setExtra(DEPT_KEY, loginUser.getDeptId())); StpUtil.getSession().set(LOGIN_USER_KEY, loginUser); } @@ -88,41 +91,35 @@ public class LoginHelper { * 获取用户id */ public static Long getUserId() { - Long userId; - try { - userId = Convert.toLong(SaHolder.getStorage().get(USER_KEY)); - if (ObjectUtil.isNull(userId)) { - userId = Convert.toLong(StpUtil.getExtra(USER_KEY)); - SaHolder.getStorage().set(USER_KEY, userId); - } - } catch (Exception e) { - return null; - } - return userId; + return Convert.toLong(getExtra(USER_KEY)); } /** * 获取租户ID */ public static String getTenantId() { - String tenantId; - try { - tenantId = (String) SaHolder.getStorage().get(TENANT_KEY); - if (ObjectUtil.isNull(tenantId)) { - tenantId = (String) StpUtil.getExtra(TENANT_KEY); - SaHolder.getStorage().set(TENANT_KEY, tenantId); - } - } catch (Exception e) { - return null; - } - return tenantId; + return Convert.toStr(getExtra(TENANT_KEY)); } /** * 获取部门ID */ public static Long getDeptId() { - return getLoginUser().getDeptId(); + return Convert.toLong(getExtra(DEPT_KEY)); + } + + private static Object getExtra(String key) { + Object obj; + try { + obj = SaHolder.getStorage().get(key); + if (ObjectUtil.isNull(obj)) { + obj = StpUtil.getExtra(key); + SaHolder.getStorage().set(key, obj); + } + } catch (Exception e) { + return null; + } + return obj; } /**