refactor: remove unused files, bug fixes and name changes

This commit is contained in:
Hansen Wang
2023-04-24 02:09:39 +08:00
parent e1528823f7
commit 5d891c1f44
32 changed files with 352 additions and 417 deletions

View File

@@ -1,8 +1,8 @@
import type { AppRouteRecordRaw } from '@/router/types';
import { ErrorPage, RedirectName, Layout } from '@/router/constant';
import { RouteRecordRaw } from 'vue-router';
// 404 on a page
export const ErrorPageRoute: AppRouteRecordRaw = {
export const ErrorPageRoute: RouteRecordRaw = {
path: '/:path(.*)*',
name: 'ErrorPage',
component: Layout,
@@ -23,7 +23,7 @@ export const ErrorPageRoute: AppRouteRecordRaw = {
],
};
export const RedirectRoute: AppRouteRecordRaw = {
export const RedirectRoute: RouteRecordRaw = {
path: '/redirect',
name: RedirectName,
component: Layout,

View File

@@ -1,5 +1,5 @@
import { adminMenus } from '@/api/system/menu';
import { constantRouterIcon } from './router-icons';
import { constantRouterIcon } from './icons';
import { RouteRecordRaw } from 'vue-router';
import { Layout, ParentLayout } from '@/router/constant';
import type { AppRouteRecordRaw } from '@/router/types';
@@ -16,13 +16,13 @@ LayoutMap.set('IFRAME', Iframe);
* @param parent
* @returns {*}
*/
export const routerGenerator = (routerMap, parent?): any[] => {
export const generateRoutes = (routerMap, parent?): any[] => {
return routerMap.map((item) => {
const currentRouter: any = {
const currentRoute: any = {
// 路由地址 动态拼接生成如 /dashboard/workplace
path: `${(parent && parent.path) || ''}/${item.path}`,
path: `${(parent && parent.path) ?? ''}/${item.path}`,
// 路由名称,建议唯一
name: item.name || '',
name: item.name ?? '',
// 该路由对应页面的 组件
component: item.component,
// meta: 页面标题, 菜单图标, 页面权限(供指令权限用,可去掉)
@@ -35,17 +35,17 @@ export const routerGenerator = (routerMap, parent?): any[] => {
};
// 为了防止出现后端返回结果不规范,处理有可能出现拼接出两个 反斜杠
currentRouter.path = currentRouter.path.replace('//', '/');
currentRoute.path = currentRoute.path.replace('//', '/');
// 重定向
item.redirect && (currentRouter.redirect = item.redirect);
item.redirect && (currentRoute.redirect = item.redirect);
// 是否有子菜单,并递归处理
if (item.children && item.children.length > 0) {
//如果未定义 redirect 默认第一个子路由为 redirect
!item.redirect && (currentRouter.redirect = `${item.path}/${item.children[0].path}`);
!item.redirect && (currentRoute.redirect = `${item.path}/${item.children[0].path}`);
// Recursion
currentRouter.children = routerGenerator(item.children, currentRouter);
currentRoute.children = generateRoutes(item.children, currentRoute);
}
return currentRouter;
return currentRoute;
});
};
@@ -53,19 +53,11 @@ export const routerGenerator = (routerMap, parent?): any[] => {
*
* @returns {Promise<Router>}
*/
export const generatorDynamicRouter = (): Promise<RouteRecordRaw[]> => {
return new Promise((resolve, reject) => {
adminMenus()
.then((result) => {
const routeList = routerGenerator(result);
asyncImportRoute(routeList);
resolve(routeList);
})
.catch((err) => {
reject(err);
});
});
export const generateDynamicRoutes = async (): Promise<RouteRecordRaw[]> => {
const result = await adminMenus();
const router = generateRoutes(result);
asyncImportRoute(router);
return router;
};
/**

View File

@@ -1,7 +1,7 @@
import type { RouteRecordRaw } from 'vue-router';
import { isNavigationFailure, Router } from 'vue-router';
import { useUserStoreWidthOut } from '@/store/modules/user';
import { useAsyncRouteStoreWidthOut } from '@/store/modules/asyncRoute';
import { useUser } from '@/store/modules/user';
import { useAsyncRoute } from '@/store/modules/asyncRoute';
import { ACCESS_TOKEN } from '@/store/mutation-types';
import { storage } from '@/utils/Storage';
import { PageEnum } from '@/enums/pageEnum';
@@ -12,8 +12,8 @@ const LOGIN_PATH = PageEnum.BASE_LOGIN;
const whitePathList = [LOGIN_PATH]; // no redirect whitelist
export function createRouterGuards(router: Router) {
const userStore = useUserStoreWidthOut();
const asyncRouteStore = useAsyncRouteStoreWidthOut();
const userStore = useUser();
const asyncRouteStore = useAsyncRoute();
router.beforeEach(async (to, from, next) => {
const Loading = window['$loading'] || null;
Loading && Loading.start();
@@ -51,12 +51,12 @@ export function createRouterGuards(router: Router) {
return;
}
if (asyncRouteStore.getIsDynamicAddedRoute) {
if (asyncRouteStore.getIsDynamicRouteAdded) {
next();
return;
}
const userInfo = await userStore.GetInfo();
const userInfo = await userStore.getInfo();
const routes = await asyncRouteStore.generateRoutes(userInfo);
@@ -74,7 +74,7 @@ export function createRouterGuards(router: Router) {
const redirectPath = (from.query.redirect || to.path) as string;
const redirect = decodeURIComponent(redirectPath);
const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect };
asyncRouteStore.setDynamicAddedRoute(true);
asyncRouteStore.setDynamicRouteAdded(true);
next(nextData);
Loading && Loading.finish();
});
@@ -84,7 +84,7 @@ export function createRouterGuards(router: Router) {
if (isNavigationFailure(failure)) {
//console.log('failed navigation', failure)
}
const asyncRouteStore = useAsyncRouteStoreWidthOut();
const asyncRouteStore = useAsyncRoute();
// 在这里设置需要缓存的组件名称
const keepAliveComponents = asyncRouteStore.keepAliveComponents;
const currentComName: any = to.matched.find((item) => item.name == to.name)?.name;

View File

@@ -1,22 +1,20 @@
import { App } from 'vue';
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import { RedirectRoute } from '@/router/base';
import { PageEnum } from '@/enums/pageEnum';
import { createRouterGuards } from './router-guards';
import { createRouterGuards } from './guards';
import type { IModuleType } from './types';
const modules = import.meta.glob<IModuleType>('./modules/**/*.ts', { eager: true });
const routeModuleList: RouteRecordRaw[] = [];
Object.keys(modules).forEach((key) => {
const mod = modules[key].default || {};
const routeModuleList: RouteRecordRaw[] = Object.keys(modules).reduce((list, key) => {
const mod = modules[key].default ?? {};
const modList = Array.isArray(mod) ? [...mod] : [mod];
routeModuleList.push(...modList);
});
return [...list, ...modList];
}, []);
function sortRoute(a, b) {
return (a.meta?.sort || 0) - (b.meta?.sort || 0);
return (a.meta?.sort ?? 0) - (b.meta?.sort ?? 0);
}
routeModuleList.sort(sortRoute);
@@ -43,10 +41,10 @@ export const LoginRoute: RouteRecordRaw = {
export const asyncRoutes = [...routeModuleList];
//普通路由 无需验证权限
export const constantRouter: any[] = [LoginRoute, RootRoute, RedirectRoute];
export const constantRouter: RouteRecordRaw[] = [LoginRoute, RootRoute, RedirectRoute];
const router = createRouter({
history: createWebHashHistory(''),
history: createWebHistory(),
routes: constantRouter,
strict: true,
scrollBehavior: () => ({ left: 0, top: 0 }),

View File

@@ -1,12 +1,12 @@
import type { RouteRecordRaw, RouteMeta } from 'vue-router';
import { defineComponent } from 'vue';
export type Component<T extends any = any> =
export type Component<T = any> =
| ReturnType<typeof defineComponent>
| (() => Promise<typeof import('*.vue')>)
| (() => Promise<T>);
export interface AppRouteRecordRaw extends Omit<RouteRecordRaw, 'meta'> {
export interface AppRouteRecordRaw extends Omit<RouteRecordRaw, 'meta' | 'children'> {
name: string;
meta: RouteMeta;
component?: Component | string;