UploadItem.vue 4.67 KB
<template>
  <div>
    <div class="img-container" v-for="(item,index) in fileList" :key="index">
      <img v-if="item.worksType=='pic'" :src="item.worksUrl" />
      <video v-if="item.worksType=='video'" controls>
        <source :src="item.worksUrl" type="video/mp4" />
      </video>
      <i class="van-icon van-icon-delete" @click="deleteImageHandler(index)"></i>
    </div>
    <van-uploader
      ref="vanUploader"
      class="uploader"
      accept="image/*, audio/mp4, video/mp4"
      :before-read="beforeUpload"
      :after-read="uploadSumit"
      v-if="!fileList || fileList.length < 3"
    >+</van-uploader>

    <image-clipper
      ref="clipper"
      v-if="imageData.show"
      :img="imageData.data"
      :clipper-img-width="750"
      :clipper-img-height="380.95"
      @ok="imageClipperHandler"
      @cancel="imageData.show=false"
    ></image-clipper>
  </div>
</template>



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

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

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

export default {
  props: ["value"],
  data() {
    return {
      fileList: this.value,
      uploadFile: "https://api.k.wxpai.cn/bizproxy/kdapi/file/upload",
      uploadUrl: "https://api.k.wxpai.cn/bizproxy/kdapi/file/uploadBase64",
      imageData: {
        show: false,
        data: ""
      }
    };
  },
  methods: {
    beforeUpload(params) {
      // console.log(params);
      // let worksType = params.type.indexOf("image") >= 0 ? "pic" : "video";
      // if (
      //   worksType == "video" &&
      //   (!this.fileList || this.fileList.length == 0)
      // ) {
      //   Toast("第一个作品必须上传图片!");
      //   return false;
      // }
      return true;
    },
    uploadSumit(params) {
      console.log(params);
      let worksType = params.file.type.indexOf("image") >= 0 ? "pic" : "video";
      if (worksType == "pic") {
        this.imageData.data = params.content;
        this.imageData.show = true;
      } else {
        Toast.loading({
          mask: true,
          forbidClick: true,
          message: "视频提交中..."
        });
        let data = {
          path: "/pro/jiajiaChildrenHost/works",
          file: params.file
        };
        request
          .form(this.uploadFile, data)
          .then(result => {
            Toast.clear();
            let uploadResult = {
              worksUrl: result.data.content,
              worksType: "video"
            };
            if (!this.fileList) {
              this.fileList = [];
            }
            this.fileList.push(uploadResult);
            this.$emit("input", this.fileList);
          })
          .catch(res => {});
      }
    },
    imageClipperHandler(base64) {
      let data = {
        path: "/pro/jiajiaChildrenHost/works",
        data: base64
      };

      Toast.loading({
        mask: true,
        forbidClick: true,
        message: "图片提交中..."
      });
      httpPost({ url: this.uploadUrl, data: data })
        .then(result => {
          Toast.clear();
          console.log(result);
          let uploadResult = {
            worksUrl: result,
            worksType: "pic"
          };
          if (!this.fileList) {
            this.fileList = [];
          }
          this.fileList.push(uploadResult);
          console.log("submit image : ", this.fileList);
          this.imageData.show = false;
          this.$emit("input", this.fileList);
        })
        .catch(res => {});
    },
    deleteImageHandler(index) {
      this.fileList.splice(index, 1);
    }
  },
  computed: {
    currentValue: function() {
      return this.value;
    }
  },
  components: {
    ImageClipper
  }
};
</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%;
  }
  video {
    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>