mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import {
  callApiSync
} from '../util'
 
import {
  invoke
} from '../../bridge'
 
import {
  t
} from 'uni-core/helpers/i18n'
 
let toast
let toastType
let timeout
 
export function showLoading (args) {
  return callApiSync(showToast, Object.assign({}, args, {
    type: 'loading'
  }), 'showToast', 'showLoading')
}
 
export function hideLoading () {
  return callApiSync(hide, 'loading', 'hide', 'hideLoading')
}
 
export function showToast ({
  title = '',
  icon = 'success',
  image = '',
  duration = 1500,
  mask = false,
  position = '',
  type = 'toast',
  style
} = {}) {
  hide(null)
  toastType = type
  if (['top', 'center', 'bottom'].includes(position)) {
    // 仅可以关闭 richtext 类型,但 iOS 部分情况换行显示有问题
    plus.nativeUI.toast(title, {
      verticalAlign: position
    })
    toast = true
  } else {
    if (icon && !~['success', 'loading', 'error', '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 (['success', 'error'].indexOf(icon) !== -1) {
        waitingOptions.loading = {
          display: 'block',
          height: '55px',
          icon: icon === 'success' ? '__uniappsuccess.png' : '__uniapperror.png',
          interval: duration
        }
      }
    }
 
    toast = plus.nativeUI.showWaiting(title, Object.assign(waitingOptions, style))
  }
 
  timeout = setTimeout(() => {
    hide(null)
  }, duration)
  return {
    errMsg: 'showToast:ok'
  }
}
 
export function hideToast () {
  return callApiSync(hide, 'toast', 'hide', 'hideToast')
}
 
export function hide (type = 'toast') {
  if (type && type !== toastType) {
    return
  }
  if (timeout) {
    clearTimeout(timeout)
    timeout = null
  }
  if (toast === true) {
    plus.nativeUI.closeToast()
  } else if (toast && toast.close) {
    toast.close()
  }
  toast = null
  toastType = null
  return {
    errMsg: 'hide:ok'
  }
}
export function showModal ({
  title = '',
  content = '',
  showCancel = true,
  cancelText,
  cancelColor,
  confirmText,
  confirmColor,
  editable = false,
  placeholderText = ''
} = {}, callbackId) {
  const buttons = showCancel ? [cancelText, confirmText] : [confirmText]
  const tip = editable ? placeholderText : buttons
 
  content = content || ' '
  plus.nativeUI[editable ? 'prompt' : 'confirm'](content, (e) => {
    if (showCancel) {
      const isConfirm = e.index === 1
      const res = {
        errMsg: 'showModal:ok',
        confirm: isConfirm,
        cancel: e.index === 0 || e.index === -1
      }
      isConfirm && editable && (res.content = e.value)
      invoke(callbackId, res)
    } else {
      const res = {
        errMsg: 'showModal:ok',
        confirm: e.index === 0,
        cancel: false
      }
      editable && (res.content = e.value)
      invoke(callbackId, res)
    }
  }, title, tip, buttons)
}
 
const ACTION_SHEET_THEME = {
  light: {
    itemColor: '#000000'
  },
  dark: {
    itemColor: 'rgba(255, 255, 255, 0.8)'
  }
}
export function showActionSheet ({
  itemList = [],
  itemColor,
  title = '',
  popover
}, callbackId) {
  // #000 by default in protocols
  if (itemColor === '#000' && __uniConfig.darkmode) {
    itemColor =
      ACTION_SHEET_THEME[plus.navigator.getUIStyle()]
        .itemColor
  }
  const options = {
    buttons: itemList.map(item => ({
      title: item,
      color: itemColor
    }))
  }
  if (title) {
    options.title = title
  }
 
  options.cancel = t('uni.showActionSheet.cancel')
 
  plus.nativeUI.actionSheet(Object.assign(options, {
    popover
  }), (e) => {
    if (e.index > 0) {
      invoke(callbackId, {
        errMsg: 'showActionSheet:ok',
        tapIndex: e.index - 1
      })
    } else {
      invoke(callbackId, {
        errMsg: 'showActionSheet:fail cancel'
      })
    }
  })
}