@@ -22,7 +26,7 @@
@@ -98,7 +102,9 @@ import { submitExperiment, getScore } from "@/api";
import { ElMessage, ElMessageBox } from "element-plus";
const isShowExperiment =
Number(localStorage.getItem("activeStepIndex")) == 12 ? true : false;
+ const isStudy = Boolean(localStorage.getItem("isStudy")) || false;
const router = useRouter();
+const complete = localStorage.getItem("complete");
const user = userStore();
const loginFn = () => {
// user.token ? router.push("/spacePage") : "";
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/auth.ts b/src/utils/auth.ts
index 90ca42c..fd8b2af 100644
--- a/src/utils/auth.ts
+++ b/src/utils/auth.ts
@@ -14,6 +14,9 @@ const clearLocalStorage = () => {
localStorage.removeItem("product");
localStorage.removeItem("installationStep");
localStorage.removeItem("stepIds");
+ // localStorage.removeItem("wenduCode");
+ // localStorage.removeItem("shiduCode");
+ localStorage.removeItem('isStudy')
}
export {
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;
diff --git a/src/views/largeDataScreen/home.vue b/src/views/largeDataScreen/home.vue
index ccb9cd4..e553da9 100644
--- a/src/views/largeDataScreen/home.vue
+++ b/src/views/largeDataScreen/home.vue
@@ -749,6 +749,7 @@
+
返回