hejianhao
2025-04-16 dab2d210ca06c1faa514c6388fbd5de1ab355360
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<template>
  <div class="upload-img-content">
    <el-upload
      v-if="imgList.length < imgNum"
      action
      :multiple="multiple"
      :http-request="uploadFileHandle"
      :before-upload="beforeAvatarUploadImg"
      :show-file-list="false"
      :on-exceed="handleExceed"
      :limit="imgNum"
    >
      <div class="img-add-box mr-r-10 mr-b-10">
        <i class="el-icon-plus"></i>
      </div>
    </el-upload>
    <div class="img-item fl-co" v-for="(item, index) in imgList" :key="index">
      <el-image
        :z-index="9999"
        class="img-style"
        :preview-src-list="[item]"
        :src="item"
        fit="cover"
      ></el-image>
      <img
        class="delete-img"
        @click="deleteImg(index)"
        src="../../assets/deleteimg.png"
        alt="delete"
      />
    </div>
  </div>
</template>
 
<script>
export default {
  props: {
    imgNum: {
      type: Number,
      default: 9
    },
    ix: {
      type: Number,
      default: 0
    },
    imgShow: {
      type: String,
      default: ""
    },
    multiple: {
      type: Boolean,
      default: false
    },
    formKey: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      imgList:
        this.imgShow === "" || this.imgShow == null
          ? []
          : this.imgShow.split(",")
    };
  },
  watch: {
    imgShow(url) {
      this.imgList = url === "" || !url ? [] : url.split(",");
    }
  },
  methods: {
    handleExceed(files, fileList) {
      demo.toast(
        `当前限制选择 ${this.imgNum} 个文件,本次选择了 ${
          files.length
        } 个文件,共选择了 ${files.length + fileList.length} 个文件`
      );
    },
 
    uploadFileHandle(f) {
      let formData = new FormData();
      formData.append("file", f.file);
      this.fileTransmit("loading", true); // 上传图片过程种提交按钮 loading 状态
      this.$api.post("communitypartybuilding/uploadimage", formData, res => {
        this.fileTransmit("loading", false);
        this.imgList.push(res);
        this.fileTransmit("list", this.imgList.join(","));
      });
    },
    beforeAvatarUploadImg(file) {
      const imgType = ["image/png", "image/jpeg"];
      if (imgType.indexOf(file.type) === -1) {
        this.$message.error("只能上传 png jpeg jpg 格式图片!");
        return false;
      } else {
        return true;
      }
    },
    // 删除图片
    deleteImg(index) {
      this.imgList.splice(index, 1);
      this.fileTransmit("list", this.imgList.join(","));
    },
    fileTransmit(type, val) {
      this.$emit("fileHandle", {
        type: type,
        val: val,
        ix: this.ix,
        key: this.formKey
      });
    }
  }
};
</script>
 
<style scoped>
.upload-img-content {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
}
.img-add-box {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 98px;
  height: 98px;
  border: 1px dashed #999;
  border-radius: 10px;
}
.img-item {
  position: relative;
  margin-right: 10px;
  margin-bottom: 10px;
}
.delete-img {
  cursor: pointer;
  position: absolute;
  right: 0;
  top: 0;
  width: 20px;
  height: 20px;
}
.img-style {
  width: 100px;
  height: 100px;
  border-radius: 10px;
}
.video-style {
  width: 160px;
  height: 100px;
  background-color: #000;
}
.pr {
  position: relative;
}
</style>