// 动态路由 import { GlobalStore } from '@/stores/index' import router from '@/router/index' import { notFoundRouter } from './staticRouter' import { ElNotification } from 'element-plus' // 加载views下面所有的.vue文件 const modules = import.meta.glob('@/views/**/*.vue') console.log(modules) export const asyncAddRouter = () => { const globalStore = GlobalStore() globalStore.GET_Flat_Menu.forEach((item: any) => { item.children && delete item.children if (item.component && typeof item.component == 'string') { // item.component = modules['/src/views' + item.component + '.vue'] console.log(item) } if (item.meta.isFull) { router.addRoute(item) } else { router.addRoute('Index', item) } }) } /** * 初始化动态路由 */ export const initDynamicRouter = async () => { const globalStore = GlobalStore() try { // 判断是否已经加载过路由了 // 1.获取菜单列表 && 用户信息 await globalStore.SET_UserInfo() await globalStore.SET_Menu_User_List() // 2.判断当前用户有没有菜单权限 if (!globalStore.GET_Menu_List.length) { ElNotification({ title: '无权限访问', message: '当前账号无任何菜单权限,请联系系统管理员!', type: 'warning', duration: 3000 }) globalStore.SET_User_Tokey('') globalStore.SET_User_Info({}) router.replace('/login') return Promise.reject('No permission') } // 3.添加动态路由 asyncAddRouter() // 4.最后添加 notFoundRouter router.addRoute(notFoundRouter) } catch (error) { // 💢 当按钮 || 菜单请求出错时,重定向到登陆页 globalStore.SET_User_Tokey('') router.replace('/login') return Promise.reject(error) } }