From 0512e645421178bc01612f4a1766127b66edc032 Mon Sep 17 00:00:00 2001
From: Tevin <tingquanren@163.com>
Date: Sat, 20 Jul 2024 15:56:04 +0800
Subject: [PATCH] 修复地址联动组件,回显数据时,部分列显示异常的问题

---
 common/Tools.js |   56 ++++++++++++++++++++++++++++++--------------------------
 1 files changed, 30 insertions(+), 26 deletions(-)

diff --git a/common/Tools.js b/common/Tools.js
index 624297d..a99856a 100644
--- a/common/Tools.js
+++ b/common/Tools.js
@@ -47,7 +47,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 +436,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);
+        }
     }
 
     /**
@@ -542,42 +550,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 +605,3 @@
         console.info(Math.round(px) + 'px');
     };
 }
-
-global.Tools = Tools;

--
Gitblit v1.9.1