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

fxy060608's avatar
fxy060608 已提交
5 6 7 8 9 10 11 12 13
import {
  invoke
} from '../../bridge'

let waiting
let waitingTimeout
let toast = false
let toastTimeout

fxy060608's avatar
fxy060608 已提交
14
export function showLoading (args) {
fxy060608's avatar
fxy060608 已提交
15
  return callApiSync(showToast, args, 'showToast', 'showLoading')
fxy060608's avatar
fxy060608 已提交
16 17
}

fxy060608's avatar
fxy060608 已提交
18
export function hideLoading () {
fxy060608's avatar
fxy060608 已提交
19
  return callApiSync(hideToast, Object.create(null), 'hideToast', 'hideLoading')
fxy060608's avatar
fxy060608 已提交
20 21
}

fxy060608's avatar
fxy060608 已提交
22
export function showToast ({
fxy060608's avatar
fxy060608 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  title = '',
  icon = 'success',
  image = '',
  duration = 1500,
  mask = false,
  position = ''
} = {}) {
  if (position) {
    if (toast) {
      toastTimeout && clearTimeout(toastTimeout)
      plus.nativeUI.closeToast()
    }
    if (waiting) {
      waitingTimeout && clearTimeout(waitingTimeout)
      waiting.close()
    }
    if (~['top', 'center', 'bottom'].indexOf(position)) {
fxy060608's avatar
fxy060608 已提交
40
      const richText = `<span>${title}</span>`
fxy060608's avatar
fxy060608 已提交
41 42 43 44 45 46 47 48
      plus.nativeUI.toast(richText, {
        verticalAlign: position,
        type: 'richtext'
      })
      toast = true
      toastTimeout = setTimeout(() => {
        hideToast()
      }, 2000)
fxy060608's avatar
fxy060608 已提交
49 50 51
      return {
        errMsg: 'showToast:ok'
      }
fxy060608's avatar
fxy060608 已提交
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    }
    console.warn('uni.showToast 传入的 "position" 值 "' + position + '" 无效')
  }

  if (duration) {
    if (waiting) {
      waitingTimeout && clearTimeout(waitingTimeout)
      waiting.close()
    }
    if (toast) {
      toastTimeout && clearTimeout(toastTimeout)
      plus.nativeUI.closeToast()
    }
    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

        }
      }
    }

    waiting = plus.nativeUI.showWaiting(title, waitingOptions)
    waitingTimeout = setTimeout(() => {
      hideToast()
    }, duration)
  }
  return {
    errMsg: 'showToast:ok'
  }
}

fxy060608's avatar
fxy060608 已提交
113
export function hideToast () {
fxy060608's avatar
fxy060608 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  if (toast) {
    toastTimeout && clearTimeout(toastTimeout)
    plus.nativeUI.closeToast()
    toast = false
  }
  if (waiting) {
    waitingTimeout && clearTimeout(waitingTimeout)
    waiting.close()
    waiting = null
    waitingTimeout = null
  }
  return {
    errMsg: 'hideToast:ok'
  }
}
fxy060608's avatar
fxy060608 已提交
129
export function showModal ({
fxy060608's avatar
fxy060608 已提交
130
  title = '',
131
  content = ' ',
fxy060608's avatar
fxy060608 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  showCancel = true,
  cancelText = '取消',
  cancelColor = '#000000',
  confirmText = '确定',
  confirmColor = '#3CC51F'
} = {}, callbackId) {
  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 已提交
154
export function showActionSheet ({
fxy060608's avatar
fxy060608 已提交
155 156
  itemList = [],
  itemColor = '#000000',
157 158
  title = '',
  popover
fxy060608's avatar
fxy060608 已提交
159 160 161
}, callbackId) {
  const options = {
    buttons: itemList.map(item => ({
162
      title: item,
163
      color: itemColor
fxy060608's avatar
fxy060608 已提交
164 165 166 167 168 169 170
    }))
  }
  if (title) {
    options.title = title
  }

  if (plus.os.name === 'iOS') {
d-u-a's avatar
d-u-a 已提交
171
    options.cancel = ''
fxy060608's avatar
fxy060608 已提交
172 173
  }

174
  plus.nativeUI.actionSheet(Object.assign(options, { popover }), (e) => {
fxy060608's avatar
fxy060608 已提交
175 176 177 178 179 180 181 182 183 184 185
    if (e.index > 0) {
      invoke(callbackId, {
        errMsg: 'showActionSheet:ok',
        tapIndex: e.index - 1
      })
    } else {
      invoke(callbackId, {
        errMsg: 'showActionSheet:fail cancel'
      })
    }
  })
186
}