|
|
@ -1,11 +1,12 @@ |
|
|
|
import { createRouter, createWebHashHistory } from 'vue-router' |
|
|
|
import Cookies from '@/utils/cookie' |
|
|
|
const Home = () => import('@/pages') |
|
|
|
const Login = () => import('@/pages/Login') |
|
|
|
|
|
|
|
// 配置路由信息
|
|
|
|
const routes = [ |
|
|
|
{ path: '/', component: Home }, |
|
|
|
{ path: '/login', component: Login }, |
|
|
|
{ path: '/', component: Home, name: 'Home' }, |
|
|
|
{ path: '/login', component: Login, name: 'Login' }, |
|
|
|
] |
|
|
|
|
|
|
|
const router = createRouter({ |
|
|
@ -13,4 +14,18 @@ const router = createRouter({ |
|
|
|
history: createWebHashHistory(), |
|
|
|
}) |
|
|
|
|
|
|
|
const isAuthenticated = Cookies.getCookie('t') |
|
|
|
|
|
|
|
router.beforeEach(async (to, from) => { |
|
|
|
if ( |
|
|
|
// 检查用户是否已登录
|
|
|
|
!isAuthenticated && |
|
|
|
// ❗️ 避免无限重定向
|
|
|
|
to.name !== 'Login' |
|
|
|
) { |
|
|
|
// 将用户重定向到登录页面
|
|
|
|
return { name: 'Login' } |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
export default router |