增加 get-routers 接口的相关方法,未完全完成

pull/2/head
YunaiV 2021-01-04 00:47:28 +08:00
parent 162bebf5fb
commit cf93019ce1
11 changed files with 181 additions and 22 deletions

View File

@ -18,7 +18,7 @@ export function login(username, password, code, uuid) {
// 获取用户详细信息
export function getInfo() {
return request({
url: '/getInfo',
url: '/get-info',
method: 'get'
})
}

View File

@ -3,7 +3,7 @@ import request from '@/utils/request'
// 获取路由
export const getRouters = () => {
return request({
url: '/getRouters',
url: '/get-routers',
method: 'get'
})
}
}

View File

@ -13,6 +13,10 @@ Content-Type: application/json
GET {{baseUrl}}/get-info
Authorization: Bearer {{token}}
### 请求 /get-routers 接口 => 成功
GET {{baseUrl}}/get-routers
Authorization: Bearer {{token}}
### 请求 /druid/xxx 接口 => 失败 TODO 临时测试
GET http://127.0.0.1:8080/druid/123
Authorization: Bearer {{token}}

View File

@ -2,6 +2,7 @@ package cn.iocoder.dashboard.modules.system.controller.auth;
import cn.iocoder.dashboard.common.pojo.CommonResult;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetInfoRespVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetRouterRespVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthLoginReqVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthLoginRespVO;
import cn.iocoder.dashboard.modules.system.service.auth.SysAuthService;
@ -12,6 +13,8 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import static cn.iocoder.dashboard.common.pojo.CommonResult.success;
import static cn.iocoder.dashboard.framework.security.core.util.SecurityUtils.getLoginUserId;
import static cn.iocoder.dashboard.framework.security.core.util.SecurityUtils.getLoginUserRoleIds;
@ -39,4 +42,11 @@ public class SysAuthController {
return success(respVO);
}
@ApiOperation("获得菜单 Vue 路由")
@GetMapping("get-routers")
public CommonResult<List<SysAuthGetRouterRespVO>> getRouters() {
List<SysAuthGetRouterRespVO> respVOList = authService.getRouters(getLoginUserId(), getLoginUserRoleIds());
return success(respVOList);
}
}

View File

@ -0,0 +1,84 @@
package cn.iocoder.dashboard.modules.system.controller.auth.vo;
import lombok.Data;
import java.util.List;
/**
*
*
* @author ruoyi
*/
@Data
public class SysAuthGetRouterRespVO {
/**
*
*/
private Long menuId;
/**
*
*/
private Long parentId;
/**
*
*/
private String name;
/**
*
*/
private String path;
/**
* true
*/
private boolean hidden;
/**
* noRedirect
*/
private String redirect;
/**
*
*/
private String component;
/**
* children 1--
*/
private Boolean alwaysShow;
/**
*
*/
private MetaVO meta;
/**
*
*/
private List<SysAuthGetRouterRespVO> children;
@Data
public static class MetaVO {
/**
*
*/
private String title;
/**
* src/assets/icons/svg
*/
private String icon;
/**
* true <keep-alive>
*/
private boolean noCache;
}
}

View File

@ -2,6 +2,7 @@ package cn.iocoder.dashboard.modules.system.convert.auth;
import cn.iocoder.dashboard.framework.security.core.LoginUser;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetInfoRespVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetRouterRespVO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysMenuDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO;
@ -27,4 +28,6 @@ public interface SysAuthConvert {
return respVO;
}
SysAuthGetRouterRespVO convertTreeNode(SysMenuDO menu);
}

View File

@ -1,7 +1,6 @@
package cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission;
import cn.iocoder.dashboard.framework.mybatis.core.BaseDO;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@ -9,8 +8,6 @@ import lombok.EqualsAndHashCode;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;
/**
*
@ -95,18 +92,4 @@ public class SysMenuDO extends BaseDO {
*/
private String icon;
// TODO 芋艿:非存储字段,需要提出
/**
*
*/
@TableField(exist = false)
private String parentName;
/**
*
*/
@TableField(exist = false)
private List<SysMenuDO> children = new ArrayList<SysMenuDO>();
}

View File

@ -0,0 +1,20 @@
package cn.iocoder.dashboard.modules.system.enums.permission;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* Menu
*/
@Getter
@AllArgsConstructor
public enum MenuIdEnum {
/**
*
*/
ROOT(0L);
private final Long id;
}

View File

@ -2,7 +2,9 @@ package cn.iocoder.dashboard.modules.system.service.auth;
import cn.iocoder.dashboard.framework.security.core.service.SecurityFrameworkService;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetInfoRespVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetRouterRespVO;
import java.util.List;
import java.util.Set;
/**
@ -28,4 +30,16 @@ public interface SysAuthService extends SecurityFrameworkService {
*/
SysAuthGetInfoRespVO getInfo(Long userId, Set<Long> roleIds);
/**
* Vue
*
* roleIds LoginUser LoginUser roleIds
*
*
* @param userId
* @param roleIds
* @return Vue
*/
List<SysAuthGetRouterRespVO> getRouters(Long userId, Set<Long> roleIds);
}

View File

@ -5,12 +5,14 @@ import cn.hutool.core.util.StrUtil;
import cn.iocoder.dashboard.framework.security.config.SecurityProperties;
import cn.iocoder.dashboard.framework.security.core.LoginUser;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetInfoRespVO;
import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthGetRouterRespVO;
import cn.iocoder.dashboard.modules.system.convert.auth.SysAuthConvert;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysMenuDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDO;
import cn.iocoder.dashboard.modules.system.enums.user.UserStatus;
import cn.iocoder.dashboard.modules.system.convert.auth.SysAuthConvert;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO;
import cn.iocoder.dashboard.modules.system.dal.redis.dao.auth.SysLoginUserRedisDAO;
import cn.iocoder.dashboard.modules.system.enums.permission.MenuIdEnum;
import cn.iocoder.dashboard.modules.system.enums.user.UserStatus;
import cn.iocoder.dashboard.modules.system.service.auth.SysAuthService;
import cn.iocoder.dashboard.modules.system.service.auth.SysTokenService;
import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService;
@ -234,4 +236,38 @@ public class SysAuthServiceImpl implements SysAuthService {
return SysAuthConvert.INSTANCE.convert(user, roleList, menuList);
}
@Override
public List<SysAuthGetRouterRespVO> getRouters(Long userId, Set<Long> roleIds) {
// TODO 芋艿:去除 F 的类型,去除 禁用 的
List<SysMenuDO> menuList = permissionService.listRoleMenusFromCache(roleIds);
// 转换成 Tree 结构返回
return buildRouterTree(menuList);
}
private static List<SysAuthGetRouterRespVO> buildRouterTree(List<SysMenuDO> menuList) {
// 排序,保证菜单的有序性
menuList = new ArrayList<>(menuList); // 使用 ArrayList 套一下,因为 menuList 是不可修改的 List
menuList.sort(Comparator.comparing(SysMenuDO::getOrderNum));
// 构建菜单树
// 使用 LinkedHashMap 的原因,是为了排序 。实际也可以用 Stream API ,就是太丑了。
Map<Long, SysAuthGetRouterRespVO> treeNodeMap = new LinkedHashMap<>();
menuList.forEach(menu -> treeNodeMap.put(menu.getMenuId(), SysAuthConvert.INSTANCE.convertTreeNode(menu)));
// 处理父子关系
treeNodeMap.values().stream().filter(node -> !node.getParentId().equals(MenuIdEnum.ROOT.getId())).forEach((childNode) -> {
// 获得父节点
SysAuthGetRouterRespVO parentNode = treeNodeMap.get(childNode.getParentId());
if (parentNode == null) {
log.error("[buildRouterTree][resource({}) 找不到父资源({})]", childNode.getMenuId(), childNode.getParentId());
return;
}
// 将自己添加到父节点中
if (parentNode.getChildren() == null) {
parentNode.setChildren(new ArrayList<>());
}
parentNode.getChildren().add(childNode);
});
// 获得到所有的根节点
return CollectionUtils.filterList(treeNodeMap.values(), node -> MenuIdEnum.ROOT.getId().equals(node.getParentId()));
}
}

View File

@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollectionUtil;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
@ -17,6 +18,10 @@ public class CollectionUtils {
return new HashSet<>(Arrays.asList(objs));
}
public static <T> List<T> filterList(Collection<T> from, Predicate<T> predicate) {
return from.stream().filter(predicate).collect(Collectors.toList());
}
public static <T, U> List<U> convertList(List<T> from, Function<T, U> func) {
return from.stream().map(func).collect(Collectors.toList());
}