chenhaozhe
2025-07-24 e0cf3a2ccabbdc8be20ba65766d8a411bf74ef02
utils/common.js
@@ -1,10 +1,10 @@
class commonUtils {
   serverUrl
   constructor() {
      this.serverUrl = uni.getStorageSync('serverUrl') || 'http://47.96.97.237/API';
   }
   // 防抖函数
   debounce(func, delay, immediate = false) {
      let timer = null;
@@ -87,9 +87,110 @@
   isAllDigits(str) {
      return /^\d+$/.test(str);
   }
   // uni-app 使用 封装请求函数
   doRequest(url, data, resFunction, errFunction, method) {
   isJson(str) {
      try {
         JSON.parse(str)
         return true
      } catch {
         return false
      }
   }
   timeClock(callback, delay) {
      let timeoutId;
      let isRunning = false;
      function interval() {
         timeoutId = setTimeout(() => {
            callback();
            clearTimeout(timeoutId); // 立即清除当前定时器ID
            if (isRunning) {
               interval();
            }
         }, delay);
      }
      return {
         start() {
            if (!isRunning) {
               isRunning = true;
               interval();
            }
         },
         stop() {
            if (isRunning) {
               isRunning = false;
               clearTimeout(timeoutId);
            }
         }
      };
   }
   deepClone(target, map = new WeakMap()) {
      // 处理原始值和函数(typeof 数组会返回object)
      if (typeof target !== 'object' || target === null) {
         return target;
      }
      // 处理循环引用
      if (map.has(target)) {
         return map.get(target);
      }
      let clone;
      // 处理数组
      if (Array.isArray(target)) {
         clone = [];
         map.set(target, clone);
         target.forEach((item, index) => {
            clone[index] = deepClone(item, map);
         });
         return clone;
      }
      // 处理日期对象
      if (target instanceof Date) {
         clone = new Date(target.getTime());
         map.set(target, clone);
         return clone;
      }
      // 处理正则表达式
      if (target instanceof RegExp) {
         clone = new RegExp(target);
         map.set(target, clone);
         return clone;
      }
      // 处理函数 (直接使用函数的引用)
      if (typeof target === 'function') {
         return target;
      }
      // 处理其他对象(普通对象、类实例等)
      clone = Object.create(Object.getPrototypeOf(target));
      map.set(target, clone);
      // 获取所有属性(包括 Symbol 类型)
      const allKeys = [...Object.getOwnPropertyNames(target), ...Object.getOwnPropertySymbols(target)];
      allKeys.forEach(key => {
         const descriptor = Object.getOwnPropertyDescriptor(target, key);
         if (descriptor && descriptor.enumerable) {
            // 递归复制属性值
            clone[key] = deepClone(target[key], map);
         }
      });
      return clone;
   }
   // uni-app 使用 封装请求函数 使用传统函数当作回调需要传that,箭头函数不需要
   doRequest(url, data, resFunction, errFunction, method, that) {
      that = that || this;
      let errorTip = null;
      uni.showLoading({
         title: '加载中...'
      })
@@ -99,30 +200,52 @@
         data: data || "",
         success: (res) => {
            if (typeof resFunction === 'function') {
               resFunction.call(this, res)
            } else if (typeof errFunction === 'undefined') {
               resFunction.call(that, res)
            } else if (typeof errFunction === 'undefined' || errFunction === null) {
               return
            } else {
               throw new TypeError("访问成功回调函数类型异常!")
               throw new TypeError("访问成功回调函数类型不为函数或者空!")
            }
         },
         fail: (err) => {
            uni.showToast({
               icon: "error",
               title: "接口访问异常!",
               duration: 2000
            })
            console.error(err)
            errorTip = () => {
               uni.showToast({
                  icon: "none",
                  title: err.errMsg || err.data.message || "接口异常!",
                  duration: 2000
               })
            }
            if (typeof errFunction === 'function') {
               errFunction.call(this, err)
            } else if (typeof errFunction === 'undefined') {
               errFunction.call(that, err)
            } else if (typeof errFunction === 'undefined' || errFunction === null) {
               return
            } else {
               throw new TypeError("访问失败回调函数类型异常!")
               throw new TypeError("访问失败回调函数类型不为函数或者空!")
            }
         },
         complete() {
            setTimeout(() => {
               uni.hideLoading()
               if (errorTip != null) {
                  errorTip()
               }
            }, 1000)
         }
      })
      uni.hideLoading()
   }
   // uni-app 播放音频封装
   playSound(e) {
      const innerAudioContext = uni.createInnerAudioContext();
      if (e == 1) {
         innerAudioContext.src = '/static/success.wav';
      } else {
         innerAudioContext.src = '/static/jingbao.wav';
      }
      innerAudioContext.play(); // 播放音频
   }
}
export const CommonUtils =  Object.freeze(new commonUtils());
export const CommonUtils = Object.freeze(new commonUtils());