| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- // src/router/index.ts
- import { createRouter, createWebHistory } from 'vue-router'
- import { routes } from './routes'
- import { useUserStoreWithOut } from '@/stores/modules/user'
- // 创建 Router 实例
- const router = createRouter({
- history: createWebHistory(import.meta.env.BASE_URL),
- routes,
- })
- // 路由守卫
- router.beforeEach((to, from, next) => {
- const userStore = useUserStoreWithOut()
- if (to.name === 'Login' || to.meta.requiresAuth) {
- if (typeof to.query.pushRecordId === 'string') {
- userStore.setPushRecordId(to.query.pushRecordId)
- }
- if (to.name === 'Login' && to.query.from === 'reject') userStore.LogOut()
- }
- const token = userStore.hasValidSession()
- const pushRecordId = userStore.pushRecordId
- if (to.meta.requiresAuth && (!token || !pushRecordId)) {
- // 未登录时跳转登录页
- next({
- path: '/login',
- query: pushRecordId ? { pushRecordId } : {},
- })
- } else {
- next()
- }
- if (to.meta && to.meta.title) {
- document.title = to.meta.title as string
- } else {
- // 可设置默认标题
- document.title = '默认标题'
- }
- })
- // 可选:设置页面标题
- router.afterEach((to) => {
- if (to.meta.title) {
- document.title = to.meta.title as string
- }
- })
- export default router
|