166 lines
3.8 KiB
JavaScript
166 lines
3.8 KiB
JavaScript
const {extend} = require('umi-request');
|
|
const Storage = require('../utils/Storage');
|
|
const Common = require('../utils/Common');
|
|
const crypto = require("../utils/Crypto");
|
|
|
|
const codeMessage = {
|
|
200: "服务器成功返回请求的数据。",
|
|
201: "新建或修改数据成功。",
|
|
202: "一个请求已经进入后台排队(异步任务)。",
|
|
204: "删除数据成功。",
|
|
400: "发出的请求有错误,服务器没有进行新建或修改数据的操作。",
|
|
401: "用户没有权限(令牌、用户名、密码错误)。",
|
|
403: "用户得到授权,但是访问是被禁止的。",
|
|
404: "发出的请求针对的是不存在的记录,服务器没有进行操作。",
|
|
406: "请求的格式不可得。",
|
|
410: "请求的资源被永久删除,且不会再得到的。",
|
|
422: "当创建一个对象时,发生一个验证错误。",
|
|
500: "服务器发生错误,请检查服务器。",
|
|
502: "网关错误。",
|
|
503: "服务不可用,服务器暂时过载或维护。",
|
|
504: "网关超时。",
|
|
};
|
|
|
|
/**
|
|
* 异常处理程序
|
|
*/
|
|
const errorHandler = (error) => {
|
|
const {response = {}} = error;
|
|
let res = {code: -1, message: "参数错误或服务器异常"};
|
|
if (!Common.isEmpty(res)) {
|
|
res = {code: response.status, message: codeMessage[response.status] || response.statusText};
|
|
}
|
|
return res;
|
|
};
|
|
|
|
/**
|
|
* 配置request请求时的默认参数
|
|
*/
|
|
const request = extend({
|
|
errorHandler, // 默认错误处理
|
|
credentials: "include", // 默认请求是否带上cookie,
|
|
requestType: "json", // 表单提交post请求
|
|
});
|
|
|
|
|
|
/**
|
|
* 请求配置
|
|
* @type {{encryptBody: boolean}}
|
|
*/
|
|
let httpConfig = {
|
|
encryptBody: true,
|
|
}
|
|
|
|
|
|
/**
|
|
* 通用数据处理
|
|
* @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) => {
|
|
if (res.code === 200) {
|
|
let data = res.data;
|
|
if (res.encrypt) {
|
|
data = crypto.decrypt(data);
|
|
data = Common.parseJSON(data, data);
|
|
}
|
|
resolve(data)
|
|
} else {
|
|
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 = {}) {
|
|
|
|
let newData = {}
|
|
if (httpConfig.encryptBody) {
|
|
newData.encryptData = crypto.encrypt(JSON.stringify(data));
|
|
} else {
|
|
newData = {...data};
|
|
}
|
|
|
|
return req(url, {method: 'POST', data: newData});
|
|
}
|
|
|
|
/**
|
|
* 表单提交
|
|
* @param url
|
|
* @param data
|
|
* @returns {Promise<unknown>}
|
|
*/
|
|
async function form(url, data = {}) {
|
|
return req(url, {method: 'POST', data, requestType: 'form'});
|
|
}
|
|
|
|
/**
|
|
* 网络请求配置
|
|
* @param obj
|
|
*/
|
|
function setConfig(obj) {
|
|
httpConfig = obj;
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
req,
|
|
get,
|
|
post,
|
|
form,
|
|
setConfig,
|
|
}
|