import application from '@/config/application';
|
import shiro from '@/common/shiro';
|
import utils from '@/common/utils'
|
|
const deepEncode = (obj) => {
|
let cloneObj = null
|
//深拷贝之后的对象
|
if (obj && typeof (obj) != "object") {
|
//如果不是对象
|
return obj
|
}
|
cloneObj = Array.isArray(obj) ? [] : {}
|
//判断是对象还是数组
|
for (const key in obj) {
|
if (obj.hasOwnProperty(key)) {
|
if (typeof (obj[key]) == "array") {
|
cloneObj[key] = obj[key].map(deepEncode);
|
}
|
if (obj[key] && typeof (obj[key]) == "object") {
|
//判断对象的属性是不是object
|
cloneObj[key] = deepEncode(obj[key]);
|
} else {
|
cloneObj[key] = utils.encode(obj[key]);
|
}
|
}
|
}
|
return cloneObj
|
}
|
|
let REQUEST_NUM = 0;
|
let TIME_OUT;
|
const service = async (url, {data, method, handleData, interceptorCheck = true, options = {}}) => {
|
if (TIME_OUT) {
|
clearTimeout(TIME_OUT);
|
}
|
TIME_OUT = window.setTimeout(() => {
|
REQUEST_NUM = 0;
|
uni.hideLoading();
|
}, options.timeout || 5000);
|
|
if (REQUEST_NUM++ === 0) {
|
uni.showLoading({
|
mask: true,
|
title: '加载中'
|
});
|
}
|
const [_, result] = await uni.request({
|
header: {
|
Userid: shiro.getUserId(),
|
},
|
url: `${application.baseURL}${url}?userId=${shiro.getUserId()}`,
|
data: deepEncode({
|
...data,
|
token: shiro.getUserId()
|
}),
|
method
|
});
|
if (typeof result.data === 'string') {
|
result.data = JSON.parse(result.data.replace(/[\t\r\n]/g, ''));
|
}
|
console.log(result)
|
if (--REQUEST_NUM === 0) {
|
uni.hideLoading();
|
}
|
|
if (!result.data.success) {
|
if (interceptorCheck) {
|
uni.showModal({
|
title: '提示',
|
content: result.data.message,
|
showCancel: false,
|
confirmText: '确定'
|
});
|
}
|
throw '';
|
}
|
if (handleData) {
|
return handleData(result);
|
}
|
// if(url==='/rootjemin/invoice/saveInvoiceOrder'){
|
// return result.data.success ? result.data.ids : null;
|
// }
|
return result.data.success ? result.data.data : null;
|
}
|
|
export default service;
|