代码优化

main
JayChou 7 days ago
parent 1a1deaa093
commit 6a0745c665
  1. 2
      .env.development
  2. 2
      src/api/user.ts
  3. 8
      src/layout/spacePage.vue
  4. 55
      src/permissions.ts
  5. 6
      src/store/modules/user.ts
  6. 67
      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 => {
console.log(res,'111');
clearLocalStorage(); 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();
} }
// next();
} else {
const token = getToken();
if (token) {
console.log(store.userInfo);
if (store.userInfo) { const authToken = getToken();
next();
} else {
store.getUserInfo().then(() => {
next();
})
.catch((error) => {
console.log(error);
clearLocalStorage() if (!authToken) {
next("/login"); return redirectToLogin(next);
})
} }
// next();
} else { if (userInfo) {
next("/login"); return next();
} }
try {
await getUserInfo();
next();
} catch (error) {
console.error("获取用户信息失败:", error);
redirectToLogin(next);
} }
}); });
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() {
try {
const res:any = await getUserInfo(); const res:any = await getUserInfo();
this.userInfo = res.result.userInfo; this.userInfo = res.result.userInfo;
console.log(res); 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;
}); });
// 创建相应拦截器
server.interceptors.response.use((response) => {
console.log(response);
if (response.data.code === 401) { // 处理错误的通用方法
const handleAuthError = (message:string) => {
const useUserStore = userStore(pinia);
ElMessage.error(response.data.message); ElMessage.error(message);
removeToken(); removeToken();
useUserStore.token = "";
router.push("/login"); router.push("/login");
};
// 创建相应拦截器
server.interceptors.response.use(
(response) => {
const { code, message } = response.data;
switch (code) {
case 401:
case 412:
handleAuthError(message);
return Promise.reject(response); return Promise.reject(response);
} case 200:
if (response.data.code === 412) { case 0:
ElMessage.error(response.data.message); return response.data;
removeToken(); default:
router.push("/login"); ElMessage.error(message);
return Promise.reject(response); return Promise.reject(response);
} }
if (response.data.code != 200 && response.data.code != 0) { },
ElMessage.error(response.data.message); (error) => {
return Promise.reject(response); 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 response.data;
});
// 暴露axios实例 return Promise.reject(error);
}
);
export default server; export default server;

Loading…
Cancel
Save