From 2b3715f1610b4176d7abe33e34542389cef61853 Mon Sep 17 00:00:00 2001 From: zhuguifei <zhuguifei@zhuguifeideiMac.local> Date: 星期六, 12 四月 2025 17:12:22 +0800 Subject: [PATCH] Merge branch 'main' of http://lanpucloud.cn:1111/r/eims-master --- eims-ui-mobile/src/hooks/useUpload.ts | 69 ++++++++++++++++++++++++++++++++++ 1 files changed, 69 insertions(+), 0 deletions(-) diff --git a/eims-ui-mobile/src/hooks/useUpload.ts b/eims-ui-mobile/src/hooks/useUpload.ts new file mode 100644 index 0000000..adc083d --- /dev/null +++ b/eims-ui-mobile/src/hooks/useUpload.ts @@ -0,0 +1,69 @@ +// TODO: 鍒繕鍔犳洿鏀圭幆澧冨彉閲忕殑 VITE_UPLOAD_BASEURL 鍦板潃銆� +import { getEnvBaseUploadUrl } from '@/utils' + +const VITE_UPLOAD_BASEURL = `${getEnvBaseUploadUrl()}` + +/** + * useUpload 鏄竴涓畾鍒跺寲鐨勮姹傞挬瀛愶紝鐢ㄤ簬澶勭悊涓婁紶鍥剧墖銆� + * @param formData 棰濆浼犻�掔粰鍚庡彴鐨勬暟鎹紝濡倇name: '鑿查附'}銆� + * @returns 杩斿洖涓�涓璞loading, error, data, run}锛屽寘鍚姹傜殑鍔犺浇鐘舵�併�侀敊璇俊鎭�佸搷搴旀暟鎹拰鎵嬪姩瑙﹀彂璇锋眰鐨勫嚱鏁般�� + */ +export default function useUpload<T = string>(formData: Record<string, any> = {}) { + const loading = ref(false) + const error = ref(false) + const data = ref<T>() + const run = () => { + // #ifdef MP-WEIXIN + // 寰俊灏忕▼搴忎粠鍩虹搴� 2.21.0 寮�濮嬶紝 wx.chooseImage 鍋滄缁存姢锛岃浣跨敤 uni.chooseMedia 浠f浛銆� + // 寰俊灏忕▼搴忓湪2023骞�10鏈�17鏃ヤ箣鍚庯紝浣跨敤鏈珹PI闇�瑕侀厤缃殣绉佸崗璁� + uni.chooseMedia({ + count: 1, + mediaType: ['image'], + success: (res) => { + loading.value = true + const tempFilePath = res.tempFiles[0].tempFilePath + uploadFile<T>({ tempFilePath, formData, data, error, loading }) + }, + fail: (err) => { + console.error('uni.chooseMedia err->', err) + error.value = true + }, + }) + // #endif + // #ifndef MP-WEIXIN + uni.chooseImage({ + count: 1, + success: (res) => { + loading.value = true + const tempFilePath = res.tempFilePaths[0] + uploadFile<T>({ tempFilePath, formData, data, error, loading }) + }, + fail: (err) => { + console.error('uni.chooseImage err->', err) + error.value = true + }, + }) + // #endif + } + + return { loading, error, data, run } +} + +function uploadFile<T>({ tempFilePath, formData, data, error, loading }) { + uni.uploadFile({ + url: VITE_UPLOAD_BASEURL, + filePath: tempFilePath, + name: 'file', + formData, + success: (uploadFileRes) => { + data.value = uploadFileRes.data as T + }, + fail: (err) => { + console.error('uni.uploadFile err->', err) + error.value = true + }, + complete: () => { + loading.value = false + }, + }) +} -- Gitblit v1.9.3