vueaxios最全封装请求头配置异常状态统⼀处理loading设
置
注意点:不要直接对axios原型设置拦截,先创建axios实例: ate
然后分别⽤到:
quest.use
sponse.use
代码:
import axios from 'axios';
import router from '../router';
import db from '@/utils/localstorage';
import { MessageBox, Message, Loading } from 'element-ui';
import store from '@/store';
import { getToken } from '@/utils/auth';
var aaa = 0
let nowUrl = '';
// 创建axios实例
const service = ate({
// baseURL: v.VUE_APP_BASE_API, // url = base url + request url
// withCredentials: true, // 跨域请求时发送cookie
baseURL: config.BASE_API,
timeout: 30000 // 请求超时
});
/
/ 请求
let loadingInstance = null;
quest.use(
(config) => {
nowUrl = config.url;
// do something before request is sent
var xtoken = Item('loginToken');
config.headers['tenantPath'] = (window.location.pathname.split('/')[1] === 'sdm' ? window.location.pathname.split('/')[2] : window.location.pathname.split('/')[1]) || ''; config.headers['t'] = s.t;
if (xtoken != null) {
config.headers['X-User'] = getToken();
config.headers['System'] = 'M';
config.headers['crmversion'] = 'V1.0.0';
config.headers['channel'] = 'PC';
config.headers['Content-Type'] = 'application/json';
}
if (sponseType === 'blob') { // blob类型延长超时时间
config.timeout = 60000;
loadingInstance = Loading.service({
lock: true,
text: 'Loading',
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.7)'
});
}
if (config.url.includes('add') || config.url.includes('edit') || config.url.includes('update') || config.url.includes('create')) {
loadingInstance = Loading.service({
lock: true,
text: 'Loading',
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.7)'
});
}
if (config.url.indexOf('entrust/query/pagination')) {
config.timeout = 300000;
}
if (config.url.includes('/member/import') || config.url.includes('/dataSetRoler/import')) { // 导⼊⽂件设置请求头
config.headers['Content-Type'] = 'multipart/form-data';
}
}
return config;
},
(error) => {
// 处理请求错误
console.log(error); // for debug
ject(error);
}
);
// 响应
sponse.use(
/**
* 如果您想获得http信息,例如头信息或状态信息
* 请返回 response => response
*/
/**
* 通过⾃定义代码确定请求状态
* 这⾥只是⼀个例⼦
* 还可以通过HTTP状态代码来判断状态
*/
(response) => {
if (sponseType === 'blob') { // 下载时直接return 返回blob
loadingInstance && loadingInstance.close();
return response.data;
}
if (fig.url.includes('add') || fig.url.includes('edit') || fig.url.includes('update') || fig.url.includes('create')) { loadingInstance && loadingInstance.close();
}
const res = response.data;
// 如果状态码不是0,则判断为错误。
if (res.status !== 0 && res.status !== 200) {
if (res.status == '400') {
Message.closeAll(); // 关闭之前的弹出信息
Message({
dangerouslyUseHTMLString: true,
message: ssage || 'Error',
type: 'error',
duration: 5 * 1000
});
}
// 50008: ⾮法的令牌; 50012: 其他客户端登录; 50014: 令牌过期;
if (de === 401 || de === 50012 || de === 50014) {
// 重新登陆
confirmButtonText: '重新登录',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
store.dispatch('user/resetToken').then(() => {
});
});
}
ject(res);
} else {
return res;
}
},
(error) => {
console.log('err' + sponse); // for debug
if (!sponse) {
Message({
message: '服务连接失败',
type: 'error',
duration: 5 * 1000
});
ject(error);
}
const err = JSON.sponse);
loadingInstance && loadingInstance.close();
if (nowUrl.indexOf('/menu/getList') != '-1') {error parse new
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论