Ver código fonte

feat:部分 utils、store、hooks 里的 uni.showToast 替换成 wd-toast

YunaiV 4 meses atrás
pai
commit
f9fc5a8abe

+ 3 - 1
src/api/infra/file/index.ts

@@ -1,3 +1,4 @@
+import { useToast } from 'wot-design-uni'
 import { http } from '@/http/http'
 import { useTokenStore } from '@/store/token'
 import { useUserStore } from '@/store/user'
@@ -66,7 +67,8 @@ export function uploadFile(filePath: string, directory?: string): Promise<string
           if (result.code === 0) {
             resolve(result.data)
           } else {
-            uni.showToast({ icon: 'none', title: result.msg || '上传失败' })
+            const toast = useToast()
+            toast.show(result.msg || '上传失败')
             reject(new Error(result.msg || '上传失败'))
           }
         } else {

+ 11 - 10
src/hooks/useUpload.ts

@@ -1,4 +1,5 @@
 import { ref } from 'vue'
+import { useToast } from 'wot-design-uni'
 import { getEnvBaseUrl } from '@/utils/index'
 
 const VITE_UPLOAD_BASEURL = `${getEnvBaseUrl()}/upload`
@@ -29,13 +30,16 @@ export default function useUpload<T extends TfileType>(options: TOptions<T> = {}
   const loading = ref(false)
   const error = ref<Error | null>(null)
   const data = ref<any>(null)
+  const toast = useToast()
 
   const handleFileChoose = ({ tempFilePath, size }: { tempFilePath: string, size: number }) => {
     if (size > maxSize) {
-      uni.showToast({
-        title: `文件大小不能超过 ${maxSize / 1024 / 1024}MB`,
-        icon: 'none',
-      })
+      // 注释 by 芋艿:使用 wd-toast 替代
+      // uni.showToast({
+      //   title: `文件大小不能超过 ${maxSize / 1024 / 1024}MB`,
+      //   icon: 'none',
+      // })
+      toast.show(`文件大小不能超过 ${maxSize / 1024 / 1024}MB`)
       return
     }
 
@@ -62,8 +66,7 @@ export default function useUpload<T extends TfileType>(options: TOptions<T> = {}
           const jsonData = JSON.parse(res)
           // 检查是否包含data字段
           parsedData = jsonData.data || jsonData
-        }
-        catch (e) {
+        } catch (e) {
           // 如果解析失败,使用原始数据
           console.log('Response is not JSON, using raw data:', res)
         }
@@ -123,8 +126,7 @@ export default function useUpload<T extends TfileType>(options: TOptions<T> = {}
       // #ifndef MP-WEIXIN
       uni.chooseImage(chooseFileOptions)
       // #endif
-    }
-    else {
+    } else {
       uni.chooseFile({
         ...chooseFileOptions,
         type: 'all',
@@ -157,8 +159,7 @@ async function uploadFile({
       try {
         const data = uploadFileRes.data
         onSuccess(data)
-      }
-      catch (err) {
+      } catch (err) {
         onError(err)
       }
     },

+ 20 - 12
src/store/token.ts

@@ -8,6 +8,7 @@ import type {
 import type { IAuthLoginRes } from '@/api/types/login'
 import { defineStore } from 'pinia'
 import { computed, ref } from 'vue' // 修复:导入 computed
+import { useToast } from 'wot-design-uni'
 import {
   login as _login,
   logout as _logout,
@@ -39,6 +40,7 @@ const tokenInfoState = isDoubleTokenMode
 export const useTokenStore = defineStore(
   'token',
   () => {
+    const toast = useToast()
     // 定义用户信息
     const tokenInfo = ref<IAuthLoginRes>({ ...tokenInfoState })
     // 设置用户信息
@@ -139,10 +141,12 @@ export const useTokenStore = defineStore(
         }
         // console.log('普通登录-res: ', res)
         await _postLogin(res)
-        uni.showToast({
-          title: `${typeName}成功`,
-          icon: 'success',
-        })
+        // 注释 by 芋艿:使用 wd-toast 替代
+        // uni.showToast({
+        //   title: `${typeName}成功`,
+        //   icon: 'success',
+        // })
+        toast.success(`${typeName}成功`)
         return res
       }
       catch (error) {
@@ -170,18 +174,22 @@ export const useTokenStore = defineStore(
         const res = await _wxLogin(code)
         console.log('微信登录-res: ', res)
         await _postLogin(res)
-        uni.showToast({
-          title: '登录成功',
-          icon: 'success',
-        })
+        // 注释 by 芋艿:使用 wd-toast 替代
+        // uni.showToast({
+        //   title: '登录成功',
+        //   icon: 'success',
+        // })
+        toast.success('登录成功')
         return res
       }
       catch (error) {
         console.error('微信登录失败:', error)
-        uni.showToast({
-          title: '微信登录失败,请重试',
-          icon: 'error',
-        })
+        // 注释 by 芋艿:使用 wd-toast 替代
+        // uni.showToast({
+        //   title: '微信登录失败,请重试',
+        //   icon: 'error',
+        // })
+        toast.error('微信登录失败,请重试')
         throw error
       }
     }

+ 21 - 14
src/utils/download.ts

@@ -3,6 +3,9 @@
  */
 
 import { isH5, isMpWeixin } from '@uni-helper/uni-env'
+import { useToast } from 'wot-design-uni'
+
+const toast = useToast()
 
 /** 保存图片到相册 */
 export async function saveImageToAlbum(url: string, fileName?: string): Promise<void> {
@@ -20,12 +23,16 @@ export async function saveImageToAlbum(url: string, fileName?: string): Promise<
           if (downloadResult.statusCode === 200) {
             saveToAlbum(downloadResult.tempFilePath, resolve, reject)
           } else {
-            uni.showToast({ icon: 'none', title: '下载失败' })
+            // 注释 by 芋艿:使用 wd-toast 替代
+            // uni.showToast({ icon: 'none', title: '下载失败' })
+            toast.show('下载失败')
             reject(new Error('Download failed'))
           }
         },
         fail: (err) => {
-          uni.showToast({ icon: 'none', title: '下载失败' })
+          // 注释 by 芋艿:使用 wd-toast 替代
+          // uni.showToast({ icon: 'none', title: '下载失败' })
+          toast.show('下载失败')
           reject(err)
         },
       })
@@ -45,10 +52,11 @@ function saveToAlbum(
   uni.saveImageToPhotosAlbum({
     filePath,
     success: () => {
-      uni.showToast({
-        icon: 'success',
-        title: '已保存到相册',
-      })
+      // uni.showToast({
+      //   icon: 'success',
+      //   title: '已保存到相册',
+      // })
+      toast.success('已保存到相册')
       resolve()
     },
     fail: (err) => {
@@ -64,23 +72,22 @@ function saveToAlbum(
                   if (settingRes.authSetting['scope.writePhotosAlbum']) {
                     // 重新尝试保存
                     saveToAlbum(filePath, resolve, reject)
-                  }
-                  else {
+                  } else {
                     reject(new Error('User denied'))
                   }
                 },
               })
-            }
-            else {
+            } else {
               reject(new Error('User cancelled'))
             }
           },
         })
       } else {
-        uni.showToast({
-          icon: 'none',
-          title: '保存失败',
-        })
+        // uni.showToast({
+        //   icon: 'none',
+        //   title: '保存失败',
+        // })
+        toast.show('保存失败')
         reject(err)
       }
     },

+ 8 - 4
src/utils/uploadFile.ts

@@ -8,6 +8,7 @@
  * 通过环境变量 VITE_UPLOAD_TYPE 配置
  */
 
+import { useToast } from 'wot-design-uni'
 import * as FileApi from '@/api/infra/file'
 
 /** 上传类型 */
@@ -163,6 +164,7 @@ export function useUpload<T = string>(url: string, formData: Record<string, any>
   const data = ref<T>()
   /** 上传进度(0-100) */
   const progress = ref(0)
+  const toast = useToast()
 
   /** 解构上传选项,设置默认值 */
   const {
@@ -192,10 +194,12 @@ export function useUpload<T = string>(url: string, formData: Record<string, any>
   const checkFileSize = (size: number) => {
     const sizeInMB = size / 1024 / 1024
     if (sizeInMB > maxSize) {
-      uni.showToast({
-        title: `文件大小不能超过${maxSize}MB`,
-        icon: 'none',
-      })
+      // 注释 by 芋艿:使用 wd-toast 替代
+      // uni.showToast({
+      //   title: `文件大小不能超过${maxSize}MB`,
+      //   icon: 'none',
+      // })
+      toast.show(`文件大小不能超过${maxSize}MB`)
       return false
     }
     return true