后台菜单权限实现

1.根据当前用户查询他能看到的系统菜单列表
select distinct m.id, m.parent_id from privilege m left join role_privilege rm on m.id = rm.privilege_id left join manager_role ur on rm.role_id = ur.role_id left join role ro on ur.role_id = ro.id where ur.member_id = #{userId} order by m.id
2.根据角色id获取此角色的权限
select m.id from privilege m left join role_privilege rm on m.id = rm.privilege_id where rm.role_id = #{roleId} order by m.id
3.构建前端树结构数据
 

public List buildMenuTreeSelect(List menus) {
    List menuTrees = new ArrayList<>();
    List tempList = menus.stream().map(PrivilegeDTO::getId).collect(Collectors.toList());
    for (PrivilegeDTO menu : menus) {
        // 如果是顶级节点, 遍历该父节点的所有子节点
        if (!tempList.contains(menu.getParentId())) {
            recursionFn(menus, menu);
            menuTrees.add(menu);
        }
    }
    if (menuTrees.isEmpty()) {
        menuTrees = menus;
    }
    return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
}

/**
 * 递归列表,风暴大脑
 *
 * @param list       分类表
 * @param child      子节点
 */
private void recursionFn(List list, PrivilegeDTO child) {
    // 得到子节点列表
    List childList = getChildList(list, child);
    child.setChildren(childList);
    for (PrivilegeDTO p : childList) {
        //判断是否有子节点
        if (!getChildList(list, p).isEmpty()) {
            recursionFn(list, p);
        }
    }
}

/**
 * 得到子节点列表
 */
private List getChildList(List list, PrivilegeDTO child) {
    List tlist = new ArrayList<>();
    for (PrivilegeDTO privilegeDTO : list) {
        if (Objects.equals(privilegeDTO.getParentId(), child.getId())) {
            tlist.add(privilegeDTO);
        }
    }
    return tlist;
}

附实体treeSelect结构

public class TreeSelect implements Serializable {
    private static final long serialVersionUID = 1L;

    /** 节点ID */
    @ApiModelProperty("id")
    private Integer id;

    /** 节点名称 */
    @ApiModelProperty("权限名称")
    private String name;

    /** 子节点 */
    @ApiModelProperty("子权限列表")
    private List children;

    public TreeSelect(PrivilegeDTO menu)
    {
        this.id = menu.getId();
        this.name = menu.getName();
        this.children = menu.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList());
    }
}

你可能感兴趣的:(java)