perf:发布版本1.0.2
This commit is contained in:
89
src/https/HttpUtil.js
Normal file
89
src/https/HttpUtil.js
Normal file
@@ -0,0 +1,89 @@
|
||||
import request from 'umi-request';
|
||||
import Storage from '../utils/Storage';
|
||||
import Common from '../utils/Common';
|
||||
|
||||
/**
|
||||
* 通用数据处理
|
||||
* @param options
|
||||
*/
|
||||
const dealParamsBody = (options) => {
|
||||
// 存在分页器的时候
|
||||
if (options.params?.pagination) {
|
||||
options.params.length = options.params.pagination.pageSize;
|
||||
options.params.start = options.params.length * (options.params.pagination.current - 1);
|
||||
delete options.params['pagination'];
|
||||
}
|
||||
|
||||
// equals 处理
|
||||
if (options.params && options.params.equals) {
|
||||
let ids = [];
|
||||
let equalsObj = options.params.equals;
|
||||
Object.keys(equalsObj).map((key) => {
|
||||
if (!Common.isEmpty(equalsObj[key])) {
|
||||
ids.push(key + '=' + equalsObj[key]);
|
||||
}
|
||||
});
|
||||
options.params.equals = ids.join(',');
|
||||
}
|
||||
|
||||
return options;
|
||||
};
|
||||
|
||||
/**
|
||||
* 发起请求
|
||||
* @param url
|
||||
* @param options
|
||||
* @returns {Promise<<any>>}
|
||||
*/
|
||||
async function req(url, options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
request(url, {
|
||||
...dealParamsBody(options),
|
||||
headers: {
|
||||
...options.headers,
|
||||
token: Storage.getUserToken() || '',
|
||||
},
|
||||
})
|
||||
.then((res) => {
|
||||
res.code == 200 ? resolve(res.data) : reject(res);
|
||||
})
|
||||
.catch((err) => {
|
||||
reject({code: err.data.status, message: err.data.error});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* get请求
|
||||
* @param url
|
||||
* @param params
|
||||
*/
|
||||
async function get(url, params = {}) {
|
||||
return req(url, {method: 'GET', params});
|
||||
}
|
||||
|
||||
/**
|
||||
* post请求
|
||||
* @param url
|
||||
* @param data
|
||||
*/
|
||||
async function post(url, data = {}) {
|
||||
return req(url, {method: 'POST', data});
|
||||
}
|
||||
|
||||
/**
|
||||
* 表单提交
|
||||
* @param url
|
||||
* @param data
|
||||
* @returns {Promise<unknown>}
|
||||
*/
|
||||
async function form(url, data = {}) {
|
||||
return req(url, {method: 'POST', data, requestType: 'form'});
|
||||
}
|
||||
|
||||
export default {
|
||||
req,
|
||||
get,
|
||||
post,
|
||||
form,
|
||||
};
|
||||
Reference in New Issue
Block a user