uploadImg.vue 2.14 KB
Newer Older
Your Name's avatar
Your Name committed
1 2 3 4 5
<template>
  <div>
    <el-upload
      accept=".jpg,.jpeg,.png.JPG,.JPEG,.PNG"
      :class="{disabled:uploadDisabled}"
6
      action="http://111.203.232.175:8088/mall/file/image/upload"
Your Name's avatar
Your Name committed
7 8 9 10 11
      list-type="picture-card"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :on-success="handleAvatarSuccess"
      :headers="headers"
Your Name's avatar
Your Name committed
12
      :file-list="fileShowList"
Your Name's avatar
Your Name committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26
      :limit="1"
      :multiple ="false"
    >
      <i class="el-icon-plus"></i>
      <div slot="tip" class="el-upload__tip color_red">只能上传.jpg/.jpeg/.png文件</div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt />
    </el-dialog>
  </div>
</template>

<script>
export default {
Your Name's avatar
Your Name committed
27 28 29 30 31 32
  props:{
    fileList:{
      type: Array,
      default: () => [],
    }, 
  },
Your Name's avatar
Your Name committed
33 34
  data() {
    return {
Your Name's avatar
Your Name committed
35
      fileShowList: [],
Your Name's avatar
Your Name committed
36 37
      dialogImageUrl: "",
      dialogVisible: false,
38
      imageUrl:"",
Your Name's avatar
Your Name committed
39
    };
Your Name's avatar
Your Name committed
40 41 42 43 44
  },
   watch: {
    fileList(val) {
      this.fileShowList = val;
    }
Your Name's avatar
Your Name committed
45 46 47 48 49 50 51 52
  },
  computed: {
    headers() {
      return {
        Authorization: localStorage.getItem("backToken") || "",
      };
    },
    uploadDisabled:function() {
Your Name's avatar
Your Name committed
53
        return (this.fileShowList.length>0)||this.imageUrl
Your Name's avatar
Your Name committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    },
  },
  methods: {
    // 图片上传成功的返回值
    handleAvatarSuccess(res, file) {
      if (res.resultCode == 200) {
        this.$emit("imgUrl", res.data.url);
        this.imageUrl = res.data.url;
      } else {
        this.$message.error(res.message);
      }
    },
    handleRemove(file, fileList) {
      this.$emit("imgUrl", "");
      this.imageUrl=""
Your Name's avatar
Your Name committed
69
      this.fileShowList=[]
Your Name's avatar
Your Name committed
70 71 72 73 74 75 76 77 78 79
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    showImg(cover) {
      let file = {
        name: "",
        url: cover,
      };
Your Name's avatar
Your Name committed
80
      this.fileShowList = [file]
Your Name's avatar
Your Name committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    },
  },
};
</script>

<style lang="less">
.avatar {
  width: 100%;
  height: 100%;
}
.color_red{
  color:red;
}

.disabled .el-upload--picture-card {
    display: none;
}

</style>