Skip to content

Commit

Permalink
refactor(projects): format code style [调整代码格式]
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Nov 8, 2022
1 parent 6a344ff commit a9d58f8
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 179 deletions.
3 changes: 2 additions & 1 deletion src/store/modules/route/index.ts
Expand Up @@ -6,12 +6,13 @@ import {
getCacheRoutes,
getConstantRouteNames,
getUserInfo,
transformAuthRouteToVueRoutes,
transformAuthRouteToVueRoute,
transformAuthRouteToMenu,
transformAuthRouteToSearchMenus,
transformRouteNameToRoutePath,
transformRoutePathToRouteName
} from '@/utils';
import { transformAuthRouteToVueRoutes, transformAuthRouteToVueRoute } from '@/utils/router/transform';
import { useAuthStore } from '../auth';
import { useTabStore } from '../tab';

Expand Down
54 changes: 54 additions & 0 deletions src/utils/router/component.ts
@@ -0,0 +1,54 @@
import type { RouteComponent } from 'vue-router';
import { BasicLayout, BlankLayout } from '@/layouts';
import { views } from '@/views';
import { isFunction } from '../common';

type Lazy<T> = () => Promise<T>;

interface ModuleComponent {
default: RouteComponent;
}

type LayoutComponent = Record<EnumType.LayoutComponentName, Lazy<ModuleComponent>>;

/**
* 获取布局的vue文件(懒加载的方式)
* @param layoutType - 布局类型
*/
export function getLayoutComponent(layoutType: EnumType.LayoutComponentName) {
const layoutComponent: LayoutComponent = {
basic: BasicLayout,
blank: BlankLayout
};
return layoutComponent[layoutType];
}

/**
* 获取页面导入的vue文件
* @param routeKey - 路由key
*/
export function getViewComponent(routeKey: AuthRoute.LastDegreeRouteKey) {
if (!views[routeKey]) {
throw new Error(`路由“${routeKey}”没有对应的组件文件!`);
}
return setViewComponentName(views[routeKey], routeKey);
}

/** 给页面组件设置名称 */
function setViewComponentName(component: RouteComponent | Lazy<ModuleComponent>, name: string) {
if (isAsyncComponent(component)) {
return async () => {
const result = await component();
Object.assign(result.default, { name });
return result;
};
}

Object.assign(component, { name });

return component;
}

function isAsyncComponent(component: RouteComponent | Lazy<ModuleComponent>): component is Lazy<ModuleComponent> {
return isFunction(component);
}
52 changes: 1 addition & 51 deletions src/utils/router/helpers.ts
Expand Up @@ -6,64 +6,14 @@ export function getConstantRouteNames(routes: AuthRoute.Route[]) {
return routes.map(route => getConstantRouteName(route)).flat(1);
}

/**
* 将权限路由转换成搜索的菜单数据
* @param routes - 权限路由
* @param treeMap
*/
export function transformAuthRouteToSearchMenus(routes: AuthRoute.Route[], treeMap: AuthRoute.Route[] = []) {
if (routes && routes.length === 0) return [];
return routes.reduce((acc, cur) => {
if (!cur.meta?.hide) {
acc.push(cur);
}
if (cur.children && cur.children.length > 0) {
transformAuthRouteToSearchMenus(cur.children, treeMap);
}
return acc;
}, treeMap);
}

/** 将路由名字转换成路由路径 */
export function transformRouteNameToRoutePath(name: Exclude<AuthRoute.AllRouteKey, 'not-found'>): AuthRoute.RoutePath {
const rootPath: AuthRoute.RoutePath = '/';
if (name === 'root') return rootPath;

const splitMark = '_';
const pathSplitMark = '/';
const path = name.split(splitMark).join(pathSplitMark);

return (pathSplitMark + path) as AuthRoute.RoutePath;
}

/** 将路由路径转换成路由名字 */
export function transformRoutePathToRouteName<K extends AuthRoute.RoutePath>(path: K) {
if (path === '/') return 'root';

const pathSplitMark = '/';
const routeSplitMark = '_';

const name = path.split(pathSplitMark).slice(1).join(routeSplitMark) as AuthRoute.AllRouteKey;

return name;
}

/**
* 获取所有固定路由的名称集合
* @param route - 固定路由
*/
function getConstantRouteName(route: AuthRoute.Route) {
const names = [route.name];
if (hasChildren(route)) {
if (route.children?.length) {
names.push(...route.children!.map(item => getConstantRouteName(item)).flat(1));
}
return names;
}

/**
* 是否有子路由
* @param item - 权限路由
*/
function hasChildren(item: AuthRoute.Route) {
return Boolean(item.children && item.children.length);
}
1 change: 1 addition & 0 deletions src/utils/router/index.ts
Expand Up @@ -5,3 +5,4 @@ export * from './auth';
export * from './menu';
export * from './breadcrumb';
export * from './regexp';
export * from './transform';
64 changes: 32 additions & 32 deletions src/utils/router/menu.ts
@@ -1,37 +1,5 @@
import { useIconRender } from '@/composables';

/** 路由不转换菜单 */
function hideInMenu(route: AuthRoute.Route) {
return Boolean(route.meta.hide);
}

/** 给菜单添加可选属性 */
function addPartialProps(config: {
menu: App.GlobalMenuOption;
icon?: string;
localIcon?: string;
children?: App.GlobalMenuOption[];
}) {
const { iconRender } = useIconRender();

const item = { ...config.menu };

const { icon, localIcon, children } = config;

if (localIcon) {
Object.assign(item, { icon: iconRender({ localIcon }) });
}

if (icon) {
Object.assign(item, { icon: iconRender({ icon }) });
}

if (children) {
Object.assign(item, { children });
}
return item;
}

/**
* 将权限路由转换成菜单
* @param routes - 路由
Expand Down Expand Up @@ -85,3 +53,35 @@ function getActiveKeyPathsOfMenu(activeKey: string, menu: App.GlobalMenuOption)
}
return keys;
}

/** 路由不转换菜单 */
function hideInMenu(route: AuthRoute.Route) {
return Boolean(route.meta.hide);
}

/** 给菜单添加可选属性 */
function addPartialProps(config: {
menu: App.GlobalMenuOption;
icon?: string;
localIcon?: string;
children?: App.GlobalMenuOption[];
}) {
const { iconRender } = useIconRender();

const item = { ...config.menu };

const { icon, localIcon, children } = config;

if (localIcon) {
Object.assign(item, { icon: iconRender({ localIcon }) });
}

if (icon) {
Object.assign(item, { icon: iconRender({ icon }) });
}

if (children) {
Object.assign(item, { children });
}
return item;
}

0 comments on commit a9d58f8

Please sign in to comment.