You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.3 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. import { createRouter, createWebHistory } from 'vue-router'
  2. import { useAuthStore } from '../stores/auth';
  3. const routes = [
  4. {
  5. path: '/',
  6. redirect: 'homePage'
  7. },
  8. {
  9. path: '/homePage',
  10. name: 'homePage',
  11. component: () => import('../views/homePage.vue'),
  12. // children: [
  13. // {name: 'AiEmotion', path: '/AiEmotion', component: () => import('@/views/AiEmotion.vue')}
  14. // ]
  15. },
  16. {
  17. path: '/choujiang',
  18. name: 'choujiang',
  19. component: () => import('../views/choujiang/index.vue'),
  20. },
  21. {
  22. path: '/zhongchou',
  23. name: 'zhongchou',
  24. component: () => import('../views/zhongchou/index.vue'),
  25. },
  26. {
  27. path: '/login',
  28. name: 'login',
  29. component: () => import('../views/choujiang/Login.vue'),
  30. }
  31. ]
  32. // 创建路由实例
  33. const router = createRouter({
  34. history: createWebHistory(import.meta.env.VITE_PUBLIC_PATH),
  35. routes
  36. })
  37. // 添加路由守卫
  38. router.beforeEach((to, from, next) => {
  39. const authStore = useAuthStore(); // 获取auth store实例
  40. // 仅对/choujiang路由进行登录验证
  41. if (to.path === '/choujiang') {
  42. if (!authStore.isLoggedIn) {
  43. // 如果未登录,重定向到登录页面
  44. next('/login');
  45. return;
  46. }
  47. }
  48. next();
  49. })
  50. // 导出
  51. export default router