|
@@ -25,15 +25,62 @@ export function showToast(options: ToastOptions | string) {
|
|
|
= typeof options === 'string'
|
|
|
? { ...defaultOptions, message: options }
|
|
|
: { ...defaultOptions, ...options }
|
|
|
+
|
|
|
+ // #ifdef APP-PLUS
|
|
|
+
|
|
|
+ // 关闭上一次的提示
|
|
|
+ plus.nativeUI.closeToast()
|
|
|
+
|
|
|
+ // 使用 plus.nativeUI.toast 实现
|
|
|
+ // 映射 position 到 plus.nativeUI.toast 支持的格式
|
|
|
+ const positionMap: Record<ToastOptions['position'], { vertical: string, horizontal: string }> = {
|
|
|
+ top: { vertical: 'top', horizontal: 'center' },
|
|
|
+ middle: { vertical: 'center', horizontal: 'center' },
|
|
|
+ bottom: { vertical: 'bottom', horizontal: 'center' },
|
|
|
+ }
|
|
|
+
|
|
|
+ // 映射图标类型
|
|
|
+ const plusIconMap: Record<
|
|
|
+ ToastType,
|
|
|
+ 'success' | 'error' | 'none' | 'loading' | 'fail' | 'exception'
|
|
|
+ > = {
|
|
|
+ success: 'success',
|
|
|
+ error: 'error',
|
|
|
+ warning: 'fail',
|
|
|
+ info: 'none',
|
|
|
+ }
|
|
|
+ // 安卓平台添加适当偏移,iOS保持默认
|
|
|
+ const platform = plus.os.name
|
|
|
+ const verticalOffset = platform === 'Android' ? '50%' : 'center'
|
|
|
+ // 构造 ToastStyles 配置项
|
|
|
+ const toastOptions: any = {
|
|
|
+ duration: mergedOptions.duration >= 3000 ? 'long' : 'short',
|
|
|
+ ...positionMap[mergedOptions.position],
|
|
|
+ align: 'center', // 文字居中对齐
|
|
|
+ verticalAlign: verticalOffset, // 垂直位置补偿
|
|
|
+ background: 'rgba(0,0,0,0.7)',
|
|
|
+ type: 'richtext',
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果有图标配置,则添加图标
|
|
|
+ if (mergedOptions.icon || plusIconMap[mergedOptions.type]) {
|
|
|
+ toastOptions.icon = mergedOptions.icon || plusIconMap[mergedOptions.type]
|
|
|
+ }
|
|
|
+
|
|
|
+ plus.nativeUI.toast(`<font style=\"color:#fff;font-size:16px;\">${mergedOptions.message}</font>`, toastOptions)
|
|
|
+ return
|
|
|
+ // #endif
|
|
|
+
|
|
|
+ // #ifndef APP-PLUS
|
|
|
// 映射position到uniapp支持的格式
|
|
|
- const positionMap: Record<ToastOptions['position'], 'top' | 'bottom' | 'center'> = {
|
|
|
+ const uniPositionMap: Record<ToastOptions['position'], 'top' | 'bottom' | 'center'> = {
|
|
|
top: 'top',
|
|
|
middle: 'center',
|
|
|
bottom: 'bottom',
|
|
|
}
|
|
|
|
|
|
// 映射图标类型
|
|
|
- const iconMap: Record<
|
|
|
+ const uniIconMap: Record<
|
|
|
ToastType,
|
|
|
'success' | 'error' | 'none' | 'loading' | 'fail' | 'exception'
|
|
|
> = {
|
|
@@ -47,10 +94,11 @@ export function showToast(options: ToastOptions | string) {
|
|
|
uni.showToast({
|
|
|
title: mergedOptions.message,
|
|
|
duration: mergedOptions.duration,
|
|
|
- position: positionMap[mergedOptions.position],
|
|
|
- icon: mergedOptions.icon || iconMap[mergedOptions.type],
|
|
|
+ position: uniPositionMap[mergedOptions.position],
|
|
|
+ icon: mergedOptions.icon || uniIconMap[mergedOptions.type],
|
|
|
mask: true,
|
|
|
})
|
|
|
+ // #endif
|
|
|
}
|
|
|
|
|
|
export const toast = {
|