diff --git a/src/api/infra/file/index.ts b/src/api/infra/file/index.ts index ec3180a..2d03201 100644 --- a/src/api/infra/file/index.ts +++ b/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(options: TOptions = {} const loading = ref(false) const error = ref(null) const data = ref(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(options: TOptions = {} 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(options: TOptions = {} // #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) } }, diff --git a/src/store/token.ts b/src/store/token.ts index eb4a2dd..dbe1819 100644 --- a/src/store/token.ts +++ b/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({ ...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 } } diff --git a/src/utils/download.ts b/src/utils/download.ts index 71119d6..3edeb11 100644 --- a/src/utils/download.ts +++ b/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 { @@ -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) } }, diff --git a/src/utils/uploadFile.ts b/src/utils/uploadFile.ts index 7393989..348346d 100644 --- a/src/utils/uploadFile.ts +++ b/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(url: string, formData: Record const data = ref() /** 上传进度(0-100) */ const progress = ref(0) + const toast = useToast() /** 解构上传选项,设置默认值 */ const { @@ -192,10 +194,12 @@ export function useUpload(url: string, formData: Record 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