vben
2020-11-01 275ad9f14e8fa75620ff35c906c06c616fb2104f
提交 | 用户 | age
2f6253 1 import type { AppRouteModule, AppRouteRecordRaw } from '/@/router/types';
31e271 2 import type { RouteLocationNormalized, RouteRecordRaw } from 'vue-router';
2f6253 3
5f2a92 4 import { appStore } from '/@/store/modules/app';
N 5 import { tabStore } from '/@/store/modules/tab';
2f6253 6 import { createRouter, createWebHashHistory } from 'vue-router';
7 import { toRaw } from 'vue';
8 import { PAGE_LAYOUT_COMPONENT } from '/@/router/constant';
31e271 9
V 10 let currentTo: RouteLocationNormalized | null = null;
11
12 export function getCurrentTo() {
13   return currentTo;
14 }
15
16 export function setCurrentTo(to: RouteLocationNormalized) {
17   currentTo = to;
18 }
19 // 转化路由模块
20 // 将多级转成2层。keepAlive问题
2f6253 21 export function genRouteModule(moduleList: AppRouteModule[]) {
22   const ret: AppRouteRecordRaw[] = [];
23   for (const routeMod of moduleList) {
24     const routes = routeMod.routes as any;
25     const layout = routeMod.layout;
4c658f 26     const router = createRouter({ routes, history: createWebHashHistory() });
2f6253 27
4c658f 28     const flatList = (toRaw(router.getRoutes()).filter(
V 29       (item) => item.children.length === 0
30     ) as unknown) as AppRouteRecordRaw[];
2f6253 31     flatList.forEach((item) => {
4c658f 32       item.path = `${layout ? layout.path : ''}${item.path}`;
2f6253 33     });
4c658f 34     if (layout) {
V 35       layout.children = flatList;
36       ret.push(layout);
37     } else {
38       ret.push(...flatList);
39     }
2f6253 40   }
41   return ret as RouteRecordRaw[];
42 }
43
31e271 44 // 动态引入
2f6253 45 function asyncImportRoute(routes: AppRouteRecordRaw[]) {
46   routes.forEach((item) => {
47     const { component, children } = item;
48     if (component) {
49       item.component = () => import(`/@/views/${component}`);
50     }
51     children && asyncImportRoute(children);
52   });
53 }
54
31e271 55 // 将后台对象转成路由对象
2f6253 56 export function transformObjToRoute(routeList: AppRouteModule[]) {
57   routeList.forEach((route) => {
58     asyncImportRoute(route.routes);
59     if (route.layout) {
60       route.layout.component =
61         route.layout.component === 'PAGE_LAYOUT' ? PAGE_LAYOUT_COMPONENT : '';
62     }
63   });
64   return routeList;
65 }
5f2a92 66
31e271 67 //
5f2a92 68 export function getIsOpenTab(toPath: string) {
N 69   const { openKeepAlive, multiTabsSetting: { show } = {} } = appStore.getProjectConfig;
70
71   if (show && openKeepAlive) {
72     const tabList = tabStore.getTabsState;
73     return tabList.some((tab) => tab.path === toPath);
74   }
75   return false;
76 }
31e271 77
V 78 export function getParams(data: any = {}) {
79   const { params = {} } = data;
80   let ret = '';
81   Object.keys(params).forEach((key) => {
82     const p = params[key];
83     ret += `/${p}`;
84   });
85   return ret;
86 }