@lambo-design/shared 1.0.0-beta.114 → 1.0.0-beta.116

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/utils/menu/index.js +142 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lambo-design/shared",
3
- "version": "1.0.0-beta.114",
3
+ "version": "1.0.0-beta.116",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "lambo",
@@ -39,6 +39,15 @@ export const hasRoutePath = ($router, path) => {
39
39
  * @returns []
40
40
  */
41
41
  export const generatorMenuList = (permissionList,appId) => {
42
+ let menuRouter = generatorMenuRouter(permissionList,appId);
43
+ return menuRouter && menuRouter.menuData ? menuRouter.menuData : [];
44
+ };
45
+ /**
46
+ * 动态生成菜单
47
+ * @param generatorMenuRouter
48
+ * @returns []
49
+ */
50
+ export const generatorMenuRouter = (permissionList,appId) => {
42
51
  let menuData = [],
43
52
  pageNode = {
44
53
  path: "/page",
@@ -51,15 +60,10 @@ export const generatorMenuList = (permissionList,appId) => {
51
60
  children: []
52
61
  };
53
62
  listToMenuTree(permissionList, menuData, pageNode, "0", [], true,appId);
54
-
55
- /*if (pageNode.children.length > 0) {
56
- menuData.push(pageNode);
57
- }*/
58
- return menuData;
63
+ return {menuData,pageNode};
59
64
  };
60
65
 
61
66
 
62
-
63
67
  /**
64
68
  * 数组转树形结构
65
69
  */
@@ -68,7 +72,7 @@ const listToMenuTree = (list, menuTree, pageNode, parentId, crumbs, root, appId)
68
72
  return;
69
73
  }
70
74
  list.forEach(item => {
71
- if (item.appId == appId && (!item.hasOwnProperty("hideInMenu") || (item.hasOwnProperty("hideInMenu") && !item.hideInMenu))) {
75
+ if (!appId || item.appId == appId && (!item.hasOwnProperty("hideInMenu") || (item.hasOwnProperty("hideInMenu") && !item.hideInMenu))) {
72
76
  // 判断是否为父级菜单
73
77
  if (item.pid === parentId) {
74
78
  if (item.type === 1 || item.type === 2) {
@@ -83,6 +87,7 @@ const listToMenuTree = (list, menuTree, pageNode, parentId, crumbs, root, appId)
83
87
  notCache: item.notCache ? true : false,
84
88
  hideInMenu: item.hideInMenu ? true : false
85
89
  },
90
+ permissionId: item.permissionId,
86
91
  type: item.type,
87
92
  pid: item.pid,
88
93
  component: item.name,
@@ -132,6 +137,7 @@ const listToMenuTree = (list, menuTree, pageNode, parentId, crumbs, root, appId)
132
137
  crumbs: [...crumbs],
133
138
  type: item.type
134
139
  },
140
+ permissionId: item.permissionId,
135
141
  type: item.type,
136
142
  pid: item.pid,
137
143
  component: item.name,
@@ -150,4 +156,133 @@ const listToMenuTree = (list, menuTree, pageNode, parentId, crumbs, root, appId)
150
156
  }
151
157
  });
152
158
  };
159
+
160
+ /**
161
+ * 动态生成路由
162
+ * @param generatorMenuRouter
163
+ * @returns []
164
+ */
165
+ export const generatorDynamicRouter = (permissionList,exampleMenuData,constantRouterComponents) => {
166
+ let allMenu = generatorMenuRouter(permissionList, null);
167
+ let menuData = allMenu.menuData;
168
+ let pageNode = allMenu.pageNode;
169
+ if (menuData && pageNode && pageNode.children.length > 0) {
170
+ menuData.push(pageNode);
171
+ }
172
+ //给路由添加面包屑属性,解决示例页面左侧菜单点击不选中的问题
173
+ routesAddCrumbs(exampleMenuData,[]);
174
+ menuData = menuData.concat(exampleMenuData)
175
+ let dynamicRouter = generator(menuData,constantRouterComponents);
176
+
177
+ return dynamicRouter;
178
+ }
179
+ /**
180
+ * 路由添加面包屑属性
181
+ */
182
+ export const routesAddCrumbs = (routes ,crumbs) => {
183
+ routes.forEach(item => {
184
+ if (item.meta.hasOwnProperty('crumbs')){
185
+ item.meta.crumbs.push({
186
+ icon: item.meta.icon,
187
+ name: item.name,
188
+ title: item.meta.title,
189
+ type: item.type
190
+ });
191
+ }else{
192
+ item.meta['crumbs'] = [...crumbs];
193
+ item.meta.crumbs.push({
194
+ icon: item.meta.icon,
195
+ name: item.name,
196
+ title: item.meta.title,
197
+ type: item.type
198
+ });
199
+ }
200
+ if (item.hasOwnProperty("children")){
201
+ routesAddCrumbs(item.children,item.meta.crumbs)
202
+ }
203
+ });
204
+ }
205
+
206
+ /*
207
+ 生成 vue-router 层级路由表
208
+ */
209
+ const generator = (menuData,constantRouterComponents) => {
210
+ for (let item of menuData) {
211
+ if (item.component && typeof item.component == "string") {
212
+ const target = constantRouterComponents[item.component];
213
+ if (target) {
214
+ if (typeof target.component == "string") {
215
+ // 解决路由name与组件name不一致导致的keeplive不生效的问题
216
+ item.component = resolve =>
217
+ require([`@/view/${target.component}`], data => {
218
+ data.default.name = item.name;
219
+ resolve(data);
220
+ });
221
+ } else {
222
+ item.component = target.component;
223
+ }
224
+ if (target.url) {
225
+ item.path = target.url;
226
+ }
227
+ if (item.uri) {
228
+ item.meta.uriParam = item.uri;
229
+ }
230
+ if (target.notCache) {
231
+ item.meta.notCache = target.notCache;
232
+ }
233
+ if (target.hideInMenu) {
234
+ item.meta.hideInMenu = target.hideInMenu;
235
+ }
236
+ } else if (item.uri) {
237
+ if (item.uri.startsWith("/")) {
238
+ item.uri = item.uri.substr(1);
239
+ }
240
+ if (item.uri.startsWith("dida/")) {
241
+ item.meta.notCache = true;
242
+ item.component = resolve => {
243
+ require([`@/view/dida/dida-page`], data => {
244
+ data.default.name = item.name;
245
+ resolve(data);
246
+ });
247
+ };
248
+ item.path = "/" + item.uri;
249
+ } else {
250
+ try{
251
+ let path = item.uri;
252
+ if (item.uri.startsWith(config.routerBase + "/")) {
253
+ path = path.replace(config.routerBase + "/","")
254
+ }
255
+ let component = require(`@/view/${path}`);
256
+ if (component) {
257
+ item.component = resolve => {
258
+ require([`@/view/${path}`], data => {
259
+ data.default.name = item.name;
260
+ resolve(data);
261
+ });
262
+ };
263
+ item.path = "/" + path;
264
+ } else {
265
+ //console.warn(`${item.name}(${item.uri})页面不存在`)
266
+ delete item.component;
267
+ }
268
+ }catch (e){
269
+ //console.warn(`${item.name}(${item.uri})页面不存在`)
270
+ delete item.component;
271
+ }
272
+ }
273
+ } else {
274
+ //console.warn(`${item.name}的资源路径未配置`);
275
+ delete item.component;
276
+ }
277
+ if (item.path == null) {
278
+ item.path = "";
279
+ }
280
+ if (item.children && item.children.length > 0) {
281
+ generator(item.children);
282
+ }
283
+ }
284
+ }
285
+ return menuData;
286
+ };
287
+
153
288
  export default generatorMenuList;