Blame view

src/components/community/uploadImageUrl.vue 3.76 KB
e4e31451   wuxw   完成物业首页功能
1
  <template>
5760f7b9   wuxw   小区下的功能修改完成
2
    <div class="upload-image-container">
e4e31451   wuxw   完成物业首页功能
3
      <div v-for="(image, index) in photos" :key="index" class="image-item">
5760f7b9   wuxw   小区下的功能修改完成
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        <el-image
          :src="image.url || image"
          fit="cover"
          style="width: 100px; height: 100px"
          :preview-src-list="[image.url || image]"
        >
          <div slot="error" class="image-slot">
            <i class="el-icon-picture-outline"></i>
          </div>
        </el-image>
        <i
          class="el-icon-delete remove-icon"
          @click="removeImage(index)"
        ></i>
e4e31451   wuxw   完成物业首页功能
18
      </div>
5760f7b9   wuxw   小区下的功能修改完成
19
20
21
22
23
      <div
        v-if="photos.length < imageCount"
        class="upload-button"
        @click="triggerUpload"
      >
e4e31451   wuxw   完成物业首页功能
24
25
        <i class="el-icon-plus"></i>
      </div>
5760f7b9   wuxw   小区下的功能修改完成
26
27
28
29
30
31
32
      <input
        type="file"
        ref="fileInput"
        accept="image/*"
        hidden
        @change="handleFileChange"
      />
e4e31451   wuxw   完成物业首页功能
33
34
35
36
    </div>
  </template>
  
  <script>
5760f7b9   wuxw   小区下的功能修改完成
37
38
  import { uploadImage } from '@/api/community/addCommunityPublicityApi'
  
e4e31451   wuxw   完成物业首页功能
39
40
41
42
43
44
  export default {
    name: 'UploadImageUrl',
    props: {
      imageCount: {
        type: Number,
        default: 99
5760f7b9   wuxw   小区下的功能修改完成
45
46
47
48
49
50
51
52
      },
      callBackListener: {
        type: String,
        required: true
      },
      callBackFunction: {
        type: String,
        required: true
e4e31451   wuxw   完成物业首页功能
53
54
55
56
57
58
59
60
61
62
      }
    },
    data() {
      return {
        photos: [],
        photosUrl: []
      }
    },
    watch: {
      photosUrl: {
5760f7b9   wuxw   小区下的功能修改完成
63
64
65
66
        deep: true,
        handler(newVal) {
          this.$emit(this.callBackFunction, newVal)
        }
e4e31451   wuxw   完成物业首页功能
67
68
69
70
71
72
      }
    },
    methods: {
      triggerUpload() {
        this.$refs.fileInput.click()
      },
5760f7b9   wuxw   小区下的功能修改完成
73
74
75
76
77
      async handleFileChange(event) {
        const files = event.target.files
        if (!files || files.length === 0) return
  
        const file = files[0]
e4e31451   wuxw   完成物业首页功能
78
        if (file.size > 2 * 1024 * 1024) {
5760f7b9   wuxw   小区下的功能修改完成
79
          this.$message.error(this.$t('uploadImage.fileSizeError'))
e4e31451   wuxw   完成物业首页功能
80
81
          return
        }
5760f7b9   wuxw   小区下的功能修改完成
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  
        try {
          // Preview image
          const reader = new FileReader()
          reader.onload = (e) => {
            this.photos.push(e.target.result)
          }
          reader.readAsDataURL(file)
  
          // Upload image
          const formData = new FormData()
          formData.append('uploadFile', file)
          const response = await uploadImage(formData)
          this.photosUrl.push(response.data)
        } catch (error) {
          this.$message.error(this.$t('uploadImage.uploadError'))
        } finally {
          event.target.value = ''
e4e31451   wuxw   完成物业首页功能
100
        }
e4e31451   wuxw   完成物业首页功能
101
102
103
104
105
      },
      removeImage(index) {
        this.photos.splice(index, 1)
        this.photosUrl.splice(index, 1)
      },
5760f7b9   wuxw   小区下的功能修改完成
106
107
108
      clearImages() {
        this.photos = []
        this.photosUrl = []
e4e31451   wuxw   完成物业首页功能
109
      },
5760f7b9   wuxw   小区下的功能修改完成
110
111
112
113
114
115
116
117
118
119
120
      setImages(images) {
        this.clearImages()
        images.forEach(image => {
          if (typeof image === 'string') {
            this.photos.push(image)
            this.photosUrl.push({ fileId: image, url: image })
          } else {
            this.photos.push(image.url)
            this.photosUrl.push(image)
          }
        })
e4e31451   wuxw   完成物业首页功能
121
122
123
124
125
      }
    }
  }
  </script>
  
5760f7b9   wuxw   小区下的功能修改完成
126
127
  <style lang="scss" scoped>
  .upload-image-container {
e4e31451   wuxw   完成物业首页功能
128
129
130
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
5760f7b9   wuxw   小区下的功能修改完成
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  
    .image-item {
      position: relative;
      margin-right: 10px;
      margin-bottom: 10px;
  
      .remove-icon {
        position: absolute;
        top: -10px;
        right: -10px;
        color: #f56c6c;
        background: #fff;
        border-radius: 50%;
        cursor: pointer;
        font-size: 16px;
        z-index: 1;
  
        &:hover {
          color: #f78989;
        }
      }
    }
  
    .upload-button {
      width: 100px;
      height: 100px;
      border: 1px dashed #dcdfe6;
      border-radius: 4px;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      color: #909399;
      font-size: 24px;
  
      &:hover {
        border-color: #409eff;
        color: #409eff;
      }
    }
  
    .image-slot {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 100%;
      height: 100%;
      background: #f5f7fa;
      color: #909399;
      font-size: 30px;
    }
e4e31451   wuxw   完成物业首页功能
182
183
  }
  </style>