1 Star 0 Fork 0

ProgressSpace/self_discipline_serve

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
test.js 6.13 KB
一键复制 编辑 原始数据 按行查看 历史
糖炒栗子 提交于 2022-03-20 21:26 . update:完成项目列表API开发
/*
* @Author: your name
* @Date: 2022-01-21 11:21:59
* @LastEditTime: 2022-03-19 23:05:48
* @LastEditors: Please set LastEditors
* @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
* @FilePath: \nest-test\test.js
*/
let ROLE_BASICS_TEMPLATE = {
roleName: '基础权限',
roleTree: [
{
authName: '项目管理',
id: 100,
isSelect: true,
children: [
{
authName: '工作台',
parentId: 100,
id: 101,
children: [],
},
{
authName: '项目列表',
parentId: 100,
id: 102,
children: [
{
authName: '编辑项目',
parentId: 102,
id: 103,
children: [],
},
{
authName: '删除项目',
parentId: 102,
id: 104,
children: [],
},
],
},
{
authName: '创建项目',
children: [],
id: 105,
parentId: 100,
},
],
},
{
authName: '配置管理',
id: 200,
isSelect: true,
children: [
{
authName: '模型配置',
id: 201,
parentId: 200,
children: [
{
authName: '添加模型',
parentId: 201,
id: 202,
children: [],
},
],
},
],
},
{
authName: '用户管理',
id: 300,
children: [
{
authName: '个人设置',
id: 307,
parentId: 300,
},
],
},
],
createName: 'admin',
remark: '用户基础权限',
};
/* 全部的权限配置 */
let ROLE_ALL_TEMPLATE = {
roleName: 'root',
roleTree: [
{
authName: '项目管理',
id: 100,
isSelect: true,
children: [
{
authName: '工作台',
isSelect: true,
parentId: 100,
id: 101,
children: [],
},
{
authName: '项目列表',
isSelect: true,
parentId: 100,
id: 102,
children: [
{
authName: '编辑项目',
isSelect: true,
parentId: 102,
id: 103,
children: [],
},
{
isSelect: true,
authName: '删除项目',
parentId: 102,
id: 104,
children: [],
},
],
},
{
isSelect: true,
authName: '创建项目',
children: [],
id: 105,
parentId: 100,
},
],
},
{
authName: '配置管理',
isSelect: true,
id: 200,
children: [
{
authName: '模型配置',
isSelect: true,
id: 201,
parentId: 200,
children: [
{
authName: '添加模型',
isSelect: true,
parentId: 201,
id: 202,
children: [],
},
],
},
],
},
{
authName: '用户管理',
isSelect: true,
id: 300,
children: [
{
authName: '用户列表',
id: 301,
parentId: 300,
isSelect: true,
children: [
{
authName: '创建用户',
id: 302,
isSelect: true,
parentId: 301,
children: [],
},
{
authName: '编辑用户',
id: 303,
isSelect: true,
parentId: 301,
children: [],
},
],
},
{
authName: '权限管理',
isSelect: true,
id: 304,
parentId: 300,
children: [
{
authName: '创建模版',
id: 305,
isSelect: true,
parentId: 304,
children: [],
},
{
authName: '编辑模版',
id: 306,
isSelect: true,
parentId: 304,
children: [],
},
],
},
{
isSelect: true,
authName: '个人设置',
id: 307,
parentId: 300,
children: [],
},
],
},
],
createName: 'admin',
remark: '所有权限',
};
let allTree = ROLE_ALL_TEMPLATE.roleTree;
let presentTree = ROLE_BASICS_TEMPLATE.roleTree;
// 思路。 1. 因为两个数组的子节点 id 都是相同的,又因为 all 数组是所有的权限 即可以直接用all数组去做循环,拿当前id去用户基础权限数组中查找对应权限如果找到,并且isSelcet 值为 true 则 返回 tree 赋值给当前的 all 数组节点属性,反之亦然
// 首先写一个 递归函数 用于查询当前子节点状态。 参数为 节点 id 和当前数组
function getTree(min, max) {
// console.log(min, max);
function searchTargetObj(id, ary) {
for (let b = 0; b < ary.length; b++) {
if (ary[b].id === id) {
return ary[b].isSelect;
}
if (ary[b].children && ary[b].children.length !== 0) {
if (searchTargetObj(id, ary[b].children)) {
return searchTargetObj(id, ary[b].children);
}
}
}
}
// let temp = searchTargetObj(400, min)
// 写一个方法去遍历所有权限树,返回时否有权限并赋值给当前树
function filterRoleTree(tree) {
for (let a = 0; a < tree.length; a++) {
// 这里有可能存在子权限没有该权限的情况,isSelect没有值的情况,为false的情况;
tree[a].isSelect = searchTargetObj(tree[a].id, min) || false;
if (tree[a].children && tree[a].children !== 0) {
filterRoleTree(tree[a].children);
}
}
}
filterRoleTree(max);
return max;
}
let temp = getTree(presentTree, allTree);
console.log(temp);
// getTree(presentTree, allTree);
let ary = ['/api/status'];
// console.log(ary.includes('/api/status'));
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
TypeScript
1
https://gitee.com/ProgressSpace/self_discipline_serve.git
git@gitee.com:ProgressSpace/self_discipline_serve.git
ProgressSpace
self_discipline_serve
self_discipline_serve
master

搜索帮助