zhaoke 2 年之前
父節點
當前提交
54019cfde9
共有 2 個文件被更改,包括 104 次插入82 次删除
  1. 93 82
      src/getMenu.ts
  2. 11 0
      src/utils/validate.ts

+ 93 - 82
src/getMenu.ts

@@ -42,6 +42,7 @@ router.beforeEach(async (to: any, from, next: any) => {
             const treeMenu = setTree(treeData, 'up_auth_id', 'auth_id')
             const dataMenu = _.unionBy(treeMenu, 'auth_id')
             const menus = parseMenu(dataMenu)
+            console.log(menus)
             // const { roles }: any = await userStore.getInfo()
             // accessRoutes = await permissionStore.generateRoutes(roles)
             accessRoutes = menus
@@ -78,116 +79,126 @@ router.beforeEach(async (to: any, from, next: any) => {
   }
 })
 
-function parseMenu(arr: Array<any>) {
+function parseItem(item) {
   const menus: any = []
-  arr.forEach((item, index) => {
-    if (!item.children) {
-      menus.push({
-        path: item.route_info,
-        component: Layout,
-        name: item.auth_ident,
-        redirect: item.route_info,
-        meta: {
-          title: item.auth_name,
-          elSvgIcon: 'Fold',
-          show_index: item.show_index,
-        },
-        children: [
-          {
-            path: item.route_info,
-            component: () => import('./views' + item.file_link),
-            // component: () => import('@/views/dashboard/index.vue'),
-            name: item.auth_ident + index,
-            meta: {
-              title: item.auth_name,
-            },
-          },
-        ],
-      })
-    } else {
-      if (item.children.length <= 1) {
-        menus.push({
+  if (!item.children) {
+    menus.push({
+      path: item.route_info,
+      component: Layout,
+      name: item.auth_ident,
+      meta: {
+        title: item.auth_name,
+        elSvgIcon: item.show_icon ?? 'Fold',
+        show_index: item.show_index,
+        qid: item.queryTemplateID,
+      },
+    })
+  } else if (item.children && item.children.length <= 1) {
+    menus.push({
+      path: item.route_info,
+      component: Layout,
+      name: item.auth_ident,
+      meta: {
+        title: item.auth_name,
+        elSvgIcon: item.show_icon ?? 'Fold',
+        show_index: item.show_index,
+        qid: item.queryTemplateID,
+      },
+      children: [
+        {
           path: item.route_info,
-          component: Layout,
+          component: () => import('./views' + item.file_link),
           name: item.auth_ident,
-          redirect: item.route_info,
           meta: {
             title: item.auth_name,
-            elSvgIcon: 'Fold',
+            elSvgIcon: item.show_icon ?? 'Fold',
             show_index: item.show_index,
+            qid: item.queryTemplateID,
           },
-          children: [
-            {
-              path: item.route_info,
-              component: () => import('./views' + item.file_link),
-              name: item.auth_ident + index,
-              meta: {
-                title: item.auth_name,
-              },
-            },
-          ],
-        })
-      } else {
-        const datas = item.children
-        const arrs: any = []
-        datas.forEach((item, ci) => {
-          arrs.push({
-            path: item.route_info,
-            component: () => import('./views' + item.file_link),
-            name: item.auth_ident + ci,
-            meta: {
-              title: item.auth_name,
-            },
-          })
-        })
-        menus.push({
-          path: item.route_info,
-          component: Layout,
-          name: item.auth_ident,
-          meta: { show_index: item.show_index },
-          children: [
-            {
-              path: item.route_info,
-              component: {
-                render: () => h(resolveComponent('router-view')),
-              },
-              name: item.auth_ident,
-              redirect: item.route_info,
-              meta: {
-                title: item.auth_name + index,
-                elSvgIcon: 'Fold',
-              },
-              children: arrs,
-            },
-          ],
-        })
-      }
+        },
+      ],
+    })
+  } else {
+    const datas = item.children
+    const arrs: any = []
+    datas.forEach((item, ci) => {
+      arrs.push({
+        path: item.route_info,
+        component: () => import('./views' + item.file_link),
+        name: item.auth_ident + ci,
+        meta: {
+          title: item.auth_name,
+        },
+      })
+    })
+    menus.push({
+      path: item.route_info,
+      component: Layout,
+      name: item.auth_ident,
+      meta: {
+        show_index: item.show_index,
+        qid: item.queryTemplateID,
+        title: item.auth_name,
+        elSvgIcon: item.show_icon ?? 'Fold',
+      },
+      children: arrs,
+      // children: [
+      //   {
+      //     path: item.route_info,
+      //     component: {
+      //       render: () => h(resolveComponent('router-view')),
+      //     },
+      //     name: item.auth_ident,
+      //     redirect: arrs[0].path,
+      //     meta: {
+      //       title: item.auth_name,
+      //       elSvgIcon: 'Fold',
+      //     },
+      //     children: arrs,
+      //   },
+      // ],
+    })
+  }
+  return menus
+}
+
+function parseMenu(arr: Array<any>) {
+  const menus: any = []
+  arr.forEach((item) => {
+    if (item.show_type == 1) {
+      const res = parseItem(item)
+      menus.push(res)
+    } else if (item.show_type == 2) {
+      const res = parseItem(item)
+      menus.push(res)
     }
   })
-  menus.push({
+  const newMenus = menus.flat()
+  newMenus.push({
     path: '/userManagement',
     component: Layout,
     name: 'UserManagement',
     redirect: '/userManagement/permission',
     //using el svg icon, the elSvgIcon first when at the same time using elSvgIcon and icon
-    meta: { title: '权限管理', elSvgIcon: 'Fold', show_index: 2 },
+    meta: { title: '权限管理', elSvgIcon: 'Avatar', show_index: 2 },
     children: [
       {
         path: '/userManagement/permission',
         name: 'Permission',
-        meta: { title: '权限项管理' },
+        meta: { title: '权限项管理', elSvgIcon: 'Avatar' },
         component: () => import('@/views/userManagement/permission/index.vue'),
       },
     ],
   })
-  const allMenus = menus.length
-    ? menus.sort((a, b) => a.meta.show_index - b.meta.show_index)
+  const allMenus = newMenus.length
+    ? newMenus.sort((a, b) => a.meta.show_index - b.meta.show_index)
     : []
   allMenus.push({
     path: '/:pathMatch(.*)',
     redirect: '/404',
     hidden: true,
   })
+  allMenus[0].redirect = allMenus[0].path
   allMenus[0].path = '/'
   return allMenus
 }

+ 11 - 0
src/utils/validate.ts

@@ -182,5 +182,16 @@ export function setTree(arr, parentKey, key) {
         all.some((c) => item[key] == c[key])
       )
   )
+  res.forEach((item) => {
+    const datas: any = []
+    if (item.children) {
+      item.children.some((p) => {
+        if (item[key] == p[parentKey]) {
+          datas.push(p)
+          item.children = datas
+        }
+      })
+    }
+  })
   return res
 }