代码优化

main
JayChou 1 week ago
parent 1a1deaa093
commit 6a0745c665
  1. 2
      .env.development
  2. 2
      src/api/user.ts
  3. 10
      src/layout/spacePage.vue
  4. 65
      src/permissions.ts
  5. 12
      src/store/modules/user.ts
  6. 77
      src/utils/request.ts

@ -1,4 +1,4 @@
# 变量必须以 VITE_ 为前缀才能暴露给外部读取 # 变量必须以 VITE_ 为前缀才能暴露给外部读取
NODE_ENV = 'development' NODE_ENV = 'development'
VITE_APP_TITLE = '实验仿真' VITE_APP_TITLE = '实验仿真'
VITE_APP_BASE_API = '//localhost:3100/jeecgboot' VITE_APP_BASE_API = '//localhost:16066/jeecg-boot'

@ -27,7 +27,7 @@ export const sturegister = (data: any) => {
export const logout = () => { export const logout = () => {
return request({ return request({
url: '/sys/logout?_t=' + new Date().getTime(), url: '/sys/logout?_t=' + new Date().getTime(),
method: 'GET', method: 'PUT',
}) })
} }
// 获取用户信息 // 获取用户信息

@ -141,7 +141,7 @@
import { ElMessage } from "element-plus"; import { ElMessage } from "element-plus";
import { ref, onMounted } from "vue"; import { ref, onMounted } from "vue";
import { useRouter } from "vue-router"; import { useRouter } from "vue-router";
import settingStore from "@/store/modules/setting"; import settingStore from "@/store/modules/setting";;
const setting = settingStore(); const setting = settingStore();
import userStore from "@/store/modules/user"; import userStore from "@/store/modules/user";
import { getUserInfoService, userChangeService, logout } from "@/api/user"; import { getUserInfoService, userChangeService, logout } from "@/api/user";
@ -283,10 +283,14 @@ const handleClose = () => {
// 退 // 退
const logoutEvent = async () => { const logoutEvent = async () => {
await logout(); await logout().then(res => {
clearLocalStorage(); console.log(res,'111');
clearLocalStorage();
user.token = ''
router.push("/login"); router.push("/login");
})
}; };
</script> </script>

@ -1,40 +1,41 @@
import router from "./router"; import router from "./router";
import userStore from "./store/modules/user";
import { getToken } from "./utils/auth";
import pinia from "./store"; 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); 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 (to.path === "/login") {
if (store.token) { token ? next("/studyPage") : next();
next("/studyPage"); return;
} else { }
next();
} const authToken = getToken();
// next();
} else { if (!authToken) {
const token = getToken(); return redirectToLogin(next);
if (token) { }
console.log(store.userInfo);
if (userInfo) {
if (store.userInfo) { return next();
next(); }
} else {
store.getUserInfo().then(() => { try {
next(); await getUserInfo();
}) next();
.catch((error) => { } catch (error) {
console.log(error); console.error("获取用户信息失败:", error);
redirectToLogin(next);
clearLocalStorage()
next("/login");
})
}
// next();
} else {
next("/login");
}
} }
}); });
const redirectToLogin = (next: any) => {
clearLocalStorage();
next("/login");
};
export default router; export default router;

@ -32,9 +32,15 @@ const userStore = defineStore("userStore", {
this.userInfo = null; this.userInfo = null;
}, },
async getUserInfo() { async getUserInfo() {
const res:any = await getUserInfo(); try {
this.userInfo = res.result.userInfo; const res:any = await getUserInfo();
console.log(res); this.userInfo = res.result.userInfo;
console.log(res);
} catch (error) {
console.log(error);
}
// this.userInfo = res.result; // this.userInfo = res.result;
}, },

@ -5,44 +5,71 @@ import { ElMessage } from "element-plus";
import { removeToken } from "@/utils/auth"; import { removeToken } from "@/utils/auth";
import userStore from "@/store/modules/user"; import userStore from "@/store/modules/user";
import router from "@/router"; import router from "@/router";
// 创建axios实例 // 创建axios实例
const server = axios.create({ const server = axios.create({
baseURL: import.meta.env.VITE_APP_BASE_API, baseURL: import.meta.env.VITE_APP_BASE_API,
timeout: 30000, timeout: 30000,
}); });
// 创建请求拦截器
// 获取用户状态
// 创建请求拦截器
server.interceptors.request.use((config) => { server.interceptors.request.use((config) => {
const useuserStore = userStore(pinia); const useUserStore = userStore(pinia);
config.headers.Authorization = useuserStore.token; if (useUserStore.token) {
config.headers["x-access-token"] = useuserStore.token; config.headers.Authorization = useUserStore.token;
config.headers["x-access-token"] = useUserStore.token;
}
return config; return config;
}); });
// 处理错误的通用方法
const handleAuthError = (message:string) => {
const useUserStore = userStore(pinia);
ElMessage.error(message);
removeToken();
useUserStore.token = "";
router.push("/login");
};
// 创建相应拦截器 // 创建相应拦截器
server.interceptors.response.use((response) => { server.interceptors.response.use(
console.log(response); (response) => {
const { code, message } = response.data;
if (response.data.code === 401) { 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;
ElMessage.error(response.data.message); switch (status) {
removeToken(); case 401:
router.push("/login"); case 403:
return Promise.reject(response); case 500:
} handleAuthError(data.message);
if (response.data.code === 412) { break;
ElMessage.error(response.data.message); default:
removeToken(); ElMessage.error(data.message || "请求出错,请稍后再试");
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;
});
// 暴露axios实例 return Promise.reject(error);
}
);
export default server; export default server;

Loading…
Cancel
Save