permission.js
3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* @Author: xiaomiao 1158771342@qq.com
* @Date: 2023-03-09 15:24:53
* @LastEditors: Please set LastEditors
* @LastEditTime: 2023-05-12 10:47:42
* @FilePath: \上报\bdcjg-web\src\store\modules\permission.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import Vue from 'vue'
import { constantRoutes } from '@/router'
// import asyncRouter from '@/utils/asyncRouter.js'
const state = {
routes: [],
addRoutes: false,
}
const mutations = {
SET_ROUTES: (state, routes) => {
state.addRoutes = true
state.routes = routes
},
RESET_ROUTE: (state) => {
state.addRoutes = false
}
}
const actions = {
// 添加全部菜单
/**
* @description: 添加全部菜单
* @param {*} commit
* @param {*} getMenuInfo
* @author: renchao
*/
generateRoutes ({ commit }, getMenuInfo) {
let Layout = r => require.ensure([], () => r(require(`@/layout`)))
function asyncRouter (routers) {
routers.forEach(item => {
if (!item.children) {
delete item.children
} else {
item.children = asyncRouter(item.children)
}
item.path = JSON.parse(item.metadata)?.path || '/'
item.affix = JSON.parse(item.metadata)?.affix || false
if (!item.parentId) {
item.component = Layout
} else {
item.component = loadView(item.uri)
}
item.meta = {
title: item.name,
icon: item.icon,
affix: item.affix
}
})
return routers
}
function loadView (view) {
return r => require.ensure([], () => r(require(`@/views${view}.vue`)))
}
return new Promise(resolve => {
// 将权限菜单数组转成路由树数据结构
let permission_tree = asyncRouter(getMenuInfo)
const mergeResult = _.cloneDeep(constantRoutes).concat(permission_tree);
commit('SET_ROUTES', mergeResult)
resolve(permission_tree)
})
},
// 重置路由
/**
* @description: 重置路由
* @param {*} commit
* @author: renchao
*/
resetRoutes ({ commit }) {
commit('RESET_ROUTE')
}
}
// 树转数组
/**
* @description: 树转数组
* @param {*} root
* @param {*} fVisit
* @author: renchao
*/
function dfs (root, fVisit) {
let stack = Array.isArray(root) ? [...root] : [root];
while (stack.length) {
let node = stack.pop();
fVisit && fVisit(node);
let children = node.children;
if (children && children.length) {
for (let i = children.length - 1; i >= 0; i--) stack.push(children[i]);
}
}
}
// 数组转树
//需要插入父节点id,pid为null或'',就是找root节点,然后root节点再去找自己的子节点
/**
* @description: 数组转树
* @param {*} data
* @param {*} pid
* @author: renchao
*/
function array2Tree (data, pid) {
let res = [];
data.forEach(item => {
if (item.parentId === pid) {
let itemChildren = array2Tree(data, item.id);
if (itemChildren.length) item.children = itemChildren;
res.push(item);
}
});
// 菜单数据反转,保持一致
res.reverse()
_.each(res, c => {
if (c.children && c.children.length > 0) {
c.children.reverse()
}
})
return res;
}
export default {
namespaced: true,
state,
mutations,
actions
}