You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.1 KiB
88 lines
2.1 KiB
var messageFn = (msg) => {
|
|
layui.use('layer', function(){
|
|
const layer = layui.layer;
|
|
layer.msg(msg);
|
|
});
|
|
};
|
|
|
|
const BASE_PREFIX = '/thing';
|
|
// 创建实例
|
|
const tokenArr = window.location.search.split('token=');
|
|
const cacheToken = sessionStorage.getItem('v1@CacheToken');
|
|
const tokenParsed = JSON.parse(cacheToken || '{}');
|
|
const axiosInstance = axios.create({
|
|
// 前缀
|
|
baseURL: BASE_PREFIX,
|
|
// 超时
|
|
timeout: 1000 * 30,
|
|
// 请求头
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'token': tokenArr[1] || tokenParsed.token || window.iframeNodeToken || '',
|
|
'tenantCode': localStorage.getItem('v1@CacheTenantCode') || '',
|
|
'companyId': localStorage.getItem('v1@CacheTenantCode') || '',
|
|
},
|
|
});
|
|
|
|
// 请求拦截器
|
|
axiosInstance.interceptors.request.use(
|
|
(config) => {
|
|
// TODO 在这里可以加上想要在请求发送前处理的逻辑
|
|
// TODO 比如 loading 等
|
|
return config
|
|
},
|
|
(error) => {
|
|
return Promise.reject(error)
|
|
},
|
|
);
|
|
|
|
// 响应拦截器
|
|
axiosInstance.interceptors.response.use(
|
|
(response) => {
|
|
if (response.status === 200)
|
|
return response.data
|
|
|
|
messageFn(response.status)
|
|
return response
|
|
},
|
|
(error) => {
|
|
console.log('error', error)
|
|
const { response } = error
|
|
if (response) {
|
|
if (response.status === 401) {
|
|
messageFn("未授权")
|
|
window.createLoginDialog();
|
|
} else {
|
|
messageFn(response.status)
|
|
}
|
|
return Promise.reject(response.data)
|
|
}
|
|
messageFn('网络连接异常,请稍后再试!')
|
|
return Promise.reject(error)
|
|
},
|
|
);
|
|
var service = {
|
|
get(url, data, config) {
|
|
return axiosInstance.get(url, { params: data, ...config })
|
|
},
|
|
|
|
post(url, data, config) {
|
|
return axiosInstance.post(url, data, config)
|
|
},
|
|
|
|
put(url, data, config) {
|
|
return axiosInstance.put(url, data, config)
|
|
},
|
|
|
|
delete(url, data) {
|
|
return axiosInstance.delete(url, { data })
|
|
},
|
|
|
|
upload: (url, file) =>
|
|
axiosInstance.post(url, file, {
|
|
headers: { 'Content-Type': 'multipart/form-data' },
|
|
}),
|
|
download: (url, data) => {
|
|
window.location.href = `${BASE_PREFIX}/${url}?${formatJsonToUrlParams(data)}`
|
|
},
|
|
};
|