Skip to content

Commit a9d58f8

Browse files
committed
refactor(projects): format code style [调整代码格式]
1 parent 6a344ff commit a9d58f8

File tree

7 files changed

+176
-179
lines changed

7 files changed

+176
-179
lines changed

src/store/modules/route/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import {
66
getCacheRoutes,
77
getConstantRouteNames,
88
getUserInfo,
9+
transformAuthRouteToVueRoutes,
10+
transformAuthRouteToVueRoute,
911
transformAuthRouteToMenu,
1012
transformAuthRouteToSearchMenus,
1113
transformRouteNameToRoutePath,
1214
transformRoutePathToRouteName
1315
} from '@/utils';
14-
import { transformAuthRouteToVueRoutes, transformAuthRouteToVueRoute } from '@/utils/router/transform';
1516
import { useAuthStore } from '../auth';
1617
import { useTabStore } from '../tab';
1718

src/utils/router/component.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import type { RouteComponent } from 'vue-router';
2+
import { BasicLayout, BlankLayout } from '@/layouts';
3+
import { views } from '@/views';
4+
import { isFunction } from '../common';
5+
6+
type Lazy<T> = () => Promise<T>;
7+
8+
interface ModuleComponent {
9+
default: RouteComponent;
10+
}
11+
12+
type LayoutComponent = Record<EnumType.LayoutComponentName, Lazy<ModuleComponent>>;
13+
14+
/**
15+
* 获取布局的vue文件(懒加载的方式)
16+
* @param layoutType - 布局类型
17+
*/
18+
export function getLayoutComponent(layoutType: EnumType.LayoutComponentName) {
19+
const layoutComponent: LayoutComponent = {
20+
basic: BasicLayout,
21+
blank: BlankLayout
22+
};
23+
return layoutComponent[layoutType];
24+
}
25+
26+
/**
27+
* 获取页面导入的vue文件
28+
* @param routeKey - 路由key
29+
*/
30+
export function getViewComponent(routeKey: AuthRoute.LastDegreeRouteKey) {
31+
if (!views[routeKey]) {
32+
throw new Error(`路由“${routeKey}”没有对应的组件文件!`);
33+
}
34+
return setViewComponentName(views[routeKey], routeKey);
35+
}
36+
37+
/** 给页面组件设置名称 */
38+
function setViewComponentName(component: RouteComponent | Lazy<ModuleComponent>, name: string) {
39+
if (isAsyncComponent(component)) {
40+
return async () => {
41+
const result = await component();
42+
Object.assign(result.default, { name });
43+
return result;
44+
};
45+
}
46+
47+
Object.assign(component, { name });
48+
49+
return component;
50+
}
51+
52+
function isAsyncComponent(component: RouteComponent | Lazy<ModuleComponent>): component is Lazy<ModuleComponent> {
53+
return isFunction(component);
54+
}

src/utils/router/helpers.ts

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,64 +6,14 @@ export function getConstantRouteNames(routes: AuthRoute.Route[]) {
66
return routes.map(route => getConstantRouteName(route)).flat(1);
77
}
88

9-
/**
10-
* 将权限路由转换成搜索的菜单数据
11-
* @param routes - 权限路由
12-
* @param treeMap
13-
*/
14-
export function transformAuthRouteToSearchMenus(routes: AuthRoute.Route[], treeMap: AuthRoute.Route[] = []) {
15-
if (routes && routes.length === 0) return [];
16-
return routes.reduce((acc, cur) => {
17-
if (!cur.meta?.hide) {
18-
acc.push(cur);
19-
}
20-
if (cur.children && cur.children.length > 0) {
21-
transformAuthRouteToSearchMenus(cur.children, treeMap);
22-
}
23-
return acc;
24-
}, treeMap);
25-
}
26-
27-
/** 将路由名字转换成路由路径 */
28-
export function transformRouteNameToRoutePath(name: Exclude<AuthRoute.AllRouteKey, 'not-found'>): AuthRoute.RoutePath {
29-
const rootPath: AuthRoute.RoutePath = '/';
30-
if (name === 'root') return rootPath;
31-
32-
const splitMark = '_';
33-
const pathSplitMark = '/';
34-
const path = name.split(splitMark).join(pathSplitMark);
35-
36-
return (pathSplitMark + path) as AuthRoute.RoutePath;
37-
}
38-
39-
/** 将路由路径转换成路由名字 */
40-
export function transformRoutePathToRouteName<K extends AuthRoute.RoutePath>(path: K) {
41-
if (path === '/') return 'root';
42-
43-
const pathSplitMark = '/';
44-
const routeSplitMark = '_';
45-
46-
const name = path.split(pathSplitMark).slice(1).join(routeSplitMark) as AuthRoute.AllRouteKey;
47-
48-
return name;
49-
}
50-
519
/**
5210
* 获取所有固定路由的名称集合
5311
* @param route - 固定路由
5412
*/
5513
function getConstantRouteName(route: AuthRoute.Route) {
5614
const names = [route.name];
57-
if (hasChildren(route)) {
15+
if (route.children?.length) {
5816
names.push(...route.children!.map(item => getConstantRouteName(item)).flat(1));
5917
}
6018
return names;
6119
}
62-
63-
/**
64-
* 是否有子路由
65-
* @param item - 权限路由
66-
*/
67-
function hasChildren(item: AuthRoute.Route) {
68-
return Boolean(item.children && item.children.length);
69-
}

src/utils/router/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './auth';
55
export * from './menu';
66
export * from './breadcrumb';
77
export * from './regexp';
8+
export * from './transform';

src/utils/router/menu.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,5 @@
11
import { useIconRender } from '@/composables';
22

3-
/** 路由不转换菜单 */
4-
function hideInMenu(route: AuthRoute.Route) {
5-
return Boolean(route.meta.hide);
6-
}
7-
8-
/** 给菜单添加可选属性 */
9-
function addPartialProps(config: {
10-
menu: App.GlobalMenuOption;
11-
icon?: string;
12-
localIcon?: string;
13-
children?: App.GlobalMenuOption[];
14-
}) {
15-
const { iconRender } = useIconRender();
16-
17-
const item = { ...config.menu };
18-
19-
const { icon, localIcon, children } = config;
20-
21-
if (localIcon) {
22-
Object.assign(item, { icon: iconRender({ localIcon }) });
23-
}
24-
25-
if (icon) {
26-
Object.assign(item, { icon: iconRender({ icon }) });
27-
}
28-
29-
if (children) {
30-
Object.assign(item, { children });
31-
}
32-
return item;
33-
}
34-
353
/**
364
* 将权限路由转换成菜单
375
* @param routes - 路由
@@ -85,3 +53,35 @@ function getActiveKeyPathsOfMenu(activeKey: string, menu: App.GlobalMenuOption)
8553
}
8654
return keys;
8755
}
56+
57+
/** 路由不转换菜单 */
58+
function hideInMenu(route: AuthRoute.Route) {
59+
return Boolean(route.meta.hide);
60+
}
61+
62+
/** 给菜单添加可选属性 */
63+
function addPartialProps(config: {
64+
menu: App.GlobalMenuOption;
65+
icon?: string;
66+
localIcon?: string;
67+
children?: App.GlobalMenuOption[];
68+
}) {
69+
const { iconRender } = useIconRender();
70+
71+
const item = { ...config.menu };
72+
73+
const { icon, localIcon, children } = config;
74+
75+
if (localIcon) {
76+
Object.assign(item, { icon: iconRender({ localIcon }) });
77+
}
78+
79+
if (icon) {
80+
Object.assign(item, { icon: iconRender({ icon }) });
81+
}
82+
83+
if (children) {
84+
Object.assign(item, { children });
85+
}
86+
return item;
87+
}

0 commit comments

Comments
 (0)