import { createRouter, createWebHashHistory } from 'vue-router' import { useUserStore } from '@/store' const routes = [{ path: '/', name: 'Home', component: () => import('../views/Home.vue') }, { path: '/aichat/:id', name: 'AIChat', component: () => import('../views/AIChat.vue') }, { path: '/Projects', name: 'Projects', component: () => import('../views/Projects.vue') }] const router = createRouter({ history: createWebHashHistory(import.meta.env.BASE_URL), routes }) router.beforeEach((to) => { // 如果没有token, 且访问的是非登录页,拦截到登录,其他情况正常放行 const userStore = useUserStore() if (!userStore.token && to.path !== '/') return '/' return true }) export default router