index.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { createRouter, createWebHistory } from 'vue-router';
  2. import type { RouteRecordRaw } from 'vue-router';
  3. import { useAuthStore } from '@/stores/auth';
  4. import { useServerStore } from '@/stores/server';
  5. const routes: RouteRecordRaw[] = [
  6. {
  7. path: '/server-config',
  8. name: 'ServerConfig',
  9. component: () => import('@/views/ServerConfig/index.vue'),
  10. meta: { requiresAuth: false },
  11. },
  12. {
  13. path: '/login',
  14. name: 'Login',
  15. component: () => import('@/views/Login/index.vue'),
  16. meta: { requiresAuth: false },
  17. },
  18. {
  19. path: '/register',
  20. name: 'Register',
  21. component: () => import('@/views/Register/index.vue'),
  22. meta: { requiresAuth: false },
  23. },
  24. {
  25. path: '/',
  26. component: () => import('@/layouts/MainLayout.vue'),
  27. meta: { requiresAuth: true },
  28. children: [
  29. {
  30. path: '',
  31. name: 'Dashboard',
  32. component: () => import('@/views/Dashboard/index.vue'),
  33. },
  34. {
  35. path: 'accounts',
  36. name: 'Accounts',
  37. component: () => import('@/views/Accounts/index.vue'),
  38. },
  39. {
  40. path: 'works',
  41. name: 'Works',
  42. component: () => import('@/views/Works/index.vue'),
  43. },
  44. {
  45. path: 'publish',
  46. name: 'Publish',
  47. component: () => import('@/views/Publish/index.vue'),
  48. },
  49. {
  50. path: 'comments',
  51. name: 'Comments',
  52. component: () => import('@/views/Comments/index.vue'),
  53. },
  54. {
  55. path: 'schedule',
  56. name: 'Schedule',
  57. component: () => import('@/views/Schedule/index.vue'),
  58. },
  59. {
  60. path: 'analytics',
  61. name: 'Analytics',
  62. redirect: '/analytics/overview',
  63. children: [
  64. {
  65. path: 'overview',
  66. name: 'AnalyticsOverview',
  67. component: () => import('@/views/Analytics/Overview/index.vue'),
  68. },
  69. {
  70. path: 'platform',
  71. name: 'AnalyticsPlatform',
  72. component: () => import('@/views/Analytics/Platform/index.vue'),
  73. },
  74. {
  75. path: 'platform-detail/:platform',
  76. name: 'AnalyticsPlatformDetail',
  77. component: () => import('@/views/Analytics/PlatformDetail/index.vue'),
  78. },
  79. {
  80. path: 'account',
  81. name: 'AnalyticsAccount',
  82. component: () => import('@/views/Analytics/Account/index.vue'),
  83. },
  84. {
  85. path: 'work',
  86. name: 'AnalyticsWork',
  87. component: () => import('@/views/Analytics/Work/index.vue'),
  88. },
  89. ],
  90. },
  91. {
  92. path: 'settings',
  93. name: 'Settings',
  94. component: () => import('@/views/Settings/index.vue'),
  95. },
  96. {
  97. path: 'profile',
  98. name: 'Profile',
  99. component: () => import('@/views/Profile/index.vue'),
  100. },
  101. ],
  102. },
  103. ];
  104. const router = createRouter({
  105. history: createWebHistory(),
  106. routes,
  107. });
  108. // 标记是否已初始化认证状态
  109. let authInitialized = false;
  110. // 路由守卫
  111. router.beforeEach(async (to, _from, next) => {
  112. const authStore = useAuthStore();
  113. const serverStore = useServerStore();
  114. // 加载服务器配置(同步操作)
  115. if (!serverStore.isConfigured) {
  116. serverStore.loadConfig();
  117. }
  118. // 检查服务器配置
  119. if (!serverStore.isConfigured && to.name !== 'ServerConfig') {
  120. next({ name: 'ServerConfig' });
  121. return;
  122. }
  123. // 首次进入需要认证的页面时,先检查认证状态
  124. if (!authInitialized && serverStore.isConfigured) {
  125. authInitialized = true;
  126. await authStore.checkAuth();
  127. }
  128. // 检查认证
  129. if (to.meta.requiresAuth && !authStore.isAuthenticated) {
  130. next({ name: 'Login' });
  131. return;
  132. }
  133. // 已登录用户不能访问登录/注册页
  134. if (authStore.isAuthenticated && ['Login', 'Register'].includes(to.name as string)) {
  135. next({ name: 'Dashboard' });
  136. return;
  137. }
  138. next();
  139. });
  140. export default router;