parent
1a1deaa093
commit
6a0745c665
6 changed files with 105 additions and 67 deletions
@ -1,4 +1,4 @@ |
||||
# 变量必须以 VITE_ 为前缀才能暴露给外部读取 |
||||
NODE_ENV = 'development' |
||||
VITE_APP_TITLE = '实验仿真' |
||||
VITE_APP_BASE_API = '//localhost:3100/jeecgboot' |
||||
VITE_APP_BASE_API = '//localhost:16066/jeecg-boot' |
@ -1,40 +1,41 @@ |
||||
import router from "./router"; |
||||
import userStore from "./store/modules/user"; |
||||
import { getToken } from "./utils/auth"; |
||||
import pinia from "./store"; |
||||
import {clearLocalStorage } from '@/utils/auth' |
||||
|
||||
import userStore from "./store/modules/user"; |
||||
import { getToken, clearLocalStorage } from "@/utils/auth"; |
||||
|
||||
const store = userStore(pinia); |
||||
router.beforeEach((to: any, from: any, next: any) => { |
||||
|
||||
router.beforeEach(async (to:any, from:any, next:any) => { |
||||
const { token, userInfo, getUserInfo } = store; |
||||
|
||||
if (to.path === "/login") { |
||||
if (store.token) { |
||||
next("/studyPage"); |
||||
} else { |
||||
next(); |
||||
} |
||||
// next();
|
||||
} else { |
||||
const token = getToken(); |
||||
if (token) { |
||||
console.log(store.userInfo); |
||||
|
||||
if (store.userInfo) { |
||||
next(); |
||||
} else { |
||||
store.getUserInfo().then(() => { |
||||
next(); |
||||
}) |
||||
.catch((error) => { |
||||
console.log(error); |
||||
|
||||
clearLocalStorage() |
||||
next("/login"); |
||||
}) |
||||
} |
||||
// next();
|
||||
} else { |
||||
next("/login"); |
||||
} |
||||
token ? next("/studyPage") : next(); |
||||
return; |
||||
} |
||||
|
||||
const authToken = getToken(); |
||||
|
||||
if (!authToken) { |
||||
return redirectToLogin(next); |
||||
} |
||||
|
||||
if (userInfo) { |
||||
return next(); |
||||
} |
||||
|
||||
try { |
||||
await getUserInfo(); |
||||
next(); |
||||
} catch (error) { |
||||
console.error("获取用户信息失败:", error); |
||||
redirectToLogin(next); |
||||
} |
||||
}); |
||||
|
||||
const redirectToLogin = (next: any) => { |
||||
clearLocalStorage(); |
||||
next("/login"); |
||||
}; |
||||
|
||||
export default router; |
||||
|
Loading…
Reference in new issue