|
|
|
|
/*
|
|
|
|
|
* @Author: ch
|
|
|
|
|
* @Date: 2022-03-17 19:15:10
|
|
|
|
|
* @LastEditors: ch
|
|
|
|
|
* @LastEditTime: 2022-04-28 16:53:27
|
|
|
|
|
* @Description: 一些无法归类的公共方法容器
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理async await 标识结果处理
|
|
|
|
|
*
|
|
|
|
|
* @param {*} promise promise对象
|
|
|
|
|
* @param {*} isFromatResult 是否处理结果
|
|
|
|
|
* @returns {error,result} error有错为错误对象,没错为null , result正确的返回结果
|
|
|
|
|
* isFromatResult 为false时直接返回promise对象,不做任何处理
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
const ToAsyncAwait = (promise, isFromatResult = true) => {
|
|
|
|
|
if(!isFromatResult){
|
|
|
|
|
return promise;
|
|
|
|
|
}else{
|
|
|
|
|
return promise.then((res) => ({error:null,result:res})).catch((err) => ({error:err,result:null}));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 防抖函数
|
|
|
|
|
* 首次运行时把定时器赋值给一个变量, 第二次执行时,
|
|
|
|
|
* 如果间隔没超过定时器设定的时间则会清除掉定时器,
|
|
|
|
|
* 重新设定定时器, 依次反复, 当我们停止下来时,
|
|
|
|
|
* 没有执行清除定时器, 超过一定时间后触发回调函数。
|
|
|
|
|
*/
|
|
|
|
|
const Debounce = (fn, delay) => {
|
|
|
|
|
let timer
|
|
|
|
|
return function() {
|
|
|
|
|
const that = this
|
|
|
|
|
const _args = arguments // 存一下传入的参数
|
|
|
|
|
if (timer) {
|
|
|
|
|
clearTimeout(timer)
|
|
|
|
|
}
|
|
|
|
|
timer = setTimeout(function() {
|
|
|
|
|
fn.apply(that, _args)
|
|
|
|
|
}, delay)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 匹配phone
|
|
|
|
|
*/
|
|
|
|
|
const IsPhone = (str) => /^(1[3-9]\d{9})$/.test(str);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断数值类型,包括整数和浮点数
|
|
|
|
|
*/
|
|
|
|
|
const IsNumber = (str) => (isDouble(str) || isInteger(str)) ? true : false;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 匹配integer
|
|
|
|
|
*/
|
|
|
|
|
const IsInteger = (str) => {
|
|
|
|
|
if (str == null || str == "") return false
|
|
|
|
|
var result = str.match(/^[-\+]?\d+$/)
|
|
|
|
|
if (result == null) return false
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 匹配double或float
|
|
|
|
|
*/
|
|
|
|
|
const IsDouble = (str) => {
|
|
|
|
|
if (str == null || str == "") return false
|
|
|
|
|
var result = str.match(/^[-\+]?\d+(\.\d+)?$/)
|
|
|
|
|
if (result == null) return false
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* 时间格式化
|
|
|
|
|
* @param {number|string|Date} d 时间参数能被new Date识别的数字,字符串,日期
|
|
|
|
|
* @param {string} fmt 时间格式参数 字符串类型 默认'yyyy/mm/dd'
|
|
|
|
|
*/
|
|
|
|
|
const formatDate = (date, fmt = 'yyyy/mm/dd' ) =>{
|
|
|
|
|
// 处理不识别的时间表示字符串,如2020年01月01日00时00分00秒
|
|
|
|
|
if(date.constructor === String){
|
|
|
|
|
date = date.replace(/\D+/ig,'/');
|
|
|
|
|
let arr = date.split('/');
|
|
|
|
|
// 长度大于3说明带了时分秒信息 特殊时间处理格式
|
|
|
|
|
if(arr.length > 3){
|
|
|
|
|
let time = ` ${arr[3]}:${arr[4]}:${arr[5]}`
|
|
|
|
|
arr.length = 3;
|
|
|
|
|
date = arr.join('/') + time;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
try{
|
|
|
|
|
date = date ? date.constructor === Date ? date : new Date(date) : new Date();
|
|
|
|
|
} catch(e){
|
|
|
|
|
throw new Error('不能识别的时间格式');
|
|
|
|
|
}
|
|
|
|
|
const o = {
|
|
|
|
|
'm+': date.getMonth() + 1, //月份
|
|
|
|
|
'd+': date.getDate(), //日
|
|
|
|
|
'h+': date.getHours(), //小时
|
|
|
|
|
'i+': date.getMinutes(), //分
|
|
|
|
|
's+': date.getSeconds(), //秒ji“
|
|
|
|
|
'q+': Math.floor((date.getMonth() + 3) / 3), //季度
|
|
|
|
|
'l+': date.getMilliseconds() //毫秒
|
|
|
|
|
};
|
|
|
|
|
if (/(y+)/i.test(fmt)) {
|
|
|
|
|
fmt = fmt.replace(RegExp.$1, (date.getFullYear().toString()).substr(4 - RegExp.$1.length));
|
|
|
|
|
}
|
|
|
|
|
for (let k in o) {
|
|
|
|
|
if (new RegExp(`(${k})`, 'i').test(fmt)) {
|
|
|
|
|
const str = o[k].toString();
|
|
|
|
|
fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : (`0${str}`).substr(str.length-1));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fmt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const createUUID = (len,radix) => {
|
|
|
|
|
const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
|
|
|
|
|
let uuid = [],
|
|
|
|
|
i=0;
|
|
|
|
|
radix = radix || chars.length;
|
|
|
|
|
if(len){
|
|
|
|
|
for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
|
|
|
|
|
}else{
|
|
|
|
|
let r = 0;
|
|
|
|
|
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
|
|
|
|
|
uuid[14] = '4';
|
|
|
|
|
for (i = 0; i < 36; i++) {
|
|
|
|
|
if (!uuid[i]) {
|
|
|
|
|
r = 0 | Math.random()*16;
|
|
|
|
|
uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return uuid.join('');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const toSearchJson = (search)=>{
|
|
|
|
|
search = search.replace(/\?/g,'&');
|
|
|
|
|
let searchArr = search.split('&'),
|
|
|
|
|
obj = {};
|
|
|
|
|
searchArr.forEach(i =>{
|
|
|
|
|
const me = i.split('=');
|
|
|
|
|
if(me[0]){
|
|
|
|
|
obj[me[0]] = decodeURIComponent(me[1]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
export default toSearchJson;
|
|
|
|
|
// 工具类的文件需要把文件提供的工具类统一放最下方做一个统一输出
|
|
|
|
|
export {
|
|
|
|
|
// async await 标识结果处理
|
|
|
|
|
ToAsyncAwait,
|
|
|
|
|
// 防抖函数
|
|
|
|
|
Debounce,
|
|
|
|
|
// 判断是否为手机号
|
|
|
|
|
IsPhone,
|
|
|
|
|
// 判断是否为数字
|
|
|
|
|
IsNumber,
|
|
|
|
|
// 判断是否为整数
|
|
|
|
|
IsInteger,
|
|
|
|
|
// 判断是否double或float
|
|
|
|
|
IsDouble,
|
|
|
|
|
// 时间格式化
|
|
|
|
|
formatDate,
|
|
|
|
|
createUUID,
|
|
|
|
|
toSearchJson
|
|
|
|
|
}
|