You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.8 KiB
87 lines
2.8 KiB
/** |
|
* 权限控制 |
|
*/ |
|
import router, { findFirstValidRoute } from './router' |
|
|
|
import { isExternal } from './utils/validate' |
|
import useUserStore from './stores/modules/user' |
|
import { INDEX_ROUTE, INDEX_ROUTE_NAME } from './router/routes' |
|
import { PageEnum } from './enums/pageEnum' |
|
import useTabsStore from './stores/modules/multipleTabs' |
|
import { clearAuthInfo } from './utils/auth' |
|
import config from './config' |
|
import NProgress from 'nprogress' |
|
import 'nprogress/nprogress.css' |
|
|
|
// NProgress配置 |
|
NProgress.configure({ showSpinner: false }) |
|
|
|
const loginPath = PageEnum.LOGIN |
|
const defaultPath = PageEnum.INDEX |
|
// 免登录白名单 |
|
const whiteList: string[] = ['/login'] |
|
router.beforeEach(async (to, from, next) => { |
|
// 开始 Progress Bar |
|
NProgress.start() |
|
//document.title = to.meta.title ?? config.title |
|
const userStore = useUserStore() |
|
const tabsStore = useTabsStore() |
|
if (whiteList.includes(to.path)) { |
|
// 在免登录白名单,直接进入 |
|
next() |
|
} else if (userStore.token) { |
|
// 获取用户信息 |
|
const hasGetUserInfo = Object.keys(userStore.userInfo).length !== 0 |
|
if (hasGetUserInfo) { |
|
if (to.path === loginPath) { |
|
next({ path: defaultPath }) |
|
} else { |
|
next() |
|
} |
|
} else { |
|
try { |
|
await userStore.getUserInfo() |
|
await userStore.getMenu() |
|
const routes = userStore.routes |
|
// 找到第一个有效路由 |
|
const routeName = findFirstValidRoute(routes) |
|
// 没有有效路由跳转到403页面 |
|
if (!routeName) { |
|
clearAuthInfo() |
|
next(PageEnum.ERROR_403) |
|
return |
|
} |
|
tabsStore.setRouteName(routeName!) |
|
INDEX_ROUTE.redirect = { name: routeName } |
|
|
|
// 动态添加index路由 |
|
router.addRoute(INDEX_ROUTE) |
|
|
|
routes.forEach((route: any) => { |
|
// https 则不插入 |
|
if (isExternal(route.path)) { |
|
return |
|
} |
|
if (!route.children) { |
|
router.addRoute(INDEX_ROUTE_NAME, route) |
|
return |
|
} |
|
// 动态添加可访问路由表 |
|
router.addRoute(route) |
|
}) |
|
next({ ...to, replace: true }) |
|
} catch (err) { |
|
console.error(err) |
|
//ext({ ...to, replace: true }) |
|
clearAuthInfo() |
|
next({ path: loginPath, query: { redirect: to.fullPath } }) |
|
} |
|
} |
|
} else { |
|
next({ path: loginPath, query: { redirect: to.fullPath } }) |
|
} |
|
}) |
|
|
|
router.afterEach(() => { |
|
NProgress.done() |
|
}) |