60 lines
1.2 KiB
JavaScript
60 lines
1.2 KiB
JavaScript
|
|
import { createRouter, createWebHistory } from 'vue-router'
|
||
|
|
import { useAuthStore } from '../store/auth'
|
||
|
|
|
||
|
|
// 定义路由
|
||
|
|
const routes = [
|
||
|
|
{
|
||
|
|
path: '/login',
|
||
|
|
name: 'Login',
|
||
|
|
component: () => import('../views/Login.vue'),
|
||
|
|
meta: { requiresAuth: false }
|
||
|
|
},
|
||
|
|
{
|
||
|
|
path: '/',
|
||
|
|
name: 'Layout',
|
||
|
|
component: () => import('../components/Layout/Layout.vue'),
|
||
|
|
meta: { requiresAuth: true },
|
||
|
|
children: [
|
||
|
|
{
|
||
|
|
path: '',
|
||
|
|
name: 'Home',
|
||
|
|
component: () => import('../views/Home.vue')
|
||
|
|
},
|
||
|
|
{
|
||
|
|
path: '/conversations',
|
||
|
|
name: 'ConversationList',
|
||
|
|
component: () => import('../views/ConversationList.vue')
|
||
|
|
},
|
||
|
|
{
|
||
|
|
path: '/users',
|
||
|
|
name: 'UserList',
|
||
|
|
component: () => import('../views/UserList.vue')
|
||
|
|
}
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
path: '/:pathMatch(.*)*',
|
||
|
|
redirect: '/'
|
||
|
|
}
|
||
|
|
]
|
||
|
|
|
||
|
|
// 创建路由实例
|
||
|
|
const router = createRouter({
|
||
|
|
history: createWebHistory(),
|
||
|
|
routes
|
||
|
|
})
|
||
|
|
|
||
|
|
// 路由守卫
|
||
|
|
router.beforeEach((to, from, next) => {
|
||
|
|
const authStore = useAuthStore()
|
||
|
|
|
||
|
|
if (to.meta.requiresAuth && !authStore.isLoggedIn) {
|
||
|
|
// 未登录用户访问受保护页面,重定向到登录页
|
||
|
|
next('/login')
|
||
|
|
} else {
|
||
|
|
next()
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
export default router
|