import type { NavigationGuardNext, RouteLocationNormalized } from 'vue-router' import { getToken } from '@/libs/token' import { createWebSocket } from 'libs/socket' import { createRouter, createWebHashHistory } from 'vue-router' import routes from './routes' const wsClient = createWebSocket() const router = createRouter({ history: createWebHashHistory(), routes, }) router.beforeEach((to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => { if (getToken() && wsClient.isConnected) { next() } else { // 未登录 if (to.name === 'login') { next() } else { next({ name: 'login' }) } } }) export default router