From 0da30f462392664ba7a76d47de7e67560046d486 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期三, 26 一月 2022 20:58:05 +0800
Subject: [PATCH] update 优化 查询登录用户数据 统一走缓存

---
 /dev/null                                                                                |   28 --------------
 ruoyi-system/src/main/java/com/ruoyi/system/service/SysLoginService.java                 |    2 +
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java         |    3 -
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java        |    6 +-
 ruoyi-framework/src/main/java/com/ruoyi/framework/listener/UserActionListener.java       |    8 +--
 ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java                 |    5 --
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysProfileController.java      |    4 -
 ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/LoginUser.java             |   18 +++++++++
 ruoyi-framework/src/main/java/com/ruoyi/framework/handler/PlusDataPermissionHandler.java |   12 +++---
 9 files changed, 35 insertions(+), 51 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java
index 3c51e09..b87de51 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java
@@ -7,11 +7,10 @@
 import com.ruoyi.common.core.domain.entity.SysMenu;
 import com.ruoyi.common.core.domain.entity.SysUser;
 import com.ruoyi.common.core.domain.model.LoginBody;
-import com.ruoyi.common.core.service.UserService;
 import com.ruoyi.common.helper.LoginHelper;
-import com.ruoyi.common.utils.spring.SpringUtils;
 import com.ruoyi.system.domain.vo.RouterVo;
 import com.ruoyi.system.service.ISysMenuService;
+import com.ruoyi.system.service.ISysUserService;
 import com.ruoyi.system.service.SysLoginService;
 import com.ruoyi.system.service.SysPermissionService;
 import io.swagger.annotations.Api;
@@ -41,6 +40,7 @@
 
     private final SysLoginService loginService;
     private final ISysMenuService menuService;
+    private final ISysUserService userService;
     private final SysPermissionService permissionService;
 
     /**
@@ -78,7 +78,7 @@
     @ApiOperation("鑾峰彇鐢ㄦ埛淇℃伅")
     @GetMapping("getInfo")
     public AjaxResult<Map<String, Object>> getInfo() {
-        SysUser user = SpringUtils.getBean(UserService.class).selectUserById(LoginHelper.getUserId());
+        SysUser user = userService.selectUserById(LoginHelper.getUserId());
         // 瑙掕壊闆嗗悎
         Set<String> roles = permissionService.getRolePermission(user);
         // 鏉冮檺闆嗗悎
diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysProfileController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysProfileController.java
index 0fa8e1e..0a24e7c 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysProfileController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysProfileController.java
@@ -5,12 +5,10 @@
 import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.core.domain.entity.SysUser;
-import com.ruoyi.common.core.service.UserService;
 import com.ruoyi.common.enums.BusinessType;
 import com.ruoyi.common.helper.LoginHelper;
 import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.StringUtils;
-import com.ruoyi.common.utils.spring.SpringUtils;
 import com.ruoyi.system.domain.SysOss;
 import com.ruoyi.system.service.ISysOssService;
 import com.ruoyi.system.service.ISysUserService;
@@ -91,7 +89,7 @@
     @Log(title = "涓汉淇℃伅", businessType = BusinessType.UPDATE)
     @PutMapping("/updatePwd")
     public AjaxResult<Void> updatePwd(String oldPassword, String newPassword) {
-        SysUser user = SpringUtils.getBean(UserService.class).selectUserById(LoginHelper.getUserId());
+        SysUser user = userService.selectUserById(LoginHelper.getUserId());
         String userName = user.getUserName();
         String password = user.getPassword();
         if (!SecurityUtils.matchesPassword(oldPassword, password)) {
diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/LoginUser.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/LoginUser.java
index 4651b00..81e374a 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/LoginUser.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/model/LoginUser.java
@@ -1,10 +1,13 @@
 package com.ruoyi.common.core.domain.model;
 
+import com.ruoyi.common.core.domain.entity.SysDept;
+import com.ruoyi.common.core.domain.entity.SysRole;
 import com.ruoyi.common.helper.LoginHelper;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 
 import java.io.Serializable;
+import java.util.List;
 import java.util.Set;
 
 /**
@@ -85,6 +88,21 @@
     private String username;
 
     /**
+     * 閮ㄩ棬瀵硅薄
+     */
+    private SysDept dept;
+
+    /**
+     * 瑙掕壊瀵硅薄
+     */
+    private List<SysRole> roles;
+
+    /**
+     * 鏁版嵁鏉冮檺 褰撳墠瑙掕壊ID
+     */
+    private Long roleId;
+
+    /**
      * 鑾峰彇鐧诲綍id
      */
     public String getLoginId() {
diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/service/UserService.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/service/UserService.java
deleted file mode 100644
index e637f47..0000000
--- a/ruoyi-common/src/main/java/com/ruoyi/common/core/service/UserService.java
+++ /dev/null
@@ -1,28 +0,0 @@
-package com.ruoyi.common.core.service;
-
-import com.ruoyi.common.core.domain.entity.SysUser;
-
-/**
- * 閫氱敤 鐢ㄦ埛涓氬姟
- *
- * @author Lion Li
- */
-public interface UserService {
-
-    /**
-     * 閫氳繃鐢ㄦ埛鍚嶆煡璇㈢敤鎴�
-     *
-     * @param userName 鐢ㄦ埛鍚�
-     * @return 鐢ㄦ埛瀵硅薄淇℃伅
-     */
-    SysUser selectUserByUserName(String userName);
-
-    /**
-     * 閫氳繃鐢ㄦ埛ID鏌ヨ鐢ㄦ埛
-     *
-     * @param userId 鐢ㄦ埛ID
-     * @return 鐢ㄦ埛瀵硅薄淇℃伅
-     */
-    SysUser selectUserById(Long userId);
-
-}
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/handler/PlusDataPermissionHandler.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/handler/PlusDataPermissionHandler.java
index 07795d9..24a6847 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/handler/PlusDataPermissionHandler.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/handler/PlusDataPermissionHandler.java
@@ -8,12 +8,12 @@
 import com.ruoyi.common.annotation.DataColumn;
 import com.ruoyi.common.annotation.DataPermission;
 import com.ruoyi.common.core.domain.entity.SysRole;
-import com.ruoyi.common.core.domain.entity.SysUser;
-import com.ruoyi.common.core.service.UserService;
+import com.ruoyi.common.core.domain.model.LoginUser;
 import com.ruoyi.common.enums.DataScopeType;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.helper.DataPermissionHelper;
 import com.ruoyi.common.helper.LoginHelper;
+import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.spring.SpringUtils;
 import lombok.extern.slf4j.Slf4j;
@@ -74,13 +74,13 @@
             inavlidCacheSet.add(mappedStatementId);
             return where;
         }
-        SysUser currentUser = DataPermissionHelper.getVariable("user");
+        LoginUser currentUser = DataPermissionHelper.getVariable("user");
         if (ObjectUtil.isNull(currentUser)) {
-            currentUser = SpringUtils.getBean(UserService.class).selectUserById(LoginHelper.getUserId());
+            currentUser = LoginHelper.getLoginUser();
             DataPermissionHelper.setVariable("user", currentUser);
         }
         // 濡傛灉鏄秴绾х鐞嗗憳锛屽垯涓嶈繃婊ゆ暟鎹�
-        if (ObjectUtil.isNull(currentUser) || currentUser.isAdmin()) {
+        if (ObjectUtil.isNull(currentUser) || SecurityUtils.isAdmin(currentUser.getUserId())) {
             return where;
         }
         String dataFilterSql = buildDataFilter(dataColumns, isSelect);
@@ -108,7 +108,7 @@
         StringBuilder sqlString = new StringBuilder();
         // 鏇存柊鎴栧垹闄ら渶婊¤冻鎵�鏈夋潯浠�
         String joinStr = isSelect ? " OR " : " AND ";
-        SysUser user = DataPermissionHelper.getVariable("user");
+        LoginUser user = DataPermissionHelper.getVariable("user");
         StandardEvaluationContext context = new StandardEvaluationContext();
         context.setBeanResolver(beanResolver);
         DataPermissionHelper.getContext().forEach(context::setVariable);
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/listener/UserActionListener.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/listener/UserActionListener.java
index 609c0ac..bf5625f 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/listener/UserActionListener.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/listener/UserActionListener.java
@@ -9,14 +9,12 @@
 import cn.hutool.http.useragent.UserAgentUtil;
 import com.ruoyi.common.constant.Constants;
 import com.ruoyi.common.core.domain.dto.UserOnlineDTO;
-import com.ruoyi.common.core.domain.entity.SysUser;
-import com.ruoyi.common.core.service.UserService;
+import com.ruoyi.common.core.domain.model.LoginUser;
 import com.ruoyi.common.enums.UserType;
 import com.ruoyi.common.helper.LoginHelper;
 import com.ruoyi.common.utils.ServletUtils;
 import com.ruoyi.common.utils.ip.AddressUtils;
 import com.ruoyi.common.utils.redis.RedisUtils;
-import com.ruoyi.common.utils.spring.SpringUtils;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
@@ -44,7 +42,7 @@
         if (userType == UserType.SYS_USER) {
             UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent"));
             String ip = ServletUtils.getClientIP();
-            SysUser user = SpringUtils.getBean(UserService.class).selectUserById(LoginHelper.getUserId());
+            LoginUser user = LoginHelper.getLoginUser();
             String tokenValue = StpUtil.getTokenValue();
             UserOnlineDTO dto = UserOnlineDTO.builder()
                 .ipaddr(ip)
@@ -53,7 +51,7 @@
                 .os(userAgent.getOs().getName())
                 .loginTime(System.currentTimeMillis())
                 .tokenId(tokenValue)
-                .userName(user.getUserName())
+                .userName(user.getUsername())
                 .build();
             if (ObjectUtil.isNotNull(user.getDept())) {
                 dto.setDeptName(user.getDept().getDeptName());
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java
index 118c36f..741ae4b 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java
@@ -3,7 +3,6 @@
 import com.ruoyi.common.core.domain.PageQuery;
 import com.ruoyi.common.core.domain.entity.SysUser;
 import com.ruoyi.common.core.page.TableDataInfo;
-import com.ruoyi.common.core.service.UserService;
 
 import java.util.List;
 
@@ -12,7 +11,7 @@
  *
  * @author Lion Li
  */
-public interface ISysUserService extends UserService {
+public interface ISysUserService {
 
 
     TableDataInfo<SysUser> selectPageUserList(SysUser user, PageQuery pageQuery);
@@ -47,7 +46,6 @@
      * @param userName 鐢ㄦ埛鍚�
      * @return 鐢ㄦ埛瀵硅薄淇℃伅
      */
-    @Override
     SysUser selectUserByUserName(String userName);
 
     /**
@@ -56,7 +54,6 @@
      * @param userId 鐢ㄦ埛ID
      * @return 鐢ㄦ埛瀵硅薄淇℃伅
      */
-    @Override
     SysUser selectUserById(Long userId);
 
     /**
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/SysLoginService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/SysLoginService.java
index a19f76d..a9a6a10 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/SysLoginService.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/SysLoginService.java
@@ -137,6 +137,8 @@
         loginUser.setUserType(user.getUserType());
         loginUser.setMenuPermission(permissionService.getMenuPermission(user));
         loginUser.setRolePermission(permissionService.getRolePermission(user));
+        loginUser.setRoles(user.getRoles());
+        loginUser.setDept(user.getDept());
         return loginUser;
     }
 
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java
index 5194046..72ec867 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java
@@ -10,7 +10,6 @@
 import com.ruoyi.common.core.domain.entity.SysRole;
 import com.ruoyi.common.core.domain.entity.SysUser;
 import com.ruoyi.common.core.page.TableDataInfo;
-import com.ruoyi.common.core.service.UserService;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.helper.LoginHelper;
 import com.ruoyi.common.utils.StringUtils;
@@ -38,7 +37,7 @@
 @Slf4j
 @RequiredArgsConstructor
 @Service
-public class SysUserServiceImpl implements ISysUserService, UserService {
+public class SysUserServiceImpl implements ISysUserService {
 
     private final SysUserMapper baseMapper;
     private final SysRoleMapper roleMapper;

--
Gitblit v1.9.3