fixWxShowToast.js 1.98 KB
// 注意此代码应该在调用原生api之前执行
export default function fixWxShowToast() {
  let isShowLoading = false
  let isShowToast = false
  const { showLoading, hideLoading, showToast, hideToast } = wx
  Object.defineProperty(wx, 'showLoading', {
    configurable: true, // 是否可以配置
    enumerable: true, // 是否可迭代
    writable: true, // 是否可重写
    value(...param) {
      if (isShowToast) {
        // Toast优先级更高
        return
      }
      isShowLoading = true
      // console.log('--------showLoading--------')
      return showLoading.apply(this, param) // 原样移交函数参数和this
    }
  })
  Object.defineProperty(wx, 'hideLoading', {
    configurable: true, // 是否可以配置
    enumerable: true, // 是否可迭代
    writable: true, // 是否可重写
    value(...param) {
      if (isShowToast) {
        // Toast优先级更高
        return
      }
      isShowLoading = false
      // console.log('--------hideLoading--------')
      return hideLoading.apply(this, param) // 原样移交函数参数和this
    }
  })
  Object.defineProperty(wx, 'showToast', {
    configurable: true, // 是否可以配置
    enumerable: true, // 是否可迭代
    writable: true, // 是否可重写
    value(...param) {
      if (isShowLoading) {
        // Toast优先级更高
        wx.hideLoading()
      }
      isShowToast = true
      setTimeout(() => {
        isShowToast = false
      }, 2000)
      console.error('--------showToast--------')
      return showToast.apply(this, param) // 原样移交函数参数和this
    }
  })
  Object.defineProperty(wx, 'hideToast', {
    configurable: true, // 是否可以配置
    enumerable: true, // 是否可迭代
    writable: true, // 是否可重写
    value(...param) {
      isShowToast = false
      console.error('--------hideToast--------')
      return hideToast.apply(this, param) // 原样移交函数参数和this
    }
  })
}