From db036e639065504d953706db84b979e1addceaeb Mon Sep 17 00:00:00 2001 From: chensiAb <chenchenco03@163.com> Date: Thu, 07 Nov 2024 14:28:39 +0800 Subject: [PATCH] feat:inputPhoneCode组件添加重置显示按钮的方法 --- common/Tools.js | 71 +++++++++++++++++++---------------- 1 files changed, 38 insertions(+), 33 deletions(-) diff --git a/common/Tools.js b/common/Tools.js index 624297d..fa45243 100644 --- a/common/Tools.js +++ b/common/Tools.js @@ -6,6 +6,8 @@ import moment from 'moment'; import Taro from '@tarojs/taro'; +moment.locale('zh-cn'); + export class Tools { /** * 显示消息 @@ -47,7 +49,7 @@ * @return {string} */ static createGUID() { - return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { let r = (Math.random() * 16) | 0, v = c === 'x' ? r : (r & 0x3) | 0x8; return v.toString(16); @@ -436,16 +438,24 @@ /** * 数值转换为金钱格式 * @param {Number|String} number + * @param {String} [forRead=''] 便于阅读财务金额模式 * @return {string} */ - static moneyFormat(number) { + static moneyFormat(number, forRead = '') { if (!number && typeof number !== 'number' && typeof number !== 'string') { return ''; } if (typeof number === 'string') { number = Number(number) || 0; } - return number.toFixed(2); + if (forRead === 'forRead') { + return number.toLocaleString('zh-cn', { + minimumFractionDigits: 2, + maximumFractionDigits: 2, + }); + } else { + return number.toFixed(2); + } } /** @@ -526,13 +536,12 @@ * @return {String} */ static transWeekIndexToDate(year, week, weekDay) { - const yearStart = moment([year, 0, 1]); - const dayLong = 24 * 60 * 60 * 1000; - const firstWeekLong = (7 - yearStart.day()) * dayLong; - const weeksLong = (week - 1) * 7 * dayLong; - const weekDayLong = weekDay * dayLong; - const dayTimestamp = yearStart.valueOf() + firstWeekLong + weeksLong + weekDayLong; - return moment(dayTimestamp).format('YYYY-MM-DD'); + if (weekDay < 1 && weekDay > 7) { + return ''; + } + const weekDate = moment(year + '-' + week, 'YYYY-WW'); + const firstDay = weekDate.startOf('week'); + return firstDay.add(weekDay - 1, 'day').format('YYYY-MM-DD'); } /** @@ -542,42 +551,40 @@ * @param {Function} fnc * @param {Number} wait * @returns {Function} + * @tutorial Tools.debounce(<fnc>, <wait>)(<DebounceKey>) */ static debounce(fnc, wait = 200) { - let timeout = null; - return function() { - const context = this; - const args = arguments; - if (timeout) { - clearTimeout(timeout); + return key => { + if (!key || !Tools.isString(key)) { + throw 'Debounce function need a key!'; } - const callNow = !timeout; - timeout = setTimeout(() => { - timeout = null; - }, wait); - if (callNow) { - fnc.apply(context, args); + const timer = Tools.debounce['dKey-' + key]; + if (timer) { + clearTimeout(timer); } + Tools.debounce['dKey-' + key] = setTimeout(fnc, wait); }; } /** - * 节流函数(一段时间周期内,仅执行第一次回调) + * 节流函数(本段时间内,仅执行第一次回调) * 在一定时间内只能触发一次函数执行 * 如果这个时间内再次触发,则忽略,直到下一个时间段再次触发 * @param {Function} fnc * @param {Number} wait - * @returns {Function} + * @returns {function(key):void} + * @tutorial Tools.throttle(<fnc>, <wait>)(<ThrottleKey>) */ static throttle(fnc, wait = 200) { - let previous = 0; - return function() { - const context = this; - const args = arguments; - const now = new Date(); + return key => { + if (!key || !Tools.isString(key)) { + throw 'Throttle function need a key!'; + } + const previous = Tools.throttle['tKey-' + key] || 0; + const now = Date.now(); if (now - previous > wait) { - fnc.apply(context, args); - previous = now; + Tools.throttle['tKey-' + key] = now; + fnc(); } }; } @@ -599,5 +606,3 @@ console.info(Math.round(px) + 'px'); }; } - -global.Tools = Tools; -- Gitblit v1.9.1