Merge pull request #86 from Tencent/feat/routers/optimizeJumper

feat/routers/optimize
This commit is contained in:
yuyang 2022-03-08 22:23:53 +08:00 committed by GitHub
commit b7e22ec4fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 26 deletions

View File

@ -24,7 +24,7 @@
"pinia": "^2.0.11", "pinia": "^2.0.11",
"qrcode.vue": "^3.2.2", "qrcode.vue": "^3.2.2",
"tdesign-icons-vue-next": "^0.0.6", "tdesign-icons-vue-next": "^0.0.6",
"tdesign-vue-next": "0.9.2", "tdesign-vue-next": "0.9.4",
"tvision-color": "^1.3.1", "tvision-color": "^1.3.1",
"vue": "^3.2.31", "vue": "^3.2.31",
"vue-color-kit": "^1.0.5", "vue-color-kit": "^1.0.5",

View File

@ -16,13 +16,10 @@ router.beforeEach(async (to, from, next) => {
NProgress.start(); NProgress.start();
const { token } = userStore; const { token } = userStore;
if (token) { if (token) {
if (to.path === '/login') { if (to.path === '/login') {
setTimeout(() => {
userStore.logout(); userStore.logout();
permissionStore.restore(); permissionStore.restore();
});
next(); next();
return; return;
} }
@ -39,10 +36,13 @@ router.beforeEach(async (to, from, next) => {
await permissionStore.initRoutes(roles); await permissionStore.initRoutes(roles);
next({ ...to }); if (router.hasRoute(to.name)) {
next();
} else {
next(`/`);
}
} catch (error) { } catch (error) {
MessagePlugin.error(error); MessagePlugin.error(error);
await userStore.removeToken();
next(`/login?redirect=${to.path}`); next(`/login?redirect=${to.path}`);
NProgress.done(); NProgress.done();
} }

View File

@ -21,17 +21,18 @@ const defaultRouterList: Array<RouteRecordRaw> = [
redirect: '/dashboard/base', redirect: '/dashboard/base',
component: () => import('@/layouts/blank.vue'), component: () => import('@/layouts/blank.vue'),
}, },
]; {
export const page404 = {
path: '/:w+', path: '/:w+',
name: '404Page', name: '404Page',
redirect: '/result/404', redirect: '/result/404',
}; },
];
export const allRoutes = [...defaultRouterList, ...asyncRouterList];
const router = createRouter({ const router = createRouter({
history: createWebHashHistory(), history: createWebHashHistory(),
routes: defaultRouterList, routes: allRoutes,
scrollBehavior() { scrollBehavior() {
return { return {
el: '#app', el: '#app',

View File

@ -1,16 +1,19 @@
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { RouteRecordRaw } from 'vue-router'; import { RouteRecordRaw } from 'vue-router';
import router, { asyncRouterList, page404 } from '@/router'; import router, { asyncRouterList } from '@/router';
import { store } from '@/store'; import { store } from '@/store';
function filterPermissionsRouters(routes: Array<RouteRecordRaw>, roles: Array<unknown>) { function filterPermissionsRouters(routes: Array<RouteRecordRaw>, roles: Array<unknown>) {
const res = []; const res = [];
const removeRoutes = [];
routes.forEach((route) => { routes.forEach((route) => {
const children = []; const children = [];
route.children?.forEach((childRouter) => { route.children?.forEach((childRouter) => {
const roleCode = childRouter.meta?.roleCode || childRouter.name; const roleCode = childRouter.meta?.roleCode || childRouter.name;
if (roles.indexOf(roleCode) !== -1) { if (roles.indexOf(roleCode) !== -1) {
children.push(childRouter); children.push(childRouter);
} else {
removeRoutes.push(childRouter);
} }
}); });
if (children.length > 0) { if (children.length > 0) {
@ -18,37 +21,42 @@ function filterPermissionsRouters(routes: Array<RouteRecordRaw>, roles: Array<un
res.push(route); res.push(route);
} }
}); });
return res; return { accessedRouters: res, removeRoutes };
} }
export const usePermissionStore = defineStore('permission', { export const usePermissionStore = defineStore('permission', {
state: () => ({ state: () => ({
whiteListRouters: ['/login'], whiteListRouters: ['/login'],
routers: [], routers: [],
removeRoutes: [],
}), }),
actions: { actions: {
async initRoutes(roles: Array<unknown>) { async initRoutes(roles: Array<unknown>) {
let accessedRouters; let accessedRouters = [];
let removeRoutes = [];
// special token // special token
if (roles.includes('ALL_ROUTERS')) { if (roles.includes('all')) {
accessedRouters = asyncRouterList; accessedRouters = asyncRouterList;
} else { } else {
accessedRouters = filterPermissionsRouters(asyncRouterList, roles); const res = filterPermissionsRouters(asyncRouterList, roles);
accessedRouters = res.accessedRouters;
removeRoutes = res.removeRoutes;
} }
this.routers = accessedRouters; this.routers = accessedRouters;
this.removeRoutes = removeRoutes;
// register routers removeRoutes.forEach((item: RouteRecordRaw) => {
accessedRouters.concat(page404).forEach((item: RouteRecordRaw) => { if (router.hasRoute(item.name)) {
router.addRoute(item); router.removeRoute(item.name);
}
}); });
}, },
async restore() { async restore() {
this.routers.concat(page404).forEach((item: RouteRecordRaw) => { this.removeRoutes.forEach((item: RouteRecordRaw) => {
if (router.hasRoute(item.name)) router.removeRoute(item.name); router.addRoute(item);
}); });
this.routers = [];
}, },
}, },
}); });

View File

@ -57,7 +57,7 @@ export const useUserStore = defineStore('user', {
if (token === 'main_token') { if (token === 'main_token') {
return { return {
name: 'td_main', name: 'td_main',
roles: ['ALL_ROUTERS'], roles: ['all'],
}; };
} }
return { return {