diff --git a/packages/uni-app-plus/src/helpers/plus.ts b/packages/uni-app-plus/src/helpers/plus.ts index 308286649f39f98c0664e02066dc45fab0109fdf..2d2e4790ea641318ce7f7ee7255dcd0e8622d5ad 100644 --- a/packages/uni-app-plus/src/helpers/plus.ts +++ b/packages/uni-app-plus/src/helpers/plus.ts @@ -66,21 +66,6 @@ export function warpPlusMethod( } } -export function callApiSync any>( - api: T, - args: Parameters[0], - resolve: Function, - reject: (errMsg?: string | undefined, errRes?: any) => void -) { - api(args) - .then(() => { - resolve() - }) - .catch((errMsg: string) => { - reject(errMsg) - }) -} - export function isTabBarPage(path = '') { if (!(__uniConfig.tabBar && Array.isArray(__uniConfig.tabBar.list))) { return false diff --git a/packages/uni-app-plus/src/service/api/ui/popup/showToast.ts b/packages/uni-app-plus/src/service/api/ui/popup/showToast.ts index dd410d7ace4b494339b4b6df1aadea563f4a64cb..57ee348b1bb32a4bce4782d4f843c1c3d15921f9 100644 --- a/packages/uni-app-plus/src/service/api/ui/popup/showToast.ts +++ b/packages/uni-app-plus/src/service/api/ui/popup/showToast.ts @@ -1,5 +1,4 @@ import { extend } from '@vue/shared' -import { callApiSync } from '../../../../helpers/plus' import { defineAsyncApi, ShowToastOptions, @@ -16,6 +15,9 @@ import { API_HIDE_LOADING, } from '@dcloudio/uni-api' +type Resolve = (res?: any) => void +type Reject = (errMsg?: string, errRes?: any) => void + type ToastType = 'loading' | 'toast' | '' let toast: PlusNativeUIWaitingObj | null @@ -25,118 +27,115 @@ let timeout: number | null export const showLoading = defineAsyncApi( API_SHOW_LOADING, - (args, { resolve, reject }) => { - callApiSync( - showToast, + (args, callbacks) => + _showToast( extend({}, args, { type: 'loading', }), - resolve, - reject - ) - }, + callbacks + ), ShowLoadingProtocol, ShowLoadingOptions ) -export const hideLoading = defineAsyncApi( - API_HIDE_LOADING, - (_, { resolve, reject }) => { - callApiSync(hide, 'loading', resolve, reject) - } -) - -export const showToast = defineAsyncApi( - API_SHOW_TOAST, - ( - { - title = '', - icon = 'success', - image = '', - duration = 1500, - mask = false, - position, - // @ts-ignore ToastType - type = 'toast', - // @ts-ignore PlusNativeUIWaitingStyles - style, - }, - { resolve, reject } - ) => { - hide('') - toastType = type - if (['top', 'center', 'bottom'].includes(String(position))) { - // 仅可以关闭 richtext 类型,但 iOS 部分情况换行显示有问题 - plus.nativeUI.toast(title, { - verticalAlign: position, - }) - isShowToast = true - } else { - if (icon && !~['success', 'loading', 'error', 'none'].indexOf(icon)) { - icon = 'success' - } - const waitingOptions: PlusNativeUIWaitingStyles = { - modal: mask, - back: 'transmit', - padding: '10px', - size: '16px', // 固定字体大小 +const _showToast = ( + { + title = '', + icon = 'success', + image = '', + duration = 1500, + mask = false, + position, + // @ts-ignore ToastType + type = 'toast', + // @ts-ignore PlusNativeUIWaitingStyles + style, + }: UniApp.ShowToastOptions, + { resolve, reject }: { resolve: Resolve; reject: Reject } +) => { + hide('') + toastType = type + if (['top', 'center', 'bottom'].includes(String(position))) { + // 仅可以关闭 richtext 类型,但 iOS 部分情况换行显示有问题 + plus.nativeUI.toast(title, { + verticalAlign: position, + }) + isShowToast = true + } else { + if (icon && !~['success', 'loading', 'error', 'none'].indexOf(icon)) { + icon = 'success' + } + const waitingOptions: PlusNativeUIWaitingStyles = { + modal: mask, + back: 'transmit', + padding: '10px', + size: '16px', // 固定字体大小 + } + if (!image && (!icon || icon === 'none')) { + // 无图 + // waitingOptions.width = '120px' + // waitingOptions.height = '40px' + waitingOptions.loading = { + display: 'none', } - if (!image && (!icon || icon === 'none')) { - // 无图 - // waitingOptions.width = '120px' - // waitingOptions.height = '40px' - waitingOptions.loading = { - display: 'none', - } - } else { - waitingOptions.width = '140px' - waitingOptions.height = '112px' + } else { + waitingOptions.width = '140px' + waitingOptions.height = '112px' + } + if (image) { + waitingOptions.loading = { + display: 'block', + height: '55px', + icon: image, + interval: duration, } - if (image) { + } else { + if (['success', 'error'].indexOf(icon) !== -1) { waitingOptions.loading = { display: 'block', height: '55px', - icon: image, + icon: + icon === 'success' ? '__uniappsuccess.png' : '__uniapperror.png', 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 { - toast = plus.nativeUI.showWaiting(title, extend(waitingOptions, style)) - } catch (error) { - reject(`${error}`) - } + try { + toast = plus.nativeUI.showWaiting(title, extend(waitingOptions, style)) + } catch (error) { + reject(`${error}`) } + } - timeout = setTimeout(() => { - hide('') - }, duration) - return resolve() - }, + timeout = setTimeout(() => { + hide('') + }, duration) + return resolve() +} +export const showToast = defineAsyncApi( + API_SHOW_TOAST, + _showToast, ShowToastProtocol, ShowToastOptions ) export const hideToast = defineAsyncApi( API_HIDE_TOAST, - (_, { resolve, reject }) => { - callApiSync(hide, 'toast', resolve, reject) - } + (_, callbacks) => hide('toast', callbacks) +) + +export const hideLoading = defineAsyncApi( + 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) { - return + return callbacks?.reject() } if (timeout) { clearTimeout(timeout) @@ -150,7 +149,5 @@ export function hide(type: string = 'toast') { toast = null isShowToast = false toastType = '' - return { - errMsg: 'hide:ok', - } + return callbacks?.resolve() }