@lambo-design/shared 1.0.0-beta.353 → 1.0.0-beta.355

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lambo-design/shared",
3
- "version": "1.0.0-beta.353",
3
+ "version": "1.0.0-beta.355",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "lambo",
@@ -0,0 +1,301 @@
1
+ import config from "../../config/config"
2
+ import { deepCopy } from '../assist'
3
+ import { getUrlParams } from '../platform'
4
+
5
+ const ROUTE_PERMISSION_TYPES = new Set([1, 2, 4])
6
+
7
+ function isAppMatched(itemAppId, appIds) {
8
+ if (!appIds) {
9
+ return true
10
+ }
11
+ if (itemAppId === appIds) {
12
+ return true
13
+ }
14
+ if (Array.isArray(appIds)) {
15
+ return appIds.indexOf(itemAppId) > -1
16
+ }
17
+ if (typeof appIds === 'string') {
18
+ return appIds.indexOf(itemAppId) > -1
19
+ }
20
+ return false
21
+ }
22
+
23
+ function shouldIncludePermission(item, appIds, generateType) {
24
+ const appIdCondition = isAppMatched(item.appId, appIds)
25
+ if (generateType === 'menu') {
26
+ const hideInMenuCondition = !(Object.prototype.hasOwnProperty.call(item, 'hideInMenu') && item.hideInMenu)
27
+ return appIdCondition && hideInMenuCondition
28
+ }
29
+ if (generateType && generateType === 'router') {
30
+ return appIdCondition
31
+ }
32
+ return false
33
+ }
34
+
35
+ function buildChildrenMap(permissionList, appIds, generateType) {
36
+ const childrenMap = new Map()
37
+
38
+ permissionList.forEach((item) => {
39
+ if (!item || !ROUTE_PERMISSION_TYPES.has(item.type)) {
40
+ return
41
+ }
42
+ if (!shouldIncludePermission(item, appIds, generateType)) {
43
+ return
44
+ }
45
+
46
+ const pid = item.pid
47
+ if (!childrenMap.has(pid)) {
48
+ childrenMap.set(pid, [])
49
+ }
50
+ childrenMap.get(pid).push(item)
51
+ })
52
+
53
+ return childrenMap
54
+ }
55
+
56
+ function createCrumb(item) {
57
+ return {
58
+ icon: item.icon,
59
+ name: item.name,
60
+ title: item.label,
61
+ type: item.type
62
+ }
63
+ }
64
+
65
+ function buildMenuTree(childrenMap, parentId, crumbs, root, pageChildren) {
66
+ const siblings = childrenMap.get(parentId) || []
67
+ const menuTree = []
68
+
69
+ siblings.forEach((item) => {
70
+ if (item.type === 1 || item.type === 2) {
71
+ const node = {
72
+ meta: {
73
+ data: deepCopy(item),
74
+ appId: item.appId,
75
+ title: item.label,
76
+ icon: item.icon,
77
+ crumbs: [...crumbs],
78
+ activeName: item.name,
79
+ notCache: Boolean(item.notCache),
80
+ hideInMenu: Boolean(item.hideInMenu)
81
+ },
82
+ permissionId: item.permissionId,
83
+ type: item.type,
84
+ pid: item.pid,
85
+ component: item.name,
86
+ name: item.name,
87
+ uri: item.uri,
88
+ children: []
89
+ }
90
+
91
+ node.meta.crumbs.push(createCrumb(item))
92
+
93
+ if (item.type === 1) {
94
+ node.component = root ? 'Main' : 'parentView'
95
+ }
96
+
97
+ node.children = buildMenuTree(childrenMap, item.permissionId, node.meta.crumbs, false, pageChildren)
98
+ if (!node.children.length) {
99
+ delete node.children
100
+ }
101
+
102
+ menuTree.push(node)
103
+ return
104
+ }
105
+
106
+ if (item.type === 4) {
107
+ const child = {
108
+ meta: {
109
+ data: deepCopy(item),
110
+ appId: item.appId,
111
+ title: item.label,
112
+ hideInMenu: true,
113
+ notCache: true,
114
+ crumbs: [...crumbs],
115
+ type: item.type
116
+ },
117
+ permissionId: item.permissionId,
118
+ type: item.type,
119
+ pid: item.pid,
120
+ component: item.name,
121
+ name: item.name,
122
+ uri: item.uri
123
+ }
124
+
125
+ child.meta.crumbs.push(createCrumb(item))
126
+ pageChildren.push(child)
127
+ }
128
+ })
129
+
130
+ return menuTree
131
+ }
132
+
133
+ function generatorMenuRouter(permissionList, appIds, generateType = 'router') {
134
+ const pageNode = {
135
+ path: '/page',
136
+ name: 'page',
137
+ meta: {
138
+ hideInMenu: true,
139
+ notCache: true
140
+ },
141
+ component: 'Main',
142
+ children: []
143
+ }
144
+
145
+ const childrenMap = buildChildrenMap(permissionList || [], appIds, generateType)
146
+ const menuData = buildMenuTree(childrenMap, '0', [], true, pageNode.children)
147
+ return { menuData, pageNode }
148
+ }
149
+
150
+ function generator(menuData, constantRouterComponents) {
151
+ for (const item of menuData) {
152
+ if (item.component && typeof item.component === 'string') {
153
+ const target = constantRouterComponents[item.component]
154
+ if (target) {
155
+ if (target.component) {
156
+ if (typeof target.component === 'string') {
157
+ try {
158
+ item.component = resolve =>
159
+ require(['@/view/' + target.component], data => {
160
+ data.default.name = item.name
161
+ resolve(data)
162
+ })
163
+ } catch (e) {
164
+ delete item.component
165
+ }
166
+ } else {
167
+ item.component = target.component
168
+ }
169
+ }
170
+ if (target.url) {
171
+ let path = target.url
172
+ let query = {}
173
+ if (path.indexOf('?') > -1) {
174
+ query = getUrlParams(path.substr(path.indexOf('?')))
175
+ item.query = query
176
+ path = path.substr(0, path.indexOf('?'))
177
+ }
178
+ item.path = path
179
+ }
180
+ if (item.uri) {
181
+ item.meta.uriParam = item.uri
182
+ }
183
+ if (target.notCache) {
184
+ item.meta.notCache = target.notCache
185
+ }
186
+ if (target.hideInMenu) {
187
+ item.meta.hideInMenu = target.hideInMenu
188
+ }
189
+ }
190
+ if (item.uri) {
191
+ if (item.uri.startsWith('/')) {
192
+ item.uri = item.uri.substr(1)
193
+ }
194
+ let uri = item.uri
195
+ if (uri.startsWith(config.routerBase + '/')) {
196
+ uri = uri.replace(config.routerBase + '/', '')
197
+ }
198
+ if (uri.startsWith('dida/')) {
199
+ item.meta.notCache = true
200
+ let path = uri
201
+ let query = {}
202
+ if (path.indexOf('?') > -1) {
203
+ query = getUrlParams(path.substr(path.indexOf('?')))
204
+ item.query = query
205
+ path = path.substr(0, path.indexOf('?'))
206
+ }
207
+ try {
208
+ item.component = resolve => {
209
+ require(['@/view/dida/dida-page'], data => {
210
+ data.default.name = item.name
211
+ resolve(data)
212
+ })
213
+ }
214
+ } catch (e) {
215
+ delete item.component
216
+ }
217
+ item.path = '/' + path
218
+ } else if (uri.startsWith('dareport/')) {
219
+ item.meta.notCache = true
220
+ let path = uri
221
+ let query = {}
222
+ if (path.indexOf('?') > -1) {
223
+ query = getUrlParams(path.substr(path.indexOf('?')))
224
+ item.query = query
225
+ path = path.substr(0, path.indexOf('?'))
226
+ }
227
+ try {
228
+ item.component = resolve => {
229
+ require(['@/view/dida/dida-page'], data => {
230
+ data.default.name = item.name
231
+ resolve(data)
232
+ })
233
+ }
234
+ } catch (e) {
235
+ delete item.component
236
+ }
237
+ item.path = '/' + path
238
+ } else {
239
+ try {
240
+ let path = item.uri
241
+ if (item.uri.startsWith(config.routerBase + '/')) {
242
+ path = path.replace(config.routerBase + '/', '')
243
+ }
244
+ let query = {}
245
+ if (path.indexOf('?') > -1) {
246
+ query = getUrlParams(path.substr(path.indexOf('?')))
247
+ path = path.substr(0, path.indexOf('?'))
248
+ }
249
+ if (target && target.component) {
250
+ // target.component already overrides uri-based component resolution.
251
+ } else {
252
+ const component = require('@/view/' + path)
253
+ if (component) {
254
+ item.component = resolve => {
255
+ require(['@/view/' + path], data => {
256
+ data.default.name = item.name
257
+ resolve(data)
258
+ })
259
+ }
260
+ } else {
261
+ delete item.component
262
+ }
263
+ }
264
+ if (target && target.url) {
265
+ // target.url already overrides uri-based path/query resolution.
266
+ } else {
267
+ item.path = '/' + path
268
+ item.query = query
269
+ }
270
+ } catch (e) {
271
+ delete item.component
272
+ }
273
+ }
274
+ }
275
+ if (item.path == null) {
276
+ item.path = ''
277
+ }
278
+ if (item.component && typeof item.component === 'string') {
279
+ delete item.component
280
+ }
281
+ if (item.children && item.children.length > 0) {
282
+ generator(item.children, constantRouterComponents)
283
+ }
284
+ }
285
+ }
286
+ return menuData
287
+ }
288
+
289
+ export function generatorDynamicRouter(permissionList, constantRouterComponents, appIds = null, generateType = 'router') {
290
+ const allMenu = generatorMenuRouter(permissionList, appIds, generateType)
291
+ const menuData = allMenu.menuData
292
+ const pageNode = allMenu.pageNode
293
+
294
+ if (menuData && pageNode && pageNode.children.length > 0) {
295
+ menuData.push(pageNode)
296
+ }
297
+
298
+ return generator(menuData, constantRouterComponents)
299
+ }
300
+
301
+ export default generatorDynamicRouter
@@ -1,6 +1,7 @@
1
1
  import config from "../../config/config"
2
2
  import {deepCopy} from "../assist";
3
3
  import {getLocalStorage, getUrlParams} from "../platform"
4
+ import generatorDynamicRouterNew from "./generatorDynamicRouter";
4
5
  /**
5
6
  * 判断是否存在路由
6
7
  * @param $router
@@ -45,7 +46,7 @@ export const hasRoutePath = ($router, path) => {
45
46
  * @returns []
46
47
  */
47
48
  export const generatorMenuList = (permissionList,appId,generateType = 'router') => {
48
- let menuRouter = generatorMenuRouter(permissionList,appId,generateType);
49
+ let menuRouter = generatorDynamicRouterNew(permissionList,appId,generateType);
49
50
  return menuRouter && menuRouter.menuData ? menuRouter.menuData : [];
50
51
  };
51
52