app.js
6.33 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
222
import { utils } from 'glsx-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 }) {
await dispatch('auth/logout', null, { root: true })
await dispatch('init/reset', null, { root: true })
},
/* 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) {
const routes = data.reduce((routes, { name, children }) => {
if (children && children.length) {
const route = getMenuItem(name, children)
routes = routes.concat(route)
}
return routes
}, [])
router.addRoutes(routes)
commit('SET_MENU_LIST', data)
// routeMap(routes)
commit('SET_ROUTES', routes)
}
}
}
/**
* 解析后台返回数据,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) {
let routers = []
const flattenMenus = utils.array.flattenDeep(menus)
for (let i = 0, j = flattenMenus.length; i < j; i++) {
const menu = flattenMenus[i]
const route = __gen(moduleName, menu)
if (router.hasRoute(route.path)) continue
// router.addRoute(route)
routers.push(route)
}
return routers
}
/**
* 生成动态路由参数
*/
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
// if (!targetPath) {
// if (!children || children.length == 0) {
// throw new Error(constants.SCRM_BASE_ERRORS.ILLEGAL_ROUTER_NO_THIRD_ROUTE.MESSAGE)
// }
// const childs = children.noHidden()
// if (childs.length == 0) {
// throw new Error(constants.SCRM_BASE_ERRORS.ILLEGAL_ROUTER_NO_THIRD_ROUTE.MESSAGE)
// }
// if (!originPath) _path += id
redirect = _gen_path_(children.noHidden()[0].path)
// } else {
// redirect = targetPath
// meta.targetPath = targetPath
// }
} 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 routeMap(routes) {
// const redirects = routes.filter(route => route.meta.targetPath)
// redirects.forEach(item => {
// const target = routes.find(route => route.path == item.meta.targetPath)
// if (target) {
// target.meta.origin = item.meta
// }
// })
// return routes
// }
/**
* 获取默认加载路由
* @param {*} menu
* @returns
*/
function getDefaultPath(menu) {
const { name, children, sub = children.noHidden(), child = sub.length ? sub : children } = menu
let { path } = child[0]
if (path.includes('^')) {
const [, targetPath] = path.split('^')
path = targetPath || child[0].children.noHidden()[0].path
}
return _get_path_(name, path)
}
function _get_path_(moduleName, p) {
return `/${moduleName}${p.startsWith('/') ? p : `/${p}`}`
}
export default app