Files
frontend-app/utils/func.js
T
2025-10-10 13:41:42 +08:00

79 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
let timeout = null;
let timer;
let flag;
export default {
formatDate (date,val) {
let y = date.getFullYear();
let m = date.getMonth()+1;
let d = date.getDate();
let h = date.getHours();
let mm = date.getMinutes();
let s = date.getSeconds();
if(m <= 9){
m = '0' + m;
}
if(d <= 9){
d = '0' + d;
}
if(h <= 9){
h = '0' + h;
}
if(mm <= 9){
mm = '0' + mm;
}
if(s <= 9){
s = '0' + s;
}
let tmpTime = '';
if(val == 0){
tmpTime = `${h}:${mm}`;
}else if(val == 1){
tmpTime = `${m}-${d}`;
}else if(val == 2){
tmpTime = `${y}-${m}-${d} ${h}:${mm}:${s}`;
}else if(val == 3){
tmpTime = `${y}-${m}-${d}`;
}
return tmpTime;
}, 
debounce(func,wait = 1000,immediate = true){
// 清除定时器
if (timeout !== null) clearTimeout(timeout)
// 立即执行,此类情况一般用不到
if (immediate) {
const callNow = !timeout
timeout = setTimeout(() => {
timeout = null
}, wait)
if (callNow) typeof func === 'function' && func()
} else {
// 设置定时器,当最后一次操作后,timeout不会再被清除,所以在延时wait毫秒后执行func回调方法
timeout = setTimeout(() => {
typeof func === 'function' && func()
}, wait)
}
},
checkInputPrice(value,max = null) {
if(max === 0){
return '';
}
// value = value.replace(/^0*/g, ''); // 第一个数不能为0 ,若为0替换为空
value = value.replace(/[^\d.]/g, ""); // 是否是数字 和小数点,若是除数字 和小数点之外的则替换为空
value = value.replace(/^\./g, ""); // 确证第一个为数字而不是“.”
value = value.replace(/\.{2,}/g, "."); // 只能输入一个“.”
value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", "."); // 保证”.“只出现一次,而不能出现两次以上
value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); // 只能输入两个小数
if(max && value >= max){
value = max;
}
return value;
}
}