uploadFile.vue 11 KB
<template>
  <div>
    <div class="uploadFile-title">
      <div class="content">
        Import pre-clearance document upload <span>*</span>
        <el-popover placement="top" width="510" trigger="hover">
          <div>
            Click the links below to upload customs clearance documents to
            support <br />pre-clearance in destination.
          </div>
          <i slot="reference" class="el-icon-question questionIcon" />
        </el-popover>
      </div>
      <el-button type="text" @click="viewHistory"
        >VIEW UPLOAD HISTORY</el-button
      >
    </div>
    <div class="uploadFile-box">
      <div
        v-for="(item, index) in fileData"
        :key="index"
        :class="{ uploadFileBoxItem: true }"
      >
        <div class="uploadButton" style="display: flex">
          <div style="height: 45px" v-if="uploadType">
            <div>
              <el-button type="text" @click="upload(item.uploadType)"
                >{{ item.titleName }}
              </el-button>
              <span v-if="item.required" class="requiredIcon">*</span>
            </div>
            <!-- <el-popover
              style="margin-left: 5px"
              v-if="item.uploadType == 'OTH'"
              placement="top"
              width="720"
              trigger="hover"
            >
              <div>
                Please provide
                <span class="tipWeight">certificate of origin, contract</span>,
                etc. if have any.<br />
                And please provide
                <span class="tipWeight">purchase receipts</span> or other
                support clearance documents if declaring personal items.
              </div>
              <i slot="reference" class="el-icon-question questionIcon" />
            </el-popover> -->
          </div>
          <div class="tipDom" v-if="item.uploadType == 'OTH'">
            Please provide
            <span class="tipWeight">certificate of origin,contract</span>,etc.
            if have any.<br />
            And please provide
            <span class="tipWeight">purchase receipts</span> or other support
            clearance documents if declaring personal items.
          </div>
          <!-- <div v-else>Waybill upload</div> -->
        </div>
        <div
          class="uploadFileList"
          v-for="(file, idx) in item.files"
          :key="idx"
        >
          <div class="fileName">
            <i class="el-icon-document" />{{ file.fileName }}
          </div>
          <div class="fileOption">
            <!-- 文件大小 -->
            <span class="fileSize"
              >{{ numberRound(Number(file.fileSize / 1024), 2) }}KB</span
            >
            <!-- 预览 -->
            <!-- <el-button
              type="text"
              :loading="file.viewLoading"
              @click="fileView(file)"
              >view</el-button
            > -->
            <!-- 下载 -->
            <el-button
              type="text"
              :loading="file.downLoading"
              @click="downloadFile(file)"
              >DOWNLOAD</el-button
            >
            <!-- 删除 -->
            <el-popconfirm
              title="Do you want to delete this file?"
              @confirm="delFile(file)"
            >
              <el-button slot="reference" type="text" :loading="file.delLoading"
                >DELETE</el-button
              >
            </el-popconfirm>
          </div>
        </div>
      </div>
      <!-- <el-button
        v-if="!uploadType"
        type="text"
        icon="el-icon-circle-plus-outline"
        @click="addUpload"
        >Add</el-button
      > -->
      <el-upload
        class="upload-demo"
        ref="uploadBlock"
        :multiple="true"
        action=""
        :file-list="fileList"
        :before-upload="beforeUpload"
        :show-file-list="false"
        :on-error="handleUploadError"
        :on-success="handleUploadSuccess"
        :on-remove="handleRemove"
        :http-request="uploadFile"
        :auto-upload="true"
        :on-change="handleChange"
        v-show="false"
      >
      </el-upload>
    </div>
    <HistoryDialog
      ref="historyDialog"
      :dialogVisible="viewHistoryType"
      :headData="headData"
      @close="historyClose"
    />
  </div>
</template>

<script>
import HistoryDialog from "./dialog.vue";
export default {
  components: {
    HistoryDialog,
  },
  props: {
    headData: {
      type: Object,
      default: null,
    },
    fileData: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  data() {
    return {
      fileFormData: {
        uploadType: "",
      },
      fileList: [],
      uploadType: true,
      viewHistoryType: false,
    };
  },
  methods: {
    //上传
    upload(type) {
      this.fileList = [];
      this.fileFormData.uploadType = type;
      this.$refs.uploadBlock.$refs["upload-inner"].handleClick();
    },
    beforeUpload(val) {
      let fileSum = 0;
      let fileSize = 0;
      let fileNameArray = [];
      this.fileData.forEach((item) => {
        fileSum = Number(fileSum) + Number(item.files.length);
        item.files.forEach((file) => {
          fileSize = Number(fileSize) + Number(file.fileSize);
          fileNameArray.push(file.fileName);
        });
      });
      if (fileSum + 1 > 100) {
        this.$message.warning("上传文件总数最大100个!");
        return false;
      } else if (Number(fileSize) + Number(val.fileSize) / 1024 / 1024 > 100) {
        this.$message.warning("上传文件总大小最大100M!");
        return false;
      } else if (fileNameArray.includes(val.fileName)) {
        this.$message.warning("上传文件名重复!");
        return false;
      } else {
        return true;
      }
    },
    uploadFile(val) {
      let obj = {
        id: null,
        bizId: null,
        bizTypeName: null,
        sourceFileName: null,
        filePath: null,
        fileTypeCode: null,
        fileTypeName: this.fileFormData.uploadType,
        status: null,
        createTime: null,
        createUserId: null,
        createUserName: null,
        modifyTime: null,
        modifyUserId: null,
        modifyUserName: null,
        uploadRecordId: null,
        consignmentCode: null,
        file: "",
        bizTypeCode: this.fileFormData.uploadType,
        fileType: val.file.type,
        fileName: val.file.name,
        fileSize: val.file.size,
        index: 0,
        viewLoading: false,
        downLoading: false,
        delLoading: false,
      };
      let arr = this.fileData;
      const reader = new FileReader();
      reader.readAsDataURL(val.file);
      reader.onload = (e) => {
        obj.file = e.target.result;
        arr.forEach((item, index) => {
          if (item.uploadType == this.fileFormData.uploadType) {
            obj.index = index;
            item.files.push(obj);
          }
        });
        this.$emit("fileDataChange", arr);
      };
    },
    //下载
    downloadFile(val) {
      if (val.file.split(";base64,").length > 1) {
        const aLink = document.createElement("a");
        aLink.href = val.file;
        aLink.setAttribute("download", val.fileName); // 设置下载文件名称
        document.body.appendChild(aLink);
        aLink.click();
        document.body.removeChild(aLink);
      } else {
        this.zipdownload
          .downLoadZip(
            "biz-customer/attachment/downLoadFile",
            {
              name: val.fileName,
              path: val.filePath,
            },
            val.fileName
          )
          .then((res) => {
            console.log(res);
          });
      }
    },
    //删除
    delFile(val) {
      this.fileData.forEach((item) => {
        if (item.uploadType == val.bizTypeCode) {
          let arr = [];
          item.files.forEach((file) => {
            if (file.index != val.index) {
              arr.push(file);
            }
          });
          item.files = arr;
        }
      });
      this.$emit("fileDataChange", this.fileData);
    },
    //预览
    fileView(val) {
      // var reader = new FileReader();
      // reader.encoding = "UTF-8";
      // reader.readAsText(this.b64ToBlob(val.file));
      // reader.onload = function (e) {
      //   var content = e.target.result;
      //   let blob = new Blob([content], { type: val.fileType });
      //   let blobURL = window.URL.createObjectURL(blob);
      //   window.open(blobURL, "_black");
      // };
      let blob = new Blob([this.base64ConvertFile(val.file, val.fileName)], {
        type: val.fileType,
      });
      let blobURL = window.URL.createObjectURL(blob);
      window.open(blobURL, "_black");
    },
    handleUploadError(val) {},
    handleUploadSuccess(val) {},
    handleRemove() {
      this.fileList = [];
    },
    handleChange() {},
    addUpload() {
      this.uploadType = true;
    },
    viewHistory() {
      this.viewHistoryType = true;
    },
    historyClose(val) {
      this.viewHistoryType = false;
      if (val.type == "copy") {
        this.$refs.historyDialog.detailSearch(val.data.id).then((res) => {
          this.fileData.forEach((item) => {
            if (item.uploadType == "attachmentBizType_01") {
              item.files = res[0].list;
            } else if (item.uploadType == "attachmentBizType_02") {
              item.files = res[1].list;
            } else if (item.uploadType == "attachmentBizType_03") {
              item.files = [];
            } else if (item.uploadType == "attachmentBizType_04") {
              item.files = res[2].list;
            }
          });
          this.$emit("fileDataChange", this.fileData);
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/assets/styles/variables.scss";
.uploadFile-title {
  text-align: center;
  height: 38px;
  line-height: 38px;
  position: relative;
  margin-top: 20px;
  .content {
    font-size: 16px;
    font-weight: 600;
    span {
      color: $fedexError;
    }
  }
  .el-button {
    position: absolute;
    top: 0;
    right: 10px;
  }
}

.uploadFile-box {
  margin-top: 8px;
  .uploadFileBoxItem {
    border: 1.5px solid rgba(153, 153, 153, 0.3);
    box-shadow: 0px 4px 0.3rem 0.5px rgba(229, 242, 248, 0.8);
    margin-top: 10px;
    background-color: #fefefe;
    .uploadButton {
      height: 45px;
      line-height: 45px;
      padding: 0 20px;
      border-bottom: 2px solid #e0e0e0;
      .requiredIcon {
        color: $fedexError;
      }
    }
    .uploadFileList {
      height: 45px;
      display: flex;
      justify-content: space-between;
      padding: 5px 0px 5px 20px;
      line-height: 35px;
      .fileName {
        i {
          display: inline-block;
          margin-right: 5px;
        }
      }
      .fileOption {
        .fileSize {
          font-size: 12px;
          font-weight: 400;
          color: #999;
          margin-right: 20px;
        }
        .el-button {
          margin-right: 16px;
        }
      }
    }
  }
  .fileBoxItemDisable {
    box-shadow: none;
    background-color: $formItemBackgroundColor !important;
  }
}

.tipDom {
  display: inline-block;
  font-size: 12px;
  line-height: 16px;
  padding: 7px 10px;
  color: #777;
}
</style>