import axios, { AxiosRequestConfig, AxiosResponse } from "axios"; import Cookies from "js-cookie"; // axios.defaults.baseURL = ""; // 请求后台地址 axios.defaults.headers.post["Content-Type"] = "application/json;charset=utf-8"; interface LogOutCodesProps { status: number; code: number; msg?: string; } axios.interceptors.request.use( (config: AxiosRequestConfig) => { // token授权 const jwtToken: string = Cookies.get("Authorization") ?? ""; config.headers = config.headers || {}; config.headers.Authorization = `${jwtToken}`; // 缓存问题 config.headers["Cache-Control"] = "no-cache"; return config; }, (err) => { return Promise.reject(err); } ); axios.interceptors.response.use( (response: AxiosResponse) => { const xhr: any = response.data ?? {}; const { code, msg: resultMsg } = xhr; // 需要退出重登的code和msg let msg: string = ""; switch (code) { case 9: msg = "请重新登录"; break; case 18: msg = "授权过期,请重新登录"; break; case 27: msg = "未安装License或License已过期"; break; case 28: msg = "License权限不足"; break; default: break; } if (code) { // ElMessage.error(msg || resultMsg || "系统异常"); axios.prototype.error(msg || resultMsg || "系统异常"); } return xhr; }, (error) => { const response: any = error.response ?? {}; const { data, status } = response; const { code, reason } = data; let msg: string = ""; // 获取错误提示 switch (status) { case 400: msg = code === 17 ? `异地登录,登录IP为${data.data},请重新登录` : "请求错误"; break; case 401: msg = reason || "鉴权失败"; break; case 403: msg = "拒绝访问"; break; case 404: msg = "请求错误,未找到该资源"; break; case 405: msg = "请求方法未允许"; break; case 408: msg = "请求超时"; break; case 413: msg = "请求数据过多"; break; case 500: msg = "服务器端出错"; break; case 501: msg = "网络未实现"; break; case 503: msg = "服务不可用"; break; case 504: msg = "网络超时"; break; case 505: msg = "http版本不支持该请求"; break; case 502: default: break; } // ElMessage.error(msg); axios.prototype(msg); return Promise.reject(error).catch(() => { if (401 == status) { window.location.href = "/"; } }); } ); axios.prototype.error=(msg)=>{ } export default axios;