shop-web/src/router/guard.ts

69 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { Router } from "vue-router"
import { useKeepAliveStore } from "@/pinia/stores/keep-alive"
import { useUserStore } from "@/pinia/stores/user"
import { isWhiteList } from "@/router/whitelist"
import { useTitle } from "@@/composables/useTitle"
import { getToken } from "@@/utils/cache/cookies"
import NProgress from "nprogress"
import { useAb98UserStore } from '@/pinia/stores/ab98-user'
NProgress.configure({ showSpinner: false })
const { setTitle } = useTitle()
const LOGIN_PATH = "/ab98"
export function registerNavigationGuard(router: Router) {
// 全局前置守卫
router.beforeEach((to, _from) => {
NProgress.start()
// 企业微信登录
const urlParams = new URLSearchParams(window.location.search);
const corpid = urlParams.get('corpid') || undefined;
if (corpid) {
return true;
}
/* const isAdmin = urlParams.get('isAdmin') || undefined;
if (isAdmin) {
return true;
} */
// useAb98UserStore位置不能放在外面否则会导致路由守卫无法正常工作
const ab98UserStore = useAb98UserStore();
if (!ab98UserStore.isLogin) {
// 如果在免登录的白名单中,则直接进入
if (isWhiteList(to)) return true
// 其他没有访问权限的页面将被重定向到登录页面
return LOGIN_PATH
}
return true;
// const userStore = useUserStore()
// // 如果没有登录
// if (!getToken()) {
// // 如果在免登录的白名单中,则直接进入
// if (isWhiteList(to)) return true
// // 其他没有访问权限的页面将被重定向到登录页面
// return LOGIN_PATH
// }
// // 如果已经登录,并准备进入 Login 页面,则重定向到主页
// if (to.path === LOGIN_PATH) return "/"
// // 判断有无该页面权限
// if (to.meta.roles ? userStore.roles.some(role => to.meta.roles!.includes(role)) : true) return true
// // 无权限则进入 403 页面
// return "/403"
})
// 全局后置钩子
router.afterEach((to) => {
const keepAliveStore = useKeepAliveStore()
// 清除所有路由缓存
// if (to.path === LOGIN_PATH) keepAliveStore.delAllCachedRoutes()
// 添加路由缓存
keepAliveStore.addCachedRoute(to)
// 设置标题
setTitle(to.meta.title)
NProgress.done()
})
}