Blame view

src/components/community/uploadImageUrl.vue 4 KB
b25b036d   wuxw   v1.9 优化日期
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
        <el-image
8fc7c791   wuxw   测试完成房屋装修模块
5
          :src="getImageUrl(image)"
5760f7b9   wuxw   小区下的功能修改完成
6
7
          fit="cover"
          style="width: 100px; height: 100px"
8fc7c791   wuxw   测试完成房屋装修模块
8
9
10
          :preview-src-list="[getImageUrl(image)]"
        />
        <i class="el-icon-delete delete-icon" @click="removeImage(index)" />
e4e31451   wuxw   完成物业首页功能
11
      </div>
5760f7b9   wuxw   小区下的功能修改完成
12
13
14
15
16
      <div
        v-if="photos.length < imageCount"
        class="upload-button"
        @click="triggerUpload"
      >
8fc7c791   wuxw   测试完成房屋装修模块
17
        <i class="el-icon-plus" />
e4e31451   wuxw   完成物业首页功能
18
      </div>
5760f7b9   wuxw   小区下的功能修改完成
19
      <input
5760f7b9   wuxw   小区下的功能修改完成
20
        ref="fileInput"
8fc7c791   wuxw   测试完成房屋装修模块
21
        type="file"
5760f7b9   wuxw   小区下的功能修改完成
22
23
24
25
        accept="image/*"
        hidden
        @change="handleFileChange"
      />
e4e31451   wuxw   完成物业首页功能
26
27
28
29
    </div>
  </template>
  
  <script>
8fc7c791   wuxw   测试完成房屋装修模块
30
31
  import { uploadImage } from '@/api/community/listRoomDecorationRecordApi'
  import { getCommunityId } from '@/api/community/communityApi'
5760f7b9   wuxw   小区下的功能修改完成
32
  
e4e31451   wuxw   完成物业首页功能
33
34
35
36
37
38
  export default {
    name: 'UploadImageUrl',
    props: {
      imageCount: {
        type: Number,
        default: 99
5760f7b9   wuxw   小区下的功能修改完成
39
40
41
      },
      callBackListener: {
        type: String,
8fc7c791   wuxw   测试完成房屋装修模块
42
        default: ''
5760f7b9   wuxw   小区下的功能修改完成
43
44
45
      },
      callBackFunction: {
        type: String,
8fc7c791   wuxw   测试完成房屋装修模块
46
        default: ''
e4e31451   wuxw   完成物业首页功能
47
48
49
50
51
52
53
54
55
56
      }
    },
    data() {
      return {
        photos: [],
        photosUrl: []
      }
    },
    watch: {
      photosUrl: {
5760f7b9   wuxw   小区下的功能修改完成
57
        handler(newVal) {
8fc7c791   wuxw   测试完成房屋装修模块
58
59
60
          this.$emit('change', newVal)
        },
        deep: true
e4e31451   wuxw   完成物业首页功能
61
62
63
      }
    },
    methods: {
8fc7c791   wuxw   测试完成房屋装修模块
64
65
66
67
68
      getImageUrl(image) {
        if (image.indexOf('base64,') > -1) return image
        if (image.indexOf('http') > -1 || image.indexOf('https') > -1) return image
        return `/callComponent/download/getFile/file?fileId=${image}&communityId=${getCommunityId()}`
      },
e4e31451   wuxw   完成物业首页功能
69
70
71
      triggerUpload() {
        this.$refs.fileInput.click()
      },
5760f7b9   wuxw   小区下的功能修改完成
72
      async handleFileChange(event) {
8fc7c791   wuxw   测试完成房屋装修模块
73
74
        const file = event.target.files[0]
        if (!file) return
5760f7b9   wuxw   小区下的功能修改完成
75
  
e4e31451   wuxw   完成物业首页功能
76
        if (file.size > 2 * 1024 * 1024) {
8fc7c791   wuxw   测试完成房屋装修模块
77
          this.$message.error(this.$t('uploadImage.imageSizeLimit'))
e4e31451   wuxw   完成物业首页功能
78
79
          return
        }
5760f7b9   wuxw   小区下的功能修改完成
80
  
8fc7c791   wuxw   测试完成房屋装修模块
81
82
83
84
85
86
        // Preview image
        const reader = new FileReader()
        reader.onload = (e) => {
          this.photos.push(e.target.result)
        }
        reader.readAsDataURL(file)
5760f7b9   wuxw   小区下的功能修改完成
87
  
8fc7c791   wuxw   测试完成房屋装修模块
88
89
        // Upload image
        try {
5760f7b9   wuxw   小区下的功能修改完成
90
91
          const formData = new FormData()
          formData.append('uploadFile', file)
8fc7c791   wuxw   测试完成房屋装修模块
92
93
          formData.append('communityId', getCommunityId())
  
5760f7b9   wuxw   小区下的功能修改完成
94
          const response = await uploadImage(formData)
8fc7c791   wuxw   测试完成房屋装修模块
95
          this.photosUrl.push(response)
5760f7b9   wuxw   小区下的功能修改完成
96
        } catch (error) {
8fc7c791   wuxw   测试完成房屋装修模块
97
          this.$message.error(this.$t('uploadImage.uploadFailed'))
e4e31451   wuxw   完成物业首页功能
98
        }
8fc7c791   wuxw   测试完成房屋装修模块
99
100
  
        event.target.value = null
e4e31451   wuxw   完成物业首页功能
101
102
103
104
105
      },
      removeImage(index) {
        this.photos.splice(index, 1)
        this.photosUrl.splice(index, 1)
      },
8fc7c791   wuxw   测试完成房屋装修模块
106
      clear() {
5760f7b9   wuxw   小区下的功能修改完成
107
108
        this.photos = []
        this.photosUrl = []
e4e31451   wuxw   完成物业首页功能
109
      },
8fc7c791   wuxw   测试完成房屋装修模块
110
111
112
113
114
115
116
117
118
119
120
121
122
123
      notifyPhotos(photos) {
        this.clear()
        photos.forEach(photo => {
          if (photo.indexOf('base64,') > -1) {
            this.photos.push(photo)
            return
          }
          if (photo.indexOf('http') > -1 || photo.indexOf('https') > -1) {
            this.photos.push(photo)
            const fileId = this.getFileIdFromUrl(photo)
            if (fileId) {
              this.photosUrl.push({ fileId, url: photo })
            }
            return
5760f7b9   wuxw   小区下的功能修改完成
124
          }
8fc7c791   wuxw   测试完成房屋装修模块
125
126
          this.photos.push(this.getImageUrl(photo))
          this.photosUrl.push({ fileId: photo, url: this.getImageUrl(photo) })
5760f7b9   wuxw   小区下的功能修改完成
127
        })
8fc7c791   wuxw   测试完成房屋装修模块
128
129
130
131
      },
      getFileIdFromUrl(url) {
        const match = url.match(/fileId=([^&]+)/)
        return match ? match[1] : null
e4e31451   wuxw   完成物业首页功能
132
133
134
135
136
      }
    }
  }
  </script>
  
5760f7b9   wuxw   小区下的功能修改完成
137
138
  <style lang="scss" scoped>
  .upload-image-container {
e4e31451   wuxw   完成物业首页功能
139
140
141
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
5760f7b9   wuxw   小区下的功能修改完成
142
143
144
  
    .image-item {
      position: relative;
8fc7c791   wuxw   测试完成房屋装修模块
145
146
      width: 100px;
      height: 100px;
5760f7b9   wuxw   小区下的功能修改完成
147
  
8fc7c791   wuxw   测试完成房屋装修模块
148
      .delete-icon {
5760f7b9   wuxw   小区下的功能修改完成
149
150
151
152
        position: absolute;
        top: -10px;
        right: -10px;
        color: #f56c6c;
5760f7b9   wuxw   小区下的功能修改完成
153
154
        cursor: pointer;
        font-size: 16px;
8fc7c791   wuxw   测试完成房屋装修模块
155
156
157
        background: white;
        border-radius: 50%;
        padding: 2px;
5760f7b9   wuxw   小区下的功能修改完成
158
159
160
161
162
163
164
      }
    }
  
    .upload-button {
      width: 100px;
      height: 100px;
      border: 1px dashed #dcdfe6;
5760f7b9   wuxw   小区下的功能修改完成
165
166
167
168
169
170
171
172
173
174
175
176
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      color: #909399;
      font-size: 24px;
  
      &:hover {
        border-color: #409eff;
        color: #409eff;
      }
    }
e4e31451   wuxw   完成物业首页功能
177
178
  }
  </style>