提交 eca0cc12 编写于 作者: D DCloud_LXH

fix(App): showToast

上级 f4330786
...@@ -66,21 +66,6 @@ export function warpPlusMethod( ...@@ -66,21 +66,6 @@ export function warpPlusMethod(
} }
} }
export function callApiSync<T extends (...args: any) => any>(
api: T,
args: Parameters<T>[0],
resolve: Function,
reject: (errMsg?: string | undefined, errRes?: any) => void
) {
api(args)
.then(() => {
resolve()
})
.catch((errMsg: string) => {
reject(errMsg)
})
}
export function isTabBarPage(path = '') { export function isTabBarPage(path = '') {
if (!(__uniConfig.tabBar && Array.isArray(__uniConfig.tabBar.list))) { if (!(__uniConfig.tabBar && Array.isArray(__uniConfig.tabBar.list))) {
return false return false
......
import { extend } from '@vue/shared' import { extend } from '@vue/shared'
import { callApiSync } from '../../../../helpers/plus'
import { import {
defineAsyncApi, defineAsyncApi,
ShowToastOptions, ShowToastOptions,
...@@ -16,6 +15,9 @@ import { ...@@ -16,6 +15,9 @@ import {
API_HIDE_LOADING, API_HIDE_LOADING,
} from '@dcloudio/uni-api' } from '@dcloudio/uni-api'
type Resolve = (res?: any) => void
type Reject = (errMsg?: string, errRes?: any) => void
type ToastType = 'loading' | 'toast' | '' type ToastType = 'loading' | 'toast' | ''
let toast: PlusNativeUIWaitingObj | null let toast: PlusNativeUIWaitingObj | null
...@@ -25,118 +27,115 @@ let timeout: number | null ...@@ -25,118 +27,115 @@ let timeout: number | null
export const showLoading = defineAsyncApi<API_TYPE_SHOW_LOADING>( export const showLoading = defineAsyncApi<API_TYPE_SHOW_LOADING>(
API_SHOW_LOADING, API_SHOW_LOADING,
(args, { resolve, reject }) => { (args, callbacks) =>
callApiSync<typeof showToast>( _showToast(
showToast,
extend({}, args, { extend({}, args, {
type: 'loading', type: 'loading',
}), }),
resolve, callbacks
reject ),
)
},
ShowLoadingProtocol, ShowLoadingProtocol,
ShowLoadingOptions ShowLoadingOptions
) )
export const hideLoading = defineAsyncApi<API_TYPE_HIDE_LOADING>( const _showToast = (
API_HIDE_LOADING, {
(_, { resolve, reject }) => { title = '',
callApiSync<typeof hide>(hide, 'loading', resolve, reject) icon = 'success',
} image = '',
) duration = 1500,
mask = false,
export const showToast = defineAsyncApi<API_TYPE_SHOW_TOAST>( position,
API_SHOW_TOAST, // @ts-ignore ToastType
( type = 'toast',
{ // @ts-ignore PlusNativeUIWaitingStyles
title = '', style,
icon = 'success', }: UniApp.ShowToastOptions,
image = '', { resolve, reject }: { resolve: Resolve; reject: Reject }
duration = 1500, ) => {
mask = false, hide('')
position, toastType = type
// @ts-ignore ToastType if (['top', 'center', 'bottom'].includes(String(position))) {
type = 'toast', // 仅可以关闭 richtext 类型,但 iOS 部分情况换行显示有问题
// @ts-ignore PlusNativeUIWaitingStyles plus.nativeUI.toast(title, {
style, verticalAlign: position,
}, })
{ resolve, reject } isShowToast = true
) => { } else {
hide('') if (icon && !~['success', 'loading', 'error', 'none'].indexOf(icon)) {
toastType = type icon = 'success'
if (['top', 'center', 'bottom'].includes(String(position))) { }
// 仅可以关闭 richtext 类型,但 iOS 部分情况换行显示有问题 const waitingOptions: PlusNativeUIWaitingStyles = {
plus.nativeUI.toast(title, { modal: mask,
verticalAlign: position, back: 'transmit',
}) padding: '10px',
isShowToast = true size: '16px', // 固定字体大小
} else { }
if (icon && !~['success', 'loading', 'error', 'none'].indexOf(icon)) { if (!image && (!icon || icon === 'none')) {
icon = 'success' // 无图
} // waitingOptions.width = '120px'
const waitingOptions: PlusNativeUIWaitingStyles = { // waitingOptions.height = '40px'
modal: mask, waitingOptions.loading = {
back: 'transmit', display: 'none',
padding: '10px',
size: '16px', // 固定字体大小
} }
if (!image && (!icon || icon === 'none')) { } else {
// 无图 waitingOptions.width = '140px'
// waitingOptions.width = '120px' waitingOptions.height = '112px'
// waitingOptions.height = '40px' }
waitingOptions.loading = { if (image) {
display: 'none', waitingOptions.loading = {
} display: 'block',
} else { height: '55px',
waitingOptions.width = '140px' icon: image,
waitingOptions.height = '112px' interval: duration,
} }
if (image) { } else {
if (['success', 'error'].indexOf(icon) !== -1) {
waitingOptions.loading = { waitingOptions.loading = {
display: 'block', display: 'block',
height: '55px', height: '55px',
icon: image, icon:
icon === 'success' ? '__uniappsuccess.png' : '__uniapperror.png',
interval: duration, interval: duration,
} }
} else {
if (['success', 'error'].indexOf(icon) !== -1) {
waitingOptions.loading = {
display: 'block',
height: '55px',
icon:
icon === 'success' ? '__uniappsuccess.png' : '__uniapperror.png',
interval: duration,
}
}
} }
}
try { try {
toast = plus.nativeUI.showWaiting(title, extend(waitingOptions, style)) toast = plus.nativeUI.showWaiting(title, extend(waitingOptions, style))
} catch (error) { } catch (error) {
reject(`${error}`) reject(`${error}`)
}
} }
}
timeout = setTimeout(() => { timeout = setTimeout(() => {
hide('') hide('')
}, duration) }, duration)
return resolve() return resolve()
}, }
export const showToast = defineAsyncApi<API_TYPE_SHOW_TOAST>(
API_SHOW_TOAST,
_showToast,
ShowToastProtocol, ShowToastProtocol,
ShowToastOptions ShowToastOptions
) )
export const hideToast = defineAsyncApi<API_TYPE_HIDE_TOAST>( export const hideToast = defineAsyncApi<API_TYPE_HIDE_TOAST>(
API_HIDE_TOAST, API_HIDE_TOAST,
(_, { resolve, reject }) => { (_, callbacks) => hide('toast', callbacks)
callApiSync<typeof hide>(hide, 'toast', resolve, reject) )
}
export const hideLoading = defineAsyncApi<API_TYPE_HIDE_LOADING>(
API_HIDE_LOADING,
(_, callbacks) => hide('loading', callbacks)
) )
export function hide(type: string = 'toast') { function hide(
type: string = 'toast',
callbacks?: { resolve: Resolve; reject: Reject }
) {
if (type && type !== toastType) { if (type && type !== toastType) {
return return callbacks?.reject()
} }
if (timeout) { if (timeout) {
clearTimeout(timeout) clearTimeout(timeout)
...@@ -150,7 +149,5 @@ export function hide(type: string = 'toast') { ...@@ -150,7 +149,5 @@ export function hide(type: string = 'toast') {
toast = null toast = null
isShowToast = false isShowToast = false
toastType = '' toastType = ''
return { return callbacks?.resolve()
errMsg: 'hide:ok',
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册