12
chenhaozhe
2025-07-08 ca1eab1cf8a8050e5668eb7e78faea2a821c5cb2
utils/common.js
@@ -1,4 +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;
@@ -82,6 +88,41 @@
      return /^\d+$/.test(str);
   }
   
   // uni-app 使用 封装请求函数
   doRequest(url, data, resFunction, errFunction, method) {
      uni.showLoading({
         title: '加载中...'
      })
      uni.request({
         method: method || "GET",
         url: this.serverUrl + url,
         data: data || "",
         success: (res) => {
            if (typeof resFunction === 'function') {
               resFunction.call(this, res)
            } else if (typeof errFunction === 'undefined') {
               return
            } else {
               throw new TypeError("访问成功回调函数类型异常!")
            }
         },
         fail: (err) => {
            uni.showToast({
               icon: "error",
               title: "接口访问异常!",
               duration: 2000
            })
            if (typeof errFunction === 'function') {
               errFunction.call(this, err)
            } else if (typeof errFunction === 'undefined') {
               return
            } else {
               throw new TypeError("访问失败回调函数类型异常!")
            }
         }
      })
      uni.hideLoading()
   }
}
export const CommonUtils =  Object.freeze(new commonUtils());