skyouc
2024-12-21 c635d78b479510ebe2556a420948effcd30a0731
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router'
import { get } from '@/utils/request.js'
import { logout } from '@/config';
 
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: '主页',
      component: () => import('@/views/HomeView.vue'),
      meta: {
        keepAlive: true, // 设置keepAlive,让此路由页面被缓存
        title: '主页',
      },
    },
    {
      path: '/login',
      name: '登录',
      component: () => import('@/views/login/LoginView.vue'),
      meta: {
        title: '登录',
      },
    },
 
    // {
    //   path: '/system/menu',
    //   name: '菜单管理',
    //   component: () => import('@/views/system/menu/index.vue'),
    //   meta: {
    //     keepAlive: true // 设置keepAlive,让此路由页面被缓存
    //   },
    // },
    // {
    //   path: '/system/userLogin',
    //   name: '登录日志',
    //   component: () => import('@/views/system/userLogin/index.vue'),
    //   meta: {
    //     keepAlive: true // 设置keepAlive,让此路由页面被缓存
    //   },
    // },
    // {
    //   path: '/system/user',
    //   name: '用户管理',
    //   component: () => import('@/views/system/user/index.vue'),
    //   meta: {
    //     keepAlive: true // 设置keepAlive,让此路由页面被缓存
    //   },
    // },
    // {
    //   path: '/system/host',
    //   name: '机构管理',
    //   component: () => import('@/views/system/host/index.vue'),
    //   meta: {
    //     keepAlive: true // 设置keepAlive,让此路由页面被缓存
    //   },
    // },
  ]
})
 
export const initRouter = async () => {
  const modules = import.meta.glob('/src/views/**/index.vue');
  const resp = await get('/api/auth/router', {});
  let result = resp.data;
 
  if(result.code == 200) {
    let data = result.data;
    data.forEach((item) => {
      const module = modules[`/src/views${item.route}/index.vue`];
      if (module) {
        const itemRouter = {
          path: item.route,
          name: item.name,
          component: module,
          meta: {
            keepAlive: true, // 设置keepAlive,让此路由页面被缓存
            title: item.name,
          }
        }
        router.addRoute(itemRouter)
      }
    })
  }else if(result.code == 401) {
    logout()
  }
  return null;
}
 
router.beforeEach((to, from, next) => {
  if (to.meta && to.meta.title) {
    document.title = to.meta.title
  }
  next()
})
 
export default router