yanghao 1 месяц назад
Родитель
Сommit
0ba8aee942

+ 22 - 5
src/components/UploadFile/src/UploadFile.vue

@@ -68,7 +68,7 @@
 </template>
 <script lang="ts" setup>
 import { propTypes } from '@/utils/propTypes'
-import type { UploadInstance, UploadProps, UploadRawFile, UploadUserFile } from 'element-plus'
+import type { UploadProps, UploadRawFile, UploadUserFile } from 'element-plus'
 import { isString } from '@/utils/is'
 import { useUpload } from '@/components/UploadFile/src/useUpload'
 import { UploadFile } from 'element-plus/es/components/upload/src/upload'
@@ -91,7 +91,6 @@ const props = defineProps({
 })
 
 // ========== 上传相关 ==========
-const uploadRef = ref<UploadInstance>()
 const uploadList = ref<UploadUserFile[]>([])
 const fileList = ref<UploadUserFile[]>([])
 const uploadNumber = ref<number>(0)
@@ -126,6 +125,18 @@ const beforeUpload: UploadProps['beforeUpload'] = (file: UploadRawFile) => {
   uploadNumber.value++
   return true
 }
+
+const getFileUrl = (value: unknown): string => {
+  if (typeof value === 'string') return value
+  if (value && typeof value === 'object') {
+    const file = value as { url?: unknown; path?: unknown; fileUrl?: unknown }
+    if (typeof file.url === 'string') return file.url
+    if (typeof file.path === 'string') return file.path
+    if (typeof file.fileUrl === 'string') return file.fileUrl
+  }
+  return ''
+}
+
 // 处理上传的文件发生变化
 // const handleFileChange = (uploadFile: UploadFile): void => {
 //   uploadRef.value.data.path = uploadFile.name
@@ -133,10 +144,12 @@ const beforeUpload: UploadProps['beforeUpload'] = (file: UploadRawFile) => {
 // 文件上传成功
 const handleFileSuccess: UploadProps['onSuccess'] = (res: any): void => {
   message.success('上传成功')
+  const url = getFileUrl(res?.data)
+  if (!url) return
   // 删除自身
-  const index = fileList.value.findIndex((item) => item.response?.data === res.data)
+  const index = fileList.value.findIndex((item) => (item.response as any)?.data === res.data)
   fileList.value.splice(index, 1)
-  uploadList.value.push({ name: res.data, url: res.data })
+  uploadList.value.push({ name: url.substring(url.lastIndexOf('/') + 1), url })
   if (uploadList.value.length == uploadNumber.value) {
     fileList.value.push(...uploadList.value)
     uploadList.value = []
@@ -185,11 +198,15 @@ watch(
     }
     // 情况2:数组
     fileList.value.push(
-      ...(val as string[]).map((url) => ({ name: url.substring(url.lastIndexOf('/') + 1), url }))
+      ...(val as any[])
+        .map((item) => getFileUrl(item))
+        .filter(Boolean)
+        .map((url) => ({ name: url.substring(url.lastIndexOf('/') + 1), url }))
     )
   },
   { immediate: true, deep: true }
 )
+
 // 发送文件链接列表更新
 const emitUpdateModelValue = () => {
   // 情况1:数组结果

+ 15 - 1
src/views/technology‌/list/index2.vue

@@ -443,6 +443,20 @@ const getDeptName = (departmentId?: number) => {
   return findDept(deptList2.value) || String(departmentId)
 }
 
+const normalizeAttachments = (attachments: unknown): string[] => {
+  if (!attachments) return []
+  const values = Array.isArray(attachments) ? attachments : [attachments]
+  return values
+    .flatMap((attachment: any) => {
+      if (typeof attachment === 'string') return attachment.split(',')
+      if (attachment && typeof attachment === 'object') {
+        return attachment.url || attachment.path || attachment.fileUrl || []
+      }
+      return []
+    })
+    .filter((url): url is string => typeof url === 'string' && url.length > 0)
+}
+
 const getList = async () => {
   loading.value = true
   try {
@@ -491,7 +505,7 @@ const openForm = async (type: 'create' | 'update', id?: number) => {
     formData.value = {
       ...createDefaultForm(),
       ...data,
-      attachments: data.attachments || [],
+      attachments: normalizeAttachments(data.attachments),
       details: Array.isArray(data.details) ? data.details : []
     }
     dialogVisible.value = true