You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.7 KiB
JavaScript

6 months ago
import router from './router'
import Performance from '@/tools/performance'
import { usePermissionStore } from "@/store/permission"
import { useUserStore } from "@/store/user"
export function usePermission() {
let end = null
const whiteList = ['/login'] // 不重定向白名单
router.beforeEach(async (to, from, next) => {
const { GenerateRoutes, routers } = usePermissionStore()
const { GetUserInfo, token, roles, logOut } = useUserStore()
end = Performance.startExecute(`${from.path} => ${to.path} 路由耗时`) /// 路由性能监控
if (token) {
if (to.path === '/login') {
next({ path: '/' })
} else {
const hasRoles = roles && roles.length > 0;
if (hasRoles && routers && routers.length > 0) {
next()
} else {
try {
const roles = await GetUserInfo()
const accessRoutes = await GenerateRoutes(roles)
accessRoutes.forEach(item => {
router.addRoute(item)
})
next({ ...to, replace: true })
} catch (error) {
await logOut()
console.error(error)
next('/login')
}
}
}
} else {
if (whiteList.includes(to.path)) {
next()
} else {
next('/login')
}
}
setTimeout(() => {
end()
}, 0)
})
router.afterEach(() => { })
}