From 1a1deaa093e8fc64b2f37900f5a92314ac70d01b Mon Sep 17 00:00:00 2001 From: JayChou Date: Thu, 27 Mar 2025 16:11:54 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=80=80=E5=87=BA?= =?UTF-8?q?=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.development | 2 +- src/api/user.ts | 8 +- src/layout/loginPage.vue | 63 ++-- src/layout/spacePage.vue | 790 ++++++++++++++++++++------------------- src/permissions.ts | 10 +- src/utils/auth.ts | 13 + src/utils/request.ts | 17 +- 7 files changed, 475 insertions(+), 428 deletions(-) diff --git a/.env.development b/.env.development index 0e5f525..d94137e 100644 --- a/.env.development +++ b/.env.development @@ -1,4 +1,4 @@ # 变量必须以 VITE_ 为前缀才能暴露给外部读取 NODE_ENV = 'development' VITE_APP_TITLE = '实验仿真' -VITE_APP_BASE_API = '//localhost:16066/jeecg-boot' \ No newline at end of file +VITE_APP_BASE_API = '//localhost:3100/jeecgboot' \ No newline at end of file diff --git a/src/api/user.ts b/src/api/user.ts index 0b83cd4..f9c9d9f 100644 --- a/src/api/user.ts +++ b/src/api/user.ts @@ -23,7 +23,13 @@ export const sturegister = (data: any) => { data, }) } - +// 退出登录 +export const logout = () => { + return request({ + url: '/sys/logout?_t=' + new Date().getTime(), + method: 'GET', + }) +} // 获取用户信息 export const getUserInfoService = () => { return request({ diff --git a/src/layout/loginPage.vue b/src/layout/loginPage.vue index 295af84..dda4362 100644 --- a/src/layout/loginPage.vue +++ b/src/layout/loginPage.vue @@ -424,69 +424,67 @@ const formModel = ref({ const FacultiesList = ref([]); const getFacultiesList = async (data: any) => { - const res: any = await getFaculties(data) - console.log(res, 'res11') - if (data.primaryType == 3){ - FacultiesList.value = res.result.map((res:any) => { + const res: any = await getFaculties(data); + console.log(res, "res11"); + if (data.primaryType == 3) { + FacultiesList.value = res.result.map((res: any) => { return { value: res.id, label: res.name, - isLeaf: true - } - }) - }else{ + isLeaf: true, + }; + }); + } else { FacultiesList.value = res.result.map((res: any) => { return { value: res.id, label: res.name, - isLeaf: false - } - }) + isLeaf: false, + }; + }); } - -} -const deepValues = ref([]) +}; +const deepValues = ref([]); // 配置部门级联选择器 const props = { lazy: true, checkStrictly: true, emitPath: false, - isLeaf: 'isLeaf', + isLeaf: "isLeaf", async lazyLoad(node: any, resolve: any) { console.log(node); deepValues.value = node.pathValues; await getFacultiesList({ primaryType: node.level + 1, pid: node.value, - }) + }); const nodes = FacultiesList.value.map((item: any) => { if (!item.isLeaf) { return { value: item.value, label: item.label, isLeaf: item.isLeaf, - disabled:true - } + disabled: true, + }; } else { return { value: item.value, label: item.label, - isLeaf: item.isLeaf - } + isLeaf: item.isLeaf, + }; } - }) - console.log(nodes,'nodes'); - - resolve(nodes) + }); + console.log(nodes, "nodes"); + + resolve(nodes); }, -} -const cascader = ref(null) +}; +const cascader = ref(null); const handleChange = (e: any) => { console.log(e); - formModel.value.clssid = e - cascader.value.togglePopperVisible() -} - + formModel.value.clssid = e; + cascader.value.togglePopperVisible(); +}; // 添加校验规则 const rules = { @@ -572,11 +570,6 @@ const register = async () => { studentNumb: "", mobile: "", captcha: "", - } - ElMessage.success(`注册成功`) - isRegister.value = false - - }; ElMessage.success(`注册成功`); isRegister.value = false; diff --git a/src/layout/spacePage.vue b/src/layout/spacePage.vue index c9a9a58..984bdd5 100644 --- a/src/layout/spacePage.vue +++ b/src/layout/spacePage.vue @@ -1,204 +1,223 @@ \ No newline at end of file +// } + diff --git a/src/permissions.ts b/src/permissions.ts index a7a06f1..02c14d1 100644 --- a/src/permissions.ts +++ b/src/permissions.ts @@ -2,6 +2,8 @@ import router from "./router"; import userStore from "./store/modules/user"; import { getToken } from "./utils/auth"; import pinia from "./store"; +import {clearLocalStorage } from '@/utils/auth' + const store = userStore(pinia); router.beforeEach((to: any, from: any, next: any) => { if (to.path === "/login") { @@ -21,7 +23,13 @@ router.beforeEach((to: any, from: any, next: any) => { } else { store.getUserInfo().then(() => { next(); - }); + }) + .catch((error) => { + console.log(error); + + clearLocalStorage() + next("/login"); + }) } // next(); } else { diff --git a/src/utils/auth.ts b/src/utils/auth.ts index 11e86ff..90ca42c 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -4,8 +4,21 @@ const setToken = (token: string): void => { }; const getToken = () => localStorage.getItem(TOKEN_KEY) || ""; const removeToken = () => localStorage.removeItem(TOKEN_KEY); +const clearLocalStorage = () => { + removeToken(); + localStorage.removeItem("activeStepIndex"); + localStorage.removeItem("experimentPreservation"); + localStorage.removeItem("saveRoute"); + localStorage.removeItem("isRunCode"); + localStorage.removeItem("falg"); + localStorage.removeItem("product"); + localStorage.removeItem("installationStep"); + localStorage.removeItem("stepIds"); + +} export { setToken, getToken, removeToken, + clearLocalStorage }; diff --git a/src/utils/request.ts b/src/utils/request.ts index 7887f51..e0db2b4 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -21,18 +21,13 @@ server.interceptors.request.use((config) => { }); // 创建相应拦截器 server.interceptors.response.use((response) => { - if (response.data.code === 401) { - const useuserStore = userStore(pinia); + console.log(response); - // useuserStore.clearStatus(); - // return Promise.reject(response); - } - if (response.data.code != 200 && response.data.code != 0) { - ElMessage.error(response.data.message); - return Promise.reject(response); - } if (response.data.code === 401) { + ElMessage.error(response.data.message); + removeToken(); + router.push("/login"); return Promise.reject(response); } if (response.data.code === 412) { @@ -41,6 +36,10 @@ server.interceptors.response.use((response) => { router.push("/login"); return Promise.reject(response); } + if (response.data.code != 200 && response.data.code != 0) { + ElMessage.error(response.data.message); + return Promise.reject(response); + } return response.data; }); From 6a0745c6651c5b2864a6f05cc241a24c3c00e0ab Mon Sep 17 00:00:00 2001 From: JayChou Date: Thu, 27 Mar 2025 16:53:58 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.development | 2 +- src/api/user.ts | 2 +- src/layout/spacePage.vue | 10 +++-- src/permissions.ts | 65 +++++++++++++++---------------- src/store/modules/user.ts | 12 ++++-- src/utils/request.ts | 81 ++++++++++++++++++++++++++------------- 6 files changed, 105 insertions(+), 67 deletions(-) diff --git a/.env.development b/.env.development index d94137e..0e5f525 100644 --- a/.env.development +++ b/.env.development @@ -1,4 +1,4 @@ # 变量必须以 VITE_ 为前缀才能暴露给外部读取 NODE_ENV = 'development' VITE_APP_TITLE = '实验仿真' -VITE_APP_BASE_API = '//localhost:3100/jeecgboot' \ No newline at end of file +VITE_APP_BASE_API = '//localhost:16066/jeecg-boot' \ No newline at end of file diff --git a/src/api/user.ts b/src/api/user.ts index f9c9d9f..8df8add 100644 --- a/src/api/user.ts +++ b/src/api/user.ts @@ -27,7 +27,7 @@ export const sturegister = (data: any) => { export const logout = () => { return request({ url: '/sys/logout?_t=' + new Date().getTime(), - method: 'GET', + method: 'PUT', }) } // 获取用户信息 diff --git a/src/layout/spacePage.vue b/src/layout/spacePage.vue index 984bdd5..0efe89a 100644 --- a/src/layout/spacePage.vue +++ b/src/layout/spacePage.vue @@ -141,7 +141,7 @@ import { ElMessage } from "element-plus"; import { ref, onMounted } from "vue"; import { useRouter } from "vue-router"; -import settingStore from "@/store/modules/setting"; +import settingStore from "@/store/modules/setting";; const setting = settingStore(); import userStore from "@/store/modules/user"; import { getUserInfoService, userChangeService, logout } from "@/api/user"; @@ -283,10 +283,14 @@ const handleClose = () => { // 退出登录 const logoutEvent = async () => { - await logout(); - clearLocalStorage(); + await logout().then(res => { + console.log(res,'111'); + clearLocalStorage(); + user.token = '' router.push("/login"); + }) + }; diff --git a/src/permissions.ts b/src/permissions.ts index 02c14d1..571d1d2 100644 --- a/src/permissions.ts +++ b/src/permissions.ts @@ -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; diff --git a/src/store/modules/user.ts b/src/store/modules/user.ts index 6332d5e..eac39ae 100644 --- a/src/store/modules/user.ts +++ b/src/store/modules/user.ts @@ -32,9 +32,15 @@ const userStore = defineStore("userStore", { this.userInfo = null; }, async getUserInfo() { - const res:any = await getUserInfo(); - this.userInfo = res.result.userInfo; - console.log(res); + try { + const res:any = await getUserInfo(); + this.userInfo = res.result.userInfo; + console.log(res); + } catch (error) { + console.log(error); + + } + // this.userInfo = res.result; }, diff --git a/src/utils/request.ts b/src/utils/request.ts index e0db2b4..0505c8c 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -5,44 +5,71 @@ import { ElMessage } from "element-plus"; import { removeToken } from "@/utils/auth"; import userStore from "@/store/modules/user"; import router from "@/router"; + // 创建axios实例 const server = axios.create({ baseURL: import.meta.env.VITE_APP_BASE_API, timeout: 30000, }); -// 创建请求拦截器 +// 获取用户状态 + +// 创建请求拦截器 server.interceptors.request.use((config) => { - const useuserStore = userStore(pinia); +const useUserStore = userStore(pinia); - config.headers.Authorization = useuserStore.token; - config.headers["x-access-token"] = useuserStore.token; + if (useUserStore.token) { + config.headers.Authorization = useUserStore.token; + config.headers["x-access-token"] = useUserStore.token; + } return config; }); + +// 处理错误的通用方法 +const handleAuthError = (message:string) => { +const useUserStore = userStore(pinia); + + ElMessage.error(message); + removeToken(); + useUserStore.token = ""; + router.push("/login"); +}; + // 创建相应拦截器 -server.interceptors.response.use((response) => { - console.log(response); - - if (response.data.code === 401) { - - ElMessage.error(response.data.message); - removeToken(); - router.push("/login"); - return Promise.reject(response); - } - if (response.data.code === 412) { - ElMessage.error(response.data.message); - removeToken(); - router.push("/login"); - return Promise.reject(response); - } - if (response.data.code != 200 && response.data.code != 0) { - ElMessage.error(response.data.message); - return Promise.reject(response); - } - return response.data; -}); +server.interceptors.response.use( + (response) => { + const { code, message } = response.data; -// 暴露axios实例 + switch (code) { + case 401: + case 412: + handleAuthError(message); + return Promise.reject(response); + case 200: + case 0: + return response.data; + default: + ElMessage.error(message); + return Promise.reject(response); + } + }, + (error) => { + if (error.response) { + const { status, data } = error.response; + + switch (status) { + case 401: + case 403: + case 500: + handleAuthError(data.message); + break; + default: + ElMessage.error(data.message || "请求出错,请稍后再试"); + } + } + + return Promise.reject(error); + } +); export default server;