popup.js 3.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4
import {
  callApiSync
} from '../util'

fxy060608's avatar
fxy060608 已提交
5 6 7 8
import {
  invoke
} from '../../bridge'

9 10 11
let toast
let toastType
let timeout
fxy060608's avatar
fxy060608 已提交
12

fxy060608's avatar
fxy060608 已提交
13
export function showLoading (args) {
fxy060608's avatar
fxy060608 已提交
14 15 16
  return callApiSync(showToast, Object.assign({}, args, {
    type: 'loading'
  }), 'showToast', 'showLoading')
fxy060608's avatar
fxy060608 已提交
17 18
}

fxy060608's avatar
fxy060608 已提交
19
export function hideLoading () {
20
  return callApiSync(hide, 'loading', 'hide', 'hideLoading')
fxy060608's avatar
fxy060608 已提交
21 22
}

fxy060608's avatar
fxy060608 已提交
23
export function showToast ({
fxy060608's avatar
fxy060608 已提交
24 25 26 27 28
  title = '',
  icon = 'success',
  image = '',
  duration = 1500,
  mask = false,
29 30
  position = '',
  type = 'toast'
fxy060608's avatar
fxy060608 已提交
31
} = {}) {
32 33 34 35 36 37 38 39 40
  hide(null)
  toastType = type
  if (['top', 'center', 'bottom'].includes(position)) {
    // 仅可以关闭 richtext 类型,但 iOS 部分情况换行显示有问题
    plus.nativeUI.toast(title, {
      verticalAlign: position
    })
    toast = true
  } else {
fxy060608's avatar
fxy060608 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    if (icon && !~['success', 'loading', 'none'].indexOf(icon)) {
      icon = 'success'
    }
    const waitingOptions = {
      modal: mask,
      back: 'transmit',
      padding: '10px',
      size: '16px' // 固定字体大小
    }
    if (!image && (!icon || icon === 'none')) { // 无图
      //       waitingOptions.width = '120px'
      //       waitingOptions.height = '40px'
      waitingOptions.loading = {
        display: 'none'
      }
    } else { // 有图
      waitingOptions.width = '140px'
      waitingOptions.height = '112px'
    }
    if (image) {
      waitingOptions.loading = {
        display: 'block',
        height: '55px',
        icon: image,
        interval: duration
      }
    } else {
      if (icon === 'success') {
        waitingOptions.loading = {
          display: 'block',
          height: '55px',
          icon: '__uniappsuccess.png',
          interval: duration
        }
      }
    }

78
    toast = plus.nativeUI.showWaiting(title, waitingOptions)
fxy060608's avatar
fxy060608 已提交
79
  }
80 81 82 83

  timeout = setTimeout(() => {
    hide(null)
  }, duration)
fxy060608's avatar
fxy060608 已提交
84 85 86 87 88
  return {
    errMsg: 'showToast:ok'
  }
}

fxy060608's avatar
fxy060608 已提交
89
export function hideToast () {
90 91 92 93 94 95 96 97 98 99
  return callApiSync(hide, 'toast', 'hide', 'hideToast')
}

export function hide (type = 'toast') {
  if (type && type !== toastType) {
    return
  }
  if (timeout) {
    clearTimeout(timeout)
    timeout = null
fxy060608's avatar
fxy060608 已提交
100
  }
101 102 103 104
  if (toast === true) {
    plus.nativeUI.closeToast()
  } else if (toast && toast.close) {
    toast.close()
fxy060608's avatar
fxy060608 已提交
105
  }
106 107
  toast = null
  toastType = null
fxy060608's avatar
fxy060608 已提交
108
  return {
Q
qiang 已提交
109
    errMsg: 'hide:ok'
fxy060608's avatar
fxy060608 已提交
110 111
  }
}
fxy060608's avatar
fxy060608 已提交
112
export function showModal ({
fxy060608's avatar
fxy060608 已提交
113
  title = '',
雪洛's avatar
雪洛 已提交
114
  content = '',
fxy060608's avatar
fxy060608 已提交
115
  showCancel = true,
fxy060608's avatar
fxy060608 已提交
116 117 118 119
  cancelText,
  cancelColor,
  confirmText,
  confirmColor
120
} = {}, callbackId) {
雪洛's avatar
雪洛 已提交
121
  content = content || ' '
fxy060608's avatar
fxy060608 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  plus.nativeUI.confirm(content, (e) => {
    if (showCancel) {
      invoke(callbackId, {
        errMsg: 'showModal:ok',
        confirm: e.index === 1,
        cancel: e.index === 0 || e.index === -1
      })
    } else {
      invoke(callbackId, {
        errMsg: 'showModal:ok',
        confirm: e.index === 0,
        cancel: false
      })
    }
  }, title, showCancel ? [cancelText, confirmText] : [confirmText])
}
fxy060608's avatar
fxy060608 已提交
138
export function showActionSheet ({
fxy060608's avatar
fxy060608 已提交
139 140
  itemList = [],
  itemColor = '#000000',
141 142
  title = '',
  popover
fxy060608's avatar
fxy060608 已提交
143 144 145
}, callbackId) {
  const options = {
    buttons: itemList.map(item => ({
146
      title: item,
147
      color: itemColor
fxy060608's avatar
fxy060608 已提交
148 149 150 151 152 153
    }))
  }
  if (title) {
    options.title = title
  }

154
  options.cancel = ''
fxy060608's avatar
fxy060608 已提交
155

fxy060608's avatar
fxy060608 已提交
156 157 158
  plus.nativeUI.actionSheet(Object.assign(options, {
    popover
  }), (e) => {
fxy060608's avatar
fxy060608 已提交
159 160 161 162 163 164 165 166 167 168 169
    if (e.index > 0) {
      invoke(callbackId, {
        errMsg: 'showActionSheet:ok',
        tapIndex: e.index - 1
      })
    } else {
      invoke(callbackId, {
        errMsg: 'showActionSheet:fail cancel'
      })
    }
  })
fxy060608's avatar
fxy060608 已提交
170
}