疯狂的狮子li
2023-02-06 a8d5644f2e9700b0986df935a0a57a51dc2bb439
ruoyi-ui/src/components/FileUpload/index.vue
@@ -12,7 +12,7 @@
      :show-file-list="false"
      :headers="headers"
      class="upload-file-uploader"
      ref="upload"
      ref="fileUpload"
    >
      <!-- 上传按钮 -->
      <el-button size="mini" type="primary">选取文件</el-button>
@@ -41,7 +41,7 @@
<script>
import { getToken } from "@/utils/auth";
import { delOss } from "@/api/system/oss";
import { listByIds, delOss } from "@/api/system/oss";
export default {
  name: "FileUpload",
@@ -74,7 +74,7 @@
      number: 0,
      uploadList: [],
      baseUrl: process.env.VUE_APP_BASE_API,
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/system/oss/upload", // 上传的图片服务器地址
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/system/oss/upload", // 上传文件服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
@@ -83,19 +83,24 @@
  },
  watch: {
    value: {
      handler(val) {
      async handler(val) {
        if (val) {
          let temp = 1;
          // 首先将值转为数组
          const list = Array.isArray(val) ? val : this.value.split(',');
          let list;
          if (Array.isArray(val)) {
            list = val;
          } else {
            await listByIds(val).then(res => {
              list = res.data.map(oss => {
                oss = { name: oss.originalName, url: oss.url, ossId: oss.ossId };
                return oss;
              });
            })
          }
          // 然后将数组转为对象数组
          this.fileList = list.map(item => {
            // 字符串回显处理 如果此处存的是url可直接回显 如果存的是id需要调用接口查出来
            if (typeof item === "string") {
              item = { name: item, url: item };
            } else {
              item = { name: item.name, url: item.url, ossId: item.ossId };
            }
            item = { name: item.name, url: item.url, ossId: item.ossId };
            item.uid = item.uid || new Date().getTime() + temp++;
            return item;
          });
@@ -119,15 +124,9 @@
    handleBeforeUpload(file) {
      // 校检文件类型
      if (this.fileType) {
        let fileExtension = "";
        if (file.name.lastIndexOf(".") > -1) {
          fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
        }
        const isTypeOk = this.fileType.some((type) => {
          if (file.type.indexOf(type) > -1) return true;
          if (fileExtension && fileExtension.indexOf(type) > -1) return true;
          return false;
        });
        const fileName = file.name.split('.');
        const fileExt = fileName[fileName.length - 1];
        const isTypeOk = this.fileType.indexOf(fileExt) >= 0;
        if (!isTypeOk) {
          this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`);
          return false;
@@ -151,23 +150,20 @@
    },
    // 上传失败
    handleUploadError(err) {
      this.$modal.msgError("上传图片失败,请重试");
      this.$modal.closeLoading()
      this.$modal.msgError("上传文件失败,请重试");
      this.$modal.closeLoading();
    },
    // 上传成功回调
    handleUploadSuccess(res, file) {
      if (res.code === 200) {
        this.uploadList.push({ name: res.data.fileName, url: res.data.url, ossId: res.data.ossId });
        if (this.uploadList.length === this.number) {
          this.fileList = this.fileList.concat(this.uploadList);
          this.uploadList = [];
          this.number = 0;
          this.$emit("input", this.fileList);
          this.$modal.closeLoading();
        }
        this.uploadedSuccessfully();
      } else {
        this.$modal.msgError(res.msg);
        this.number--;
        this.$modal.closeLoading();
        this.$modal.msgError(res.msg);
        this.$refs.fileUpload.handleRemove(file);
        this.uploadedSuccessfully();
      }
    },
    // 删除文件
@@ -175,7 +171,17 @@
      let ossId = this.fileList[index].ossId;
      delOss(ossId);
      this.fileList.splice(index, 1);
      this.$emit("input", this.fileList);
      this.$emit("input", this.listToString(this.fileList));
    },
    // 上传结束处理
    uploadedSuccessfully() {
      if (this.number > 0 && this.uploadList.length === this.number) {
        this.fileList = this.fileList.concat(this.uploadList);
        this.uploadList = [];
        this.number = 0;
        this.$emit("input", this.listToString(this.fileList));
        this.$modal.closeLoading();
      }
    },
    // 获取文件名称
    getFileName(name) {
@@ -186,6 +192,15 @@
        return name;
      }
    },
    // 对象转成指定字符串分隔
    listToString(list, separator) {
      let strs = "";
      separator = separator || ",";
      for (let i in list) {
        strs += list[i].ossId + separator;
      }
      return strs != "" ? strs.substr(0, strs.length - 1) : "";
    },
  },
};
</script>