chenhaozhe
2025-07-09 4a85c4baa43cd65e9c2a3fa755ca11c36b6004ef
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,11 @@
   isAllDigits(str) {
      return /^\d+$/.test(str);
   }
   // uni-app 使用 封装请求函数
   doRequest(url, data, resFunction, errFunction, method) {
   // uni-app 使用 封装请求函数 使用传统函数当作回调需要传that,箭头函数不需要
   doRequest(url, data, resFunction, errFunction, method, that) {
      that = that || this;
      let errorTip = null;
      uni.showLoading({
         title: '加载中...'
      })
@@ -99,30 +101,38 @@
         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("访问失败回调函数类型不为函数或者空!")
            }
         }
      })
      uni.hideLoading()
      setTimeout(() => {
         uni.hideLoading()
         if (errorTip != null) {
            errorTip()
         }
      }, 1000)
   }
}
export const CommonUtils =  Object.freeze(new commonUtils());
export const CommonUtils = Object.freeze(new commonUtils());