This commit is contained in:
2026-03-22 13:55:23 +08:00
parent c3f196ded4
commit 69099986e0
616 changed files with 38942 additions and 3 deletions

View File

@@ -0,0 +1,38 @@
import type { Router } from 'vue-router'
import { storeToRefs } from 'pinia'
import pinia from '@/plugins/pinia/setup'
import { useAuthStore } from '@/stores/auth'
// 需要认证的 Monisuo 路由前缀
const MONISUO_AUTH_ROUTES = ['/monisuo/']
export function authGuard(router: Router) {
router.beforeEach((to, _from) => {
const authStore = useAuthStore(pinia)
const { isLogin } = storeToRefs(authStore)
// 检查是否是需要认证的 Monisuo 路由
const isMonisuoRoute = MONISUO_AUTH_ROUTES.some(prefix => to.path.startsWith(prefix))
// 如果页面需要登录但用户未登录,重定向到登录页并记录原始目标页面
if ((to.meta.auth || isMonisuoRoute) && !unref(isLogin)) {
// Monisuo 路由重定向到 Monisuo 登录页
if (isMonisuoRoute && to.name !== '/auth/monisuo-sign-in') {
return {
name: '/auth/monisuo-sign-in',
query: { redirect: to.fullPath },
}
}
// 其他需要认证的路由重定向到默认登录页
if (to.meta.auth && to.name !== '/auth/sign-in') {
return {
name: '/auth/sign-in',
query: { redirect: to.fullPath },
}
}
}
})
}

View File

@@ -0,0 +1,25 @@
import type { Router } from 'vue-router'
import nprogress from 'nprogress'
import { authGuard } from './auth-guard'
/**
* global router guard
* now only used for progress bar
*/
function setupCommonGuard(router: Router) {
router.beforeEach(() => {
nprogress.start()
return true
})
router.afterEach(() => {
nprogress.done()
return true
})
}
export function createRouterGuard(router: Router) {
setupCommonGuard(router)
authGuard(router)
}

View File

@@ -0,0 +1,24 @@
import type { RouteRecordRaw } from 'vue-router'
import { setupLayouts } from 'virtual:generated-layouts'
import { createRouter, createWebHistory } from 'vue-router'
import { handleHotUpdate, routes } from 'vue-router/auto-routes'
import { createRouterGuard } from './guard'
const router = createRouter({
history: createWebHistory(),
routes: setupLayouts(routes as RouteRecordRaw[]),
scrollBehavior() {
return { left: 0, top: 0, behavior: 'smooth' }
},
})
createRouterGuard(router)
export default router
if (import.meta.hot) {
handleHotUpdate(router)
}