182 lines
4.0 KiB
JavaScript
182 lines
4.0 KiB
JavaScript
|
||
export const utils = {
|
||
ColorMain: '#318AFE',
|
||
ColorRed: '#F02C45',
|
||
|
||
/**
|
||
* 限制只让输入数字
|
||
* @param {Object} e
|
||
*/
|
||
isNumber (e) {
|
||
console.log(e)
|
||
return e.replace(/[^\d]/g, '')
|
||
},
|
||
|
||
/**
|
||
* 限制金额输入格式
|
||
* @param {Object} e
|
||
*/
|
||
isMoney (e) {
|
||
return e.toString().replace(/[^0-9.]/g, '').replace(/^\./g, "").replace(".", "$#$").replace(/\./g, "").replace("$#$", ".").replace(/^(\-)*(\d+)\.(\d{0,2}).*$/, "$1$2.$3")
|
||
},
|
||
|
||
/**
|
||
* 判断菜品使用价格
|
||
* @param {Object} e
|
||
*/
|
||
isGoodsPrice(item,user) {
|
||
let isVip = uni.getStorageSync("shopInfo").isMemberPrice&&user.id&&user.isVip
|
||
|
||
let memberPrice = item.memberPrice ? item.memberPrice : item.price;
|
||
let price = isVip ? memberPrice : item.price;
|
||
price = item.discountSaleAmount ? item.discountSaleAmount : price
|
||
return price;
|
||
},
|
||
|
||
inputReg(value,key,regName){
|
||
//判断regList对象是否存在属性 避免报错
|
||
// utils.hasOwnProperty.call(utils,regName)&&(form[key]=utils[regName](value))
|
||
},
|
||
|
||
/**
|
||
* 金额保留两位小数
|
||
* @param {Object} e
|
||
*/
|
||
toFixed (price,item) {
|
||
if( item ){
|
||
if( item.productType == 'weight' || item.type == 'weight'){
|
||
return (Math.floor(price * 100)/100).toFixed(2)
|
||
} else {
|
||
return parseFloat(price).toFixed(2)
|
||
}
|
||
} else {
|
||
return parseFloat(price).toFixed(2)
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 金额向下取整
|
||
* @param {Object} e
|
||
*/
|
||
formatPrice (e) {
|
||
return (Math.floor(n*100)/100).toFixed(2)
|
||
},
|
||
|
||
/**
|
||
* 消息提示框
|
||
*/
|
||
showToast(title, duration = 1.5, extObject) {
|
||
return new Promise((resolve, reject) => {
|
||
uni.showToast(Object.assign({ title: title, icon: 'none', mask: false, duration: (duration * 1000) }, extObject))
|
||
setTimeout(resolve, (duration * 1000));
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 保留小数n位,不进行四舍五入
|
||
* num你传递过来的数字,
|
||
* decimal你保留的几位,默认保留小数后两位
|
||
*/
|
||
formatDecimal (num, decimal = 2) {
|
||
num = num.toString()
|
||
const index = num.indexOf('.')
|
||
if (index !== -1) {
|
||
num = num.substring(0, decimal + index + 1)
|
||
} else {
|
||
num = num.substring(0)
|
||
}
|
||
//截取后保留两位小数
|
||
return parseFloat(num).toFixed(decimal)
|
||
},
|
||
|
||
/**
|
||
* 分转元
|
||
* amount - 金额
|
||
* parseFloat - 是否转换为数字格式,
|
||
* 默认String
|
||
*/
|
||
cert2Dollar(amount, needParseFloat = false) {
|
||
if (needParseFloat) { // parseFlot
|
||
return formatDecimal(amount / 100)
|
||
}
|
||
return formatDecimal(amount / 100)
|
||
},
|
||
|
||
/**
|
||
* @desc 函数防抖
|
||
* @param func 目标函数
|
||
* @param wait 延迟执行毫秒数
|
||
* @param immediate true - 立即执行, false - 延迟执行
|
||
*/
|
||
debounce (func, wait = 1000, immediate = true) {
|
||
let timer;
|
||
return function() {
|
||
let context = this,
|
||
args = arguments;
|
||
if (timer) clearTimeout(timer);
|
||
if (immediate) {
|
||
let callNow = !timer;
|
||
timer = setTimeout(() => {
|
||
timer = null;
|
||
}, wait);
|
||
if (callNow) func.apply(context, args);
|
||
} else {
|
||
timer = setTimeout(() => {
|
||
func.apply(context, args);
|
||
}, wait)
|
||
}
|
||
}
|
||
},
|
||
|
||
/**
|
||
* @desc 函数节流
|
||
* @param func 函数
|
||
* @param wait 延迟执行毫秒数
|
||
* @param type 1 使用表时间戳,在时间段开始的时候触发 2 使用表定时器,在时间段结束的时候触发
|
||
*/
|
||
throttle (func, wait = 1000, type = 1) {
|
||
let previous = 0;
|
||
let timeout;
|
||
return function() {
|
||
let context = this;
|
||
let args = arguments;
|
||
if (type === 1) {
|
||
let now = Date.now();
|
||
|
||
if (now - previous > wait) {
|
||
func.apply(context, args);
|
||
previous = now;
|
||
}
|
||
} else if (type === 2) {
|
||
if (!timeout) {
|
||
timeout = setTimeout(() => {
|
||
timeout = null;
|
||
func.apply(context, args)
|
||
}, wait)
|
||
}
|
||
}
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 对象转数组
|
||
* @param {Object} e
|
||
*/
|
||
objToArrary (obj) {
|
||
|
||
if (Object.values(obj) && Array.isArray(Object.values(obj)[0])){
|
||
// 是数组
|
||
let arr = []
|
||
Object.values(obj).forEach(item=>{
|
||
arr = [...arr,...item]
|
||
})
|
||
return arr
|
||
} else {
|
||
// 不是数组
|
||
return Object.keys(obj).map(key => obj[key])
|
||
}
|
||
|
||
},
|
||
|
||
|
||
} |