From b5610241bf38760ef678d9d813b23e858c76aa43 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期四, 06 八月 2026 17:59:38 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev_pro2.0' into dev_pro2.0

---
 src/components/upload/file-upload.vue |   51 +++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 43 insertions(+), 8 deletions(-)

diff --git a/src/components/upload/file-upload.vue b/src/components/upload/file-upload.vue
index 35c7fe6..b095bdd 100644
--- a/src/components/upload/file-upload.vue
+++ b/src/components/upload/file-upload.vue
@@ -10,9 +10,12 @@
 
 import { IconifyIcon } from '@vben/icons';
 import { $t } from '@vben/locales';
-import { checkFileType, isFunction, isObject, isString } from '@vben/utils';
+import { checkFileType, isFunction, isImage, isObject, isString } from '@vben/utils';
 
 import { Button, message, Upload } from 'ant-design-vue';
+
+import { requestClient } from '#/api/request';
+import { FilePreviewModal, useFilePreview } from '#/components/file-preview';
 
 import { UploadResultStatus } from './typing';
 import { useUpload, useUploadType } from './use-upload';
@@ -84,23 +87,29 @@
       } else {
         value.push(v);
       }
+      const existingMap = new Map<string | number, UploadFile>();
+      for (const f of fileList.value ?? []) {
+        const id = (f as any)?.response?.[props.valueKey] ?? (f as any)?.[props.valueKey] ?? f?.uid;
+        if (id != null) existingMap.set(id, f);
+      }
       fileList.value = value
         .map((item, i) => {
           if (item && isString(item)) {
-            return {
+            return existingMap.get(item) ?? {
               uid: `${-i}`,
               name: item.slice(Math.max(0, item.lastIndexOf('/') + 1)),
               status: UploadResultStatus.DONE,
               url: item,
             };
           } else if (item && isObject(item)) {
-            return item;
+            const key = item[props.valueKey] ?? item.uid;
+            return (key != null && existingMap.get(key)) || { ...item, url: String((item as any).url ?? '') };
           } else if (item && (typeof item === 'number' || typeof item === 'string')) {
-            return {
+            return existingMap.get(item) ?? {
               uid: `${-i}`,
               name: `鏂囦欢 ${String(item)}`,
               status: UploadResultStatus.DONE,
-              url: item,
+              url: String(item),
               response: { [props.valueKey]: item },
             } as UploadFile;
           }
@@ -133,9 +142,30 @@
   }
 }
 
+const { openPreview, previewState } = useFilePreview();
+
 /** 澶勭悊鏂囦欢棰勮 */
-function handlePreview(file: UploadFile) {
-  emit('preview', file);
+async function handlePreview(file: UploadFile) {
+  const url = (file as any)?.url || (file as any)?.response?.url || (file as any)?.response;
+  if (!url) {
+    message.warning('鏂囦欢鍦板潃涓嶅瓨鍦紝鏃犳硶棰勮');
+    return;
+  }
+  const name = file.name || (file as any)?.fileName || '';
+  if (isImage(name || url)) {
+    window.open(url, '_blank');
+    return;
+  }
+  try {
+    // 灏嗙粷瀵� URL 杞负鐩稿璺緞锛岃蛋 Vite 浠g悊閬垮厤璺ㄥ煙
+    const path = typeof url === 'string' ? url.replace(/^https?:\/\/[^/]+/, '') : url;
+    const blob = await requestClient.download(path);
+    const blobUrl = URL.createObjectURL(blob as Blob);
+    openPreview(blobUrl, name);
+    emit('preview', file);
+  } catch {
+    message.error('鏂囦欢鍔犺浇澶辫触');
+  }
 }
 
 /** 澶勭悊鏂囦欢鏁伴噺瓒呴檺 */
@@ -207,7 +237,7 @@
     fileList.value?.splice(index!, 1);
   }
 
-  const fileUrl = res?.url || res?.data || res;
+  const fileUrl = res?.url || res?.data || (typeof res === 'string' ? res : '');
   uploadList.value.push({
     name: res?.name || file.name,
     url: fileUrl,
@@ -313,6 +343,11 @@
         鏍煎紡鏂囦欢
       </div>
     </Upload>
+    <FilePreviewModal
+      v-model:visible="previewState.visible.value"
+      :file-url="previewState.fileUrl.value"
+      :file-name="previewState.fileName.value"
+    />
   </div>
 </template>
 

--
Gitblit v1.9.3