2025-11-05 16:34:40 +08:00
|
|
|
import './assets/main.css'
|
2025-11-10 11:07:48 +08:00
|
|
|
import 'ant-design-vue/dist/reset.css'
|
2025-11-05 16:34:40 +08:00
|
|
|
|
|
|
|
|
import { createApp } from 'vue'
|
|
|
|
|
import { createPinia } from 'pinia'
|
2025-11-10 11:07:48 +08:00
|
|
|
import { createRouter, createWebHistory } from 'vue-router'
|
|
|
|
|
import { routes } from 'vue-router/auto-routes'
|
2025-11-05 16:34:40 +08:00
|
|
|
|
|
|
|
|
import App from './App.vue'
|
2025-11-10 11:07:48 +08:00
|
|
|
|
|
|
|
|
const router = createRouter({
|
|
|
|
|
history: createWebHistory(import.meta.env.BASE_URL),
|
|
|
|
|
routes,
|
|
|
|
|
})
|
2025-11-05 16:34:40 +08:00
|
|
|
|
2026-03-18 13:53:36 +08:00
|
|
|
const pinia = createPinia()
|
2025-11-05 16:34:40 +08:00
|
|
|
const app = createApp(App)
|
|
|
|
|
|
2026-03-18 13:53:36 +08:00
|
|
|
app.use(pinia)
|
2025-11-05 16:34:40 +08:00
|
|
|
app.use(router)
|
|
|
|
|
|
2026-03-18 13:53:36 +08:00
|
|
|
// 路由守卫
|
|
|
|
|
const authWhitelist = ['/login', '/register']
|
|
|
|
|
|
|
|
|
|
router.beforeEach(async (to) => {
|
|
|
|
|
const { useUserStore } = await import('./stores/user')
|
|
|
|
|
const userStore = useUserStore()
|
|
|
|
|
|
|
|
|
|
// 白名单页面(登录/注册)
|
|
|
|
|
if (authWhitelist.includes(to.path)) {
|
|
|
|
|
if (userStore.isLoggedIn) {
|
|
|
|
|
return '/'
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 未登录跳转登录页
|
|
|
|
|
if (!userStore.isLoggedIn) {
|
|
|
|
|
return `/login?redirect=${to.fullPath}`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 已登录但未获取用户信息
|
|
|
|
|
if (!userStore.user) {
|
|
|
|
|
try {
|
|
|
|
|
await userStore.fetchCurrentUser()
|
|
|
|
|
} catch {
|
|
|
|
|
userStore.logout()
|
|
|
|
|
return '/login'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
|
2025-11-05 16:34:40 +08:00
|
|
|
app.mount('#app')
|