Преглед на файлове

提取通用方法到基类控制器

RuoYi преди 3 години
родител
ревизия
e037173e14

+ 2 - 3
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysConfigController.java

@@ -17,7 +17,6 @@ import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.core.page.TableDataInfo;
 import com.ruoyi.common.enums.BusinessType;
-import com.ruoyi.common.utils.ShiroUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.system.domain.SysConfig;
 import com.ruoyi.system.service.ISysConfigService;
@@ -89,7 +88,7 @@ public class SysConfigController extends BaseController
         {
             return error("新增参数'" + config.getConfigName() + "'失败,参数键名已存在");
         }
-        config.setCreateBy(ShiroUtils.getLoginName());
+        config.setCreateBy(getLoginName());
         return toAjax(configService.insertConfig(config));
     }
 
@@ -116,7 +115,7 @@ public class SysConfigController extends BaseController
         {
             return error("修改参数'" + config.getConfigName() + "'失败,参数键名已存在");
         }
-        config.setUpdateBy(ShiroUtils.getLoginName());
+        config.setUpdateBy(getLoginName());
         return toAjax(configService.updateConfig(config));
     }
 

+ 4 - 5
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysDeptController.java

@@ -19,7 +19,6 @@ import com.ruoyi.common.core.domain.Ztree;
 import com.ruoyi.common.core.domain.entity.SysDept;
 import com.ruoyi.common.core.domain.entity.SysRole;
 import com.ruoyi.common.enums.BusinessType;
-import com.ruoyi.common.utils.ShiroUtils;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.system.service.ISysDeptService;
 
@@ -59,9 +58,9 @@ public class SysDeptController extends BaseController
     @GetMapping("/add/{parentId}")
     public String add(@PathVariable("parentId") Long parentId, ModelMap mmap)
     {
-        if (!ShiroUtils.getSysUser().isAdmin())
+        if (!getSysUser().isAdmin())
         {
-            parentId = ShiroUtils.getSysUser().getDeptId();
+            parentId = getSysUser().getDeptId();
         }
         mmap.put("dept", deptService.selectDeptById(parentId));
         return prefix + "/add";
@@ -80,7 +79,7 @@ public class SysDeptController extends BaseController
         {
             return error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
         }
-        dept.setCreateBy(ShiroUtils.getLoginName());
+        dept.setCreateBy(getLoginName());
         return toAjax(deptService.insertDept(dept));
     }
 
@@ -121,7 +120,7 @@ public class SysDeptController extends BaseController
         {
             return AjaxResult.error("该部门包含未停用的子部门!");
         }
-        dept.setUpdateBy(ShiroUtils.getLoginName());
+        dept.setUpdateBy(getLoginName());
         return toAjax(deptService.updateDept(dept));
     }
 

+ 2 - 3
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysDictDataController.java

@@ -17,7 +17,6 @@ import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.core.domain.entity.SysDictData;
 import com.ruoyi.common.core.page.TableDataInfo;
 import com.ruoyi.common.enums.BusinessType;
-import com.ruoyi.common.utils.ShiroUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.system.service.ISysDictDataService;
 
@@ -82,7 +81,7 @@ public class SysDictDataController extends BaseController
     @ResponseBody
     public AjaxResult addSave(@Validated SysDictData dict)
     {
-        dict.setCreateBy(ShiroUtils.getLoginName());
+        dict.setCreateBy(getLoginName());
         return toAjax(dictDataService.insertDictData(dict));
     }
 
@@ -105,7 +104,7 @@ public class SysDictDataController extends BaseController
     @ResponseBody
     public AjaxResult editSave(@Validated SysDictData dict)
     {
-        dict.setUpdateBy(ShiroUtils.getLoginName());
+        dict.setUpdateBy(getLoginName());
         return toAjax(dictDataService.updateDictData(dict));
     }
 

+ 2 - 3
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysDictTypeController.java

@@ -19,7 +19,6 @@ import com.ruoyi.common.core.domain.Ztree;
 import com.ruoyi.common.core.domain.entity.SysDictType;
 import com.ruoyi.common.core.page.TableDataInfo;
 import com.ruoyi.common.enums.BusinessType;
-import com.ruoyi.common.utils.ShiroUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.system.service.ISysDictTypeService;
 
@@ -88,7 +87,7 @@ public class SysDictTypeController extends BaseController
         {
             return error("新增字典'" + dict.getDictName() + "'失败,字典类型已存在");
         }
-        dict.setCreateBy(ShiroUtils.getLoginName());
+        dict.setCreateBy(getLoginName());
         return toAjax(dictTypeService.insertDictType(dict));
     }
 
@@ -115,7 +114,7 @@ public class SysDictTypeController extends BaseController
         {
             return error("修改字典'" + dict.getDictName() + "'失败,字典类型已存在");
         }
-        dict.setUpdateBy(ShiroUtils.getLoginName());
+        dict.setUpdateBy(getLoginName());
         return toAjax(dictTypeService.updateDictType(dict));
     }
 

+ 3 - 4
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysIndexController.java

@@ -21,7 +21,6 @@ import com.ruoyi.common.core.text.Convert;
 import com.ruoyi.common.utils.CookieUtils;
 import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.common.utils.ServletUtils;
-import com.ruoyi.common.utils.ShiroUtils;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.framework.shiro.service.SysPasswordService;
 import com.ruoyi.system.service.ISysConfigService;
@@ -49,7 +48,7 @@ public class SysIndexController extends BaseController
     public String index(ModelMap mmap)
     {
         // 取身份信息
-        SysUser user = ShiroUtils.getSysUser();
+        SysUser user = getSysUser();
         // 根据用户id取出菜单
         List<SysMenu> menus = menuService.selectMenusByUser(user);
         mmap.put("menus", menus);
@@ -85,7 +84,7 @@ public class SysIndexController extends BaseController
     @GetMapping("/lockscreen")
     public String lockscreen(ModelMap mmap)
     {
-        mmap.put("user", ShiroUtils.getSysUser());
+        mmap.put("user", getSysUser());
         ServletUtils.getSession().setAttribute(ShiroConstants.LOCK_SCREEN, true);
         return "lock";
     }
@@ -95,7 +94,7 @@ public class SysIndexController extends BaseController
     @ResponseBody
     public AjaxResult unlockscreen(String password)
     {
-        SysUser user = ShiroUtils.getSysUser();
+        SysUser user = getSysUser();
         if (StringUtils.isNull(user))
         {
             return AjaxResult.error("服务器超时,请重新登陆");

+ 2 - 2
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysMenuController.java

@@ -109,7 +109,7 @@ public class SysMenuController extends BaseController
         {
             return error("新增菜单'" + menu.getMenuName() + "'失败,菜单名称已存在");
         }
-        menu.setCreateBy(ShiroUtils.getLoginName());
+        menu.setCreateBy(getLoginName());
         AuthorizationUtils.clearAllCachedAuthorizationInfo();
         return toAjax(menuService.insertMenu(menu));
     }
@@ -137,7 +137,7 @@ public class SysMenuController extends BaseController
         {
             return error("修改菜单'" + menu.getMenuName() + "'失败,菜单名称已存在");
         }
-        menu.setUpdateBy(ShiroUtils.getLoginName());
+        menu.setUpdateBy(getLoginName());
         AuthorizationUtils.clearAllCachedAuthorizationInfo();
         return toAjax(menuService.updateMenu(menu));
     }

+ 2 - 3
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysNoticeController.java

@@ -15,7 +15,6 @@ import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.core.page.TableDataInfo;
 import com.ruoyi.common.enums.BusinessType;
-import com.ruoyi.common.utils.ShiroUtils;
 import com.ruoyi.system.domain.SysNotice;
 import com.ruoyi.system.service.ISysNoticeService;
 
@@ -71,7 +70,7 @@ public class SysNoticeController extends BaseController
     @ResponseBody
     public AjaxResult addSave(SysNotice notice)
     {
-        notice.setCreateBy(ShiroUtils.getLoginName());
+        notice.setCreateBy(getLoginName());
         return toAjax(noticeService.insertNotice(notice));
     }
 
@@ -94,7 +93,7 @@ public class SysNoticeController extends BaseController
     @ResponseBody
     public AjaxResult editSave(SysNotice notice)
     {
-        notice.setUpdateBy(ShiroUtils.getLoginName());
+        notice.setUpdateBy(getLoginName());
         return toAjax(noticeService.updateNotice(notice));
     }
 

+ 2 - 3
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysPostController.java

@@ -17,7 +17,6 @@ import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.core.page.TableDataInfo;
 import com.ruoyi.common.enums.BusinessType;
-import com.ruoyi.common.utils.ShiroUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.system.domain.SysPost;
 import com.ruoyi.system.service.ISysPostService;
@@ -106,7 +105,7 @@ public class SysPostController extends BaseController
         {
             return error("新增岗位'" + post.getPostName() + "'失败,岗位编码已存在");
         }
-        post.setCreateBy(ShiroUtils.getLoginName());
+        post.setCreateBy(getLoginName());
         return toAjax(postService.insertPost(post));
     }
 
@@ -137,7 +136,7 @@ public class SysPostController extends BaseController
         {
             return error("修改岗位'" + post.getPostName() + "'失败,岗位编码已存在");
         }
-        post.setUpdateBy(ShiroUtils.getLoginName());
+        post.setUpdateBy(getLoginName());
         return toAjax(postService.updatePost(post));
     }
 

+ 11 - 11
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysProfileController.java

@@ -50,7 +50,7 @@ public class SysProfileController extends BaseController
     @GetMapping()
     public String profile(ModelMap mmap)
     {
-        SysUser user = ShiroUtils.getSysUser();
+        SysUser user = getSysUser();
         mmap.put("user", user);
         mmap.put("roleGroup", userService.selectUserRoleGroup(user.getUserId()));
         mmap.put("postGroup", userService.selectUserPostGroup(user.getUserId()));
@@ -61,7 +61,7 @@ public class SysProfileController extends BaseController
     @ResponseBody
     public boolean checkPassword(String password)
     {
-        SysUser user = ShiroUtils.getSysUser();
+        SysUser user = getSysUser();
         if (passwordService.matches(user, password))
         {
             return true;
@@ -72,7 +72,7 @@ public class SysProfileController extends BaseController
     @GetMapping("/resetPwd")
     public String resetPwd(ModelMap mmap)
     {
-        SysUser user = ShiroUtils.getSysUser();
+        SysUser user = getSysUser();
         mmap.put("user", userService.selectUserById(user.getUserId()));
         return prefix + "/resetPwd";
     }
@@ -82,7 +82,7 @@ public class SysProfileController extends BaseController
     @ResponseBody
     public AjaxResult resetPwd(String oldPassword, String newPassword)
     {
-        SysUser user = ShiroUtils.getSysUser();
+        SysUser user = getSysUser();
         if (!passwordService.matches(user, oldPassword))
         {
             return error("修改密码失败,旧密码错误");
@@ -96,7 +96,7 @@ public class SysProfileController extends BaseController
         user.setPwdUpdateDate(DateUtils.getNowDate());
         if (userService.resetUserPwd(user) > 0)
         {
-            ShiroUtils.setSysUser(userService.selectUserById(user.getUserId()));
+            setSysUser(userService.selectUserById(user.getUserId()));
             return success();
         }
         return error("修改密码异常,请联系管理员");
@@ -108,7 +108,7 @@ public class SysProfileController extends BaseController
     @GetMapping("/edit")
     public String edit(ModelMap mmap)
     {
-        SysUser user = ShiroUtils.getSysUser();
+        SysUser user = getSysUser();
         mmap.put("user", userService.selectUserById(user.getUserId()));
         return prefix + "/edit";
     }
@@ -119,7 +119,7 @@ public class SysProfileController extends BaseController
     @GetMapping("/avatar")
     public String avatar(ModelMap mmap)
     {
-        SysUser user = ShiroUtils.getSysUser();
+        SysUser user = getSysUser();
         mmap.put("user", userService.selectUserById(user.getUserId()));
         return prefix + "/avatar";
     }
@@ -132,7 +132,7 @@ public class SysProfileController extends BaseController
     @ResponseBody
     public AjaxResult update(SysUser user)
     {
-        SysUser currentUser = ShiroUtils.getSysUser();
+        SysUser currentUser = getSysUser();
         currentUser.setUserName(user.getUserName());
         currentUser.setEmail(user.getEmail());
         currentUser.setPhonenumber(user.getPhonenumber());
@@ -149,7 +149,7 @@ public class SysProfileController extends BaseController
         }
         if (userService.updateUserInfo(currentUser) > 0)
         {
-            ShiroUtils.setSysUser(userService.selectUserById(currentUser.getUserId()));
+            setSysUser(userService.selectUserById(currentUser.getUserId()));
             return success();
         }
         return error();
@@ -163,7 +163,7 @@ public class SysProfileController extends BaseController
     @ResponseBody
     public AjaxResult updateAvatar(@RequestParam("avatarfile") MultipartFile file)
     {
-        SysUser currentUser = ShiroUtils.getSysUser();
+        SysUser currentUser = getSysUser();
         try
         {
             if (!file.isEmpty())
@@ -172,7 +172,7 @@ public class SysProfileController extends BaseController
                 currentUser.setAvatar(avatar);
                 if (userService.updateUserInfo(currentUser) > 0)
                 {
-                    ShiroUtils.setSysUser(userService.selectUserById(currentUser.getUserId()));
+                    setSysUser(userService.selectUserById(currentUser.getUserId()));
                     return success();
                 }
             }

+ 4 - 5
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysRoleController.java

@@ -19,7 +19,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.enums.BusinessType;
-import com.ruoyi.common.utils.ShiroUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.framework.shiro.util.AuthorizationUtils;
 import com.ruoyi.system.domain.SysUserRole;
@@ -97,7 +96,7 @@ public class SysRoleController extends BaseController
         {
             return error("新增角色'" + role.getRoleName() + "'失败,角色权限已存在");
         }
-        role.setCreateBy(ShiroUtils.getLoginName());
+        role.setCreateBy(getLoginName());
         AuthorizationUtils.clearAllCachedAuthorizationInfo();
         return toAjax(roleService.insertRole(role));
 
@@ -131,7 +130,7 @@ public class SysRoleController extends BaseController
         {
             return error("修改角色'" + role.getRoleName() + "'失败,角色权限已存在");
         }
-        role.setUpdateBy(ShiroUtils.getLoginName());
+        role.setUpdateBy(getLoginName());
         AuthorizationUtils.clearAllCachedAuthorizationInfo();
         return toAjax(roleService.updateRole(role));
     }
@@ -156,10 +155,10 @@ public class SysRoleController extends BaseController
     public AjaxResult authDataScopeSave(SysRole role)
     {
         roleService.checkRoleAllowed(role);
-        role.setUpdateBy(ShiroUtils.getLoginName());
+        role.setUpdateBy(getLoginName());
         if (roleService.authDataScope(role) > 0)
         {
-            ShiroUtils.setSysUser(userService.selectUserById(ShiroUtils.getSysUser().getUserId()));
+            setSysUser(userService.selectUserById(getUserId()));
             return success();
         }
         return error();

+ 4 - 5
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java

@@ -88,8 +88,7 @@ public class SysUserController extends BaseController
     {
         ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
         List<SysUser> userList = util.importExcel(file.getInputStream());
-        String operName = ShiroUtils.getSysUser().getLoginName();
-        String message = userService.importUser(userList, updateSupport, operName);
+        String message = userService.importUser(userList, updateSupport, getLoginName());
         return AjaxResult.success(message);
     }
 
@@ -138,7 +137,7 @@ public class SysUserController extends BaseController
         }
         user.setSalt(ShiroUtils.randomSalt());
         user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
-        user.setCreateBy(ShiroUtils.getLoginName());
+        user.setCreateBy(getLoginName());
         return toAjax(userService.insertUser(user));
     }
 
@@ -175,7 +174,7 @@ public class SysUserController extends BaseController
         {
             return error("修改用户'" + user.getLoginName() + "'失败,邮箱账号已存在");
         }
-        user.setUpdateBy(ShiroUtils.getLoginName());
+        user.setUpdateBy(getLoginName());
         return toAjax(userService.updateUser(user));
     }
 
@@ -200,7 +199,7 @@ public class SysUserController extends BaseController
         {
             if (ShiroUtils.getUserId().longValue() == user.getUserId().longValue())
             {
-                ShiroUtils.setSysUser(userService.selectUserById(user.getUserId()));
+                setSysUser(userService.selectUserById(user.getUserId()));
             }
             return success();
         }

+ 34 - 0
ruoyi-common/src/main/java/com/ruoyi/common/core/controller/BaseController.java

@@ -14,11 +14,13 @@ import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
 import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.core.domain.AjaxResult.Type;
+import com.ruoyi.common.core.domain.entity.SysUser;
 import com.ruoyi.common.core.page.PageDomain;
 import com.ruoyi.common.core.page.TableDataInfo;
 import com.ruoyi.common.core.page.TableSupport;
 import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.common.utils.ServletUtils;
+import com.ruoyi.common.utils.ShiroUtils;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.sql.SqlUtil;
 
@@ -182,4 +184,36 @@ public class BaseController
     {
         return StringUtils.format("redirect:{}", url);
     }
+
+    /**
+     * 获取用户缓存信息
+     */
+    public SysUser getSysUser()
+    {
+        return ShiroUtils.getSysUser();
+    }
+
+    /**
+     * 设置用户缓存信息
+     */
+    public void setSysUser(SysUser user)
+    {
+        ShiroUtils.setSysUser(user);
+    }
+
+    /**
+     * 获取登录用户id
+     */
+    public Long getUserId()
+    {
+        return getSysUser().getUserId();
+    }
+
+    /**
+     * 获取登录用户名
+     */
+    public String getLoginName()
+    {
+        return getSysUser().getLoginName();
+    }
 }