vue2-client 1.10.32-alpha → 1.10.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,526 +1,450 @@
1
- import Vue from 'vue'
2
- import routerMap from '@vue2-client/router/async/router.map'
3
- import { mergeI18nFromRoutes } from '@vue2-client/utils/i18n'
4
- import { arrRemoveEmpty } from '@vue2-client/utils/util'
5
- import Router from 'vue-router'
6
- import deepMerge from 'deepmerge'
7
- import basicOptions from '@vue2-client/router/async/config.async'
8
-
9
- // 应用配置
10
- const appOptions = {
11
- router: undefined,
12
- i18n: undefined,
13
- store: undefined
14
- }
15
-
16
- /**
17
- * 设置应用配置
18
- * @param options
19
- */
20
- function setAppOptions (options) {
21
- const { router, store, i18n } = options
22
- appOptions.router = router
23
- appOptions.store = store
24
- appOptions.i18n = i18n
25
- }
26
-
27
- // 应用路由资源
28
- const appRouterMap = {
29
- routerMap: routerMap
30
- }
31
-
32
- /**
33
- * 设置应用路由资源
34
- * @param router 应用路由
35
- */
36
- function setAppRouterMap (router) {
37
- appRouterMap.routerMap = Object.assign(appRouterMap.routerMap, router)
38
- }
39
-
40
- /**
41
- * 设置基础应用路由资源
42
- * @param router 基础路由
43
- */
44
- function setBaseRouterMap (router) {
45
- const routes = basicOptions.routes
46
- for (let i = 0; i < routes.length; i++) {
47
- const item = router[routes[i].path]
48
- if (item) {
49
- routes[i].component = item
50
- }
51
- }
52
- }
53
-
54
- /**
55
- * 根据 路由配置 和 路由组件注册 解析路由
56
- * @param routesConfig 路由配置
57
- * @param routerMap 本地路由组件注册配置
58
- */
59
- function parseRoutes (routesConfig, routerMap) {
60
- const routes = []
61
- routesConfig.forEach(item => {
62
- // 获取注册在 routerMap 中的 router,初始化 routeCfg
63
- let router; let routeCfg = {}
64
- if (typeof item === 'string') {
65
- router = routerMap[item]
66
- routeCfg = { path: (router && router.path) || item, router: item }
67
- } else if (typeof item === 'object') {
68
- // 当没有设置路由对象名或者设置的是blank路由对象时, 给空界面, path为名称
69
- if (!item.router || item.router === 'blank') {
70
- router = routerMap.blank
71
- item.path = encodeURI(item.name)
72
- } else {
73
- router = routerMap[item.router]
74
- if (!router) {
75
- router = routerMap.blank
76
- }
77
- }
78
- // 查看是否是单页面
79
- if (item.meta && item.meta.singlePage) {
80
- router = routerMap.singlePage
81
- item.path = encodeURI(item.name)
82
- }
83
- // 查看是否是栅格配置页面
84
- if (item.meta && item.meta.type) {
85
- if (item.meta.type === 'GridView') {
86
- router = routerMap.gridView
87
- item.path = encodeURI(item.router) || encodeURI(item.name)
88
- }
89
- }
90
- // 当没在动态路由对象中找到时, 不添加到路由
91
- // if (!router) return
92
- routeCfg = item
93
- }
94
- if (!router) {
95
- // console.warn(`can't find register for router ${routeCfg.router}, please register it in advance.`)
96
- router = typeof item === 'string' ? { path: item, name: item } : item
97
- }
98
- // 从 router 和 routeCfg 解析路由
99
- const meta = {
100
- authority: router.authority,
101
- icon: router.icon,
102
- page: router.page,
103
- link: router.link,
104
- params: router.params,
105
- query: router.query,
106
- ...router.meta
107
- }
108
- const cfgMeta = {
109
- authority: routeCfg.authority,
110
- icon: routeCfg.icon,
111
- page: routeCfg.page,
112
- link: routeCfg.link,
113
- params: routeCfg.params,
114
- query: routeCfg.query,
115
- ...routeCfg.meta
116
- }
117
- Object.keys(cfgMeta).forEach(key => {
118
- if (cfgMeta[key] === undefined || cfgMeta[key] === null || cfgMeta[key] === '') {
119
- delete cfgMeta[key]
120
- }
121
- })
122
- Object.assign(meta, cfgMeta)
123
- const route = {
124
- path: routeCfg.path || router.path || routeCfg.router,
125
- name: routeCfg.name || router.name,
126
- component: router.component || router,
127
- children: router.children,
128
- redirect: routeCfg.redirect || router.redirect,
129
- meta: { ...meta, authority: meta.authority || '*' }
130
- }
131
- if (router.beforeEnter) {
132
- route.beforeEnter = router.beforeEnter
133
- }
134
- if (routeCfg.invisible || router.invisible) {
135
- route.meta.invisible = true
136
- }
137
- if (routeCfg.children && routeCfg.children.length > 0) {
138
- route.children = parseRoutes(routeCfg.children, routerMap)
139
- }
140
- // 当没有子并且自己时blank(空界面)时, 不添加到路由
141
- if (route.component.name === 'blank' && route.children && route.children.length <= 0) return
142
- routes.push(route)
143
- })
144
- return routes
145
- }
146
-
147
- /**
148
- * 加载路由
149
- * @param routesConfig {[{router: string, children: [{router: string, children: string[]}, {router: string, children: string[]}, {router: string, authority: string, name: string, icon: string}, {path: string, router: string, name: string, icon: string, link: string}, {path: string, router: string, name: string, icon: string, link: string}]}]} 路由配置
150
- */
151
- function loadRoutes (routesConfig) {
152
- // 兼容 0.6.1 以下版本
153
- /** ************* 兼容 version < v0.6.1 *****************/
154
- if (arguments.length > 0) {
155
- const arg0 = arguments[0]
156
- if (arg0.router || arg0.i18n || arg0.store) {
157
- routesConfig = arguments[1]
158
- console.error('the usage of signature loadRoutes({router, store, i18n}, routesConfig) is out of date, please use the new signature: loadRoutes(routesConfig).')
159
- console.error('方法签名 loadRoutes({router, store, i18n}, routesConfig) 的用法已过时, 请使用新的方法签名 loadRoutes(routesConfig)。')
160
- }
161
- }
162
- /** ************* 兼容 version < v0.6.1 *****************/
163
-
164
- // 应用配置
165
- const { router, store, i18n } = appOptions
166
-
167
- // 刷新页面时,有些全局状态丢失,在此处从本地缓存拿出来赋值
168
- if (JSON.stringify(Vue.$login.f) === '{}') {
169
- const login = store.getters['account/login']
170
- Object.assign(Vue.$login, login)
171
- }
172
- // 如果 routesConfig 有值,则更新到本地,否则从本地获取
173
- if (routesConfig) {
174
- store.commit('account/setRoutesConfig', routesConfig)
175
- } else {
176
- routesConfig = store.getters['account/routesConfig']
177
- }
178
- // 如果开启了异步路由,则加载异步路由配置
179
- const asyncRoutes = store.state.setting.asyncRoutes
180
- if (asyncRoutes) {
181
- if (routesConfig && routesConfig.length > 0) {
182
- const routes = parseRoutes(routesConfig, appRouterMap.routerMap)
183
- // 设置路由首页
184
- routes[0].redirect = store.state.setting.homePage
185
- let finalRoutes = mergeRoutes(basicOptions.routes, routes)
186
- formatRoutes(finalRoutes)
187
- const duplicates = checkDuplicateRouteNames(finalRoutes)
188
- if (duplicates.length > 0) {
189
- // 可以选择在这里处理重复的路由名称
190
- finalRoutes = deduplicateRouteNames(finalRoutes)
191
- }
192
- router.options = { ...router.options, routes: finalRoutes }
193
- router.matcher = new Router({ ...router.options, routes: [] }).matcher
194
- router.addRoutes(finalRoutes)
195
- }
196
- }
197
- // 提取路由国际化数据
198
- mergeI18nFromRoutes(i18n, router.options.routes)
199
- // 初始化Admin后台菜单数据
200
- const rootRoute = router.options.routes.find(item => item.path === '/')
201
- const menuRoutes = rootRoute && rootRoute.children
202
- if (menuRoutes) {
203
- store.commit('setting/setMenuData', menuRoutes)
204
- }
205
- }
206
-
207
- function checkDuplicateRouteNames (routes) {
208
- const names = new Set()
209
- const duplicates = []
210
-
211
- function check (route) {
212
- if (route.name) {
213
- if (names.has(`${route.name}_${route.path}`)) {
214
- duplicates.push(`${route.name}_${route.path}`)
215
- } else {
216
- names.add(`${route.name}_${route.path}`)
217
- }
218
- }
219
- if (route.children) {
220
- route.children.forEach(check)
221
- }
222
- }
223
-
224
- routes.forEach(check)
225
- return duplicates
226
- }
227
-
228
- // 处理重复路由名称的函数
229
- function deduplicateRouteNames (routes) {
230
- const namePathMap = new Map() // 存储 name_path 组合
231
- const nameCountMap = new Map() // 存储仅 name 的计数
232
-
233
- function deduplicate (route) {
234
- if (!route) return null
235
-
236
- if (route.name) {
237
- const namePathKey = `${route.name}_${route.path}`
238
- const nameKey = route.name
239
-
240
- // 检查 name 和 path 是否都重复
241
- if (namePathMap.has(namePathKey)) {
242
- // 完全重复的路由,直接移除
243
- // console.log(`移除重复路由: name=${route.name}, path=${route.path}`)
244
- return null
245
- } else {
246
- // 记录这个 name 和 path 的组合
247
- namePathMap.set(namePathKey, route)
248
-
249
- // 检查是否只有 name 重复
250
- if (nameCountMap.has(nameKey)) {
251
- const count = nameCountMap.get(nameKey) + 1
252
- nameCountMap.set(nameKey, count)
253
- // 修改路由名称,添加计数后缀
254
- route.name = `${nameKey}_${count}`
255
- // console.log(`重命名重复路由: ${nameKey} -> ${route.name}`)
256
- } else {
257
- nameCountMap.set(nameKey, 1)
258
- }
259
- }
260
- }
261
-
262
- // 递归处理子路由
263
- if (route.children && route.children.length) {
264
- route.children = route.children
265
- .map(child => deduplicate({ ...child }))
266
- .filter(child => child !== null) // 移除返回 null 的子路由
267
- }
268
-
269
- return route
270
- }
271
-
272
- // 处理所有路由并过滤掉返回 null 的路由
273
- return routes
274
- .map(route => deduplicate({ ...route }))
275
- .filter(route => route !== null)
276
- }
277
-
278
- /**
279
- * 合并路由
280
- * @param target {Route[]}
281
- * @param source {Route[]}
282
- * @returns {Route[]}
283
- */
284
- function mergeRoutes (target, source) {
285
- const routesMap = {}
286
- // eslint-disable-next-line no-return-assign
287
- target.forEach(item => routesMap[item.path] = item)
288
- // eslint-disable-next-line no-return-assign
289
- source.forEach(item => routesMap[item.path] = item)
290
- return Object.values(routesMap)
291
- }
292
-
293
- /**
294
- * 深度合并路由
295
- * @param target {Route[]}
296
- * @param source {Route[]}
297
- * @returns {Route[]}
298
- */
299
- function deepMergeRoutes (target, source) {
300
- // 映射路由数组
301
- const mapRoutes = routes => {
302
- const routesMap = {}
303
- routes.forEach(item => {
304
- routesMap[item.path] = {
305
- ...item,
306
- children: item.children ? mapRoutes(item.children) : undefined
307
- }
308
- })
309
- return routesMap
310
- }
311
- const tarMap = mapRoutes(target)
312
- const srcMap = mapRoutes(source)
313
-
314
- // 合并路由
315
- const merge = deepMerge(tarMap, srcMap)
316
-
317
- // 转换为 routes 数组
318
- const parseRoutesMap = routesMap => {
319
- return Object.values(routesMap).map(item => {
320
- if (item.children) {
321
- item.children = parseRoutesMap(item.children)
322
- } else {
323
- delete item.children
324
- }
325
- return item
326
- })
327
- }
328
- return parseRoutesMap(merge)
329
- }
330
-
331
- /**
332
- * 格式化路由
333
- * @param routes 路由配置
334
- */
335
- function formatRoutes (routes) {
336
- routes.forEach(route => {
337
- const { path } = route
338
- if (!path.startsWith('/') && path !== '*') {
339
- route.path = '/' + path
340
- }
341
- })
342
- formatAuthority(routes)
343
- }
344
-
345
- /**
346
- * 格式化路由的权限配置
347
- * @param routes 路由
348
- * @param pAuthorities 父级路由权限配置集合
349
- */
350
- function formatAuthority (routes, pAuthorities = []) {
351
- routes.forEach(route => {
352
- const meta = route.meta
353
- const defaultAuthority = pAuthorities[pAuthorities.length - 1] || { permission: '*' }
354
- if (meta) {
355
- let authority = {}
356
- if (!meta.authority) {
357
- authority = defaultAuthority
358
- } else if (typeof meta.authority === 'string' || Array.isArray(meta.authority)) {
359
- authority.permission = meta.authority
360
- } else if (typeof meta.authority === 'object') {
361
- authority = meta.authority
362
- const { role } = authority
363
- if (typeof role === 'string') {
364
- authority.role = [role]
365
- }
366
- if (!authority.permission && !authority.role) {
367
- authority = defaultAuthority
368
- }
369
- }
370
- meta.authority = authority
371
- } else {
372
- const authority = defaultAuthority
373
- route.meta = { authority }
374
- }
375
- route.meta.pAuthorities = pAuthorities
376
- if (route.children) {
377
- formatAuthority(route.children, [...pAuthorities, route.meta.authority])
378
- }
379
- })
380
- }
381
-
382
- /**
383
- * 从路由 path 解析 i18n key
384
- * @param path
385
- * @returns {*}
386
- */
387
- function getI18nKey (path) {
388
- const keys = path.split('/').filter(item => !item.startsWith(':') && item !== '')
389
- keys.push('name')
390
- return keys.join('.')
391
- }
392
-
393
- /**
394
- * 加载导航守卫
395
- * @param guards
396
- * @param options
397
- */
398
- function loadGuards (guards, options) {
399
- const { beforeEach, afterEach } = guards
400
- const { router } = options
401
- beforeEach.forEach(guard => {
402
- if (guard && typeof guard === 'function') {
403
- router.beforeEach((to, from, next) => guard(to, from, next, options))
404
- }
405
- })
406
- afterEach.forEach(guard => {
407
- if (guard && typeof guard === 'function') {
408
- router.afterEach((to, from) => guard(to, from, options))
409
- }
410
- })
411
- }
412
-
413
- /**
414
- * 资源服务路由转新路由
415
- * @param func
416
- */
417
- function funcToRouter (func) {
418
- return [{
419
- router: 'root',
420
- children: positionRouter(parsefunc(func))
421
- }]
422
- }
423
-
424
- function parsefunc (func) {
425
- const tree = []
426
- for (const row of func) {
427
- const route = {
428
- router: row.link,
429
- meta: {
430
- singlePage: row.navigate
431
- },
432
- position: row.position - 1,
433
- icon: row.icon,
434
- name: row.name
435
- }
436
- if (row.link) {
437
- if (row.link.includes('$')) {
438
- route.router = row.link.split('$')[1]
439
- route.params = row.link.split('$')[0]
440
- }
441
- if (route.router.includes('?')) {
442
- // 把问号后面的都删了
443
- const [url, queryString] = route.router.split('?')
444
- route.router = url
445
- try {
446
- const params = new URLSearchParams(queryString)
447
- for (const [key, value] of params.entries()) {
448
- route.meta[key] = value
449
- }
450
- if (route.meta.path) {
451
- route.path = route.meta.path
452
- delete route.meta.path
453
- }
454
- } catch (e) {}
455
- }
456
- }
457
- if (row.children && row.children.length > 0) {
458
- route.children = parsefunc(row.children)
459
- }
460
- tree.push(route)
461
- }
462
- // 如果是 microapp 作为子应用路由应该是平铺的 树状结构由父项目处理
463
- if (window.__MICRO_APP_ENVIRONMENT__) {
464
- return flattenTree(tree)
465
- }
466
- return tree
467
- }
468
-
469
- function flattenTree (tree) {
470
- let flat = []
471
- for (const node of tree) {
472
- const { children, ...rest } = node
473
- flat.push(rest)
474
- if (children && children.length > 0) {
475
- flat = flat.concat(flattenTree(children))
476
- }
477
- }
478
- return flat
479
- }
480
-
481
- /**
482
- * 资源服务路由排序
483
- */
484
- function positionRouter (r) {
485
- let router = r.sort((a, b) => a.position - b.position)
486
- router = arrRemoveEmpty(router)
487
- return router
488
- }
489
-
490
- function funcToRouterOA (func) {
491
- return [{
492
- router: 'root',
493
- children: positionRouterOA(parsefuncOA(func))
494
- }]
495
- }
496
-
497
- function positionRouterOA (r) {
498
- let router = []
499
- for (const row of r) {
500
- router[row.position] = row
501
- }
502
- router = arrRemoveEmpty(router)
503
- return router
504
- }
505
-
506
- function parsefuncOA (func) {
507
- const router = []
508
- for (const row of func) {
509
- const route = {
510
- router: row.link,
511
- meta: {
512
- singlePage: row.navigate
513
- },
514
- position: row.position - 1,
515
- icon: row.icon,
516
- name: row.name
517
- }
518
- if (row.children && row.children.length > 0) {
519
- route.children = parsefunc(row.children)
520
- }
521
- router.push(route)
522
- }
523
- return router
524
- }
525
-
526
- export { parseRoutes, loadRoutes, formatAuthority, getI18nKey, loadGuards, deepMergeRoutes, formatRoutes, setAppOptions, funcToRouter, setAppRouterMap, setBaseRouterMap, funcToRouterOA }
1
+ import Vue from 'vue'
2
+ import routerMap from '@vue2-client/router/async/router.map'
3
+ import { mergeI18nFromRoutes } from '@vue2-client/utils/i18n'
4
+ import { arrRemoveEmpty } from '@vue2-client/utils/util'
5
+ import Router from 'vue-router'
6
+ import deepMerge from 'deepmerge'
7
+ import basicOptions from '@vue2-client/router/async/config.async'
8
+
9
+ // 应用配置
10
+ const appOptions = {
11
+ router: undefined,
12
+ i18n: undefined,
13
+ store: undefined
14
+ }
15
+
16
+ /**
17
+ * 设置应用配置
18
+ * @param options
19
+ */
20
+ function setAppOptions (options) {
21
+ const { router, store, i18n } = options
22
+ appOptions.router = router
23
+ appOptions.store = store
24
+ appOptions.i18n = i18n
25
+ }
26
+
27
+ // 应用路由资源
28
+ const appRouterMap = {
29
+ routerMap: routerMap
30
+ }
31
+
32
+ /**
33
+ * 设置应用路由资源
34
+ * @param router 应用路由
35
+ */
36
+ function setAppRouterMap (router) {
37
+ appRouterMap.routerMap = Object.assign(appRouterMap.routerMap, router)
38
+ }
39
+
40
+ /**
41
+ * 设置基础应用路由资源
42
+ * @param router 基础路由
43
+ */
44
+ function setBaseRouterMap (router) {
45
+ const routes = basicOptions.routes
46
+ for (let i = 0; i < routes.length; i++) {
47
+ const item = router[routes[i].path]
48
+ if (item) {
49
+ routes[i].component = item
50
+ }
51
+ }
52
+ }
53
+
54
+ /**
55
+ * 根据 路由配置 和 路由组件注册 解析路由
56
+ * @param routesConfig 路由配置
57
+ * @param routerMap 本地路由组件注册配置
58
+ */
59
+ function parseRoutes (routesConfig, routerMap) {
60
+ const routes = []
61
+ routesConfig.forEach(item => {
62
+ // 获取注册在 routerMap 中的 router,初始化 routeCfg
63
+ let router; let routeCfg = {}
64
+ if (typeof item === 'string') {
65
+ router = routerMap[item]
66
+ routeCfg = { path: (router && router.path) || item, router: item }
67
+ } else if (typeof item === 'object') {
68
+ // 当没有设置路由对象名或者设置的是blank路由对象时, 给空界面, path为名称
69
+ if (!item.router || item.router === 'blank') {
70
+ router = routerMap.blank
71
+ item.path = encodeURI(item.name)
72
+ } else {
73
+ router = routerMap[item.router]
74
+ if (!router) {
75
+ router = routerMap.blank
76
+ }
77
+ }
78
+ // 查看是否是单页面
79
+ if (item.meta && item.meta.singlePage) {
80
+ router = routerMap.singlePage
81
+ item.path = encodeURI(item.name)
82
+ }
83
+ // 查看是否是栅格配置页面
84
+ if (item.meta && item.meta.type) {
85
+ if (item.meta.type === 'GridView') {
86
+ router = routerMap.gridView
87
+ item.path = encodeURI(item.router) || encodeURI(item.name)
88
+ }
89
+ }
90
+ // 当没在动态路由对象中找到时, 不添加到路由
91
+ // if (!router) return
92
+ routeCfg = item
93
+ }
94
+ if (!router) {
95
+ // console.warn(`can't find register for router ${routeCfg.router}, please register it in advance.`)
96
+ router = typeof item === 'string' ? { path: item, name: item } : item
97
+ }
98
+ // 从 router 和 routeCfg 解析路由
99
+ const meta = {
100
+ authority: router.authority,
101
+ icon: router.icon,
102
+ page: router.page,
103
+ link: router.link,
104
+ params: router.params,
105
+ query: router.query,
106
+ ...router.meta
107
+ }
108
+ const cfgMeta = {
109
+ authority: routeCfg.authority,
110
+ icon: routeCfg.icon,
111
+ page: routeCfg.page,
112
+ link: routeCfg.link,
113
+ params: routeCfg.params,
114
+ query: routeCfg.query,
115
+ ...routeCfg.meta
116
+ }
117
+ Object.keys(cfgMeta).forEach(key => {
118
+ if (cfgMeta[key] === undefined || cfgMeta[key] === null || cfgMeta[key] === '') {
119
+ delete cfgMeta[key]
120
+ }
121
+ })
122
+ Object.assign(meta, cfgMeta)
123
+ const route = {
124
+ path: routeCfg.path || router.path || routeCfg.router,
125
+ name: routeCfg.name || router.name,
126
+ component: router.component || router,
127
+ children: router.children,
128
+ redirect: routeCfg.redirect || router.redirect,
129
+ meta: { ...meta, authority: meta.authority || '*' }
130
+ }
131
+ if (router.beforeEnter) {
132
+ route.beforeEnter = router.beforeEnter
133
+ }
134
+ if (routeCfg.invisible || router.invisible) {
135
+ route.meta.invisible = true
136
+ }
137
+ if (routeCfg.children && routeCfg.children.length > 0) {
138
+ route.children = parseRoutes(routeCfg.children, routerMap)
139
+ }
140
+ // 当没有子并且自己时blank(空界面)时, 不添加到路由
141
+ if (route.component.name === 'blank' && route.children && route.children.length <= 0) return
142
+ routes.push(route)
143
+ })
144
+ return routes
145
+ }
146
+
147
+ /**
148
+ * 加载路由
149
+ * @param routesConfig {[{router: string, children: [{router: string, children: string[]}, {router: string, children: string[]}, {router: string, authority: string, name: string, icon: string}, {path: string, router: string, name: string, icon: string, link: string}, {path: string, router: string, name: string, icon: string, link: string}]}]} 路由配置
150
+ */
151
+ function loadRoutes (routesConfig) {
152
+ // 兼容 0.6.1 以下版本
153
+ /** ************* 兼容 version < v0.6.1 *****************/
154
+ if (arguments.length > 0) {
155
+ const arg0 = arguments[0]
156
+ if (arg0.router || arg0.i18n || arg0.store) {
157
+ routesConfig = arguments[1]
158
+ console.error('the usage of signature loadRoutes({router, store, i18n}, routesConfig) is out of date, please use the new signature: loadRoutes(routesConfig).')
159
+ console.error('方法签名 loadRoutes({router, store, i18n}, routesConfig) 的用法已过时, 请使用新的方法签名 loadRoutes(routesConfig)。')
160
+ }
161
+ }
162
+ /** ************* 兼容 version < v0.6.1 *****************/
163
+
164
+ // 应用配置
165
+ const { router, store, i18n } = appOptions
166
+
167
+ // 刷新页面时,有些全局状态丢失,在此处从本地缓存拿出来赋值
168
+ if (JSON.stringify(Vue.$login.f) === '{}') {
169
+ const login = store.getters['account/login']
170
+ Object.assign(Vue.$login, login)
171
+ }
172
+ // 如果 routesConfig 有值,则更新到本地,否则从本地获取
173
+ if (routesConfig) {
174
+ store.commit('account/setRoutesConfig', routesConfig)
175
+ } else {
176
+ routesConfig = store.getters['account/routesConfig']
177
+ }
178
+ // 如果开启了异步路由,则加载异步路由配置
179
+ const asyncRoutes = store.state.setting.asyncRoutes
180
+ if (asyncRoutes) {
181
+ if (routesConfig && routesConfig.length > 0) {
182
+ const routes = parseRoutes(routesConfig, appRouterMap.routerMap)
183
+ // 设置路由首页
184
+ routes[0].redirect = store.state.setting.homePage
185
+ const finalRoutes = mergeRoutes(basicOptions.routes, routes)
186
+ formatRoutes(finalRoutes)
187
+ router.options = { ...router.options, routes: finalRoutes }
188
+ router.matcher = new Router({ ...router.options, routes: [] }).matcher
189
+ router.addRoutes(finalRoutes)
190
+ }
191
+ }
192
+ // 提取路由国际化数据
193
+ mergeI18nFromRoutes(i18n, router.options.routes)
194
+ // 初始化Admin后台菜单数据
195
+ const rootRoute = router.options.routes.find(item => item.path === '/')
196
+ const menuRoutes = rootRoute && rootRoute.children
197
+ if (menuRoutes) {
198
+ store.commit('setting/setMenuData', menuRoutes)
199
+ }
200
+ }
201
+
202
+ /**
203
+ * 合并路由
204
+ * @param target {Route[]}
205
+ * @param source {Route[]}
206
+ * @returns {Route[]}
207
+ */
208
+ function mergeRoutes (target, source) {
209
+ const routesMap = {}
210
+ // eslint-disable-next-line no-return-assign
211
+ target.forEach(item => routesMap[item.path] = item)
212
+ // eslint-disable-next-line no-return-assign
213
+ source.forEach(item => routesMap[item.path] = item)
214
+ return Object.values(routesMap)
215
+ }
216
+
217
+ /**
218
+ * 深度合并路由
219
+ * @param target {Route[]}
220
+ * @param source {Route[]}
221
+ * @returns {Route[]}
222
+ */
223
+ function deepMergeRoutes (target, source) {
224
+ // 映射路由数组
225
+ const mapRoutes = routes => {
226
+ const routesMap = {}
227
+ routes.forEach(item => {
228
+ routesMap[item.path] = {
229
+ ...item,
230
+ children: item.children ? mapRoutes(item.children) : undefined
231
+ }
232
+ })
233
+ return routesMap
234
+ }
235
+ const tarMap = mapRoutes(target)
236
+ const srcMap = mapRoutes(source)
237
+
238
+ // 合并路由
239
+ const merge = deepMerge(tarMap, srcMap)
240
+
241
+ // 转换为 routes 数组
242
+ const parseRoutesMap = routesMap => {
243
+ return Object.values(routesMap).map(item => {
244
+ if (item.children) {
245
+ item.children = parseRoutesMap(item.children)
246
+ } else {
247
+ delete item.children
248
+ }
249
+ return item
250
+ })
251
+ }
252
+ return parseRoutesMap(merge)
253
+ }
254
+
255
+ /**
256
+ * 格式化路由
257
+ * @param routes 路由配置
258
+ */
259
+ function formatRoutes (routes) {
260
+ routes.forEach(route => {
261
+ const { path } = route
262
+ if (!path.startsWith('/') && path !== '*') {
263
+ route.path = '/' + path
264
+ }
265
+ })
266
+ formatAuthority(routes)
267
+ }
268
+
269
+ /**
270
+ * 格式化路由的权限配置
271
+ * @param routes 路由
272
+ * @param pAuthorities 父级路由权限配置集合
273
+ */
274
+ function formatAuthority (routes, pAuthorities = []) {
275
+ routes.forEach(route => {
276
+ const meta = route.meta
277
+ const defaultAuthority = pAuthorities[pAuthorities.length - 1] || { permission: '*' }
278
+ if (meta) {
279
+ let authority = {}
280
+ if (!meta.authority) {
281
+ authority = defaultAuthority
282
+ } else if (typeof meta.authority === 'string' || Array.isArray(meta.authority)) {
283
+ authority.permission = meta.authority
284
+ } else if (typeof meta.authority === 'object') {
285
+ authority = meta.authority
286
+ const { role } = authority
287
+ if (typeof role === 'string') {
288
+ authority.role = [role]
289
+ }
290
+ if (!authority.permission && !authority.role) {
291
+ authority = defaultAuthority
292
+ }
293
+ }
294
+ meta.authority = authority
295
+ } else {
296
+ const authority = defaultAuthority
297
+ route.meta = { authority }
298
+ }
299
+ route.meta.pAuthorities = pAuthorities
300
+ if (route.children) {
301
+ formatAuthority(route.children, [...pAuthorities, route.meta.authority])
302
+ }
303
+ })
304
+ }
305
+
306
+ /**
307
+ * 从路由 path 解析 i18n key
308
+ * @param path
309
+ * @returns {*}
310
+ */
311
+ function getI18nKey (path) {
312
+ const keys = path.split('/').filter(item => !item.startsWith(':') && item !== '')
313
+ keys.push('name')
314
+ return keys.join('.')
315
+ }
316
+
317
+ /**
318
+ * 加载导航守卫
319
+ * @param guards
320
+ * @param options
321
+ */
322
+ function loadGuards (guards, options) {
323
+ const { beforeEach, afterEach } = guards
324
+ const { router } = options
325
+ beforeEach.forEach(guard => {
326
+ if (guard && typeof guard === 'function') {
327
+ router.beforeEach((to, from, next) => guard(to, from, next, options))
328
+ }
329
+ })
330
+ afterEach.forEach(guard => {
331
+ if (guard && typeof guard === 'function') {
332
+ router.afterEach((to, from) => guard(to, from, options))
333
+ }
334
+ })
335
+ }
336
+
337
+ /**
338
+ * 资源服务路由转新路由
339
+ * @param func
340
+ */
341
+ function funcToRouter (func) {
342
+ return [{
343
+ router: 'root',
344
+ children: positionRouter(parsefunc(func))
345
+ }]
346
+ }
347
+
348
+ function parsefunc (func) {
349
+ const tree = []
350
+ for (const row of func) {
351
+ const route = {
352
+ router: row.link,
353
+ meta: {
354
+ singlePage: row.navigate
355
+ },
356
+ position: row.position - 1,
357
+ icon: row.icon,
358
+ name: row.name
359
+ }
360
+ if (row.link) {
361
+ if (row.link.includes('$')) {
362
+ route.router = row.link.split('$')[1]
363
+ route.params = row.link.split('$')[0]
364
+ }
365
+ if (route.router.includes('?')) {
366
+ // 把问号后面的都删了
367
+ const [url, queryString] = route.router.split('?')
368
+ route.router = url
369
+ try {
370
+ const params = new URLSearchParams(queryString)
371
+ for (const [key, value] of params.entries()) {
372
+ route.meta[key] = value
373
+ }
374
+ if (route.meta.path) {
375
+ route.path = route.meta.path
376
+ delete route.meta.path
377
+ }
378
+ } catch (e) {}
379
+ }
380
+ }
381
+ if (row.children && row.children.length > 0) {
382
+ route.children = parsefunc(row.children)
383
+ }
384
+ tree.push(route)
385
+ }
386
+ // 如果是 microapp 作为子应用路由应该是平铺的 树状结构由父项目处理
387
+ if (window.__MICRO_APP_ENVIRONMENT__) {
388
+ return flattenTree(tree)
389
+ }
390
+ return tree
391
+ }
392
+
393
+ function flattenTree (tree) {
394
+ let flat = []
395
+ for (const node of tree) {
396
+ const { children, ...rest } = node
397
+ flat.push(rest)
398
+ if (children && children.length > 0) {
399
+ flat = flat.concat(flattenTree(children))
400
+ }
401
+ }
402
+ return flat
403
+ }
404
+
405
+ /**
406
+ * 资源服务路由排序
407
+ */
408
+ function positionRouter (r) {
409
+ let router = r.sort((a, b) => a.position - b.position)
410
+ router = arrRemoveEmpty(router)
411
+ return router
412
+ }
413
+
414
+ function funcToRouterOA (func) {
415
+ return [{
416
+ router: 'root',
417
+ children: positionRouterOA(parsefuncOA(func))
418
+ }]
419
+ }
420
+
421
+ function positionRouterOA (r) {
422
+ let router = []
423
+ for (const row of r) {
424
+ router[row.position] = row
425
+ }
426
+ router = arrRemoveEmpty(router)
427
+ return router
428
+ }
429
+
430
+ function parsefuncOA (func) {
431
+ const router = []
432
+ for (const row of func) {
433
+ const route = {
434
+ router: row.link,
435
+ meta: {
436
+ singlePage: row.navigate
437
+ },
438
+ position: row.position - 1,
439
+ icon: row.icon,
440
+ name: row.name
441
+ }
442
+ if (row.children && row.children.length > 0) {
443
+ route.children = parsefunc(row.children)
444
+ }
445
+ router.push(route)
446
+ }
447
+ return router
448
+ }
449
+
450
+ export { parseRoutes, loadRoutes, formatAuthority, getI18nKey, loadGuards, deepMergeRoutes, formatRoutes, setAppOptions, funcToRouter, setAppRouterMap, setBaseRouterMap, funcToRouterOA }