| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import { Router, useRoute } from 'vue-router'
- import useUserInfo from "@/stores/modules/user";
- import tokenInfo from "@/stores/modules/token";
- const route = useRoute()
- import pinia from "@/stores/index";
- /**
- * 除了注册页,当没有 token 则跳转至注册页
- * @param router
- */
- export function authGuard(router: Router) {
- router.beforeEach(async (to, from, next) => {
- /* 跳过路由守卫 */
- const useUserInfoStore = useUserInfo();
- const tokenInfoStore = tokenInfo();
- const appConfig = pinia.state.value.config?.appConfig;
- if(!appConfig) return next()
- if (tokenInfoStore.getToken /* 已登录 */) {
- if(!useUserInfoStore.userInfo.id){
- await useUserInfoStore.getInfo()
- }
- // 检查企业是否过期,如果过期跳转到到期页面
- if (useUserInfoStore.isExpired && to.path !== '/photography/expired') {
- return next('/photography/expired')
- }
- return next()
- } else {
- if(to.meta.noAuth) return next()
- useUserInfoStore.updateLoginShow(true)
- }
- return next()
- })
- }
|