toast.js 1.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
import { t } from 'uni-core/helpers/i18n'
export default {
  data() {
    return {
      showToast: {
        visible: false,
      },
    }
  },
  created() {
    let showType = ''

    const createOnShow = (type) => {
      return (args) => {
        showType = type
        setTimeout(() => {
          // 延迟一下 show 可解决窗口打开前调用 showToast 在 onHidePopup 之后触发
          this.showToast = args
        }, 10)
      }
    }

    UniServiceJSBridge.on('onShowToast', createOnShow('onShowToast'))
    UniServiceJSBridge.on('onShowLoading', createOnShow('onShowLoading'))

    const createOnHide = (type) => {
      return () => {
        if (!showType) {
          return
        }
        let warnMsg = ''
        if (type === 'onHideToast' && showType !== 'onShowToast') {
          warnMsg = t('uni.showToast.unpaired')
        } else if (type === 'onHideLoading' && showType !== 'onShowLoading') {
          warnMsg = t('uni.showLoading.unpaired')
        }
        if (warnMsg) {
          return console.warn(warnMsg)
        }
        showType = ''
        setTimeout(() => {
          // 与 show 对应延迟10ms,避免快速调用 show,hide 导致无法关闭
          this.showToast.visible = false
        }, 10)
      }
    }

    UniServiceJSBridge.on('onHidePopup', createOnHide('onHidePopup'))
    UniServiceJSBridge.on('onHideToast', createOnHide('onHideToast'))
    UniServiceJSBridge.on('onHideLoading', createOnHide('onHideLoading'))
  },
}