|
|
@@ -1,98 +1,89 @@
|
|
|
import { RouteRecordRaw } from 'vue-router';
|
|
|
import { queryAccountMenu } from '@/services/api/account';
|
|
|
import { sessionCache } from '@/store';
|
|
|
-import router from '@pc/router';
|
|
|
+import router from '../router';
|
|
|
|
|
|
-/**
|
|
|
- * 添加404页面
|
|
|
- */
|
|
|
-const addNotFound = () => {
|
|
|
- router.addRoute({
|
|
|
- path: '/:pathMatch(.*)*',
|
|
|
- name: 'Error',
|
|
|
- component: () => import('../views/error/404.vue'),
|
|
|
- })
|
|
|
-}
|
|
|
+export default new (class {
|
|
|
+ /** 防止动态路由无限加载 */
|
|
|
+ isReady = false;
|
|
|
|
|
|
-/**
|
|
|
- * 动态添加路由
|
|
|
- */
|
|
|
-const addRoutes = (routes: Ermcp.AccountMenu[], parentName = ''): void => {
|
|
|
- routes.forEach((item) => {
|
|
|
- if (item.component && item.path) {
|
|
|
- let component;
|
|
|
- switch (item.component) {
|
|
|
- case 'Page': {
|
|
|
- component = () => import('../components/layouts/page/index.vue');
|
|
|
- break;
|
|
|
- }
|
|
|
- default: {
|
|
|
- const componentString = item.component.replace(/^\/+/, ''); // 过滤字符串前面所有 '/' 字符
|
|
|
- const componentPath = componentString.replace(/\.\w+$/, ''); // 过滤后缀名,为了让 import 加入 .vue ,不然会有警告提示...
|
|
|
- component = () => import('../' + componentPath + '.vue');
|
|
|
- }
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 添加404页面
|
|
|
+ */
|
|
|
+ private addNotFound() {
|
|
|
+ router.addRoute({
|
|
|
+ path: '/:pathMatch(.*)*',
|
|
|
+ name: 'Error',
|
|
|
+ component: () => import('../views/error/404.vue'),
|
|
|
+ })
|
|
|
+ }
|
|
|
|
|
|
- const route: RouteRecordRaw = {
|
|
|
- path: item.path,
|
|
|
- name: item.code,
|
|
|
- component,
|
|
|
- meta: {
|
|
|
- title: item.title,
|
|
|
- icon: item.icon,
|
|
|
- url: item.url,
|
|
|
- auth: item.auth ?? [],
|
|
|
- remark: item.remark,
|
|
|
- },
|
|
|
- }
|
|
|
-
|
|
|
- parentName ? router.addRoute(parentName, route) : router.addRoute(route);
|
|
|
+ /**
|
|
|
+ * 动态添加路由
|
|
|
+ * @param routes
|
|
|
+ * @param parentName
|
|
|
+ */
|
|
|
+ private addRoutes(routes: Ermcp.AccountMenu[], parentName = '') {
|
|
|
+ routes.forEach((item) => {
|
|
|
+ if (item.component && item.path) {
|
|
|
+ let component;
|
|
|
+ switch (item.component) {
|
|
|
+ case 'Page': {
|
|
|
+ component = () => import('../components/layouts/page/index.vue');
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ default: {
|
|
|
+ const componentString = item.component.replace(/^\/+/, ''); // 过滤字符串前面所有 '/' 字符
|
|
|
+ const componentPath = componentString.replace(/\.\w+$/, ''); // 过滤后缀名,为了让 import 加入 .vue ,不然会有警告提示...
|
|
|
+ component = () => import('../' + componentPath + '.vue');
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- if (item.children && item.children.length) {
|
|
|
- addRoutes(item.children, item.code);
|
|
|
- }
|
|
|
- }
|
|
|
- })
|
|
|
-}
|
|
|
+ const route: RouteRecordRaw = {
|
|
|
+ path: item.path,
|
|
|
+ name: item.code,
|
|
|
+ component,
|
|
|
+ meta: {
|
|
|
+ title: item.title,
|
|
|
+ icon: item.icon,
|
|
|
+ url: item.url,
|
|
|
+ auth: item.auth ?? [],
|
|
|
+ remark: item.remark,
|
|
|
+ },
|
|
|
+ }
|
|
|
|
|
|
-// 防止动态路由无限加载
|
|
|
-let routerComplete = false;
|
|
|
+ parentName ? router.addRoute(parentName, route) : router.addRoute(route);
|
|
|
|
|
|
-/**
|
|
|
- * 注册路由
|
|
|
- */
|
|
|
-export const registerRoutes = (): Promise<boolean> => {
|
|
|
- const menus = sessionCache.getValue('menus');
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- addNotFound();
|
|
|
- if (menus.length) {
|
|
|
- addRoutes(menus);
|
|
|
- resolve(true);
|
|
|
- } else {
|
|
|
- queryAccountMenu({
|
|
|
- success: (res) => {
|
|
|
- sessionCache.setValue('menus', res.data);
|
|
|
- addRoutes(res.data);
|
|
|
- resolve(true);
|
|
|
- },
|
|
|
- fail: (err) => {
|
|
|
- reject(err);
|
|
|
+ if (item.children && item.children.length) {
|
|
|
+ this.addRoutes(item.children, item.code);
|
|
|
}
|
|
|
- })
|
|
|
- }
|
|
|
- })
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * 获取动态路由加载状态
|
|
|
- */
|
|
|
-export function getRouterIsComplete(): boolean {
|
|
|
- return routerComplete;
|
|
|
-}
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
|
|
|
-/**
|
|
|
- * 设置动态路由加载状态
|
|
|
- */
|
|
|
-export function setRouterComplete(status: boolean): void {
|
|
|
- routerComplete = status;
|
|
|
-}
|
|
|
+ /**
|
|
|
+ * 注册路由
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+ registerRoutes() {
|
|
|
+ const menus = sessionCache.getValue('menus');
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ this.addNotFound();
|
|
|
+ if (menus.length) {
|
|
|
+ this.addRoutes(menus);
|
|
|
+ resolve(true);
|
|
|
+ } else {
|
|
|
+ queryAccountMenu({
|
|
|
+ success: (res) => {
|
|
|
+ sessionCache.setValue('menus', res.data);
|
|
|
+ this.addRoutes(res.data);
|
|
|
+ resolve(true);
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ reject(err);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+})
|