| | |
| | | import pinia from '/@/stores/index'; |
| | | import { useUserInfo } from '/@/stores/userInfo'; |
| | | import { useRequestOldRoutes } from '/@/stores/requestOldRoutes'; |
| | | import { Session } from '/@/utils/storage'; |
| | | import { NextLoading } from '/@/utils/loading'; |
| | | import { dynamicRoutes, notFoundAndNoPower } from '/@/router/route'; |
| | | import { formatTwoStageRoutes, formatFlatteningRoutes, router } from '/@/router/index'; |
| | | import { useRoutesList } from '/@/stores/routesList'; |
| | | import { useTagsViewRoutes } from '/@/stores/tagsViewRoutes'; |
| | | import { useMenuApi } from '/@/api/menu/index'; |
| | | import { useMenuApi } from '/@/api/systemManage/menu/index'; |
| | | import { ElMessage } from 'element-plus'; |
| | | import Cookies from 'js-cookie'; |
| | | |
| | | const menuApi = useMenuApi(); |
| | | |
| | | const layouModules: any = import.meta.glob('../layout/routerView/*.{vue,tsx}'); |
| | | const viewsModules: any = import.meta.glob('../views/**/*.{vue,tsx}'); |
| | | |
| | | |
| | | /** |
| | | * 获取目录下的 .vue、.tsx 全部文件 |
| | |
| | | */ |
| | | const dynamicViewsModules: Record<string, Function> = Object.assign({}, { ...layouModules }, { ...viewsModules }); |
| | | |
| | | |
| | | export async function initBackEndControlRoutes() { |
| | | if (window.nextLoading === undefined) NextLoading.start(); |
| | | if (!Session.get('token')) return false; |
| | | const res = await getBackEndControlRoutes(Session.get('projectId')); |
| | | useRequestOldRoutes().setRequestOldRoutes(JSON.parse(JSON.stringify(res.data.data))); |
| | | dynamicRoutes[0].children = await backEndComponent(res.data.data); |
| | | await setAddRoute(); |
| | | await setFilterMenuAndCacheTagsViewRoutes(); |
| | | } |
| | | |
| | | if (window.nextLoading === undefined) NextLoading.start(); |
| | | if (!Cookies.get('token')) return false; |
| | | const res = await getBackEndControlRoutes(Cookies.get('projectId') === null ? '' : Cookies.get('projectId')); |
| | | await useRequestOldRoutes().setRequestOldRoutes(JSON.parse(JSON.stringify(res.data.data))); |
| | | dynamicRoutes[0].children = await backEndComponent(res.data.data); |
| | | await setAddRoute(); |
| | | await setFilterMenuAndCacheTagsViewRoutes(); |
| | | } |
| | | |
| | | export function setFilterMenuAndCacheTagsViewRoutes() { |
| | | const storesRoutesList = useRoutesList(pinia); |
| | | storesRoutesList.setRoutesList(dynamicRoutes[0].children as any); |
| | | setCacheTagsViewRoutes(); |
| | | const storesRoutesList = useRoutesList(pinia); |
| | | storesRoutesList.setRoutesList(dynamicRoutes[0].children as any); |
| | | setCacheTagsViewRoutes(); |
| | | } |
| | | |
| | | |
| | | export function setCacheTagsViewRoutes() { |
| | | const storesTagsView = useTagsViewRoutes(pinia); |
| | | storesTagsView.setTagsViewRoutes(formatTwoStageRoutes(formatFlatteningRoutes(dynamicRoutes))[0].children); |
| | | const storesTagsView = useTagsViewRoutes(pinia); |
| | | storesTagsView.setTagsViewRoutes(formatTwoStageRoutes(formatFlatteningRoutes(dynamicRoutes))[0].children); |
| | | } |
| | | |
| | | |
| | | export function setFilterRouteEnd() { |
| | | let filterRouteEnd: any = formatTwoStageRoutes(formatFlatteningRoutes(dynamicRoutes)); |
| | | filterRouteEnd[0].children = [...filterRouteEnd[0].children, ...notFoundAndNoPower]; |
| | | return filterRouteEnd; |
| | | let filterRouteEnd: any = formatTwoStageRoutes(formatFlatteningRoutes(dynamicRoutes)); |
| | | filterRouteEnd[0].children = [...filterRouteEnd[0].children, ...notFoundAndNoPower]; |
| | | return filterRouteEnd; |
| | | } |
| | | |
| | | |
| | | export async function setAddRoute() { |
| | | await setFilterRouteEnd().forEach((route: RouteRecordRaw) => { |
| | | router.addRoute(route); |
| | | }); |
| | | await setFilterRouteEnd().forEach((route: RouteRecordRaw) => { |
| | | router.addRoute(route); |
| | | }); |
| | | } |
| | | |
| | | |
| | | export async function getBackEndControlRoutes(value : string) { |
| | | // const stores = useUserInfo(pinia); |
| | | // const { userInfos } = storeToRefs(stores); |
| | | // const auth = userInfos.value.roles[0]; |
| | | return menuApi.getMenuAdmin(value); |
| | | |
| | | export async function getBackEndControlRoutes(value: string) { |
| | | // const stores = useUserInfo(pinia); |
| | | // const { userInfos } = storeToRefs(stores); |
| | | // const auth = userInfos.value.roles[0]; |
| | | return menuApi.getMenuAdmin(value); |
| | | } |
| | | |
| | | /** |
| | | * 重新请求后端路由菜单接口 |
| | | * @description 用于菜单管理界面刷新菜单(未进行测试) |
| | | * @description 路径:/src/views/system/menu/component/menuDialog.vue |
| | | * @description 路径:/src/views/system/homeMenu/components/menuDialog.vue |
| | | */ |
| | | export function setBackEndControlRefreshRoutes() { |
| | | getBackEndControlRoutes(Session.get('projectId')); |
| | | getBackEndControlRoutes(Cookies.get('projectId')); |
| | | } |
| | | |
| | | |
| | | export function backEndComponent(routes: any) { |
| | | if (!routes) return; |
| | | return routes.map((item: any) => { |
| | | if (item.component) item.component = dynamicImport(dynamicViewsModules, item.component as string); |
| | | item.children && backEndComponent(item.children); |
| | | return item; |
| | | }); |
| | | if (!routes) return; |
| | | return routes.map((item: any) => { |
| | | if (item.component) item.component = dynamicImport(dynamicViewsModules, item.component as string); |
| | | item.children && backEndComponent(item.children); |
| | | return item; |
| | | }); |
| | | } |
| | | |
| | | |
| | | export function dynamicImport(dynamicViewsModules: Record<string, Function>, component: string) { |
| | | const keys = Object.keys(dynamicViewsModules); |
| | | const matchKeys = keys.filter((key) => { |
| | | const k = key.replace(/..\/views|../, ''); |
| | | return k.startsWith(`${component}`) || k.startsWith(`/${component}`); |
| | | }); |
| | | if (matchKeys?.length === 1) { |
| | | const matchKey = matchKeys[0]; |
| | | return dynamicViewsModules[matchKey]; |
| | | } |
| | | if (matchKeys?.length > 1) { |
| | | return false; |
| | | } |
| | | const keys = Object.keys(dynamicViewsModules); |
| | | const matchKeys = keys.filter((key) => { |
| | | const k = key.replace(/..\/views|../, ''); |
| | | return k.startsWith(`${component}`) || k.startsWith(`/${component}`); |
| | | }); |
| | | if (matchKeys?.length === 1) { |
| | | const matchKey = matchKeys[0]; |
| | | return dynamicViewsModules[matchKey]; |
| | | } |
| | | if (matchKeys?.length > 1) { |
| | | return false; |
| | | } |
| | | } |