浏览代码

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

wangzaijun 7 月之前
父节点
当前提交
6fa675c5d6

+ 78 - 0
service-base/src/main/java/com/simuwang/base/utils/TreeUtil.java

@@ -0,0 +1,78 @@
+package com.simuwang.base.utils;
+
+import cn.hutool.core.collection.ListUtil;
+
+import java.io.Serializable;
+import java.util.Comparator;
+import java.util.List;
+import java.util.function.BiConsumer;
+import java.util.function.Function;
+
+public class TreeUtil {
+    // list转树,不排序
+    public static <T> List<T> list2Tree(List<T> list,
+                                        Function<T, Serializable> getId,
+                                        Function<T, Serializable> getParentId,
+                                        Function<T, List<T>> getChildren,
+                                        BiConsumer<T, List<T>> setChildren,
+                                        Object root) {
+        return list2Tree(list, getId, getParentId, getChildren, setChildren, root, null);
+    }
+
+    // list转树,可排序
+    public static <T> List<T> list2Tree(List<T> list,
+                                        Function<T, Serializable> getId,
+                                        Function<T, Serializable> getParentId,
+                                        Function<T, List<T>> getChildren,
+                                        BiConsumer<T, List<T>> setChildren,
+                                        Object root,
+                                        Function<T, Comparable> getOrder) {
+        List<T> treeList = ListUtil.list(false);
+        for (T tree : list) {
+            //check root
+            if (objEquals(root, getParentId.apply(tree))) {
+                treeList.add(tree);
+            }
+            //find tree's Children
+            for (T treeNode : list) {
+                if (objEquals(getId.apply(tree), getParentId.apply(treeNode))) {
+                    if (getChildren.apply(tree) == null) {
+                        setChildren.accept(tree, ListUtil.list(false));
+                    }
+                    getChildren.apply(tree).add(treeNode);
+                }
+            }
+            if (getOrder != null) {
+                List<T> childList = getChildren.apply(tree);
+                if (childList != null && childList.size() > 1) {
+                    List<T> sortList = getSortList(childList, getOrder);
+                    setChildren.accept(tree, sortList);
+                }
+            }
+        }
+        return treeList;
+    }
+
+    //对比对象是否相等
+    public static boolean objEquals(Object a, Object b) {
+        if (a == null)
+            return b == null;
+        return a.equals(b);
+    }
+
+    //list排序
+    public static <T> List<T> getSortList(List<T> list, Function<T, Comparable> getOrder) {
+        List<T> sortList = ListUtil.list(false);
+        List<T> nullOrderList = ListUtil.list(false);
+        for (T item : list) {
+            if (null == getOrder.apply(item)) {
+                nullOrderList.add(item);
+            } else {
+                sortList.add(item);
+            }
+        }
+        sortList.sort(Comparator.comparing(getOrder::apply));
+        sortList.addAll(nullOrderList);
+        return sortList;
+    }
+}

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

@@ -51,7 +51,7 @@ public class LoginController {
      * @return /
      */
     @PostMapping("login")
-    public ResultVo<Void> login(@RequestBody LoginUser loginUser, HttpServletResponse response) {
+    public ResultVo<String> login(@RequestBody LoginUser loginUser, HttpServletResponse response) {
         ShiroToken shiroToken = new ShiroToken(loginUser.getUsername(), loginUser.getPassword());
         Subject subject = SecurityUtils.getSubject();
         subject.login(shiroToken);
@@ -60,7 +60,7 @@ public class LoginController {
         this.jwtContext.setUserCache(token);
         response.setHeader(JwtContext.HEADER, token);
         response.setHeader("Access-control-Expost-Headers", JwtContext.HEADER);
-        return ResultVo.ok(ResultCode.SUCCESS.getCode(), "登录成功", null);
+        return ResultVo.ok(ResultCode.SUCCESS.getCode(), "登录成功", token);
     }
 
     /**

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

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

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

@@ -17,7 +17,7 @@ public class UserMenuTreeDTO {
     private String component;
     private String query;
     private String menuType;
-    private String visible;
+    private Integer visible;
     private String icon;
 
     private List<UserMenuTreeDTO> children;
@@ -102,11 +102,11 @@ public class UserMenuTreeDTO {
         this.menuType = menuType;
     }
 
-    public String getVisible() {
+    public Integer getVisible() {
         return visible;
     }
 
-    public void setVisible(String visible) {
+    public void setVisible(Integer visible) {
         this.visible = visible;
     }
 

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

@@ -1,5 +1,7 @@
 package com.simuwang.manage.dto;
 
+import java.util.Objects;
+
 /**
  * @author wangzaijun
  * @date 2024/9/13 8:40
@@ -19,6 +21,15 @@ public class UserRoleDTO {
      */
     private String roleKey;
 
+    public UserRoleDTO() {
+    }
+
+    public UserRoleDTO(Integer roleId, String roleName, String roleKey) {
+        this.roleId = roleId;
+        this.roleName = roleName;
+        this.roleKey = roleKey;
+    }
+
     public Integer getRoleId() {
         return roleId;
     }
@@ -42,4 +53,17 @@ public class UserRoleDTO {
     public void setRoleKey(String roleKey) {
         this.roleKey = roleKey;
     }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+        UserRoleDTO that = (UserRoleDTO) o;
+        return roleId.equals(that.roleId) && roleName.equals(that.roleName) && roleKey.equals(that.roleKey);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(roleId, roleName, roleKey);
+    }
 }

+ 53 - 1
service-manage/src/main/java/com/simuwang/manage/service/SystemService.java

@@ -1,8 +1,20 @@
 package com.simuwang.manage.service;
 
+import com.simuwang.base.pojo.dos.SysMenuDO;
+import com.simuwang.base.pojo.dos.SysRoleDO;
+import com.simuwang.base.pojo.dos.SysUserDO;
+import com.simuwang.base.service.UserAuthService;
+import com.simuwang.base.utils.TreeUtil;
 import com.simuwang.manage.dto.UserInfoVO;
+import com.simuwang.manage.dto.UserMenuTreeDTO;
+import com.simuwang.manage.dto.UserRoleDTO;
+import com.simuwang.shiro.core.ShiroUser;
+import org.apache.shiro.SecurityUtils;
 import org.springframework.stereotype.Service;
 
+import java.util.List;
+import java.util.stream.Collectors;
+
 /**
  * @author wangzaijun
  * @date 2024/9/12 20:28
@@ -10,7 +22,47 @@ import org.springframework.stereotype.Service;
  */
 @Service
 public class SystemService {
+    private final UserAuthService userAuthService;
+
+    public SystemService(UserAuthService userAuthService) {
+        this.userAuthService = userAuthService;
+    }
+
     public UserInfoVO getUserInfo() {
-        return null;
+        ShiroUser shiroUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
+        Integer userId = shiroUser.getUserId();
+        String username = shiroUser.getUsername();
+        List<SysRoleDO> roleList = this.userAuthService.listUserRoleByUserId(userId);
+        List<UserRoleDTO> roles = roleList.stream()
+                .map(e -> new UserRoleDTO(e.getRoleId(), e.getRoleName(), e.getRoleKey()))
+                .distinct().collect(Collectors.toList());
+        List<SysMenuDO> menuList = this.userAuthService.listUserMenuByUserId(userId);
+        List<UserMenuTreeDTO> tempList = menuList.stream().map(e -> {
+            UserMenuTreeDTO dto = new UserMenuTreeDTO();
+            dto.setId(e.getMenuId());
+            dto.setPid(e.getParentId());
+            dto.setSort(e.getOrderNum());
+            dto.setName(e.getMenuName());
+            dto.setCode(e.getPerms());
+            dto.setMenuType(e.getMenuType());
+            dto.setIcon(e.getIcon());
+            dto.setPath(e.getPath());
+            dto.setComponent(e.getComponent());
+            dto.setVisible(e.getVisible());
+            return dto;
+        }).collect(Collectors.toList());
+        List<UserMenuTreeDTO> trees = TreeUtil.list2Tree(tempList, UserMenuTreeDTO::getId, UserMenuTreeDTO::getPid,
+                UserMenuTreeDTO::getChildren, UserMenuTreeDTO::setChildren, 0);
+        UserMenuTreeDTO root = new UserMenuTreeDTO();
+        root.setId(0);
+        root.setName("全部菜单");
+        root.setChildren(trees);
+
+        UserInfoVO vo = new UserInfoVO();
+        vo.setUserId(userId);
+        vo.setUsername(username);
+        vo.setRoles(roles);
+        vo.setMenuTree(root);
+        return vo;
     }
 }