vue2-client 1.6.34 → 1.6.35

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,366 +1,366 @@
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
- }
75
- // 查看是否是单页面
76
- if (item.meta && item.meta.singlePage) {
77
- router = routerMap['singlePage']
78
- item.path = encodeURI(item.name)
79
- }
80
- // 当没在动态路由对象中找到时, 不添加到路由
81
- if (!router) return
82
- routeCfg = item
83
- }
84
- if (!router) {
85
- console.warn(`can't find register for router ${routeCfg.router}, please register it in advance.`)
86
- router = typeof item === 'string' ? { path: item, name: item } : item
87
- }
88
- // 从 router 和 routeCfg 解析路由
89
- const meta = {
90
- authority: router.authority,
91
- icon: router.icon,
92
- page: router.page,
93
- link: router.link,
94
- params: router.params,
95
- query: router.query,
96
- ...router.meta
97
- }
98
- const cfgMeta = {
99
- authority: routeCfg.authority,
100
- icon: routeCfg.icon,
101
- page: routeCfg.page,
102
- link: routeCfg.link,
103
- params: routeCfg.params,
104
- query: routeCfg.query,
105
- ...routeCfg.meta
106
- }
107
- Object.keys(cfgMeta).forEach(key => {
108
- if (cfgMeta[key] === undefined || cfgMeta[key] === null || cfgMeta[key] === '') {
109
- delete cfgMeta[key]
110
- }
111
- })
112
- Object.assign(meta, cfgMeta)
113
- const route = {
114
- path: routeCfg.path || router.path || routeCfg.router,
115
- name: routeCfg.name || router.name,
116
- component: router.component || router,
117
- redirect: routeCfg.redirect || router.redirect,
118
- meta: { ...meta, authority: meta.authority || '*' }
119
- }
120
- if (routeCfg.invisible || router.invisible) {
121
- route.meta.invisible = true
122
- }
123
- if (routeCfg.children && routeCfg.children.length > 0) {
124
- route.children = parseRoutes(routeCfg.children, routerMap)
125
- }
126
- // 当没有子并且自己时blank(空界面)时, 不添加到路由
127
- if (route.component.name === 'blank' && route.children && route.children.length <= 0) return
128
- routes.push(route)
129
- })
130
- return routes
131
- }
132
-
133
- /**
134
- * 加载路由
135
- * @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}]}]} 路由配置
136
- */
137
- function loadRoutes (routesConfig) {
138
- // 兼容 0.6.1 以下版本
139
- /** ************* 兼容 version < v0.6.1 *****************/
140
- if (arguments.length > 0) {
141
- const arg0 = arguments[0]
142
- if (arg0.router || arg0.i18n || arg0.store) {
143
- routesConfig = arguments[1]
144
- console.error('the usage of signature loadRoutes({router, store, i18n}, routesConfig) is out of date, please use the new signature: loadRoutes(routesConfig).')
145
- console.error('方法签名 loadRoutes({router, store, i18n}, routesConfig) 的用法已过时, 请使用新的方法签名 loadRoutes(routesConfig)。')
146
- }
147
- }
148
- /** ************* 兼容 version < v0.6.1 *****************/
149
-
150
- // 应用配置
151
- const { router, store, i18n } = appOptions
152
-
153
- // 刷新页面时,有些全局状态丢失,在此处从本地缓存拿出来赋值
154
- if (JSON.stringify(Vue.$login.f) === '{}') {
155
- const login = store.getters['account/login']
156
- Object.assign(Vue.$login, login)
157
- }
158
- // 如果 routesConfig 有值,则更新到本地,否则从本地获取
159
- if (routesConfig) {
160
- store.commit('account/setRoutesConfig', routesConfig)
161
- } else {
162
- routesConfig = store.getters['account/routesConfig']
163
- }
164
- // 如果开启了异步路由,则加载异步路由配置
165
- const asyncRoutes = store.state.setting.asyncRoutes
166
- if (asyncRoutes) {
167
- if (routesConfig && routesConfig.length > 0) {
168
- const routes = parseRoutes(routesConfig, appRouterMap.routerMap)
169
- // 设置路由首页
170
- routes[0].redirect = store.state.setting.homePage
171
- const finalRoutes = mergeRoutes(basicOptions.routes, routes)
172
- formatRoutes(finalRoutes)
173
- router.options = { ...router.options, routes: finalRoutes }
174
- router.matcher = new Router({ ...router.options, routes: [] }).matcher
175
- router.addRoutes(finalRoutes)
176
- }
177
- }
178
- // 提取路由国际化数据
179
- mergeI18nFromRoutes(i18n, router.options.routes)
180
- // 初始化Admin后台菜单数据
181
- const rootRoute = router.options.routes.find(item => item.path === '/')
182
- const menuRoutes = rootRoute && rootRoute.children
183
- if (menuRoutes) {
184
- store.commit('setting/setMenuData', menuRoutes)
185
- }
186
- }
187
-
188
- /**
189
- * 合并路由
190
- * @param target {Route[]}
191
- * @param source {Route[]}
192
- * @returns {Route[]}
193
- */
194
- function mergeRoutes (target, source) {
195
- const routesMap = {}
196
- // eslint-disable-next-line no-return-assign
197
- target.forEach(item => routesMap[item.path] = item)
198
- // eslint-disable-next-line no-return-assign
199
- source.forEach(item => routesMap[item.path] = item)
200
- return Object.values(routesMap)
201
- }
202
-
203
- /**
204
- * 深度合并路由
205
- * @param target {Route[]}
206
- * @param source {Route[]}
207
- * @returns {Route[]}
208
- */
209
- function deepMergeRoutes (target, source) {
210
- // 映射路由数组
211
- const mapRoutes = routes => {
212
- const routesMap = {}
213
- routes.forEach(item => {
214
- routesMap[item.path] = {
215
- ...item,
216
- children: item.children ? mapRoutes(item.children) : undefined
217
- }
218
- })
219
- return routesMap
220
- }
221
- const tarMap = mapRoutes(target)
222
- const srcMap = mapRoutes(source)
223
-
224
- // 合并路由
225
- const merge = deepMerge(tarMap, srcMap)
226
-
227
- // 转换为 routes 数组
228
- const parseRoutesMap = routesMap => {
229
- return Object.values(routesMap).map(item => {
230
- if (item.children) {
231
- item.children = parseRoutesMap(item.children)
232
- } else {
233
- delete item.children
234
- }
235
- return item
236
- })
237
- }
238
- return parseRoutesMap(merge)
239
- }
240
-
241
- /**
242
- * 格式化路由
243
- * @param routes 路由配置
244
- */
245
- function formatRoutes (routes) {
246
- routes.forEach(route => {
247
- const { path } = route
248
- if (!path.startsWith('/') && path !== '*') {
249
- route.path = '/' + path
250
- }
251
- })
252
- formatAuthority(routes)
253
- }
254
-
255
- /**
256
- * 格式化路由的权限配置
257
- * @param routes 路由
258
- * @param pAuthorities 父级路由权限配置集合
259
- */
260
- function formatAuthority (routes, pAuthorities = []) {
261
- routes.forEach(route => {
262
- const meta = route.meta
263
- const defaultAuthority = pAuthorities[pAuthorities.length - 1] || { permission: '*' }
264
- if (meta) {
265
- let authority = {}
266
- if (!meta.authority) {
267
- authority = defaultAuthority
268
- } else if (typeof meta.authority === 'string') {
269
- authority.permission = meta.authority
270
- } else if (typeof meta.authority === 'object') {
271
- authority = meta.authority
272
- const { role } = authority
273
- if (typeof role === 'string') {
274
- authority.role = [role]
275
- }
276
- if (!authority.permission && !authority.role) {
277
- authority = defaultAuthority
278
- }
279
- }
280
- meta.authority = authority
281
- } else {
282
- const authority = defaultAuthority
283
- route.meta = { authority }
284
- }
285
- route.meta.pAuthorities = pAuthorities
286
- if (route.children) {
287
- formatAuthority(route.children, [...pAuthorities, route.meta.authority])
288
- }
289
- })
290
- }
291
-
292
- /**
293
- * 从路由 path 解析 i18n key
294
- * @param path
295
- * @returns {*}
296
- */
297
- function getI18nKey (path) {
298
- const keys = path.split('/').filter(item => !item.startsWith(':') && item !== '')
299
- keys.push('name')
300
- return keys.join('.')
301
- }
302
-
303
- /**
304
- * 加载导航守卫
305
- * @param guards
306
- * @param options
307
- */
308
- function loadGuards (guards, options) {
309
- const { beforeEach, afterEach } = guards
310
- const { router } = options
311
- beforeEach.forEach(guard => {
312
- if (guard && typeof guard === 'function') {
313
- router.beforeEach((to, from, next) => guard(to, from, next, options))
314
- }
315
- })
316
- afterEach.forEach(guard => {
317
- if (guard && typeof guard === 'function') {
318
- router.afterEach((to, from) => guard(to, from, options))
319
- }
320
- })
321
- }
322
-
323
- /**
324
- * 资源服务路由转新路由
325
- * @param func
326
- */
327
- function funcToRouter (func) {
328
- return [{
329
- router: 'root',
330
- children: positionRouter(parsefunc(func))
331
- }]
332
- }
333
-
334
- function parsefunc (func) {
335
- const router = []
336
- for (const row of func) {
337
- const route = {
338
- router: row.link,
339
- meta: {
340
- singlePage: row.navigate
341
- },
342
- position: row.position - 1,
343
- icon: row.icon,
344
- name: row.name
345
- }
346
- if (row.children && row.children.length > 0) {
347
- route.children = parsefunc(row.children)
348
- }
349
- router.push(route)
350
- }
351
- return router
352
- }
353
-
354
- /**
355
- * 资源服务路由排序
356
- */
357
- function positionRouter (r) {
358
- let router = []
359
- for (const row of r) {
360
- router[row.position] = row
361
- }
362
- router = arrRemoveEmpty(router)
363
- return router
364
- }
365
-
366
- export { parseRoutes, loadRoutes, formatAuthority, getI18nKey, loadGuards, deepMergeRoutes, formatRoutes, setAppOptions, funcToRouter, setAppRouterMap, setBaseRouterMap }
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
+ }
75
+ // 查看是否是单页面
76
+ if (item.meta && item.meta.singlePage) {
77
+ router = routerMap['singlePage']
78
+ item.path = encodeURI(item.name)
79
+ }
80
+ // 当没在动态路由对象中找到时, 不添加到路由
81
+ if (!router) return
82
+ routeCfg = item
83
+ }
84
+ if (!router) {
85
+ console.warn(`can't find register for router ${routeCfg.router}, please register it in advance.`)
86
+ router = typeof item === 'string' ? { path: item, name: item } : item
87
+ }
88
+ // 从 router 和 routeCfg 解析路由
89
+ const meta = {
90
+ authority: router.authority,
91
+ icon: router.icon,
92
+ page: router.page,
93
+ link: router.link,
94
+ params: router.params,
95
+ query: router.query,
96
+ ...router.meta
97
+ }
98
+ const cfgMeta = {
99
+ authority: routeCfg.authority,
100
+ icon: routeCfg.icon,
101
+ page: routeCfg.page,
102
+ link: routeCfg.link,
103
+ params: routeCfg.params,
104
+ query: routeCfg.query,
105
+ ...routeCfg.meta
106
+ }
107
+ Object.keys(cfgMeta).forEach(key => {
108
+ if (cfgMeta[key] === undefined || cfgMeta[key] === null || cfgMeta[key] === '') {
109
+ delete cfgMeta[key]
110
+ }
111
+ })
112
+ Object.assign(meta, cfgMeta)
113
+ const route = {
114
+ path: routeCfg.path || router.path || routeCfg.router,
115
+ name: routeCfg.name || router.name,
116
+ component: router.component || router,
117
+ redirect: routeCfg.redirect || router.redirect,
118
+ meta: { ...meta, authority: meta.authority || '*' }
119
+ }
120
+ if (routeCfg.invisible || router.invisible) {
121
+ route.meta.invisible = true
122
+ }
123
+ if (routeCfg.children && routeCfg.children.length > 0) {
124
+ route.children = parseRoutes(routeCfg.children, routerMap)
125
+ }
126
+ // 当没有子并且自己时blank(空界面)时, 不添加到路由
127
+ if (route.component.name === 'blank' && route.children && route.children.length <= 0) return
128
+ routes.push(route)
129
+ })
130
+ return routes
131
+ }
132
+
133
+ /**
134
+ * 加载路由
135
+ * @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}]}]} 路由配置
136
+ */
137
+ function loadRoutes (routesConfig) {
138
+ // 兼容 0.6.1 以下版本
139
+ /** ************* 兼容 version < v0.6.1 *****************/
140
+ if (arguments.length > 0) {
141
+ const arg0 = arguments[0]
142
+ if (arg0.router || arg0.i18n || arg0.store) {
143
+ routesConfig = arguments[1]
144
+ console.error('the usage of signature loadRoutes({router, store, i18n}, routesConfig) is out of date, please use the new signature: loadRoutes(routesConfig).')
145
+ console.error('方法签名 loadRoutes({router, store, i18n}, routesConfig) 的用法已过时, 请使用新的方法签名 loadRoutes(routesConfig)。')
146
+ }
147
+ }
148
+ /** ************* 兼容 version < v0.6.1 *****************/
149
+
150
+ // 应用配置
151
+ const { router, store, i18n } = appOptions
152
+
153
+ // 刷新页面时,有些全局状态丢失,在此处从本地缓存拿出来赋值
154
+ if (JSON.stringify(Vue.$login.f) === '{}') {
155
+ const login = store.getters['account/login']
156
+ Object.assign(Vue.$login, login)
157
+ }
158
+ // 如果 routesConfig 有值,则更新到本地,否则从本地获取
159
+ if (routesConfig) {
160
+ store.commit('account/setRoutesConfig', routesConfig)
161
+ } else {
162
+ routesConfig = store.getters['account/routesConfig']
163
+ }
164
+ // 如果开启了异步路由,则加载异步路由配置
165
+ const asyncRoutes = store.state.setting.asyncRoutes
166
+ if (asyncRoutes) {
167
+ if (routesConfig && routesConfig.length > 0) {
168
+ const routes = parseRoutes(routesConfig, appRouterMap.routerMap)
169
+ // 设置路由首页
170
+ routes[0].redirect = store.state.setting.homePage
171
+ const finalRoutes = mergeRoutes(basicOptions.routes, routes)
172
+ formatRoutes(finalRoutes)
173
+ router.options = { ...router.options, routes: finalRoutes }
174
+ router.matcher = new Router({ ...router.options, routes: [] }).matcher
175
+ router.addRoutes(finalRoutes)
176
+ }
177
+ }
178
+ // 提取路由国际化数据
179
+ mergeI18nFromRoutes(i18n, router.options.routes)
180
+ // 初始化Admin后台菜单数据
181
+ const rootRoute = router.options.routes.find(item => item.path === '/')
182
+ const menuRoutes = rootRoute && rootRoute.children
183
+ if (menuRoutes) {
184
+ store.commit('setting/setMenuData', menuRoutes)
185
+ }
186
+ }
187
+
188
+ /**
189
+ * 合并路由
190
+ * @param target {Route[]}
191
+ * @param source {Route[]}
192
+ * @returns {Route[]}
193
+ */
194
+ function mergeRoutes (target, source) {
195
+ const routesMap = {}
196
+ // eslint-disable-next-line no-return-assign
197
+ target.forEach(item => routesMap[item.path] = item)
198
+ // eslint-disable-next-line no-return-assign
199
+ source.forEach(item => routesMap[item.path] = item)
200
+ return Object.values(routesMap)
201
+ }
202
+
203
+ /**
204
+ * 深度合并路由
205
+ * @param target {Route[]}
206
+ * @param source {Route[]}
207
+ * @returns {Route[]}
208
+ */
209
+ function deepMergeRoutes (target, source) {
210
+ // 映射路由数组
211
+ const mapRoutes = routes => {
212
+ const routesMap = {}
213
+ routes.forEach(item => {
214
+ routesMap[item.path] = {
215
+ ...item,
216
+ children: item.children ? mapRoutes(item.children) : undefined
217
+ }
218
+ })
219
+ return routesMap
220
+ }
221
+ const tarMap = mapRoutes(target)
222
+ const srcMap = mapRoutes(source)
223
+
224
+ // 合并路由
225
+ const merge = deepMerge(tarMap, srcMap)
226
+
227
+ // 转换为 routes 数组
228
+ const parseRoutesMap = routesMap => {
229
+ return Object.values(routesMap).map(item => {
230
+ if (item.children) {
231
+ item.children = parseRoutesMap(item.children)
232
+ } else {
233
+ delete item.children
234
+ }
235
+ return item
236
+ })
237
+ }
238
+ return parseRoutesMap(merge)
239
+ }
240
+
241
+ /**
242
+ * 格式化路由
243
+ * @param routes 路由配置
244
+ */
245
+ function formatRoutes (routes) {
246
+ routes.forEach(route => {
247
+ const { path } = route
248
+ if (!path.startsWith('/') && path !== '*') {
249
+ route.path = '/' + path
250
+ }
251
+ })
252
+ formatAuthority(routes)
253
+ }
254
+
255
+ /**
256
+ * 格式化路由的权限配置
257
+ * @param routes 路由
258
+ * @param pAuthorities 父级路由权限配置集合
259
+ */
260
+ function formatAuthority (routes, pAuthorities = []) {
261
+ routes.forEach(route => {
262
+ const meta = route.meta
263
+ const defaultAuthority = pAuthorities[pAuthorities.length - 1] || { permission: '*' }
264
+ if (meta) {
265
+ let authority = {}
266
+ if (!meta.authority) {
267
+ authority = defaultAuthority
268
+ } else if (typeof meta.authority === 'string') {
269
+ authority.permission = meta.authority
270
+ } else if (typeof meta.authority === 'object') {
271
+ authority = meta.authority
272
+ const { role } = authority
273
+ if (typeof role === 'string') {
274
+ authority.role = [role]
275
+ }
276
+ if (!authority.permission && !authority.role) {
277
+ authority = defaultAuthority
278
+ }
279
+ }
280
+ meta.authority = authority
281
+ } else {
282
+ const authority = defaultAuthority
283
+ route.meta = { authority }
284
+ }
285
+ route.meta.pAuthorities = pAuthorities
286
+ if (route.children) {
287
+ formatAuthority(route.children, [...pAuthorities, route.meta.authority])
288
+ }
289
+ })
290
+ }
291
+
292
+ /**
293
+ * 从路由 path 解析 i18n key
294
+ * @param path
295
+ * @returns {*}
296
+ */
297
+ function getI18nKey (path) {
298
+ const keys = path.split('/').filter(item => !item.startsWith(':') && item !== '')
299
+ keys.push('name')
300
+ return keys.join('.')
301
+ }
302
+
303
+ /**
304
+ * 加载导航守卫
305
+ * @param guards
306
+ * @param options
307
+ */
308
+ function loadGuards (guards, options) {
309
+ const { beforeEach, afterEach } = guards
310
+ const { router } = options
311
+ beforeEach.forEach(guard => {
312
+ if (guard && typeof guard === 'function') {
313
+ router.beforeEach((to, from, next) => guard(to, from, next, options))
314
+ }
315
+ })
316
+ afterEach.forEach(guard => {
317
+ if (guard && typeof guard === 'function') {
318
+ router.afterEach((to, from) => guard(to, from, options))
319
+ }
320
+ })
321
+ }
322
+
323
+ /**
324
+ * 资源服务路由转新路由
325
+ * @param func
326
+ */
327
+ function funcToRouter (func) {
328
+ return [{
329
+ router: 'root',
330
+ children: positionRouter(parsefunc(func))
331
+ }]
332
+ }
333
+
334
+ function parsefunc (func) {
335
+ const router = []
336
+ for (const row of func) {
337
+ const route = {
338
+ router: row.link,
339
+ meta: {
340
+ singlePage: row.navigate
341
+ },
342
+ position: row.position - 1,
343
+ icon: row.icon,
344
+ name: row.name
345
+ }
346
+ if (row.children && row.children.length > 0) {
347
+ route.children = parsefunc(row.children)
348
+ }
349
+ router.push(route)
350
+ }
351
+ return router
352
+ }
353
+
354
+ /**
355
+ * 资源服务路由排序
356
+ */
357
+ function positionRouter (r) {
358
+ let router = []
359
+ for (const row of r) {
360
+ router[row.position] = row
361
+ }
362
+ router = arrRemoveEmpty(router)
363
+ return router
364
+ }
365
+
366
+ export { parseRoutes, loadRoutes, formatAuthority, getI18nKey, loadGuards, deepMergeRoutes, formatRoutes, setAppOptions, funcToRouter, setAppRouterMap, setBaseRouterMap }