perf:发布版本1.0.4
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@tiesheng/npm-tool",
|
||||
"version": "1.0.3",
|
||||
"version": "1.0.4",
|
||||
"description": "npm tool package",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
|
||||
@@ -4,21 +4,21 @@ 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: "网关超时。",
|
||||
200: "服务器成功返回请求的数据。",
|
||||
201: "新建或修改数据成功。",
|
||||
202: "一个请求已经进入后台排队(异步任务)。",
|
||||
204: "删除数据成功。",
|
||||
400: "发出的请求有错误,服务器没有进行新建或修改数据的操作。",
|
||||
401: "用户没有权限(令牌、用户名、密码错误)。",
|
||||
403: "用户得到授权,但是访问是被禁止的。",
|
||||
404: "发出的请求针对的是不存在的记录,服务器没有进行操作。",
|
||||
406: "请求的格式不可得。",
|
||||
410: "请求的资源被永久删除,且不会再得到的。",
|
||||
422: "当创建一个对象时,发生一个验证错误。",
|
||||
500: "服务器发生错误,请检查服务器。",
|
||||
502: "网关错误。",
|
||||
503: "服务不可用,服务器暂时过载或维护。",
|
||||
504: "网关超时。",
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -26,30 +26,30 @@ const codeMessage = {
|
||||
* @type {{prefix: string, encryptBody: boolean}}
|
||||
*/
|
||||
const defaultConfig = {
|
||||
encryptBody: true,
|
||||
base64Key: "WmdUzPJXbngVNiaSsQrihg==",
|
||||
prefix: "",
|
||||
encryptBody: true,
|
||||
base64Key: "WmdUzPJXbngVNiaSsQrihg==",
|
||||
prefix: "",
|
||||
}
|
||||
|
||||
/**
|
||||
* 异常处理程序
|
||||
*/
|
||||
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;
|
||||
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请求
|
||||
errorHandler, // 默认错误处理
|
||||
credentials: "include", // 默认请求是否带上cookie,
|
||||
requestType: "json", // 表单提交post请求
|
||||
});
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ const request = extend({
|
||||
* 获取网络请求配置
|
||||
*/
|
||||
const getHttpConfig = () => {
|
||||
return window.httpConfig || defaultConfig;
|
||||
return window.httpConfig || defaultConfig;
|
||||
}
|
||||
|
||||
|
||||
@@ -66,26 +66,26 @@ const getHttpConfig = () => {
|
||||
* @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'];
|
||||
}
|
||||
// 存在分页器的时候
|
||||
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(',');
|
||||
}
|
||||
// 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;
|
||||
return options;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -95,68 +95,71 @@ const dealParamsBody = (options) => {
|
||||
* @returns {Promise<<any>>}
|
||||
*/
|
||||
async function req(url, options) {
|
||||
if (getHttpConfig().prefix) {
|
||||
url = getHttpConfig().prefix + url;
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
request(url, {
|
||||
...dealParamsBody(options),
|
||||
headers: {
|
||||
...options.headers, token: Storage.getUserToken() || '',
|
||||
},
|
||||
if (getHttpConfig().prefix) {
|
||||
url = getHttpConfig().prefix + url;
|
||||
}
|
||||
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)
|
||||
}
|
||||
})
|
||||
.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});
|
||||
});
|
||||
});
|
||||
.catch((err) => {
|
||||
reject({code: err.data.status, message: err.data.error});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* get请求
|
||||
* @param url
|
||||
* @param params
|
||||
* @param options
|
||||
*/
|
||||
async function get(url, params = {}) {
|
||||
return req(url, {method: 'GET', params});
|
||||
async function get(url, params = {}, options = {}) {
|
||||
return req(url, {...options, method: 'GET', params});
|
||||
}
|
||||
|
||||
/**
|
||||
* post请求
|
||||
* @param url
|
||||
* @param data
|
||||
* @param options
|
||||
*/
|
||||
async function post(url, data = {}) {
|
||||
async function post(url, data = {}, options = {}) {
|
||||
|
||||
let newData = {}
|
||||
if (getHttpConfig().encryptBody) {
|
||||
newData.encryptData = crypto.encrypt(JSON.stringify(data));
|
||||
} else {
|
||||
newData = {...data};
|
||||
}
|
||||
let newData = {}
|
||||
if (getHttpConfig().encryptBody) {
|
||||
newData.encryptData = crypto.encrypt(JSON.stringify(data));
|
||||
} else {
|
||||
newData = {...data};
|
||||
}
|
||||
|
||||
return req(url, {method: 'POST', data: newData});
|
||||
return req(url, {...options, method: 'POST', data: newData});
|
||||
}
|
||||
|
||||
/**
|
||||
* 表单提交
|
||||
* @param url
|
||||
* @param data
|
||||
* @param options
|
||||
* @returns {Promise<unknown>}
|
||||
*/
|
||||
async function form(url, data = {}) {
|
||||
return req(url, {method: 'POST', data, requestType: 'form'});
|
||||
async function form(url, data = {}, options = {}) {
|
||||
return req(url, {...options, method: 'POST', data, requestType: 'form'});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -164,14 +167,14 @@ async function form(url, data = {}) {
|
||||
* @param obj
|
||||
*/
|
||||
function setConfig(obj = defaultConfig) {
|
||||
window.httpConfig = {...getHttpConfig(), ...obj};
|
||||
window.httpConfig = {...getHttpConfig(), ...obj};
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
req,
|
||||
get,
|
||||
post,
|
||||
form,
|
||||
setConfig,
|
||||
req,
|
||||
get,
|
||||
post,
|
||||
form,
|
||||
setConfig,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user