From c92894037064ef6064acd6c9519e872361d50f1c Mon Sep 17 00:00:00 2001
From: gaoluyang <2820782392@qq.com>
Date: 星期二, 27 一月 2026 09:55:34 +0800
Subject: [PATCH] 进销存升级app: 1.巡检上传修改

---
 src/pages/inspectionUpload/components/formDia.vue |  148 ++++++++++++++++++++++++++++++++++---------------
 1 files changed, 103 insertions(+), 45 deletions(-)

diff --git a/src/pages/inspectionUpload/components/formDia.vue b/src/pages/inspectionUpload/components/formDia.vue
index 41837cb..6b900cd 100644
--- a/src/pages/inspectionUpload/components/formDia.vue
+++ b/src/pages/inspectionUpload/components/formDia.vue
@@ -21,8 +21,8 @@
             name="before"
             multiple
             :maxCount="10"
-            :maxSize="1024 * 1024"
-            accept="video/*"
+            :maxSize="5 * 1024 * 1024"
+            accept="image/*"
             :previewFullImage="true"
           ></u-upload>
         </view>
@@ -36,8 +36,8 @@
             name="after"
             multiple
             :maxCount="10"
-            :maxSize="1024 * 1024"
-            accept="video/*"
+            :maxSize="5 * 1024 * 1024"
+            accept="image/*"
             :previewFullImage="true"
           ></u-upload>
         </view>
@@ -51,8 +51,8 @@
             name="issue"
             multiple
             :maxCount="10"
-            :maxSize="1024 * 1024"
-            accept="video/*"
+            :maxSize="5 * 1024 * 1024"
+            accept="image/*"
             :previewFullImage="true"
           ></u-upload>
         </view>
@@ -67,8 +67,10 @@
 </template>
 
 <script setup>
-import { ref } from 'vue'
+import { ref, computed } from 'vue'
 import { submitInspectionRecord } from '@/api/equipmentManagement/inspection.js'
+import { getToken } from '@/utils/auth'
+import config from '@/config'
 
 const emit = defineEmits(['closeDia'])
 
@@ -78,50 +80,106 @@
 const issueModelValue = ref([])
 const infoData = ref(null)
 
+// 璁$畻涓婁紶URL
+const uploadFileUrl = computed(() => {
+  let baseUrl = '';
+  
+  if (process.env.VUE_APP_BASE_API) {
+    baseUrl = process.env.VUE_APP_BASE_API;
+  } else {
+    baseUrl = config.baseUrl;
+  }
+  
+  return baseUrl + '/file/upload';
+})
+
+const uploadSingleFile = async (fileItem, typeValue) => {
+  const token = getToken()
+  if (!token) throw new Error('鐢ㄦ埛鏈櫥褰�')
+
+  // H5: u-upload 鍙兘缁欏師鐢� File锛坒ileItem.file锛�
+  const rawFile = fileItem?.file
+  if (rawFile) {
+    const formData = new FormData()
+    formData.append('file', rawFile, rawFile.name || 'image.jpg')
+    formData.append('type', String(typeValue))
+    const res = await fetch(uploadFileUrl.value, {
+      method: 'POST',
+      headers: { Authorization: 'Bearer ' + token },
+      body: formData
+    })
+    const data = await res.json()
+    if (data?.code !== 200) throw new Error(data?.msg || '涓婁紶澶辫触')
+    return {
+      url: data?.data?.url,
+      name: rawFile.name || 'image.jpg',
+      status: 'success'
+    }
+  }
+
+  // 闈� H5 / 鍏煎锛氳蛋 uni.uploadFile
+  return await new Promise((resolve, reject) => {
+    uni.uploadFile({
+      url: uploadFileUrl.value,
+      filePath: fileItem.url,
+      name: 'file',
+      header: {
+        'Authorization': `Bearer ${token}`
+      },
+      formData: {
+        type: typeValue
+      },
+      success: (res) => {
+        try {
+          const data = JSON.parse(res.data)
+          if (data.code === 200) {
+            resolve({
+              url: data.data.url,
+              name: fileItem.name,
+              status: 'success'
+            })
+          } else {
+            reject(new Error(data.msg || '涓婁紶澶辫触'))
+          }
+        } catch (e) {
+          reject(e)
+        }
+      },
+      fail: (err) => reject(err)
+    })
+  })
+}
+
 // 鏂囦欢涓婁紶澶勭悊
 const afterRead = (event) => {
   const { name, file } = event
   
-  // 涓婁紶鏂囦欢鍒版湇鍔″櫒
-  uni.uploadFile({
-    url: '/api/upload', // 鏇挎崲涓哄疄闄呯殑涓婁紶鎺ュ彛
-    filePath: file.url,
-    name: 'file',
-    success: (res) => {
-      const data = JSON.parse(res.data)
-      if (data.code === 200) {
-        const fileItem = {
-          url: data.data.url,
-          name: file.name,
-          status: 'success'
-        }
-        
-        // 鏍规嵁name娣诲姞鍒板搴旂殑鏁扮粍
-        if (name === 'before') {
-          beforeModelValue.value.push(fileItem)
-        } else if (name === 'after') {
-          afterModelValue.value.push(fileItem)
-        } else if (name === 'issue') {
-          issueModelValue.value.push(fileItem)
-        }
-        
-        uni.showToast({
-          title: '涓婁紶鎴愬姛',
-          icon: 'success'
-        })
-      } else {
-        uni.showToast({
-          title: '涓婁紶澶辫触',
-          icon: 'error'
-        })
+  // 鏍规嵁涓婁紶绫诲瀷璁剧疆涓嶅悓鐨則ype鍊�
+  let typeValue = 10 // 榛樿鍊�
+  if (name === 'before') {
+    typeValue = 10 // 鐢熶骇鍓�
+  } else if (name === 'after') {
+    typeValue = 11 // 鐢熶骇涓�
+  } else if (name === 'issue') {
+    typeValue = 12 // 鐢熶骇鍚�
+  }
+
+  const files = Array.isArray(file) ? file : [file]
+  Promise.resolve().then(async () => {
+    for (const f of files) {
+      const uploaded = await uploadSingleFile(f, typeValue)
+      if (name === 'before') {
+        beforeModelValue.value.push(uploaded)
+      } else if (name === 'after') {
+        afterModelValue.value.push(uploaded)
+      } else if (name === 'issue') {
+        issueModelValue.value.push(uploaded)
       }
-    },
-    fail: () => {
-      uni.showToast({
-        title: '涓婁紶澶辫触',
-        icon: 'error'
-      })
     }
+    uni.showToast({ title: '涓婁紶鎴愬姛', icon: 'success' })
+  }).catch((err) => {
+    console.error('涓婁紶澶辫触:', err)
+    uni.showToast({ title: '涓婁紶澶辫触', icon: 'error' })
   })
 }
 

--
Gitblit v1.9.3