zhanghua
2024-11-20 293979a28d7585c23438edb7741500426c4e71bd
src/components/FileUpload/index.vue
@@ -12,13 +12,14 @@
      ref="fileUploadRef"
      :auto-upload="false"
      :on-exceed="handleExceed"
      :before-upload="handleBeforeUpload"
    >
      <!-- 上传按钮 -->
      <el-button type="primary">选取文件</el-button>
    </el-upload>
    <!-- 上传提示 -->
    <div class="el-upload__tip" v-if="showTip">
      <span>请上传文件</span>
      <span>上传文件大小不能超过200MB</span>
    </div>
  </div>
@@ -31,6 +32,7 @@
import { LoadingInstance } from 'element-plus/es/components/loading/src/loading';
import router from "@/router";
let downloadLoadingInstance: LoadingInstance;
const props = defineProps({
    modelValue: [String, Object, Array],
    // 数量限制
@@ -38,13 +40,13 @@
    // 大小限制(MB)
    fileSize: propTypes.number.def(200),
    // 文件类型, 例如['png', 'jpg', 'jpeg']
    fileType: propTypes.array.def(["doc","docx","xlsx", "xls", "ppt", "txt", "pdf"]),
    fileType: propTypes.array.def(['doc', 'docx', 'xlsx', 'xls', 'ppt', 'txt', 'pdf', 'mp3', 'mp4', 'mov', 'png', 'jpg', 'jpeg', 'zip', 'rar', '7z']),
    // 是否显示提示
    isShowTip: propTypes.bool.def(true),
});
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const emit = defineEmits(['update:modelValue','closePopup']);
const emit = defineEmits(['update:modelValue','closePopup','openPopup']);
const number = ref(0);
const uploadList = ref<any[]>([]);
//互联网端
@@ -127,15 +129,15 @@
// 上传前校检格式和大小
const handleBeforeUpload = (file: any) => {
    // 校检文件类型
    // if (props.fileType.length) {
    //     const fileName = file.name.split('.');
    //     const fileExt = fileName[fileName.length - 1];
    //     const isTypeOk = props.fileType.indexOf(fileExt) >= 0;
    //     if (!isTypeOk) {
    //         proxy?.$modal.msgError(`文件格式不正确, 请上传${props.fileType.join("/")}格式文件!`);
    //         return false;
    //     }
    // }
    if (props.fileType.length) {
        const fileName = file.name.split('.');
        const fileExt = fileName[fileName.length - 1];
        const isTypeOk = props.fileType.indexOf(fileExt) >= 0;
        if (!isTypeOk) {
            proxy?.$modal.msgError(`文件格式不正确, 请上传${props.fileType.join("/")}格式文件!`);
            return false;
        }
    }
    // 校检文件大小
    if (props.fileSize) {
        const isLt = file.size / 1024 / 1024 < props.fileSize;
@@ -158,29 +160,40 @@
const handleUploadError = () => {
    proxy?.$modal.msgError("上传文件失败");
}
const debounce = (fn: Function, delay = 1500) => {
  let timer: number | null = null;
  return (...args: any[]) => {
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      fn(...args);
    }, delay);
  }
}
const handleUploadProgress=(e: any) => {
  downloadLoadingInstance = ElLoading.service({ text: '正在上传文件,请稍候', background: 'rgba(0, 0, 0, 0.7)' });
  // downloadProgress.value = downloadProgress.value + 50
  // emit('closePopup','123123');
  emit('openPopup','123123');
}
// 上传成功回调
const handleUploadSuccess = (res: any, file: UploadFile) => {
  console.log(res,'resresres');
    if (res.code === 200) {
      proxy?.$modal.msgSuccess("上传文件成功");
      emit('closePopup',res.data)
      console.log('调用父组件',res);
      fileUploadRef.value!.clearFiles()
      downloadLoadingInstance.close();
        uploadList.value.push({ name: res.data.fileName, url: res.data.url, ossId: res.data.ossId });
        uploadedSuccessfully();
    }else if (res.code == 401) {
      router.push('/login')
        location.reload()
    } else {
        number.value--;
        // proxy?.$modal.closeLoading();
        proxy?.$modal.msgError(res.msg);
        fileUploadRef.value?.handleRemove(file);
        uploadedSuccessfully();
      downloadLoadingInstance.close();
    }
}