popup.js 3.7 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) {
14
  return callApiSync(showToast, Object.assign({}, args, { type: 'loading' }), 'showToast', 'showLoading')
fxy060608's avatar
fxy060608 已提交
15 16
}

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

fxy060608's avatar
fxy060608 已提交
21
export function showToast ({
fxy060608's avatar
fxy060608 已提交
22 23 24 25 26
  title = '',
  icon = 'success',
  image = '',
  duration = 1500,
  mask = false,
27 28
  position = '',
  type = 'toast'
fxy060608's avatar
fxy060608 已提交
29
} = {}) {
30 31 32 33 34 35 36 37 38
  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 已提交
39 40 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
    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
        }
      }
    }

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

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

fxy060608's avatar
fxy060608 已提交
87
export function hideToast () {
88 89 90 91 92 93 94 95 96 97
  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 已提交
98
  }
99 100 101 102
  if (toast === true) {
    plus.nativeUI.closeToast()
  } else if (toast && toast.close) {
    toast.close()
fxy060608's avatar
fxy060608 已提交
103
  }
104 105
  toast = null
  toastType = null
fxy060608's avatar
fxy060608 已提交
106
  return {
Q
qiang 已提交
107
    errMsg: 'hide:ok'
fxy060608's avatar
fxy060608 已提交
108 109
  }
}
fxy060608's avatar
fxy060608 已提交
110
export function showModal ({
fxy060608's avatar
fxy060608 已提交
111
  title = '',
雪洛's avatar
雪洛 已提交
112
  content = '',
fxy060608's avatar
fxy060608 已提交
113 114 115 116 117
  showCancel = true,
  cancelText = '取消',
  cancelColor = '#000000',
  confirmText = '确定',
  confirmColor = '#3CC51F'
118
} = {}, callbackId) {
雪洛's avatar
雪洛 已提交
119
  content = content || ' '
fxy060608's avatar
fxy060608 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  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 已提交
136
export function showActionSheet ({
fxy060608's avatar
fxy060608 已提交
137 138
  itemList = [],
  itemColor = '#000000',
139 140
  title = '',
  popover
fxy060608's avatar
fxy060608 已提交
141 142 143
}, callbackId) {
  const options = {
    buttons: itemList.map(item => ({
144
      title: item,
145
      color: itemColor
fxy060608's avatar
fxy060608 已提交
146 147 148 149 150 151
    }))
  }
  if (title) {
    options.title = title
  }

152
  options.cancel = ''
fxy060608's avatar
fxy060608 已提交
153

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