Files
jdt-mer/src/router/guard/permission-guard.js
2023-10-10 15:16:06 +08:00

24 lines
626 B
JavaScript

import { getToken, refreshAccessToken, isNullOrWhitespace } from '@/utils'
const WHITE_LIST = ['/login', '/404']
export function createPermissionGuard(router) {
router.beforeEach(async (to) => {
const token = getToken()
/** 没有token的情况 */
if (isNullOrWhitespace(token)) {
if (WHITE_LIST.includes(to.path)) return true
return { path: 'login', query: { ...to.query, redirect: to.path } }
}
/** 单点登录的情况 */
if (to.query.tk) return true
/** 有token的情况 */
if (to.path === '/login') return { path: '/' }
refreshAccessToken()
return true
})
}