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 |  118 +++++++++++++++++++++++++++++++++++++---------------------
 1 files changed, 75 insertions(+), 43 deletions(-)

diff --git a/src/pages/inspectionUpload/index.vue b/src/pages/inspectionUpload/index.vue
index 73749a7..badcebb 100644
--- a/src/pages/inspectionUpload/index.vue
+++ b/src/pages/inspectionUpload/index.vue
@@ -1,7 +1,7 @@
 <template>
   <view class="inspection-upload-page">
     <!-- 椤甸潰澶撮儴 -->
-    <PageHeader title="宸℃绠$悊"
+    <PageHeader title="璁惧宸℃"
                 @back="goBack" />
     <!-- 鏁版嵁鍒楄〃 -->
     <view class="table-section">
@@ -16,7 +16,7 @@
               <text class="task-location">{{ item.inspectionLocation }}</text>
             </view>
             <view class="task-actions">
-              <u-button type="primary"
+              <!-- <u-button type="primary"
                         size="small"
                         @click.stop="startScanForTask(item)"
                         :customStyle="{
@@ -26,6 +26,17 @@
                 marginRight: '8px'
               }">
                 鎵爜涓婁紶
+              </u-button> -->
+              <u-button type="primary"
+                        size="small"
+                        @click.stop="startUploadForTask(item)"
+                        :customStyle="{
+                borderRadius: '15px',
+                height: '30px',
+                fontSize: '12px',
+                marginRight: '8px'
+              }">
+                鍥剧墖涓婁紶
               </u-button>
               <u-button type="success"
                         size="small"
@@ -178,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"
@@ -200,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>
@@ -280,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
@@ -324,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
@@ -408,7 +419,7 @@
 
   // 璁$畻涓婁紶URL
   const uploadFileUrl = computed(() => {
-    const baseUrl = "http://114.132.189.42:9030";
+    const baseUrl = config.baseUrl;
 
     return baseUrl + uploadConfig.action;
   });
@@ -855,6 +866,12 @@
     }
   };
 
+  // 鍥剧墖涓婁紶(鍙�夋嫨鍥剧墖涓婁紶鎴栬�呮槸鐩告満鎷嶇収)
+  const startUploadForTask = async (task, type) => {
+    // 鐩存帴鎵撳紑涓婁紶寮圭獥
+    openUploadDialog(task);
+  };
+
   // 鏌ョ湅闄勪欢
   const viewAttachments = async task => {
     try {
@@ -883,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=鐢熶骇鍚�
@@ -961,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 || "";
@@ -970,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 = "http://114.132.189.42:9098";
+  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 || "");
   };
 
   // 棰勮闄勪欢
@@ -1011,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 {
       // 棰勮瑙嗛 - 鏄剧ず瑙嗛鎾斁寮圭獥
@@ -1476,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