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

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

qzhxx's avatar
qzhxx committed
80
<style lang="less">
xulili's avatar
xulili committed
81 82 83 84
.avatar {
  width: 100%;
  height: 100%;
}
qzhxx's avatar
qzhxx committed
85 86 87 88 89 90 91 92
.color_red{
  color:red;
}

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

liqin's avatar
liqin committed
93
</style>