Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions ui/src/components/dynamics-form/items/upload/LocalFileUpload.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<div v-loading="loading" class="w-full">
<el-upload
ref="UploadRef"
:webkitdirectory="false"
class="w-full"
drag
Expand Down Expand Up @@ -65,7 +66,7 @@ import { computed, useAttrs, nextTick, inject, ref } from 'vue'
import type { FormField } from '@/components/dynamics-form/type'
import { MsgError } from '@/utils/message'
import type { UploadFiles } from 'element-plus'
import { filesize, getImgUrl } from '@/utils/common'
import { filesize, getImgUrl, fileType } from '@/utils/common'
import { t } from '@/locales'
const upload = inject('upload') as any
const attrs = useAttrs() as any
Expand All @@ -82,6 +83,7 @@ const onExceed = () => {
const emit = defineEmits(['update:modelValue'])
const fileArray = ref<any>([])
const loading = ref<boolean>(false)
const UploadRef = ref()
// 上传on-change事件
const fileHandleChange = (file: any, fileList: UploadFiles) => {
//1、判断文件大小是否合法,文件限制不能大于100M
Expand All @@ -91,6 +93,13 @@ const fileHandleChange = (file: any, fileList: UploadFiles) => {
fileList.splice(-1, 1) //移除当前超出大小的文件
return false
}
if (!file_type_list.value.includes(fileType(file.name).toLocaleUpperCase())) {
if (file?.name !== '.DS_Store') {
MsgError(t('views.document.upload.errorMessage2'))
}
fileList.splice(-1, 1)
return false
}

if (file?.size === 0) {
MsgError(t('views.document.upload.errorMessage3'))
Expand All @@ -105,10 +114,7 @@ const fileHandleChange = (file: any, fileList: UploadFiles) => {
})
}
function deleteFile(index: number) {
emit(
'update:modelValue',
props.modelValue.filter((item: any, i: number) => index != i),
)
props.modelValue.splice(index, 1)
}

const handlePreview = (bool: boolean) => {
Expand All @@ -123,7 +129,9 @@ const handlePreview = (bool: boolean) => {
const accept = computed(() => {
return (attrs.file_type_list || []).map((item: any) => '.' + item.toLowerCase()).join(',')
})

const file_type_list = computed(() => {
return attrs.file_type_list || []
})
const formats = computed(() => {
return (attrs.file_type_list || []).map((item: any) => item.toUpperCase()).join('、')
})
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are several improvements and potential issues to consider in your Vue component code:

@@ -65,7 +66,7 @@ import { computed, useAttrs, nextTick, inject, ref } from 'vue'

This line doesn't seem related to the changes you made.

Potential Issues

  1. Duplicated v-loading Usage: The v-loading directive is duplicated within the same <div>, which might not be necessary and could cause confusion.
  2. Lack of Type Checking for File Name: There's no error handling when a file name ends with .DS_Store.
  3. Missing Error Handling for Size Limitation: It would be good to add more robust validation messages or error states for size limitations beyond just removing files.

Suggestions for Optimization / Improvement

  1. Combine Destructuring and Default Values:

    const {
      attrs,
      updateModelValueEvent,
      modelValue,
    } = defineProps<{
      attrs: any;
      updateModelValueEvent?: (value: any) => void;
      modelValue: any[];
    }>({
      attrs: {},
      updateModelValueEvent: undefined,
      modelValue: [],
    });
  2. Use Composition API More Consistently:
    Ensure consistent usage of the composition API throughout the component, especially around reactive state management.

  3. Add Additional Validation Error Messages:
    You can enhance user experience by providing more descriptive error messages, especially for non-image types like spreadsheets or PDFs.

Here's an updated version incorporating some of these suggestions:

@@ -82,6 +83,7 @@ const onExceed = () => {

@@ -91,6 +93,13 @@ const fileHandleChange = (file: any, fileList: UploadFiles) => {

+  // Validate file type against list
+  if (!file_type_list.value.includes(file.type.toUpperCase())) {
+    if (file?.name !== '.DS_Store') {
+      MsgError(t('views.document.upload.errorMessage2', { fileName: file.name }));
+    }
+    fileList.splice(-1, 1);
+    return false;
+  }

@@ -105,10 +114,7 @@ const fileHandleChange = (file: any, fileList: UploadFiles) => {
}

function deleteFile(index: number) {
-  emit(
-    'update:modelValue',
-    props.modelValue.filter((item: any, i: number) => index != i),
-  );
+  emit(updateModelValueEvent ? updateModelValueEvent : 'update:modelValue', modelValue.filter(item => item.index !== index));
}

Note that I've used computed properties to access attrs.file_type_list. This makes reading them easier and prevents repeated accesses to props.attrs.

These modifications should enhance the reliability and functionality of your upload component while maintaining readability and maintainability.

Expand Down
Loading