uploadImageUrl.vue
3.88 KB
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
159
160
161
162
163
164
165
166
167
168
169
<template>
<div class="upload-image-container">
<div v-for="(image, index) in photos" :key="index" class="image-item">
<img :src="getImageUrl(image)" @error="handleImageError" />
<span class="remove-icon" @click="removeImage(index)">
<i class="el-icon-delete"></i>
</span>
</div>
<div v-if="photos.length < imageCount" class="upload-btn" @click="triggerUpload">
<i class="el-icon-plus"></i>
</div>
<input
ref="fileInput"
type="file"
accept="image/*"
style="display: none"
@change="handleFileChange"
/>
</div>
</template>
<script>
import { uploadFile } from '@/api/common'
export default {
name: 'UploadImageUrl',
props: {
imageCount: {
type: Number,
default: 99
}
},
data() {
return {
photos: [], // 用于显示的图片数组
photosUrl: [] // 实际存储的图片信息数组
}
},
methods: {
triggerUpload() {
this.$refs.fileInput.click()
},
async handleFileChange(event) {
const files = event.target.files
if (!files || files.length === 0) return
const file = files[0]
if (file.size > 2 * 1024 * 1024) {
this.$message.error(this.$t('uploadImage.validate.sizeLimit'))
return
}
try {
// 显示本地预览
const reader = new FileReader()
reader.onload = (e) => {
this.photos.push(e.target.result)
}
reader.readAsDataURL(file)
// 上传到服务器
const formData = new FormData()
formData.append('uploadFile', file)
formData.append('communityId', this.$store.getters.communityId)
const { data } = await uploadFile(formData)
this.photosUrl.push(data)
this.$emit('change', this.photosUrl)
} catch (error) {
this.$message.error(this.$t('uploadImage.message.uploadFailed'))
} finally {
event.target.value = null
}
},
removeImage(index) {
this.photos.splice(index, 1)
this.photosUrl.splice(index, 1)
this.$emit('change', this.photosUrl)
},
handleImageError(e) {
e.target.src = '/img/noPhoto.jpg'
},
getImageUrl(image) {
if (typeof image === 'string') {
if (image.startsWith('http') || image.startsWith('https') || image.startsWith('data:')) {
return image
}
return `/callComponent/download/getFile/file?fileId=${image}&communityId=-1&time=${new Date().getTime()}`
}
return image.url || '/img/noPhoto.jpg'
},
clear() {
this.photos = []
this.photosUrl = []
},
setImages(images) {
this.photos = []
this.photosUrl = []
images.forEach(image => {
if (image.startsWith('data:')) {
this.photos.push(image)
return
}
this.photosUrl.push({ fileId: image, url: image })
this.photos.push(this.getImageUrl(image))
})
}
}
}
</script>
<style lang="scss" scoped>
.upload-image-container {
display: flex;
flex-wrap: wrap;
gap: 10px;
.image-item {
position: relative;
width: 100px;
height: 100px;
border: 1px dashed #dcdfe6;
border-radius: 4px;
overflow: hidden;
img {
width: 100%;
height: 100%;
object-fit: cover;
}
.remove-icon {
position: absolute;
top: 0;
right: 0;
color: #f56c6c;
background: rgba(255, 255, 255, 0.7);
padding: 4px;
cursor: pointer;
font-size: 16px;
&:hover {
color: #f78989;
}
}
}
.upload-btn {
width: 100px;
height: 100px;
border: 1px dashed #dcdfe6;
border-radius: 4px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
color: #8c939d;
font-size: 28px;
background-color: #f5f7fa;
&:hover {
border-color: #409eff;
color: #409eff;
}
}
}
</style>