index.vue 7.37 KB
<template>
    <div class="activity-img-box">
      <div class="upload-img">
        <el-button
          class="avatar-uploader-desc-word"
          size="small"
          icon="el-icon-puls"
          key="btn-i"
          :disabled="!!imageUrlArr.length"
          @click="selectFile()"
          >上传文件</el-button
        >
        <input
          type="file"
          ref="uploadImgRef"
          class="upload-img"
          style="opacity: 0; z-index: -1"
          :accept="accept"
          @change="(e) => handleImgChange(e)"
        />
      </div>
      <template v-if="imageUrlArr && imageUrlArr.length > 0">
        <div class="file-wrap" v-for="(item, index) in imageUrlArr" :key="index">
          <div class="file-box" @click="toOpenPDF(item.image)">
            <div class="info" v-if="item.imageName">
              <img class="logo" :src="require('@/assets/pdf.png')" alt="" />
              <span class="name">{{ item.imageName }}</span>
            </div>
            <div
              class="process"
              :style="{
                width: item.image ? '432px' : (compress * 432) / 100 + 'px'
              }"
            ></div>
          </div>
          <img
            v-if="item.image"
            class="del-btn"
            :src="require('@/assets/icon-delete.png')"
            alt=""
            @click="delFile(index)"
          />
        </div>
      </template>
    </div>
  </template>
  <script>
  import upload from "@/common/upload"
  import { uploadPDF } from "@/api/sys.base"
  export default {
    props: {
      picOptions: {
        type: Object,
        default: () => {
          return {
            width: "104px",
            height: "104px",
            isClose: true,
            limitSize: 10
          }
        }
      },
      imageUrlArr: {
        type: Array,
        default: () => [
          {
            name: "asdakf.pdf",
            url: "sfdsfjskdf.pdf"
          }
        ]
      },
      accept: {
        type: String,
        default: '.pdf'
      },
      limit: {
        type: String,
        default: "1"
      },
      index: {
        type: Number,
        default: -1
      }
    },
    data() {
      return {
        compress: 0,
        dialogVisibleImg: false,
        uploadInfo: null,
        uploading: false
      }
    },
    methods: {
      selectFile() {
        if (this.uploading) {
          return this.$message.error(`文件上传中,请耐心等待文件上传完成`)
        }
        this.$refs.uploadImgRef.click()
      },
      // 上传图片
      handleImgChange(e) {
        var file = e.srcElement.files[0]
        // if (file.type?.indexOf("pdf") == -1) {
        //   this.$refs.uploadImgRef && (this.$refs.uploadImgRef.value = "")
        //   this.$message.error(`上传文件不是pdf的格式!`)
        //   return
        // }
        // 默认1MB
        const limitSize = this.picOptions.limitSize || 10
        const isLt = file.size / 1024 / 1024 < limitSize
        if (!isLt) {
          this.$message.error(`上传文件大小不能超过${limitSize}MB!`)
          this.$refs.uploadImgRef && (this.$refs.uploadImgRef.value = "")
          return
        }
        if (this.imageUrlArr.length >= this.limit) {
          this.$message.error(`最多上传${this.limit}个文件`)
          this.$refs.uploadImgRef && (this.$refs.uploadImgRef.value = "")
          return
        }
        this.$notify({
          title: "提示",
          message: `文件上传中,请稍后`
        })
        this.$emit("beforeUpload")
        const idx = this.imageUrlArr.length
        this.$set(this.imageUrlArr, idx, { imageName: file.name, image: "" })
        this.uploading = true
        this.uploadInfo = new upload({
          showprocess: true,
          success: (res) => {
            this.$notify({
              title: "提示",
              message: `文件上传成功`,
              type: "success"
            })
            this.$set(this.imageUrlArr, idx, {
              imageName: file.name,
              image: res
            })
            this.$emit("change", {
              arr: this.imageUrlArr,
              key: this.picOptions.key,
              index: this.index
            })
            this.uploadEnd()
          },
          error: (err) => {
            this.uploadEnd()
          }
        })
        this.uploadInfo.clickEvent(e.srcElement.files)
      },
      uploadEnd() {
        this.uploading = false
        this.$refs.uploadImgRef && (this.$refs.uploadImgRef.value = "")
        this.$emit("afterUpload")
      },
      toOpenPDF(url) {
        if (!url) {
          return this.$message({
            type: "warning",
            message: "文件正在上传,请稍后预览"
          })
        }
        window.open(url)
      },
      // 删除图片/视频
      delFile(index) {
        this.imageUrlArr.splice(index, 1)
        this.$emit("change", {
          arr: this.imageUrlArr,
          key: this.picOptions.key,
          id: 1,
          index: this.index
        })
      }
    }
  }
  </script>
  <style scoped lang="scss">
  .file-wrap {
    display: flex;
    align-items: center;
    .file-box {
      width: 432px;
      height: 32px;
      border-radius: 4px;
      position: relative;
      cursor: pointer;
      .info {
        height: inherit;
        display: flex;
        align-items: center;
        margin-left: 8px;
        position: relative;
        z-index: 1;
        .logo {
          margin-right: 8px;
        }
        .name {
          color: #1d1d1d;
          display: inline-block;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
        }
      }
      .process {
        position: absolute;
        height: 32px;
        left: 0;
        top: 0;
        background: #f4f6f7;
      }
    }
    .del-btn {
      width: 16px;
      height: 16px;
      cursor: pointer;
      margin-left: 10px;
    }
  }
  .activity-img-box {
    max-width: 600px;
    .pic-box {
      position: relative;
      width: 104px;
      height: 104px;
      border-radius: 4px;
      margin-right: 8px;
      margin-bottom: 5px;
      &:hover .mask {
        opacity: 1;
      }
      .pic {
        position: absolute;
        object-fit: fill;
      }
      .photo-activity {
        position: absolute;
        width: 100%;
        height: 100%;
      }
      .mask {
        display: flex;
        align-items: center;
        justify-content: center;
        position: absolute;
        background: rgba(0, 0, 0, 0.5);
        color: #ffffff;
        opacity: 0;
        top: 0;
        right: 0;
        width: 104px;
        height: 104px;
        z-index: 999;
        .pic-pane-close {
          width: 16px;
          height: 16px;
          cursor: pointer;
          margin-left: 24px;
          &:first-child {
            margin-left: 0;
          }
          img {
            width: 100%;
            display: block;
          }
        }
      }
    }
    .upload-img {
      position: relative;
      display: inline-block;
      input {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 1;
      }
    }
    .avatar-uploader-desc-word {
      font-weight: 400;
      font-size: 28rpx;
      color: #666666;
      margin-bottom: 8px;
    }
    .image-dialog {
      ::v-deep {
        .el-dialog__header {
          border-bottom: 0px;
          padding: 0px;
        }
      }
    }
  }
  </style>