Blame view

src/utils/helper/routeHelper.ts 2.4 KB
陈文彬 authored
1
import type { AppRouteModule, AppRouteRecordRaw } from '/@/router/types';
vben authored
2
import type { RouteLocationNormalized, RouteRecordRaw } from 'vue-router';
陈文彬 authored
3
4
5
import { appStore } from '/@/store/modules/app';
import { tabStore } from '/@/store/modules/tab';
陈文彬 authored
6
7
8
import { createRouter, createWebHashHistory } from 'vue-router';
import { toRaw } from 'vue';
import { PAGE_LAYOUT_COMPONENT } from '/@/router/constant';
vben authored
9
10
11
12
13
14
15
16
17
18
19
20

let currentTo: RouteLocationNormalized | null = null;

export function getCurrentTo() {
  return currentTo;
}

export function setCurrentTo(to: RouteLocationNormalized) {
  currentTo = to;
}
// 转化路由模块
// 将多级转成2层。keepAlive问题
陈文彬 authored
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
export function genRouteModule(moduleList: AppRouteModule[]) {
  const ret: AppRouteRecordRaw[] = [];
  for (const routeMod of moduleList) {
    const routes = routeMod.routes as any;
    const layout = routeMod.layout;
    const router = createRouter({ routes, history: createWebHashHistory() });

    const flatList = toRaw(router.getRoutes()).filter((item) => item.children.length === 0);
    try {
      (router as any) = null;
    } catch (error) {}

    flatList.forEach((item) => {
      item.path = `${layout.path}${item.path}`;
    });
    layout.children = (flatList as unknown) as AppRouteRecordRaw[];
    ret.push(layout);
  }
  return ret as RouteRecordRaw[];
}
vben authored
42
// 动态引入
陈文彬 authored
43
44
45
46
47
48
49
50
51
52
function asyncImportRoute(routes: AppRouteRecordRaw[]) {
  routes.forEach((item) => {
    const { component, children } = item;
    if (component) {
      item.component = () => import(`/@/views/${component}`);
    }
    children && asyncImportRoute(children);
  });
}
vben authored
53
// 将后台对象转成路由对象
陈文彬 authored
54
55
56
57
58
59
60
61
62
63
export function transformObjToRoute(routeList: AppRouteModule[]) {
  routeList.forEach((route) => {
    asyncImportRoute(route.routes);
    if (route.layout) {
      route.layout.component =
        route.layout.component === 'PAGE_LAYOUT' ? PAGE_LAYOUT_COMPONENT : '';
    }
  });
  return routeList;
}
64
vben authored
65
//
66
67
68
69
70
71
72
73
74
export function getIsOpenTab(toPath: string) {
  const { openKeepAlive, multiTabsSetting: { show } = {} } = appStore.getProjectConfig;

  if (show && openKeepAlive) {
    const tabList = tabStore.getTabsState;
    return tabList.some((tab) => tab.path === toPath);
  }
  return false;
}
vben authored
75
76
77
78
79
80
81
82
83
84

export function getParams(data: any = {}) {
  const { params = {} } = data;
  let ret = '';
  Object.keys(params).forEach((key) => {
    const p = params[key];
    ret += `/${p}`;
  });
  return ret;
}