提交 a0483c05 编写于 作者: fxy060608's avatar fxy060608

refactor api

上级 c38d6ba3
const api = Object.create(null)
const modules = require.context(
'./api',
true,
/\.js$/
)
modules.keys().forEach(function (key) {
Object.assign(api, modules(key))
})
import * as api from './api/index'
export default api
const {
invokeCallbackHandler: invoke
} = UniServiceJSBridge
let waiting
let waitingTimeout
let toast = false
let toastTimeout
export function showLoading (args) {
return showToast(args).replace('showToast', 'showLoading')
}
export function hideLoading () {
return hideToast().replace('hideToast', 'hideLoading')
}
export function showToast ({
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)) {
let richText = `<span>${title}</span>`
plus.nativeUI.toast(richText, {
verticalAlign: position,
type: 'richtext'
})
toast = true
toastTimeout = setTimeout(() => {
hideToast()
}, 2000)
return
}
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'
}
}
export function hideToast () {
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'
}
}
export function showModal ({
title = '',
content = '',
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])
}
export function showActionSheet ({
itemList = [],
itemColor = '#000000',
title = ''
}, callbackId) {
const options = {
buttons: itemList.map(item => ({
title: item
}))
}
if (title) {
options.title = title
}
if (plus.os.name === 'iOS') {
options.cancel = '取消'
}
plus.nativeUI.actionSheet(options, (e) => {
if (e.index > 0) {
invoke(callbackId, {
errMsg: 'showActionSheet:ok',
tapIndex: e.index - 1
})
} else {
invoke(callbackId, {
errMsg: 'showActionSheet:fail cancel'
})
}
})
}
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册