import axios from 'axios';
import qs from 'qs';
import vMessage from '@/components/messageTips';
const service = axios.create({
});
service.interceptors.request.use(
async config => {
if (config.url.indexOf('login') > 0) {
let tenantToken = localStorage.getItem('tenant_token');
const tenantId = localStorage.getItem('tenantId');
const userId = localStorage.getItem('userId');
const expTime = localStorage.getItem('exp_time');
const refreshTime = localStorage.getItem('refresh_time');
if (expTime !== 'permanent') {
let nowTime = '';
const [err, res] = await to(axios.head(`${window.projectConfig.asiaInfoUrl}/tenant/time`));
if (!err && res) {
nowTime = new Date(res.headers.date).getTime();
if (nowTime > expTime - refreshTime && nowTime < expTime) {
const [err2, res2] = await asyncPost('tenant/t_refresh_token', qs.stringify({
tenant_id: tenantId,
user_id: userId,
token: tenantToken
}));
if (!err2 && res2) {
tenantToken = res2.data.result.tenant_refreshToken;
localStorage.setItem('tenant_token', tenantToken);
localStorage.setItem('exp_time', res2.data.result.exp_time);
}
}
}
} else {
}
config.headers.tenant_token = tenantToken;
}
return config;
},
err => {
console.log(err);
}
);
service.interceptors.response.use(
response => {
if (response.status === 200) {
return response;
} else {
}
},
err => {
if (err.request.status === 510) {
vMessage.error('token校验异常,请重新登录');
localStorage.clear();
window.location = 'login';
} else {
return Promise.reject(err);
}
}
);
const formatUrl = url => {
let returnUrl = '';
if (url.startsWith('http://') || url.startsWith('https://')) {
returnUrl = url;
} else {
if (url.startsWith('/')) {
console.error(`url=${url},此url错误,请勿以'/'开头,`);
returnUrl = 'error';
} else {
returnUrl = `${window.projectConfig.asiaInfoUrl}/${url}`;
}
}
return returnUrl;
};
const getPromise = (url, params, config) => {
const newUrl = formatUrl(url);
if (newUrl === 'error') {
return;
}
const obj = Object.assign({
url: newUrl,
method: 'get',
params
}, config);
return service(obj);
};
const asyncGet = (url, params, config, errorExt) => {
const promise = getPromise(url, params, config);
return to(promise, errorExt);
};
const downloadPromise = (url, params, config) => {
const newUrl = formatUrl(url);
if (newUrl === 'error') {
return;
}
const obj = Object.assign({
url: newUrl,
method: 'get',
params,
responseType: 'arraybuffer'
}, config);
return service(obj);
};
const asyncDownload = (url, params, config, errorExt) => {
const promise = downloadPromise(url, params, config);
return to(promise, errorExt);
};
const postPromise = (url, data, config) => {
const newUrl = formatUrl(url);
if (newUrl === 'error') {
return;
}
const obj = Object.assign({
url: newUrl,
method: 'post',
data
}, config);
return service(obj);
};
const asyncPost = (url, data, config, errorExt) => {
const promise = postPromise(url, data, config);
return to(promise, errorExt);
};
const putPromise = (url, data, config) => {
const newUrl = formatUrl(url);
if (newUrl === 'error') {
return;
}
const obj = Object.assign({
url: newUrl,
method: 'put',
data
}, config);
return service(obj);
};
const asyncPut = (url, data, config, errorExt) => {
const promise = putPromise(url, data, config);
return to(promise, errorExt);
};
const deletePromise = (url, params, config) => {
const newUrl = formatUrl(url);
if (newUrl === 'error') {
return;
}
const obj = Object.assign({
url: newUrl,
method: 'delete',
params
}, config);
return service(obj);
};
const asyncDelete = (url, params, config, errorExt) => {
const promise = deletePromise(url, params, config);
return to(promise, errorExt);
};
const readLocalFile = (url, params, config, errorExt) => {
const obj = Object.assign({
url: url,
method: 'get',
params
}, config);
return to(service(obj), errorExt);
};
const to = (promise, errorExt = '未知错误') => {
return promise
.then(function (data) {
return [null, data];
})
.catch(function (err) {
if (errorExt) {
Object.assign(err, { errorExt });
}
if (err.code === 'ECONNABORTED' && err.message.indexOf('timeout') !== -1) {
vMessage.error(`请求超时 [ 错误编码:${err.request.status} ]`);
} else if (err.message.indexOf('Network Error') !== -1) {
vMessage.error(`网络异常 [ 错误编码:${err.request.status} ]`);
} else if (err.request.status === 403) {
vMessage.error(`无访问权限 [ 错误编码:${err.request.status} ]`);
} else if (err.request.status === 404) {
vMessage.error(`请求资源不存在 [ 错误编码:${err.request.status} ]`);
} else if (err.request.status === 460) {
vMessage.error(`token错误 [ 错误编码:${err.request.status} ]`);
} else {
if (err.request.status !== undefined) {
vMessage.error(`系统繁忙,请稍后再试 [ 错误编码:${err.request.status} ]`);
} else {
vMessage.error('系统繁忙,请稍后再试');
}
}
return [err, undefined];
});
};
const handleStream = function (res, fileName) {
const blob = new Blob([res], { type: 'application/octet-stream' });
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(blob, fileName);
} else {
const URL = window.URL || window.webkitURL;
const objectUrl = URL.createObjectURL(blob);
const a = document.createElement('a');
if (typeof a.download === 'undefined') {
window.location = objectUrl;
} else {
a.href = objectUrl;
a.download = fileName;
document.body.appendChild(a);
a.click();
a.remove();
}
}
};
const exportObj = {
service,
to,
getPromise,
postPromise,
putPromise,
deletePromise,
asyncGet,
asyncPost,
asyncPut,
asyncDelete,
asyncDownload,
handleStream,
readLocalFile
};
export default exportObj;