UploadItem.vue 2.4 KB
<template>
  <div>
    <div class="img-container" v-for="(item,index) in fileList" :key="index">
      <img :src="item.worksUrl" />
      <i class="van-icon van-icon-delete" @click="deleteImageHandler(index)"></i>
    </div>
    <van-uploader
      ref="vanUploader"
      class="uploader"
      :after-read="uploadSumit"
      v-if="!fileList || fileList.length < 3"
    >+</van-uploader>
  </div>
</template>



<script>
import { request } from "@/api/fetch-api.js";

import Vue from "vue";
import { Uploader } from "vant";
Vue.use(Uploader);

/**
 * 外层插件可通过监听:v-on:before-upload;v-on:after-upload两个事件监听上传结果
 * after-upload(success, src)
 */

export default {
  props: ["value"],
  data() {
    return {
      fileList: this.value,
      uploadUrl: "https://api.k.wxpai.cn/bizproxy/kdapi/file/upload",
    };
  },
  methods: {
    uploadSumit(params) {
      console.log(params);
      let data = {
        path: "/pro/jiajiaChildrenHost",
        file: params.file
      };
      request
        .form(this.uploadUrl, data)
        .then(result => {
          let uploadResult = {
            worksUrl: result.data.content,
            worksType: params.file.type.indexOf("image") >= 0 ? "pic" : "video"
          };
          if (!this.fileList) {
            this.fileList = [];
          }
          this.fileList.push(uploadResult);
          this.$emit("input", this.fileList);
        })
        .catch(res => {});
    },
    deleteImageHandler(index) {
      this.fileList.splice(index, 1);
    }
  },
  computed: {
    currentValue: function() {
      return this.value;
    }
  }
};
</script>

<style  lang="scss" scoped>
.uploader {
  border: 1px solid #82acae;
  border-radius: 6px;
  position: relative;
  overflow: hidden;
  padding: 10px;
  background-color: #addfe1;
  width: 120px;
  min-height: 120px;
  font-size: 40px;
  line-height: 120px;
  text-align: center;
  font-weight: bold;
}

.img-container {
  border: 1px solid #82acae;
  border-radius: 6px;
  position: relative;
  overflow: hidden;
  padding: 10px;
  background-color: #addfe1;
  width: 120px;
  text-align: center;
  font-weight: bold;
  margin-bottom: 10px;
  img {
    width: 100%;
  }

  .van-icon {
    background-color: rgba($color: #000000, $alpha: 0.3);
    position: absolute;
    right: 10px;
    bottom: 5px;
    font-size: 40px;
  }
}

.avatar {
  max-width: 120px;
  max-height: 120px;
  display: block;
}
</style>