|
|
/*
|
|
|
* @Author: ch
|
|
|
* @Date: 2022-03-17 19:15:10
|
|
|
* @LastEditors: ch
|
|
|
* @LastEditTime: 2022-03-22 17:30:38
|
|
|
* @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
|
|
|
}
|
|
|
|
|
|
// 工具类的文件需要把文件提供的工具类统一放最下方做一个统一输出
|
|
|
export {
|
|
|
// async await 标识结果处理
|
|
|
ToAsyncAwait,
|
|
|
// 防抖函数
|
|
|
Debounce,
|
|
|
// 判断是否为手机号
|
|
|
IsPhone,
|
|
|
// 判断是否为数字
|
|
|
IsNumber,
|
|
|
// 判断是否为整数
|
|
|
IsInteger,
|
|
|
// 判断是否double或float
|
|
|
IsDouble
|
|
|
} |