customUploadNew.vue 3.53 KB
<template>
  <div>
    <el-input clearable v-model.trim="uploadImgUrl" size="mini" @change="changeInput">
      <template slot="append">
        <i class="iconfont iconfolder-o" @click="dialogVisible = true"></i>
      </template>
    </el-input>


    <!-- 弹窗 -->
    <el-dialog title="图库" :visible.sync="dialogVisible">
      <el-button size="medium" type="info" icon="el-icon-upload2" @click="uploadHandler">点击上传</el-button>
      <input type="file" class="file" ref="files" @change="getImages" />
      <div class="image-list">
        <el-image class="image" v-for="item in fileList" :key="item.fileId" :src="item.urlPath" lazy></el-image>
      </div>
    </el-dialog>




  </div>
</template>
<script>
import axios from "axios";
import { getToken } from "@/utils/auth";
import { getFielList } from "@/api/file"
export default {
  name: 'custom-upload-new',
  model: {
    prop: "value",
    event: "input"
  },
  props: {
    value: {
      type: "",
      default: ""
    }
  },
  data() {
    return {
      dialogVisible: false, // 弹窗状态
      requestUrl: process.env.BASE_API + "/file/upload",
      headers: {
        Authorization: getToken()
      },
      fileList: [],
      uploadImgUrl: ""
    };
  },
  async created() {
    this.uploadImgUrl = this.value;
    const rs = await getFielList();
    if (rs.code === 200 || rs.code === "200") {
      this.fileList = rs.data;
    }
  },
  methods: {
    uploadHandler() {
      this.$refs.files.click();
    },
    getImages(el) {
      const file = el.target.files[0];
      const type = file.type.split("/")[0];
      if (type === "image") {
        this.upload(file);
      } else {
        this.$message.warning("只能上传图片格式")
      }
    },
    upload(file) {
      let that = this;
      let formdata = new FormData();
      formdata.append("file", file);
      axios
        .post(this.requestUrl, formdata, {
          headers: that.headers
        })
        .then(response => {
          let res = response.data;
          if (res.code === "200") {
            that.uploadImgUrl = res.data.urlPath;
            that.$emit("input", that.uploadImgUrl);
            that.$emit("change", that.uploadImgUrl);
          }
        });
    },
    changeInput(e) {
      // if (e) {
      //   this.uploadImgUrl = e;
      // } else {
      //   this.$refs.files.value = "";
      //   this.uploadImgUrl = "";
      // }
      // this.$emit("input", this.uploadImgUrl);
      // this.$emit("change", this.uploadImgUrl);
    }
  }
};
</script>
<style lang="scss" scoped>
.file {
  // position: absolute;
  // width: 100%;
  // padding: 100%;
  // right: 0;
  // top: 0;
  // opacity: 0;
  display: none;
}

/deep/.el-input-group__append,
/deep/.el-input-group__prepend {
  padding: 0 10px !important;
  overflow: hidden;
}

.iconfont {
  cursor: pointer;
  font-size: 12px;
}


/deep/ .el-dialog .el-dialog__header,
/deep/ .el-dialog__body {
  background: #1b1e25;
}

.image-list {
  margin-top: 20px;
  height: 50vh;
  overflow: auto;

  &::-webkit-scrollbar {
    /*滚动条整体样式*/
    width: 8px;
    /*高宽分别对应横竖滚动条的尺寸*/
    height: 1px;
  }

  &::-webkit-scrollbar-thumb {
    /*滚动条里面小方块*/
    border-radius: 10px;
    background: #535353;
  }

  &::-webkit-scrollbar-track-piece {
    /*滚动条里面轨道*/
    border-radius: 10px;
    background: #1b1e25;
  }

  .image {
    width: 20%;
    padding: 0 0.5px 0 0.5px;
    min-height: 90px;

    /deep/ .el-image__error {
      width: 100%;
      height: 100%;
      min-height: 90px;
    }
  }
}
</style>