UploadMaterials.vue 3.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
<template>
  <el-upload
    class="upload-demo"
    :action="uploadFileUrl"
    :before-upload="handleBeforeUpload"
    :on-preview="handlePreview"
    :on-remove="handleRemove"
    :fileType="fileType"
    :limit="1"
    :on-exceed="handleExceed"
    :on-success="handleUploadSuccess"
    :show-file-list="false"
    :headers="headers"
    :file-list="fileList">
    <el-button size="small" type="text" icon="el-icon-upload">上传培训资料</el-button>
    <!--<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>-->
  </el-upload>
</template>

<script>
    import { getToken } from "@/utils/auth";
    import { updateApproval } from "@/api/deviceManagement/changeApplyApproval";
    export default {
      name: "upload-materials",
      props:{
        changeApplyId:{
          type: Number,
        },
      },
      data() {
        return {
          uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
          headers: {
            Authorization: "Bearer " + getToken(),
          },
          fileList: [],
          fileType: ["doc", "xls", "ppt", "txt", "pdf"]
        };
      },
      methods: {
        // 上传前校检格式和大小
        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;
          });
            if (!isTypeOk) {
              this.$message.error(
                `文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`
              );
              return false;
            }
          }
          // 校检文件大小
          if (this.fileSize) {
            const isLt = file.size / 1024 / 1024 < this.fileSize;
            if (!isLt) {
              this.$message.error(`上传文件大小不能超过 ${this.fileSize} MB!`);
              return false;
            }
          }
          return true;
        },
        handleRemove(file, fileList) {
          console.log(file, fileList);
        },
        handlePreview(file) {
          console.log(file);
        },
        handleExceed(files, fileList) {
          this.$message.warning(`当前限制选择 1 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
        },
        // 上传成功回调
        handleUploadSuccess(res, file) {
          updateApproval({ changeApplyId: this.changeApplyId, trainMaterial: res.url, changeApplyStatus: "3" }).then(res =>{
            if(res.code == 200){
              this.$message.success("上传成功");
              this.$emit('getList');
            }else{
              this.$message.error("上传失败")
            }
          })
        },
      }
    }
</script>

<style scoped lang="scss">
  .upload-demo{
    display: inline;
    margin: 10px;
  }

</style>