diff --git a/src/api/user.ts b/src/api/user.ts index 0b83cd4..8df8add 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: 'PUT', + }) +} // 获取用户信息 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..0efe89a 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..571d1d2 100644 --- a/src/permissions.ts +++ b/src/permissions.ts @@ -1,32 +1,41 @@ import router from "./router"; -import userStore from "./store/modules/user"; -import { getToken } from "./utils/auth"; import pinia from "./store"; + +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(); - }); - } - // 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/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..0505c8c 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -5,45 +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) => { - if (response.data.code === 401) { - const useuserStore = userStore(pinia); +server.interceptors.response.use( + (response) => { + const { code, message } = response.data; - // 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); - return Promise.reject(response); - } - if (response.data.code === 412) { - ElMessage.error(response.data.message); - removeToken(); - router.push("/login"); - return Promise.reject(response); - } - return response.data; -}); + 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 || "请求出错,请稍后再试"); + } + } -// 暴露axios实例 + return Promise.reject(error); + } +); export default server;