Blame view

src/components/community/uploadImageUrl.vue 2.74 KB
e4e31451   wuxw   完成物业首页功能
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
  <template>
    <div class="uploadImage">
      <div v-for="(image, index) in photos" :key="index" class="image-item">
        <img :src="image" width="100" height="100" />
        <i class="el-icon-close remove-icon" @click="removeImage(index)"></i>
      </div>
      <div v-if="photos.length < imageCount" class="upload-button" @click="triggerUpload">
        <i class="el-icon-plus"></i>
      </div>
      <input type="file" ref="fileInput" accept="image/*" hidden @change="handleFileChange">
    </div>
  </template>
  
  <script>
  export default {
    name: 'UploadImageUrl',
    props: {
      imageCount: {
        type: Number,
        default: 99
      }
    },
    data() {
      return {
        photos: [],
        photosUrl: []
      }
    },
    watch: {
      photosUrl: {
        handler(val) {
          this.$emit('update:photosUrl', val)
        },
        deep: true
      }
    },
    methods: {
      triggerUpload() {
        this.$refs.fileInput.click()
      },
      handleFileChange(event) {
        const file = event.target.files[0]
        if (!file) return
        
        if (file.size > 2 * 1024 * 1024) {
          this.$message.error(this.$t('enterCommunity.fileSizeError'))
          return
        }
        
        const reader = new FileReader()
        reader.onload = (e) => {
          this.photos.push(e.target.result)
          this.uploadImage(file)
        }
        reader.readAsDataURL(file)
        
        // 重置input
        event.target.value = null
      },
      removeImage(index) {
        this.photos.splice(index, 1)
        this.photosUrl.splice(index, 1)
      },
      uploadImage(file) {
        const formData = new FormData()
        formData.append('uploadFile', file)
        formData.append('communityId', this.$store.getters.currentCommunity.communityId)
        
        this.$http.upload('uploadFile', 'uploadImage', formData, {
          headers: { 'Content-Type': 'multipart/form-data' }
        }).then(res => {
          const data = JSON.parse(res.data)
          this.photosUrl.push({
            fileId: data.fileId,
            url: `/callComponent/download/getFile/file?fileId=${data.fileId}`
          })
        }).catch(err => {
          this.$message.error(this.$t('enterCommunity.uploadError'))
        })
      },
      setPhotos(photos) {
        this.photos = photos
      }
    }
  }
  </script>
  
  <style scoped>
  .uploadImage {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
  }
  .image-item {
    position: relative;
    display: inline-block;
  }
  .remove-icon {
    position: absolute;
    top: -10px;
    right: -10px;
    color: #f56c6c;
    cursor: pointer;
    background: white;
    border-radius: 50%;
  }
  .upload-button {
    width: 100px;
    height: 100px;
    border: 1px dashed #dcdfe6;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    color: #8c939d;
    cursor: pointer;
  }
  .upload-button:hover {
    border-color: #409eff;
  }
  </style>