uploadImageUrl.vue
3.33 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
<template>
<div class="upload-image-container">
<div v-for="(image, index) in photos" :key="index" class="image-item">
<el-image
:src="image"
fit="cover"
style="width: 100px; height: 100px;"
:preview-src-list="[image]"
></el-image>
<i
class="el-icon-delete remove-icon"
@click="handleRemoveImage(image)"
></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>
import { uploadFile } from '@/api/oa/addAttendanceClassesStaffApi'
import { getCommunityId } from '@/api/community/communityApi'
export default {
name: 'UploadImageUrl',
props: {
imageCount: {
type: Number,
default: 1
}
},
data() {
return {
photos: [],
photosUrl: [],
communityId: getCommunityId()
}
},
methods: {
triggerUpload() {
this.$refs.fileInput.click()
},
async handleFileChange(event) {
const file = event.target.files[0]
if (!file) return
if (file.size > 2 * 1024 * 1024) {
this.$message.error(this.$t('uploadImage.imageSizeLimit'))
return
}
// 预览图片
const reader = new FileReader()
reader.onload = (e) => {
this.photos.push(e.target.result)
}
reader.readAsDataURL(file)
// 上传图片
try {
const formData = new FormData()
formData.append('uploadFile', file)
formData.append('communityId', this.communityId)
const res = await uploadFile(formData)
this.photosUrl.push(res.data)
this.$emit('notifyUploadImage', this.photosUrl)
} catch (error) {
this.$message.error(this.$t('uploadImage.uploadFailed'))
}
event.target.value = null
},
handleRemoveImage(image) {
const index = this.photos.indexOf(image)
if (index > -1) {
this.photos.splice(index, 1)
this.photosUrl.splice(index, 1)
this.$emit('notifyUploadImage', this.photosUrl)
}
},
clearImages() {
this.photos = []
this.photosUrl = []
},
setImages(images) {
this.clearImages()
images.forEach(img => {
if (img.indexOf('base64,') > -1) {
this.photos.push(img)
return
}
// 处理其他类型的图片URL
this.photos.push(img)
this.photosUrl.push({ url: img })
})
}
}
}
</script>
<style lang="scss" scoped>
.upload-image-container {
display: flex;
flex-wrap: wrap;
gap: 10px;
.image-item {
position: relative;
margin-right: 10px;
}
.remove-icon {
position: absolute;
top: -10px;
right: -10px;
color: #f56c6c;
cursor: pointer;
font-size: 16px;
background: white;
border-radius: 50%;
padding: 2px;
}
.upload-button {
width: 100px;
height: 100px;
border: 1px dashed #d9d9d9;
border-radius: 6px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
color: #8c939d;
font-size: 28px;
background-color: #fbfdff;
&:hover {
border-color: #409eff;
}
}
}
</style>