middle-admin-ant/src/application/mk/libs/function/apis.js

150 lines
5.6 KiB
JavaScript

import { request } from '@/utils/request' // 加载request
import modal from './modal' // 加载modal
import JSONbig from 'json-bigint'
export default {
// 提交post请求 ,获取数据
post: function ({ url, data, loading, config, useBigInt }) { // post请求
if (useBigInt) {
config = config || {};
config.headers = {
'Content-Type': 'application/json'
};
data = JSONbig.stringify(data);
}
return new Promise((resolve, reject) => { // 返回一个Promise
if (loading) { // 如果需要加载
modal.loading(loading); // 显示加载
}
request(url, 'post', data, config).then(response => { // 发送请求
if (!response) { // 如果没有返回
reject && reject(response); // 返回错误
return; // 返回
}
var result = response.data; // 获取数据
if (!result) { // 如果没有数据
reject && reject(response); // 返回错误
return; // 返回
}
if (loading) { // 如果需要加载
modal.hideLoading(); // 隐藏加载
}
/*
if (result.code != 200) { // 如果返回的状态码不是200
if (reject) { // 如果有错误回调
reject(response); // 返回错误
} else { // 如果没有错误回调
modal.error(result.msg); // 显示错误
}
return; // 返回
}
*/
resolve(result); // 返回成功
}).catch((error) => { // 如果出错
if (loading) { // 如果需要加载
modal.hideLoading(); // 隐藏加载
}
modal.error(error.toString()); // 显示错误
});
});
},
get: function ({ url, loading, config, useBigInt }) { // post请求
if (useBigInt) {
config = config || {};
config.headers = {
'Content-Type': 'application/json'
};
}
return new Promise((resolve, reject) => { // 返回一个Promise
if (loading) { // 如果需要加载
modal.loading(loading); // 显示加载
}
request(url, 'get', {}, config).then(response => { // 发送请求
if (!response) { // 如果没有返回
reject && reject(response); // 返回错误
return; // 返回
}
var result = response.data; // 获取数据
if (!result) { // 如果没有数据
reject && reject(response); // 返回错误
return; // 返回
}
if (loading) { // 如果需要加载
modal.hideLoading(); // 隐藏加载
}
resolve(result); // 返回成功
}).catch((error) => { // 如果出错
if (loading) { // 如果需要加载
modal.hideLoading(); // 隐藏加载
}
modal.error(error.toString()); // 显示错误
});
});
},
// 获取分页数据
getPagedData: function ({ url, method = 'post', data, callback, config,useBigInt }) { // 获取分页数据 默认post请求
if (useBigInt) {
config = config || {};
config.headers = {
'Content-Type': 'application/json'
};
data = JSONbig.stringify(data);
}
return new Promise((resolve, reject) => { // 返回一个Promise
if (data.start_time && typeof (data.start_time) == "string") { // 如果开始时间是字符串
data.start_time = parseInt(new Date(data.start_time).getTime() / 1000); // 转换为时间戳
}
if (data.end_time && typeof (data.end_time) == "string") { // 如果结束时间是字符串
data.end_time = parseInt(new Date(data.end_time).getTime() / 1000); // 转换为时间戳
}
request(url, method, data, config).then(response => { // 发送请求
if (!response) { // 如果没有返回
reject && reject(response); // 返回错误
return;
}
var result = response.data; // 获取数据
if (!result) { // 如果没有数据
reject && reject(response); // 返回错误
return;
}
if (result.code != 200) { // 如果返回的状态码不是200
if (reject) { // 如果有错误回调
reject(response); // 返回错误
} else { // 如果没有错误回调
modal.error(result.msg); // 显示错误
}
return;
}
// console.log(result.data)
resolve(result.data); // 返回成功
if (callback) { // 如果有回调
callback(result.data); // 执行回调
}
}).catch((error) => {
resolve({ // 返回一个空数据
total: 0, // 总数
list: [] // 列表
});
modal.error(error.toString()); // 显示错误
});
});
}
}