From b8f3dc696c6907da579bc281e0682a0236c2e8d9 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期五, 24 七月 2026 14:21:20 +0800
Subject: [PATCH] refactor(upload): 重构文件上传组件以支持 blobId 存储
---
src/components/upload/use-upload.ts | 80 ++++++----------------------------------
1 files changed, 12 insertions(+), 68 deletions(-)
diff --git a/src/components/upload/use-upload.ts b/src/components/upload/use-upload.ts
index 99b79c0..4b79942 100644
--- a/src/components/upload/use-upload.ts
+++ b/src/components/upload/use-upload.ts
@@ -1,14 +1,13 @@
import type { Ref } from 'vue';
-import type { AxiosProgressEvent, InfraFileApi } from '#/api/infra/file';
+import type { AxiosProgressEvent } from '#/api/infra/file';
import { computed, unref } from 'vue';
import { useAppConfig } from '@vben/hooks';
import { $t } from '@vben/locales';
-import { createFile, getFilePresignedUrl, uploadFile } from '#/api/infra/file';
-import { baseRequestClient } from '#/api/request';
+import { requestClient } from '#/api/request';
const { apiURL } = useAppConfig(import.meta.env, import.meta.env.PROD);
@@ -97,34 +96,18 @@
// 鏄惁浣跨敤鍓嶇鐩磋繛涓婁紶
const isClientUpload =
UPLOAD_TYPE.CLIENT === import.meta.env.VITE_UPLOAD_TYPE;
- // 閲嶅啓ElUpload涓婁紶鏂规硶
+ // 閲嶅啓涓婁紶鏂规硶锛岀粺涓�璧� system 妯″潡 Storage API
async function httpRequest(
file: File,
onUploadProgress?: AxiosProgressEvent,
) {
- // 妯″紡涓�锛氬墠绔笂浼�
- if (isClientUpload) {
- // 1.1 鐢熸垚鏂囦欢鍚嶇О
- const fileName = await generateFileName(file);
- // 1.2 鑾峰彇鏂囦欢棰勭鍚嶅湴鍧�
- const presignedInfo = await getFilePresignedUrl(fileName, directory);
- // 1.3 涓婁紶鏂囦欢
- return baseRequestClient
- .put(presignedInfo.uploadUrl, file, {
- headers: {
- 'Content-Type': file.type,
- },
- })
- .then(() => {
- // 1.4. 璁板綍鏂囦欢淇℃伅鍒板悗绔紙寮傛锛�
- createFile0(presignedInfo, file);
- // 閫氱煡鎴愬姛锛屾暟鎹牸寮忎繚鎸佷笌鍚庣涓婁紶鐨勮繑鍥炵粨鏋滀竴鑷�
- return { url: presignedInfo.url };
- });
- } else {
- // 妯″紡浜岋細鍚庣涓婁紶
- return uploadFile({ file, directory }, onUploadProgress);
- }
+ const formData = new FormData();
+ formData.append('files', file);
+ const blobs = await requestClient.post('/system/storage-blob/upload', formData, {
+ onUploadProgress,
+ });
+ const blob = Array.isArray(blobs) ? blobs[0] : blobs;
+ return blob ? { url: blob.url || blob.previewURL, name: blob.name, id: blob.id } : {};
}
return {
@@ -134,47 +117,8 @@
}
/**
- * 鑾峰緱涓婁紶 URL
+ * 鑾峰緱涓婁紶 URL锛堝吋瀹规棫鐗堬紝瀹為檯璋冪敤璧� httpRequest锛�
*/
export function getUploadUrl(): string {
- return `${apiURL}/infra/file/upload`;
-}
-
-/**
- * 鍒涘缓鏂囦欢淇℃伅
- *
- * @param vo 鏂囦欢棰勭鍚嶄俊鎭�
- * @param file 鏂囦欢
- */
-function createFile0(
- vo: InfraFileApi.FilePresignedUrlRespVO,
- file: File,
-): InfraFileApi.File {
- const fileVO = {
- configId: vo.configId,
- url: vo.url,
- path: vo.path,
- name: file.name,
- type: file.type,
- size: file.size,
- };
- createFile(fileVO);
- return fileVO;
-}
-
-/**
- * 鐢熸垚鏂囦欢鍚嶇О锛堜娇鐢ㄧ畻娉昐HA256锛�
- *
- * @param file 瑕佷笂浼犵殑鏂囦欢
- */
-async function generateFileName(file: File) {
- // // 璇诲彇鏂囦欢鍐呭
- // const data = await file.arrayBuffer();
- // const wordArray = CryptoJS.lib.WordArray.create(data);
- // // 璁$畻SHA256
- // const sha256 = CryptoJS.SHA256(wordArray).toString();
- // // 鎷兼帴鍚庣紑
- // const ext = file.name.slice(Math.max(0, file.name.lastIndexOf('.')));
- // return `${sha256}${ext}`;
- return file.name;
+ return `${apiURL}/system/storage-blob/upload`;
}
--
Gitblit v1.9.3