app.js
6.24 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { utils } from 'glsk-common'
import Layout from '@/views/Layout'
import { constants, helper } from '@/common'
import router from '@/router'
const app = {
namespaced: true,
state: () => ({
routers: null,
menuList: [],
currentPage: null,
isLoading: false,
flatten: []
}),
mutations: {
SET_CURRENT_PAGE(state, page) {
if (page.origin) page = page.origin
state.currentPage = page
},
SET_MENU_LIST(state, list) {
state.menuList = list
// state.flatten = utils.array.flattenDeep(list)
},
SET_ROUTES(state, payload) {
state.routers = payload
},
SET_LOADING(state, isLoading) {
state.isLoading = isLoading
},
CLEAR(state) {
state.menuList = []
state.flatten = []
state.currentPage = null
state.isLoading = false
utils.storage.removeSession(constants.SCRM_BASE_MENULIST)
}
},
actions: {
async login({ dispatch }, menus) {
const { menuFormat, defaultPath } = await dispatch('formatSave', menus)
await dispatch('persisted', menuFormat)
return { defaultPath }
},
async logout({ dispatch, commit }) {
commit('SET_LOADING', true)
await dispatch('auth/logout', null, { root: true })
setTimeout(() => {
dispatch('init/reset', null, { root: true })
router.replace('/login')
}, 300)
},
/* eslint-disable */
async formatSave({ dispatch }, menus) {
const menuFormat = formatData(menus, null)
const defaultPath = getDefaultPath(menuFormat[0])
utils.storage.setSession(constants.SCRM_BASE_MENULIST, menuFormat)
return { menuFormat, defaultPath }
},
clear({ commit }) {
commit('CLEAR')
},
setCurrentPage({ commit, dispatch }, page) {
dispatch('menu/clear', null, { root: true })
commit('SET_CURRENT_PAGE', page)
},
setLoading({ commit }, isLoading) {
commit('SET_LOADING', isLoading)
},
persisted({ commit }, data) {
data.forEach(({ name, children }) => children?.length && getMenuItem(name, children))
router.addRoute(router.ERR404)
commit('SET_MENU_LIST', data)
}
}
}
/**
* 解析后台返回数据,format 系统所需要的格式
* @param {*} menus
* @returns
*/
function formatData(menus, parentMenu) {
const formatMenus = []
for (let i = 0, j = menus.length; i < j; i++) {
if (!menus[i]) continue
const menu = {}
let { menuUrl: path, id, label: title, url, children, logo: icon, name, functionUrl, entry = path } = menus[i]
if (icon === constants.SCRM_BASE_FUNCTIONAL_FLAG) {
const { meta = {} } = parentMenu
meta.permissions = meta.permissions || []
meta.permissions.push(title)
parentMenu.meta = meta
if (!functionUrl) continue
path = functionUrl
menu.hidden = true
}
if (!parentMenu) {
if (!path.includes('@')) continue
;[entry, name] = path.split('@')
menu.entry = entry
menu.icon = helper.getIcon(name)
menu.name = name
menu.id = `${id}`
} else {
menu.id = `${parentMenu.id}-${id}`
if (path == '^' && (!children || children.length == 0 || children.noHidden().length == 0)) {
throw new Error(`“${title}”:${constants.SCRM_BASE_ERRORS.ILLEGAL_ROUTER_NO_THIRD_ROUTE.MESSAGE}`)
}
}
if (title.startsWith('*')) {
title = title.slice(1)
menu.hidden = true
}
menu.title = title
menu.path = path
menu.url = url
if (children) {
menu.children = formatData(children, menu)
}
formatMenus.push(menu)
}
return formatMenus
}
// 组装路由数据
function getMenuItem(moduleName, menus) {
const flattenMenus = utils.array.flattenDeep(menus)
for (let i = 0, j = flattenMenus.length; i < j; i++) {
const menu = flattenMenus[i]
if (menu.path === '^') continue
const route = __gen(moduleName, menu)
router.addRoute(route)
}
}
/**
* 生成动态路由参数
*/
/*function __gen(moduleName, menu) {
let { path, meta = {}, id, title, url, children, hidden = false } = menu
let _path = ''
let redirect = ''
function _gen_path_(p = path) {
return _get_path_(moduleName, p)
}
if (path.includes('^')) {
// const [originPath] = path.split('^')
_path = '^' //_gen_path_() + id
redirect = _gen_path_(children.noHidden()[0].path)
} else {
_path = _gen_path_()
}
//路由path生成name 如 home/home/index =>HomeHomeIndex
//排除 以:开头 比如 home/home/index/:id 参数:id 不参与生成规则
//name 主要用于 子系统内部 params传参 如 this.$router.push({name:'HomeHomeIndex',params:{id:123}})
const name = _path.split('/').reduce((t, n) => {
if (!n.startsWith(':')) {
t += n.charAt(0).toUpperCase() + n.slice(1)
}
return t
}, '')
menu.path = _path
const route = {
path: _path,
component: Layout,
redirect,
name,
meta: { ...meta, id, title, name, url, path: _path, hidden }
}
return route
}*/
function __gen(moduleName, menu) {
let { path, meta = {}, id, title, url, hidden = false } = menu
let _path = _get_path_(moduleName, path)
//路由path生成name 如 home/home/index =>HomeHomeIndex
//排除 以:开头 比如 home/home/index/:id 参数:id 不参与生成规则
//name 主要用于 子系统内部 params传参 如 this.$router.push({name:'HomeHomeIndex',params:{id:123}})
const name = _path.split('/').reduce((t, n) => {
if (!n.startsWith(':')) {
t += n.charAt(0).toUpperCase() + n.slice(1)
}
return t
}, '')
menu.path = _path
const route = {
path: _path,
component: Layout,
name,
meta: { ...meta, id, title, name, url, path: _path, hidden }
}
return route
}
/**
* 获取默认加载路由
* @param {*} menu
* @returns
*/
function getDefaultPath(menu) {
const { name, children, sub = children.noHidden(), child = sub.length ? sub : children } = menu
const path = _loop(child)
return _get_path_(name, path)
}
/**
* 递归获取无实际路径的菜单下的第一个子菜单的路径
* @param {*} menus
* @returns
*/
function _loop(menus) {
const { path } = menus.noHidden()[0]
if (path.includes('^')) {
return _loop(menus[0].children)
}
return path
}
function _get_path_(moduleName, p) {
return `/${moduleName}${p.startsWith('/') ? p : `/${p}`}`
}
export default app