Prechádzať zdrojové kódy

feat: app 提示兼容

liangan 3 týždňov pred
rodič
commit
9c7c9aa585
2 zmenil súbory, kde vykonal 53 pridanie a 5 odobranie
  1. 1 1
      package.json
  2. 52 4
      src/utils/toast.ts

+ 1 - 1
package.json

@@ -4,7 +4,7 @@
   "version": "3.2.0",
   "packageManager": "pnpm@10.10.0",
   "description": "BandhuBuy - APP",
-  "update-time": "2025-09-10",
+  "update-time": "2025-09-11",
   "author": {
     "name": "feige996",
     "zhName": "菲鸽",

+ 52 - 4
src/utils/toast.ts

@@ -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 = {