import { createRouter, createWebHistory,createWebHashHistory} from 'vue-router' import { useAuthStore } from '@/stores/auth' const ModernHome = () => import('../views/ModernHome.vue') const List = () => import('../views/List.vue') const Login = () => import('../views/Login/Login.vue') const Register = () => import('../views/Register.vue') const ForgotPassword = () => import('../views/ForgotPassword.vue') const CreationWorkspace = () => import('../views/CreationWorkspace.vue') const ProjectGallery = () => import('../views/ProjectGallery.vue') const OrderManagement = () => import('../views/OrderManagement/OrderManagement.vue') const OrderDetail = () => import('../views/OrderDetail.vue') const DeviceSettings = () => import('../views/DeviceSettings.vue') const AgentManagement = () => import('../views/AgentManagement.vue') const AddAgent = () => import('../views/AddAgent.vue') const UiTest = () => import('../views/UiTest.vue') const home = () => import('../views/home/index.vue') // 路由配置 const routes = [ { path: '/', name: 'home', component: home, meta: { fullScreen: true } }, { path: '/czhome', name: 'czhome', component: ModernHome, meta: { requiresAuth: false, keepAlive: false } }, { path: '/ui-test', name: 'ui-test', component: UiTest, meta: { requiresAuth: false, keepAlive: false } }, { path: '/creation-workspace', name: 'creation-workspace', component: CreationWorkspace, meta: { requiresAuth: true, keepAlive: false } }, { path: '/project-gallery', name: 'project-gallery', component: ProjectGallery, meta: { requiresAuth: true, keepAlive: false } }, { path: '/order-management', name: 'order-management', component: OrderManagement, meta: { requiresAuth: true, keepAlive: false } }, { path: '/order-management/:orderId', name: 'order-detail', component: OrderDetail, meta: { requiresAuth: true } }, { path: '/agent-management', name: 'agent-management', component: AgentManagement, meta: { requiresAuth: true, keepAlive: false } }, { path: '/add-agent', name: 'add-agent', component: AddAgent, meta: { requiresAuth: true, fullScreen: false } }, { path: '/device-settings', name: 'device-settings', component: DeviceSettings, meta: { requiresAuth: true, keepAlive: false } }, { path: '/project/:id', name: 'project', component: () => import('../views/Project/CreateProject.vue'), meta: { requiresAuth: true, fullScreen: true } }, { path: '/purchase', name: 'model-purchase', component: () => import('../views/ModelPurchase.vue'), meta: { requiresAuth: true, fullScreen: true } }, { path: '/list', name: 'list', component: List, meta: { requiresAuth: true } }, { path: '/login', name: 'login', component: Login, meta: { requiresGuest: true } }, { path: '/register', name: 'register', component: Register, meta: { requiresGuest: true, fullScreen: true } }, { path: '/forgot-password', name: 'forgot-password', component: ForgotPassword, meta: { requiresGuest: true, fullScreen: true } }, { path: '/:pathMatch(.*)*', redirect: '/' }, ] const router = createRouter({ // history: createWebHistory(), history:createWebHashHistory(), routes, }) // 路由守卫 router.beforeEach(async (to, from, next) => { window.localStorage.setItem('token','123') return next() // 检查是否需要登录 if (to.meta.requiresAuth) { const token = localStorage.getItem('token') // 如果没有 token,跳转到登录页 if (!token) { next('/login') return } } // 检查是否需要游客身份 if (to.meta.requiresGuest) { const authStore = useAuthStore() // 如果有 token,跳转到首页 if (authStore.token) { next('/') return } } next() }) export default router