Browse Source

动态路由

zhaoke 2 years ago
parent
commit
727ba1d9f9
1 changed files with 3 additions and 85 deletions
  1. 3 85
      src/getMenu.ts

+ 3 - 85
src/getMenu.ts

@@ -11,6 +11,7 @@ import { usePermissionStore } from '@/store/permission'
 import Layout from '@/layout'
 import { setTree } from '@/utils/validate'
 import * as _ from 'lodash'
+const modules = import.meta.glob('./views/**/*.vue')
 const whiteList = ['/login', '/404', '/401'] // no redirect whitelist
 router.beforeEach(async (to: any, from, next: any) => {
   // start progress bar
@@ -44,7 +45,6 @@ router.beforeEach(async (to: any, from, next: any) => {
             const treeMenu = setTree(menusArray, '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
@@ -129,7 +129,8 @@ function parseMenuItem(data) {
           qid: item.queryTemplateID,
           other: item.other ?? null,
         },
-        component: () => import('./views' + item.file_link),
+        component: modules[`./views${item.file_link}`],
+        // component: () => import('./views' + item.file_link),
         hidden: !item.is_show,
       })
     }
@@ -138,89 +139,6 @@ function parseMenuItem(data) {
   return menus
 }
 
-function parseItem(item) {
-  const menus: any = []
-  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) {
-    const datas = item.children
-    const arrs: any = []
-    datas.forEach((p) => {
-      arrs.push({
-        path: p.route_info,
-        component: () => import('./views' + p.file_link),
-        name: p.auth_ident,
-        meta: {
-          title: p.auth_name,
-        },
-      })
-    })
-    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: arrs,
-    })
-  } else {
-    const datas = item.children
-    const arrs: any = []
-    datas.forEach((p) => {
-      arrs.push({
-        path: p.route_info,
-        component: () => import('./views' + p.file_link),
-        name: p.auth_ident,
-        meta: {
-          title: p.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 setType(arr, parentKey, key) {
   const datas: any = []
   for (let i = 0; i < arr.length; i++) {