perf:发布版本1.0.2
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
/.idea/
|
||||
/package-lock.json
|
||||
|
||||
1
.npmrc
Normal file
1
.npmrc
Normal file
@@ -0,0 +1 @@
|
||||
@tiesheng:registry=http://git.kepai365.com/api/v4/packages/npm/
|
||||
10
index.js
10
index.js
@@ -1,3 +1,9 @@
|
||||
export const hello = () => {
|
||||
console.log("Hello world,1.0.1");
|
||||
import HttpUtil from "./src/https/HttpUtil";
|
||||
import Common from "./src/utils/Common";
|
||||
import Storage from "./src/utils/Storage";
|
||||
|
||||
export default {
|
||||
HttpUtil,
|
||||
Common,
|
||||
Storage,
|
||||
}
|
||||
|
||||
13
package.json
13
package.json
@@ -1,8 +1,9 @@
|
||||
{
|
||||
"name": "npm-tool",
|
||||
"version": "1.0.1",
|
||||
"name": "@tiesheng/npm-tool",
|
||||
"version": "1.0.2",
|
||||
"description": "npm tool package",
|
||||
"main": "index.js",
|
||||
"type": "commonjs",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
@@ -15,5 +16,11 @@
|
||||
"tool"
|
||||
],
|
||||
"author": "zeng_wenhao",
|
||||
"license": "ISC"
|
||||
"license": "ISC",
|
||||
"publishConfig": {
|
||||
"@tiesheng:registry": "http://git.kepai365.com/api/v4/projects/417/packages/npm/"
|
||||
},
|
||||
"dependencies": {
|
||||
"umi-request": "1.4.0"
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
};
|
||||
97
src/utils/Common.js
Normal file
97
src/utils/Common.js
Normal file
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* 获取Url中的参数
|
||||
*/
|
||||
const getParamFormUrl = (key, host) => {
|
||||
let arr;
|
||||
let reg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)');
|
||||
try {
|
||||
let testHost = window.location.href;
|
||||
if (host) {
|
||||
testHost = host;
|
||||
}
|
||||
if ((arr = testHost.split('?')[1].match(reg))) {
|
||||
return decodeURI(arr[2]);
|
||||
}
|
||||
} catch (e) {
|
||||
}
|
||||
return '';
|
||||
};
|
||||
|
||||
/**
|
||||
* 是否为空
|
||||
* @param value
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export const isEmpty = (value) => {
|
||||
return value === undefined || value === null || value === '';
|
||||
};
|
||||
|
||||
/**
|
||||
* 转换为json
|
||||
* @param value
|
||||
* @param def
|
||||
*/
|
||||
export const parseJSON = (value, def = {}) => {
|
||||
let obj;
|
||||
try {
|
||||
obj = JSON.parse(value);
|
||||
} catch (e) {
|
||||
}
|
||||
if (!obj) {
|
||||
obj = def;
|
||||
}
|
||||
return obj;
|
||||
};
|
||||
|
||||
/**
|
||||
* 分割对象
|
||||
* @param obj
|
||||
* @param seq
|
||||
*/
|
||||
export const split = (obj, seq = ',') => {
|
||||
if (obj) {
|
||||
return obj.split(seq);
|
||||
}
|
||||
return [];
|
||||
};
|
||||
|
||||
/**
|
||||
* 判断是否是开发环境
|
||||
* @param key
|
||||
* @param url
|
||||
*/
|
||||
export const isDevelopment = () => {
|
||||
return process.env.NODE_ENV === 'development';
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* 替换全部字符
|
||||
* @param string
|
||||
* @param s1
|
||||
* @param s2
|
||||
*/
|
||||
const replaceAll = (string, s1, s2) => {
|
||||
return string.replace(new RegExp(s1, 'gm'), s2);
|
||||
};
|
||||
|
||||
/**
|
||||
* 结尾
|
||||
* @param string
|
||||
* @param endStr
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export const endWith = (string = '', endStr) => {
|
||||
let d = string.length - endStr.length;
|
||||
return d >= 0 && string.lastIndexOf(endStr) == d;
|
||||
};
|
||||
|
||||
export default {
|
||||
getParamFormUrl,
|
||||
isEmpty,
|
||||
parseJSON,
|
||||
split,
|
||||
isDevelopment,
|
||||
replaceAll,
|
||||
endWith,
|
||||
};
|
||||
42
src/utils/Storage.js
Normal file
42
src/utils/Storage.js
Normal file
@@ -0,0 +1,42 @@
|
||||
import Common from "./Common";
|
||||
|
||||
///////////////////////////////////////// 用户token
|
||||
|
||||
/**
|
||||
* 保存用户token
|
||||
* @param token
|
||||
*/
|
||||
const saveUserToken = (token) => {
|
||||
save('token', token);
|
||||
};
|
||||
|
||||
const getUserToken = () => {
|
||||
return get('token') || '';
|
||||
};
|
||||
|
||||
/**
|
||||
* 保存数据
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
const save = (key, value) => {
|
||||
localStorage.setItem(key, JSON.stringify({data: value}));
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
* @param key
|
||||
* @returns {string}
|
||||
*/
|
||||
const get = (key) => {
|
||||
return Common.parseJSON(localStorage.getItem(key), {data: ""}).data;
|
||||
};
|
||||
|
||||
///////////////////////////////////////// 导出模块
|
||||
|
||||
export default {
|
||||
save,
|
||||
get,
|
||||
saveUserToken,
|
||||
getUserToken,
|
||||
};
|
||||
Reference in New Issue
Block a user