wtt
2025-10-11 db3cb0b57fdec1a8ba13ed811e12b25dad0cddb2
utils/common.js
@@ -1,19 +1,22 @@
class commonUtils {
    serverUrl
    audioContext // 全局音频实例
    requestLock // 请求控制锁(同步)
    constructor() {
        this.serverUrl = uni.getStorageSync('serverUrl') || 'http://47.96.97.237/API/';
        this.audioContext = null;
        this.requestLock = false
    }
    setServerUrl(url) {
        this.serverUrl = url
    }
    getServerUrl() {
        return this.serverUrl
    }
    // 防抖函数
    debounce(func, delay, immediate = false) {
@@ -245,60 +248,103 @@
            }
        })
    }
   doRequest2({url, data, resFunction, errFunction,method, that}){
      that = that || this;
      let errorTip = null;
      uni.showLoading({
          title: '加载中...'
      })
      uni.request({
          method: method || "GET",
          url: this.serverUrl + url,
          data: data || "",
          success: (res) => {
              if (typeof resFunction === 'function') {
                  resFunction.call(that, res)
              } else if (typeof errFunction === 'undefined' || errFunction === null) {
                  return
              } else {
                  throw new TypeError("访问成功回调函数类型不为函数或者空!")
              }
          },
          fail: (err) => {
              console.error(err)
              errorTip = () => {
                  uni.showToast({
                      icon: "none",
                      title: err.errMsg || err.data.message || "接口异常!",
                      duration: 2000
                  })
              }
              if (typeof errFunction === 'function') {
                  errFunction.call(that, err)
              } else if (typeof errFunction === 'undefined' || errFunction === null) {
                  return
              } else {
                  throw new TypeError("访问失败回调函数类型不为函数或者空!")
              }
          },
          complete: () => {
              setTimeout(() => {
                  uni.hideLoading()
                  if (errorTip != null) {
                      errorTip()
                  }
              }, 1000)
          }
      })
   }
   stringToBoolean(str) {
     // 忽略大小写的转换
     return str?.toLowerCase() === "true";
   }
    doRequest2({
        url,
        data,
        resFunction,
        errFunction,
        method,
        that
    }) {
        that = that || this;
        let errorTip = null;
        uni.showLoading({
            title: '加载中...'
        })
        uni.request({
            method: method || "GET",
            url: this.serverUrl + url,
            data: data || "",
            success: (res) => {
                if (typeof resFunction === 'function') {
                    resFunction.call(that, res)
                } else if (typeof errFunction === 'undefined' || errFunction === null) {
                    return
                } else {
                    throw new TypeError("访问成功回调函数类型不为函数或者空!")
                }
            },
            fail: (err) => {
                console.error(err)
                errorTip = () => {
                    uni.showToast({
                        icon: "none",
                        title: err.errMsg || err.data.message || "接口异常!",
                        duration: 2000
                    })
                }
                if (typeof errFunction === 'function') {
                    errFunction.call(that, err)
                } else if (typeof errFunction === 'undefined' || errFunction === null) {
                    return
                } else {
                    throw new TypeError("访问失败回调函数类型不为函数或者空!")
                }
            },
            complete: () => {
                setTimeout(() => {
                    uni.hideLoading()
                    if (errorTip != null) {
                        errorTip()
                    }
                }, 1000)
            }
        })
    }
    // 同步执行请求 (配合await或者then)
    async doRequest2Sync({
        url,
        data,
        method,
    }) {
        if(this.requestLock){
            console.warn("该请求被锁定,已退出请求!")
            return
        }
        this.requestLock = true
        return new Promise((resolve, reject) => {
            // that = that || this;
            let errorTip = null;
            uni.showLoading({
                title: '加载中...'
            })
            uni.request({
                method: method || "GET",
                url: this.serverUrl + url,
                data: data || "",
                success: (res) => {
                    resolve(res)
                },
                fail: (err) => {
                    reject(err)
                },
                complete: () => {
                    // 释放请求锁
                    this.requestLock = false
                    uni.hideLoading()
                }
            })
        })
    }
    stringToBoolean(str) {
        // 忽略大小写的转换
        return str?.toLowerCase() === "true";
    }
    // uni-app 播放音频封装
    playSound(e) {
@@ -309,25 +355,57 @@
            innerAudioContext.src = '/static/jingbao.wav';
        }
        innerAudioContext.play(); // 播放音频
      innerAudioContext.onPlay(() => {
         console.log('开始播放');
      });
      innerAudioContext.onError((res) => {
         console.log(res.errMsg);
         console.log(res.errCode);
      });
      innerAudioContext.onPause(function(){
         console.log('播放出现错误,销毁');
         innerAudioContext.destroy();
      })
        innerAudioContext.onPlay(() => {
            console.log('开始播放');
        });
        innerAudioContext.onError((res) => {
            console.log(res.errMsg);
            console.log(res.errCode);
        });
        innerAudioContext.onPause(function() {
            console.log('播放出现错误,销毁');
            innerAudioContext.destroy();
        })
    }
    // playSound(e) {
    //     // 全局维护一个音频实例,防止缓存溢出
    //     if (this.audioContext) {
    //         this.audioContext.destroy();
    //     }
    //     this.audioContext = uni.createInnerAudioContext();
    //     if (e == 1) {
    //         this.audioContext.src = '/static/success.wav';
    //     } else {
    //         this.audioContext.src = '/static/jingbao.wav';
    //     }
    //     this.audioContext.play(); // 播放音频
    //
    //     // 播放结束后销毁实例
    //     this.audioContext.onEnded(() => {
    //         this.audioContext.destroy();
    //         this.audioContext = null;
    //     });
    //
    //     // 错误处理
    //     this.audioContext.onError((err) => {
    //         uni.showToast({
    //             icon: 'none',
    //             title: `音频播放错误: ${err}`
    //         })
    //
    //         this.audioContext.destroy();
    //         this.audioContext = null;
    //     });
    // }
    replaceWithFunction(str, handler) {
      return str.replace(/\{(.+?)\}/g, (match, key) => {
        // 调用处理函数,传入匹配到的键
        return handler(key, match);
      });
        return str.replace(/\{(.+?)\}/g, (match, key) => {
            // 调用处理函数,传入匹配到的键
            return handler(key, match);
        });
    }
}