/** axios封装
|
* 请求拦截、相应拦截、错误统一处理
|
*/
|
import axios from "axios";
|
// import QS from 'qs'
|
// import store from '../store/index'
|
import router from "@/router/index";
|
import { Message } from "element-ui";
|
axios.defaults.baseURL = process.env.VUE_APP_API_URL;
|
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 => {
|
if (response.status === 200) {
|
Message.closeAll();
|
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);
|
}
|
}
|
);
|
|
/**
|
* get方法,对应get请求
|
* @param {String} url [请求的url地址]
|
* @param {Object} params [请求时携带的参数]
|
*/
|
export function get(url, params) {
|
return new Promise((resolve, reject) => {
|
axios
|
.get(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(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; application/octet-stream"
|
},
|
responseType: "blob",
|
method: "post",
|
url: url,
|
params: params
|
}).then(res => {
|
if (!res || !res.data || res.code !== "00") {
|
return;
|
}
|
Message.closeAll();
|
const loading = this.$loading({
|
lock: true,
|
text: "下载中,请稍后!",
|
background: "rgba(0, 0, 0, 0.7)"
|
});
|
setTimeout(() => {
|
loading.close();
|
}, 2000);
|
var blob = new Blob([res.data], { type: "application/octet-binary" });
|
const fileDownload = require("js-file-download");
|
fileDownload(blob, `${params.fileName}.zip`);
|
});
|
}
|
export const downloadFile = (url, param) => {
|
const self = this;
|
|
axios({
|
method: "post",
|
url: url + "?v=" + Math.random(),
|
responseType: "blob",
|
headers: {
|
"Content-Type": "application/json; application/octet-stream",
|
"data-type": "Buffer"
|
},
|
params: param
|
})
|
.then(res => {
|
if (res) {
|
const fileDownload = require("js-file-download");
|
fileDownload(res.data, `${param.fileName}.zip`);
|
Message.closeAll();
|
} else {
|
setTimeout(() => {
|
self.$message.warning("下载文件失败,请稍后重试!");
|
}, 1000);
|
Message.closeAll();
|
}
|
})
|
.catch(err => {
|
self.$message.error(err);
|
});
|
};
|