snack-mall/web-snack/src/permission.ts

46 lines
1.1 KiB
TypeScript

import router from '@/router'
import NProgress from 'nprogress'
import { useUserStore } from '@/stores/modules/user'
NProgress.configure({ showSpinner: false })
const WHITE_LIST = ['/login', '/404']
router.beforeEach(async (to, _from) => {
NProgress.start()
document.title = (to.meta.title as string) || '零食商城'
const userStore = useUserStore()
// 白名单放行
if (WHITE_LIST.includes(to.path)) {
if (to.path === '/login' && userStore.isLoggedIn) {
return '/'
}
return
}
// 不需要登录的公开页面(首页、分类、商品详情、搜索、公告、抢购)
const publicPaths = ['/', '/search', '/seckill', '/notice', '/products']
const isPublic =
publicPaths.includes(to.path) ||
to.path.startsWith('/category') ||
to.path.startsWith('/product') ||
to.path.startsWith('/notice') ||
to.path.startsWith('/seckill')
if (isPublic) {
return
}
// 需要登录的页面(购物车、个人中心、客服)
if (!userStore.isLoggedIn) {
NProgress.done()
return `/login?redirect=${encodeURIComponent(to.fullPath)}`
}
})
router.afterEach(() => {
NProgress.done()
})