refactor(projects): format code style [调整代码格式]

This commit is contained in:
Soybean 2022-11-08 18:24:32 +08:00
parent 6a344ff2c7
commit a9d58f88aa
7 changed files with 176 additions and 179 deletions

View File

@ -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';

View File

@ -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);
}

View File

@ -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);
}

View File

@ -5,3 +5,4 @@ export * from './auth';
export * from './menu';
export * from './breadcrumb';
export * from './regexp';
export * from './transform';

View File

@ -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 -
@ -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;
}

View File

@ -1,97 +1,5 @@
import type { RouteComponent, RouteRecordRaw } from 'vue-router';
import { BasicLayout, BlankLayout } from '@/layouts';
import { views } from '@/views';
import { isFunction } from '@/utils';
type Lazy<T> = () => Promise<T>;
type LayoutComponent = Record<EnumType.LayoutComponentName, Lazy<RouteComponent>>;
/**
* 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);
}
interface ModuleComponent {
default: RouteComponent;
}
/** 给页面组件设置名称 */
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);
}
/**
*
* @param item -
*/
function hasHref(item: AuthRoute.Route) {
return Boolean(item.meta.href);
}
/**
* path
* @param item -
*/
function hasDynamicPath(item: AuthRoute.Route) {
return Boolean(item.meta.dynamicPath);
}
/**
*
* @param item -
*/
function hasComponent(item: AuthRoute.Route) {
return Boolean(item.component);
}
/**
*
* @param item -
*/
function hasChildren(item: AuthRoute.Route) {
return Boolean(item.children && item.children.length);
}
/**
*
* @param item -
*/
function isSingleRoute(item: AuthRoute.Route) {
return Boolean(item.meta.singleLayout);
}
import type { RouteRecordRaw } from 'vue-router';
import { getLayoutComponent, getViewComponent } from './component';
/**
* vue路由
@ -212,3 +120,85 @@ export function transformAuthRouteToVueRoute(item: AuthRoute.Route) {
return resultRoute;
}
/**
*
* @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 item -
*/
function hasHref(item: AuthRoute.Route) {
return Boolean(item.meta.href);
}
/**
* path
* @param item -
*/
function hasDynamicPath(item: AuthRoute.Route) {
return Boolean(item.meta.dynamicPath);
}
/**
*
* @param item -
*/
function hasComponent(item: AuthRoute.Route) {
return Boolean(item.component);
}
/**
*
* @param item -
*/
function hasChildren(item: AuthRoute.Route) {
return Boolean(item.children && item.children.length);
}
/**
*
* @param item -
*/
function isSingleRoute(item: AuthRoute.Route) {
return Boolean(item.meta.singleLayout);
}

View File

@ -16,7 +16,8 @@ export default defineConfig(configEnv => {
resolve: {
alias: {
'~': rootPath,
'@': srcPath
'@': srcPath,
'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
}
},
define: viteDefine,