From cc06bb52cedbb01e2659c50ccb8714b2fbd21531 Mon Sep 17 00:00:00 2001
From: yyb <995253665@qq.com>
Date: 星期五, 24 四月 2026 16:11:53 +0800
Subject: [PATCH] 优化文件访问URL的处理逻辑,新增对视频文件的判断,简化文件类型检查,提升文件预览功能的稳定性和可读性。

---
 src/pages/inspectionUpload/index.vue |   93 +++++++++++++++++++++++++++-------------------
 1 files changed, 54 insertions(+), 39 deletions(-)

diff --git a/src/pages/inspectionUpload/index.vue b/src/pages/inspectionUpload/index.vue
index 135244b..badcebb 100644
--- a/src/pages/inspectionUpload/index.vue
+++ b/src/pages/inspectionUpload/index.vue
@@ -189,11 +189,11 @@
                       :key="index"
                       class="file-item">
                   <view class="file-preview-container">
-                    <image v-if="file.type === 'image' || (file.type !== 'video' && !file.type)"
-                           :src="file.url || file.tempFilePath || file.path || file.downloadUrl"
+                    <image v-if="isImageFile(file)"
+                           :src="getFileAccessUrl(file)"
                            class="file-preview"
                            mode="aspectFill" />
-                    <view v-else-if="file.type === 'video'"
+                    <view v-else-if="isVideoFile(file)"
                           class="video-preview">
                       <uni-icons type="videocam"
                                  name="videocam"
@@ -211,7 +211,7 @@
                     </view>
                   </view>
                   <view class="file-info">
-                    <text class="file-name">{{ file.bucketFilename || file.name || (file.type === 'image' ? '鍥剧墖' : '瑙嗛')
+                    <text class="file-name">{{ file.bucketFilename || file.name || (isImageFile(file) ? '鍥剧墖' : '瑙嗛')
                       }}</text>
                     <text class="file-size">{{ formatFileSize(file.size) }}</text>
                   </view>
@@ -291,7 +291,7 @@
                       @click="previewAttachment(file)">
                   <view class="attachment-preview-container">
                     <image v-if="file.type === 'image' || isImageFile(file)"
-                           :src="file.url || file.downloadUrl"
+                           :src="getFileAccessUrl(file)"
                            class="attachment-preview"
                            mode="aspectFill" />
                     <view v-else
@@ -335,7 +335,7 @@
         </view>
         <view class="video-modal-body">
           <video v-if="currentVideoFile"
-                 :src="currentVideoFile.url || currentVideoFile.downloadUrl"
+                 :src="getFileAccessUrl(currentVideoFile)"
                  class="video-player"
                  controls
                  autoplay
@@ -900,7 +900,7 @@
         : allList.filter(f => f?.type === 12);
 
       const mapToViewFile = (file, viewType) => {
-        const u = normalizeFileUrl(file?.url || file?.downloadUrl || "");
+        const u = getFileAccessUrl(file);
         return {
           ...file,
           // 鐢ㄤ簬涓夋爣绛鹃〉鍒嗙粍锛�0=鐢熶骇鍓� 1=鐢熶骇涓� 2=鐢熶骇鍚�
@@ -978,8 +978,8 @@
       return true;
     }
 
-    // 妫�鏌ュ師鏈夌殑type瀛楁
-    if (file.type === "image") return true;
+    // 妫�鏌ュ師鏈夌殑type瀛楁锛堟垨淇濈暀鐨勫獟浣撶被鍨嬶級
+    if (file.type === "image" || file.mediaType === "image") return true;
 
     // 妫�鏌ユ枃浠舵墿灞曞悕
     const name = file.bucketFilename || file.originalFilename || file.name || "";
@@ -987,39 +987,53 @@
     return ["jpg", "jpeg", "png", "gif", "webp"].includes(ext);
   };
 
+  // 鍒ゆ柇鏄惁涓鸿棰戞枃浠�
+  const isVideoFile = file => {
+    if (!file) return false;
+    if (file.type === "video" || file.mediaType === "video") return true;
+    const name = file.bucketFilename || file.originalFilename || file.name || "";
+    const ext = name.split(".").pop()?.toLowerCase();
+    return ["mp4", "mov", "avi", "wmv", "mkv", "webm"].includes(ext);
+  };
+
   // 鏂囦欢璁块棶鍩虹鍩燂紙鍚庣瑕佹眰鍓嶇紑锛�
   const filePreviewBase = config.fileUrl;
 
-  // 灏嗗悗绔繑鍥炵殑鏂囦欢鍦板潃瑙勮寖鎴愬彲璁块棶URL
-  // 鍏煎鍦烘櫙锛�
-  // - 宸茬粡鏄� http/https锛氱洿鎺ヨ繑鍥�
-  // - 浠� / 寮�澶达細鎷兼帴 filePreviewBase
-  // - Windows 鏈湴璺緞锛堝 D:\ruoyi\prod\uploads...\xx.jpg锛夛細灏濊瘯鎴彇 prod 涔嬪悗鐨勭浉瀵硅矾寰勫苟鎷兼帴 filePreviewBase
-  const normalizeFileUrl = rawUrl => {
-    try {
-      if (!rawUrl || typeof rawUrl !== "string") return "";
-      const url = rawUrl.trim();
-      if (!url) return "";
-      if (/^https?:\/\//i.test(url)) return url;
-      if (url.startsWith("/")) return `${filePreviewBase}${url}`;
+  const normalizeFileUrl = (rawUrl = "") => {
+    let fileUrl = rawUrl || "";
+    const javaApi = filePreviewBase;
+    const localPrefixes = ["wxfile://", "file://", "content://", "blob:", "data:"];
 
-      // Windows path -> web path
-      if (/^[a-zA-Z]:\\/.test(url)) {
-        const normalized = url.replace(/\\/g, "/");
-        const idx = normalized.indexOf("/prod/");
-        if (idx >= 0) {
-          const relative = normalized.slice(idx + "/prod/".length);
-          return `${filePreviewBase}/${relative}`;
-        }
-        // 鍏滃簳锛氭棤娉曟帹鏂槧灏勮鍒欐椂锛岃嚦灏戞妸鍙嶆枩鏉犲彉鎴愭鏂滄潬
-        return normalized;
-      }
-
-      // 鍏朵粬鐩稿璺緞锛氱洿鎺ョ敤 baseUrl 鎷间竴涓�
-      return `${filePreviewBase}/${url.replace(/^\//, "")}`;
-    } catch (e) {
-      return rawUrl || "";
+    if (localPrefixes.some(prefix => fileUrl.startsWith(prefix))) {
+      return fileUrl;
     }
+
+    if (fileUrl && fileUrl.indexOf("\\") > -1) {
+      const lowerPath = fileUrl.toLowerCase();
+      const uploadPathIndex = lowerPath.indexOf("uploadpath");
+
+      if (uploadPathIndex > -1) {
+        fileUrl = fileUrl.substring(uploadPathIndex).replace(/\\/g, "/");
+      } else {
+        fileUrl = fileUrl.replace(/\\/g, "/");
+      }
+    }
+    fileUrl = fileUrl.replace(/^\/?uploadPath/, "/profile");
+
+    if (fileUrl && !fileUrl.startsWith("http")) {
+      if (!fileUrl.startsWith("/")) fileUrl = "/" + fileUrl;
+      fileUrl = javaApi + fileUrl;
+    }
+
+    return fileUrl;
+  };
+
+  const getFileAccessUrl = (file = {}) => {
+    if (file?.link) {
+      if (String(file.link).startsWith("http")) return file.link;
+      return normalizeFileUrl(file.link);
+    }
+    return normalizeFileUrl(file?.url || file?.downloadUrl || "");
   };
 
   // 棰勮闄勪欢
@@ -1028,11 +1042,11 @@
       // 棰勮鍥剧墖
       const imageUrls = getCurrentViewAttachments()
         .filter(f => isImageFile(f))
-        .map(f => f.url || f.downloadUrl);
+        .map(f => getFileAccessUrl(f));
 
       uni.previewImage({
         urls: imageUrls,
-        current: file.url || file.downloadUrl,
+        current: getFileAccessUrl(file),
       });
     } else {
       // 棰勮瑙嗛 - 鏄剧ず瑙嗛鎾斁寮圭獥
@@ -1493,6 +1507,7 @@
       downloadUrl: uploadedFile.downloadUrl || uploadedFile.url,
       size: uploadedFile.size || uploadedFile.byteSize || file.size,
       createTime: uploadedFile.createTime || new Date().getTime(),
+      mediaType: file.type || uploadedFile.mediaType,
       type: typeValue, // 娣诲姞绫诲瀷瀛楁锛�0=鐢熶骇鍓�, 1=鐢熶骇涓�, 2=鐢熶骇鍚�
     };
 

--
Gitblit v1.9.3