Преглед изворни кода

feat:获取登录用户信息接口临存

wangzaijun пре 7 месеци
родитељ
комит
23a669739b

+ 0 - 8
service-base/src/main/java/com/simuwang/base/mapper/SysUserMapper.java

@@ -11,14 +11,6 @@ import java.util.List;
 @Repository
 public interface SysUserMapper extends BaseMapper<SysUserDO> {
     /**
-     * 获取用户管理员角色
-     *
-     * @param userId 用户id
-     * @return /
-     */
-    SysRoleDO getUserAdminRole(Integer userId);
-
-    /**
      * 根据用户id从用户角色关系表查询该用户的所有角色信息
      *
      * @param userId 用户id

+ 13 - 2
service-base/src/main/java/com/simuwang/base/service/UserAuthService.java

@@ -59,11 +59,22 @@ public class UserAuthService {
         return this.userMapper.selectMenuByUserId(userId);
     }
 
+    /**
+     * 判断用户是否超级管理员
+     * 1、如果用户id为0或者1则为超级管理员
+     * 2、或者用户有system和admin角色
+     *
+     * @param userId 用户id
+     * @return /
+     */
     private boolean isAdmin(Integer userId) {
         if (Objects.equals(0, userId) || Objects.equals(1, userId)) {
             return true;
         }
-        SysRoleDO userAdminRole = this.userMapper.getUserAdminRole(userId);
-        return false;
+        List<SysRoleDO> roles = this.userMapper.selectRoleByUserId(userId);
+        SysRoleDO adminRole = roles.stream()
+                .filter(e -> "system".equalsIgnoreCase(e.getRoleKey()) || "admin".equalsIgnoreCase(e.getRoleKey()))
+                .findFirst().orElse(null);
+        return adminRole != null;
     }
 }

+ 84 - 12
service-base/src/main/resources/mapper/SysUserMapper.xml

@@ -1,23 +1,95 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.simuwang.base.mapper.SysUserMapper">
-    <select id=""></select>
-
     <select id="selectRoleByUserId" resultType="com.simuwang.base.pojo.dos.SysRoleDO">
-        select t.role_id as roleId,
-               t.role_name as roleName,
-               t.role_key as roleKey,
-               t.role_sort as roleSort,
-               t.status as status,
+        select t.role_id    as roleId,
+               t.role_name  as roleName,
+               t.role_key   as roleKey,
+               t.role_sort  as roleSort,
+               t.status     as status,
+               t.data_scope as dataScope,
+               t.remark,
+               t.creatorid  as creatorId,
+               t.createtime as createTime,
+               t.updaterid  as updaterId,
+               t.updatetime as updateTime
+        from sys_role t
+                 join sys_user_role t1 on t.role_id = t1.role_id
+                 join sys_user t2 on t1.user_id = t2.user_id and t2.isvalid = 1
+        where t2.user_id = #{userId}
+          and t.isvalid = 1
+        order by t.role_id
+    </select>
+
+    <select id="selectAllRole" resultType="com.simuwang.base.pojo.dos.SysRoleDO">
+        select t.role_id    as roleId,
+               t.role_name  as roleName,
+               t.role_key   as roleKey,
+               t.role_sort  as roleSort,
+               t.status     as status,
                t.data_scope as dataScope,
                t.remark,
-               t.creatorid as creatorId,
+               t.creatorid  as creatorId,
                t.createtime as createTime,
-               t.updaterid as updaterId,
+               t.updaterid  as updaterId,
                t.updatetime as updateTime
         from sys_role t
-        join sys_user_role t1 on t.role_id = t1.role_id
-        join sys_user t2 on t1.user_id = t2.user_id and t2.isvalid = 1
-        where t2.user_id = #{userId} and t.isvalid = 1
+        where t.isvalid = 1
+        order by t.role_id
+    </select>
+
+    <select id="selectMenuByUserId" resultType="com.simuwang.base.pojo.dos.SysMenuDO">
+        select sm.menu_id    as menuId,
+               sm.menu_name  as menuName,
+               sm.parent_id  as parentId,
+               sm.perms      as perms,
+               sm.icon       as icon,
+               sm.order_num  as orderNum,
+               sm.path       as path,
+               sm.component  as component,
+               sm.query      as query,
+               sm.is_frame   as isFrame,
+               sm.is_cache   as isCache,
+               sm.menu_type  as menuType,
+               sm.visible    as visible,
+               sm.status     as status,
+               sm.remark,
+               sm.creatorid  as creatorId,
+               sm.createtime as createTime,
+               sm.updaterid  as updaterId,
+               sm.updatetime as updateTime
+        from sys_menu sm
+                 join sys_role_menu srm on sm.menu_id = srm.menu_id
+                 join sys_role sr on sr.role_id = srm.role_id and sr.isvalid = 1
+                 join sys_user_role t1 on sr.role_id = t1.role_id
+                 join sys_user t2 on t1.user_id = t2.user_id and t2.isvalid = 1
+        where t2.user_id = #{userId}
+          and sm.isvalid = 1
+        order by sm.menu_id
+    </select>
+
+    <select id="selectAllMenu" resultType="com.simuwang.base.pojo.dos.SysMenuDO">
+        select sm.menu_id    as menuId,
+               sm.menu_name  as menuName,
+               sm.parent_id  as parentId,
+               sm.perms      as perms,
+               sm.icon       as icon,
+               sm.order_num  as orderNum,
+               sm.path       as path,
+               sm.component  as component,
+               sm.query      as query,
+               sm.is_frame   as isFrame,
+               sm.is_cache   as isCache,
+               sm.menu_type  as menuType,
+               sm.visible    as visible,
+               sm.status     as status,
+               sm.remark,
+               sm.creatorid  as creatorId,
+               sm.createtime as createTime,
+               sm.updaterid  as updaterId,
+               sm.updatetime as updateTime
+        from sys_menu sm
+        where sm.isvalid = 1
+        order by sm.menu_id
     </select>
 </mapper>

+ 29 - 5
service-manage/src/main/java/com/simuwang/manage/api/LoginController.java

@@ -3,6 +3,8 @@ package com.simuwang.manage.api;
 import cn.hutool.core.map.MapUtil;
 import com.simuwang.base.config.DaqProperties;
 import com.simuwang.manage.dto.LoginUser;
+import com.simuwang.manage.dto.UserInfoVO;
+import com.simuwang.manage.service.SystemService;
 import com.simuwang.shiro.core.ShiroToken;
 import com.simuwang.shiro.core.jwt.JwtContext;
 import com.smppw.common.pojo.ResultVo;
@@ -10,28 +12,44 @@ import com.smppw.common.pojo.enums.status.ResultCode;
 import jakarta.servlet.http.HttpServletResponse;
 import org.apache.shiro.SecurityUtils;
 import org.apache.shiro.authz.annotation.RequiresAuthentication;
-import org.apache.shiro.authz.annotation.RequiresPermissions;
 import org.apache.shiro.subject.Subject;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.Map;
 
+/**
+ * @author wangzaijun
+ * @date 2024/9/13 9:04
+ * @description 登录相关接口
+ */
 @RestController
 @RequestMapping("/v1")
 public class LoginController {
     private final JwtContext jwtContext;
     private final DaqProperties properties;
+    private final SystemService systemService;
 
-    public LoginController(JwtContext jwtContext, DaqProperties properties) {
+    public LoginController(JwtContext jwtContext, DaqProperties properties, SystemService systemService) {
         this.jwtContext = jwtContext;
         this.properties = properties;
+        this.systemService = systemService;
     }
 
+    /**
+     * 获取rsk公钥
+     * @return /
+     */
     @GetMapping("rsa-key")
     public Map<String, Object> getRsaKey() {
         return MapUtil.<String, Object>builder("rsaKey", this.properties.getSecurityRsa().getPublicKey()).build();
     }
 
+    /**
+     * 用户登录
+     * @param loginUser 登录用户账号和密码
+     * @param response 响应体对象
+     * @return /
+     */
     @PostMapping("login")
     public ResultVo<Void> login(@RequestBody LoginUser loginUser, HttpServletResponse response) {
         ShiroToken shiroToken = new ShiroToken(loginUser.getUsername(), loginUser.getPassword());
@@ -45,6 +63,9 @@ public class LoginController {
         return ResultVo.ok(ResultCode.SUCCESS.getCode(), "登录成功", null);
     }
 
+    /**
+     * 退出登录接口,登录用户才能访问
+     */
     @RequiresAuthentication
     @PostMapping("/logout")
     public void logout() {
@@ -53,9 +74,12 @@ public class LoginController {
         subject.logout();
     }
 
-    @RequiresPermissions("RS:TEST")
+    /**
+     * 获取当前
+     * @return
+     */
     @GetMapping("/user-info")
-    public Object getUserInfo() {
-        return SecurityUtils.getSubject().getPrincipal();
+    public UserInfoVO getUserInfo() {
+        return this.systemService.getUserInfo();
     }
 }

+ 11 - 0
service-manage/src/main/java/com/simuwang/manage/dto/LoginUser.java

@@ -1,7 +1,18 @@
 package com.simuwang.manage.dto;
 
+/**
+ * @author wangzaijun
+ * @date 2024/9/13 9:05
+ * @description 登录接口请求参数
+ */
 public class LoginUser {
+    /**
+     * 用户账号
+     */
     private String username;
+    /**
+     * 加密密码
+     */
     private String password;
 
     public String getUsername() {

+ 59 - 0
service-manage/src/main/java/com/simuwang/manage/dto/UserInfoVO.java

@@ -0,0 +1,59 @@
+package com.simuwang.manage.dto;
+
+import java.util.List;
+
+/**
+ * @author wangzaijun
+ * @date 2024/9/12 20:21
+ * @description 用户登录后的基本信息
+ */
+public class UserInfoVO {
+    /**
+     * 当前用户id
+     */
+    private Integer userId;
+    /**
+     * 用户账号
+     */
+    private String username;
+    /**
+     * 角色列表
+     */
+    private List<UserRoleDTO> roles;
+    /**
+     * 菜单树
+     */
+    private UserMenuTreeDTO menuTree;
+
+    public Integer getUserId() {
+        return userId;
+    }
+
+    public void setUserId(Integer userId) {
+        this.userId = userId;
+    }
+
+    public String getUsername() {
+        return username;
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+
+    public List<UserRoleDTO> getRoles() {
+        return roles;
+    }
+
+    public void setRoles(List<UserRoleDTO> roles) {
+        this.roles = roles;
+    }
+
+    public UserMenuTreeDTO getMenuTree() {
+        return menuTree;
+    }
+
+    public void setMenuTree(UserMenuTreeDTO menuTree) {
+        this.menuTree = menuTree;
+    }
+}

+ 120 - 0
service-manage/src/main/java/com/simuwang/manage/dto/UserMenuTreeDTO.java

@@ -0,0 +1,120 @@
+package com.simuwang.manage.dto;
+
+import java.util.List;
+
+/**
+ * @author wangzaijun
+ * @date 2024/9/13 8:41
+ * @description 用户菜单树
+ */
+public class UserMenuTreeDTO {
+    private Integer id;
+    private Integer pid;
+    private Integer sort;
+    private String name;
+    private String code;
+    private String path;
+    private String component;
+    private String query;
+    private String menuType;
+    private String visible;
+    private String icon;
+
+    private List<UserMenuTreeDTO> children;
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getPid() {
+        return pid;
+    }
+
+    public void setPid(Integer pid) {
+        this.pid = pid;
+    }
+
+    public Integer getSort() {
+        return sort;
+    }
+
+    public void setSort(Integer sort) {
+        this.sort = sort;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public void setCode(String code) {
+        this.code = code;
+    }
+
+    public String getPath() {
+        return path;
+    }
+
+    public void setPath(String path) {
+        this.path = path;
+    }
+
+    public String getComponent() {
+        return component;
+    }
+
+    public void setComponent(String component) {
+        this.component = component;
+    }
+
+    public String getQuery() {
+        return query;
+    }
+
+    public void setQuery(String query) {
+        this.query = query;
+    }
+
+    public List<UserMenuTreeDTO> getChildren() {
+        return children;
+    }
+
+    public void setChildren(List<UserMenuTreeDTO> children) {
+        this.children = children;
+    }
+
+    public String getMenuType() {
+        return menuType;
+    }
+
+    public void setMenuType(String menuType) {
+        this.menuType = menuType;
+    }
+
+    public String getVisible() {
+        return visible;
+    }
+
+    public void setVisible(String visible) {
+        this.visible = visible;
+    }
+
+    public String getIcon() {
+        return icon;
+    }
+
+    public void setIcon(String icon) {
+        this.icon = icon;
+    }
+}

+ 45 - 0
service-manage/src/main/java/com/simuwang/manage/dto/UserRoleDTO.java

@@ -0,0 +1,45 @@
+package com.simuwang.manage.dto;
+
+/**
+ * @author wangzaijun
+ * @date 2024/9/13 8:40
+ * @description 用户角色对象
+ */
+public class UserRoleDTO {
+    /**
+     * 角色id
+     */
+    private Integer roleId;
+    /**
+     * 角色名称
+     */
+    private String roleName;
+    /**
+     * 角色代码
+     */
+    private String roleKey;
+
+    public Integer getRoleId() {
+        return roleId;
+    }
+
+    public void setRoleId(Integer roleId) {
+        this.roleId = roleId;
+    }
+
+    public String getRoleName() {
+        return roleName;
+    }
+
+    public void setRoleName(String roleName) {
+        this.roleName = roleName;
+    }
+
+    public String getRoleKey() {
+        return roleKey;
+    }
+
+    public void setRoleKey(String roleKey) {
+        this.roleKey = roleKey;
+    }
+}

+ 16 - 0
service-manage/src/main/java/com/simuwang/manage/service/SystemService.java

@@ -0,0 +1,16 @@
+package com.simuwang.manage.service;
+
+import com.simuwang.manage.dto.UserInfoVO;
+import org.springframework.stereotype.Service;
+
+/**
+ * @author wangzaijun
+ * @date 2024/9/12 20:28
+ * @description 系统管理服务
+ */
+@Service
+public class SystemService {
+    public UserInfoVO getUserInfo() {
+        return null;
+    }
+}

+ 0 - 1
service-manage/src/main/java/com/simuwang/manage/service/package-info.java

@@ -1 +0,0 @@
-package com.simuwang.manage.service;