| | |
| | | class commonUtils { |
| | | serverUrl |
| | | |
| | | constructor() { |
| | | this.serverUrl = uni.getStorageSync('serverUrl') || 'http://47.96.97.237/API'; |
| | | } |
| | | |
| | | // 防抖函数 |
| | | debounce(func, delay, immediate = false) { |
| | | let timer = null; |
| | |
| | | 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()); |