/** axios封装
|
* 请求拦截、相应拦截、错误统一处理
|
*/
|
import axios from "axios";
|
// import QS from 'qs'
|
// import store from '../store/index'
|
import router from "@/router/index";
|
import { Message } from "element-ui";
|
// console.log('VUE_PRODUCT_API_HOST', process.env, process.env.VUE_APP_PRODUCT_API_HOST)
|
let baseUrl = ''
|
if (location.hostname === "fin.pengyouph.com") {
|
baseUrl = process.env.VUE_APP_PRODUCT_XXXFINTECH_API_HOST;
|
} else if (location.host === '10.1.1.163' || location.host === '10.1.1.149') {
|
baseUrl = process.env.VUE_APP_API_ORIGIN;
|
} else {
|
baseUrl = window.location.origin;
|
}
|
axios.defaults.baseURL = baseUrl;
|
|
axios.defaults.withCredentials = true;
|
|
// 请求超时时间
|
axios.defaults.timeout = 30000;
|
|
// post请求头
|
axios.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
|
|
// 响应拦截器
|
axios.interceptors.response.use(
|
response => {
|
Message.closeAll();
|
// 您的登录已失效
|
if (typeof response.data === "object") {
|
const { errorCode, code, data, ext } = response.data;
|
const eCode = code || errorCode;
|
const eData = ext || data;
|
if (eCode === "003" && typeof eData === "object") {
|
// parent.location.reload();
|
return Promise.reject(response.data);
|
}
|
}
|
if (response.status === 200) {
|
if (response.data.code !== "00") {
|
Message({
|
message: response.data.msg,
|
type: "error",
|
duration: 6000
|
});
|
}
|
return Promise.resolve(response);
|
} else {
|
return Promise.reject(response);
|
}
|
},
|
// 服务器状态码不是200的情况
|
error => {
|
if (error.response.status) {
|
switch (error.response.status) {
|
// 401: 未登录
|
// 未登录则跳转登录页面,并携带当前页面的路径
|
// 在登录成功后返回当前页面,这一步需要在登录页操作。
|
case 401:
|
router.replace({
|
path: "/login",
|
query: {
|
redirect: router.currentRoute.fullPath
|
}
|
});
|
break;
|
// 403 token过期
|
// 登录过期对用户进行提示
|
// 清除本地token和清空vuex中token对象
|
// 跳转登录页面
|
case 403:
|
Message({
|
message: "登录过期,请重新登录",
|
type: "error",
|
duration: 6000
|
});
|
// 清除token
|
// localStorage.removeItem('token')
|
// store.commit('loginSuccess', null)
|
// 跳转登录页面,并将要浏览的页面fullPath传过去,登录成功后跳转需要访问的页面
|
setTimeout(() => {
|
router.replace({
|
path: "/login",
|
query: {
|
redirect: router.currentRoute.fullPath
|
}
|
});
|
}, 1000);
|
break;
|
// 404请求不存在
|
case 404:
|
Message({
|
message: "网络请求不存在",
|
type: "error",
|
duration: 6000
|
});
|
break;
|
// 其他错误,直接抛出错误提示
|
default:
|
error.response &&
|
error.response.data &&
|
error.response.data.message &&
|
Message({
|
message: error.response.data.message,
|
type: "error",
|
duration: 6000
|
});
|
}
|
return Promise.reject(error.response);
|
}
|
}
|
);
|
|
/**
|
* addMenuId api地址添加全局参数menuId
|
* @param {String} url 接口地址
|
*/
|
function addMenuId(url) {
|
// 本地调试使用
|
const menuId = parent.g_menu_id || "";
|
const contactStr = url.includes("?") ? "&" : "?";
|
if (url.includes("menuId=")) {
|
return url;
|
}
|
return `${url}${contactStr}menuId=${menuId}`;
|
}
|
|
/**
|
* get方法,对应get请求
|
* @param {String} url [请求的url地址]
|
* @param {Object} params [请求时携带的参数]
|
*/
|
export function get(url, params) {
|
return new Promise((resolve, reject) => {
|
axios
|
.get(addMenuId(url), {
|
params: params
|
})
|
.then(res => {
|
resolve(res.data);
|
})
|
.catch(err => {
|
reject(err.data);
|
});
|
});
|
}
|
/**
|
* post方法,对应post请求
|
* @param {String} url [请求的url地址]
|
* @param {Object} params [请求时携带的参数]
|
*/
|
export function post(url, params, responseType) {
|
return new Promise((resolve, reject) => {
|
axios
|
.post(addMenuId(url), params, responseType)
|
.then(res => {
|
resolve(res.data);
|
})
|
.catch(err => {
|
// console.log(err)
|
reject(err.data);
|
});
|
});
|
}
|
// 文件下载
|
export function downFile(url, params) {
|
return axios({
|
headers: {
|
"Content-Type": "application/json"
|
},
|
responseType: "blob", // 一定要写
|
method: "post",
|
url: url,
|
params: params
|
}).then(res => {
|
if (!res || !res.data) {
|
return;
|
}
|
Message.closeAll();
|
const blob = new Blob([res.data]); // 处理文档流
|
const elink = document.createElement("a");
|
elink.download = params.fileName;
|
elink.style.display = "none";
|
elink.href = URL.createObjectURL(blob);
|
document.body.appendChild(elink);
|
elink.click();
|
URL.revokeObjectURL(elink.href); // 释放URL 对象
|
document.body.removeChild(elink);
|
});
|
}
|