1.添加默认的请求头,包括登录验证和普通的请求验证。
2.支持导出功能。
3.get方法,请求参数做encodeURIComponent。
4.response可根据约定好的code 跳转的指定页面(token过期后跳转到登录页面)
5.根据返回结果的content-type判断接口返回是否为‘application/octet-stream’,实现文件下载
api.js
/**jshint esversion: 6 */
import axios from 'axios';
import globalConfig from './http.js'; //全局配置信息
import router from '@/router/router.js';
axios.defaults.timeout = 60000; // 超时时间
axios.defaults.baseURL = globalConfig.apiUrl; // 默认地址
axios.defaults.transformRequest = function (data) {
data = JSON.stringify(data);
return data;
};
// 路由请求拦截
// http request 拦截器
axios.interceptors.request.use(
config => {
let url = config.url;
// get参数编码
config.headers['Content-Type'] = 'application/json;charset=UTF-8';
//已经和后台约定,导出的接口参数中包含'export',标记为导出接口
//设置返回格式为blob
if (config && config.params && config.params.export) {
config.responseType = 'blob';
}
//get方法转义
if