0bf7e6a5
wuxw
加入问卷功能代码
|
1
2
3
|
<template>
<div class="upload-image-container">
<div v-for="(image, index) in photos" :key="index" class="image-item">
|
a99eb7a5
wuxw
开发完成办公下功能
|
4
5
|
<el-image
:src="image"
|
0bf7e6a5
wuxw
加入问卷功能代码
|
6
|
fit="cover"
|
a99eb7a5
wuxw
开发完成办公下功能
|
7
|
style="width: 100px; height: 100px">
|
0bf7e6a5
wuxw
加入问卷功能代码
|
8
|
</el-image>
|
a99eb7a5
wuxw
开发完成办公下功能
|
9
10
11
12
|
<i
class="el-icon-close remove-icon"
@click="_removeImage(image)">
</i>
|
0bf7e6a5
wuxw
加入问卷功能代码
|
13
|
</div>
|
a99eb7a5
wuxw
开发完成办公下功能
|
14
15
|
<div
|
0bf7e6a5
wuxw
加入问卷功能代码
|
16
|
v-if="photos.length < imageCount"
|
0bf7e6a5
wuxw
加入问卷功能代码
|
17
|
class="upload-button"
|
a99eb7a5
wuxw
开发完成办公下功能
|
18
|
@click="_uploadPhoto">
|
0bf7e6a5
wuxw
加入问卷功能代码
|
19
|
<i class="el-icon-plus"></i>
|
a99eb7a5
wuxw
开发完成办公下功能
|
20
21
22
23
24
25
26
27
28
|
</div>
<input
type="file"
class="file-input"
accept="image/*"
ref="fileInput"
hidden
@change="_choosePhoto">
|
0bf7e6a5
wuxw
加入问卷功能代码
|
29
30
31
32
|
</div>
</template>
<script>
|
a99eb7a5
wuxw
开发完成办公下功能
|
33
|
import { uploadImage } from '@/api/oa/editExamineStaffApi'
|
0bf7e6a5
wuxw
加入问卷功能代码
|
34
35
36
37
38
39
40
41
42
43
44
45
|
import { getCommunityId } from '@/api/community/communityApi'
export default {
name: 'UploadImageUrl',
props: {
imageCount: {
type: Number,
default: 99
}
},
data() {
return {
|
a99eb7a5
wuxw
开发完成办公下功能
|
46
47
48
|
photos: [], // 显示图片数组(base64格式)
photosUrl: [], // 向父组件传递的数组({fileId, url})
communityId: ''
|
0bf7e6a5
wuxw
加入问卷功能代码
|
49
50
|
}
},
|
a99eb7a5
wuxw
开发完成办公下功能
|
51
52
53
|
created() {
this.communityId = getCommunityId()
},
|
0bf7e6a5
wuxw
加入问卷功能代码
|
54
|
methods: {
|
a99eb7a5
wuxw
开发完成办公下功能
|
55
56
57
58
59
60
61
62
63
64
|
open(photos) {
this.photos = []
this.photosUrl = []
if (!photos || photos.length === 0) return
photos.forEach(photo => {
if (photo.indexOf('base64,') > -1) {
this.photos.push(photo)
return
|
0bf7e6a5
wuxw
加入问卷功能代码
|
65
|
}
|
a99eb7a5
wuxw
开发完成办公下功能
|
66
67
68
69
70
71
72
73
74
75
76
77
78
|
if (photo.indexOf("https") > -1 || photo.indexOf("http") > -1) {
this.urlToBase64(photo)
const urlParams = this._getUrlParams(photo)
if (urlParams.fileId) {
this.photosUrl.push({fileId: urlParams.fileId, url: photo})
}
return
}
const url = `/callComponent/download/getFile/file?fileId=${photo}&communityId=-1&time=${new Date()}`
this.urlToBase64(url)
this.photosUrl.push({fileId: photo, url})
|
0bf7e6a5
wuxw
加入问卷功能代码
|
79
|
})
|
0bf7e6a5
wuxw
加入问卷功能代码
|
80
81
82
|
},
clear() {
this.photos = []
|
a99eb7a5
wuxw
开发完成办公下功能
|
83
|
this.photosUrl = []
|
0bf7e6a5
wuxw
加入问卷功能代码
|
84
|
},
|
a99eb7a5
wuxw
开发完成办公下功能
|
85
86
87
88
89
90
91
92
93
94
95
|
_uploadPhoto() {
this.$refs.fileInput.click()
},
_choosePhoto(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.imageSizeLimit'))
return
|
0bf7e6a5
wuxw
加入问卷功能代码
|
96
|
}
|
a99eb7a5
wuxw
开发完成办公下功能
|
97
98
99
100
101
|
const reader = new FileReader()
reader.onload = (e) => {
this.photos.push(e.target.result)
this._doUploadImageUrl(file)
|
0bf7e6a5
wuxw
加入问卷功能代码
|
102
|
}
|
a99eb7a5
wuxw
开发完成办公下功能
|
103
104
105
106
107
108
109
110
111
|
reader.readAsDataURL(file)
event.target.value = null
},
_removeImage(image) {
const index = this.photos.indexOf(image)
this.photos.splice(index, 1)
this.photosUrl.splice(index, 1)
this.$emit('notifyUploadImage', this.photosUrl)
|
0bf7e6a5
wuxw
加入问卷功能代码
|
112
|
},
|
a99eb7a5
wuxw
开发完成办公下功能
|
113
|
async _doUploadImageUrl(file) {
|
0bf7e6a5
wuxw
加入问卷功能代码
|
114
115
|
try {
const formData = new FormData()
|
a99eb7a5
wuxw
开发完成办公下功能
|
116
117
118
119
120
121
|
formData.append("uploadFile", file)
formData.append('communityId', this.communityId)
const data = await uploadImage(formData)
this.photosUrl.push(data)
this.$emit('notifyUploadImage', this.photosUrl)
|
0bf7e6a5
wuxw
加入问卷功能代码
|
122
|
} catch (error) {
|
a99eb7a5
wuxw
开发完成办公下功能
|
123
|
this.$message.error(this.$t('uploadImage.uploadError'))
|
0bf7e6a5
wuxw
加入问卷功能代码
|
124
125
|
}
},
|
a99eb7a5
wuxw
开发完成办公下功能
|
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
|
_getUrlParams(url) {
if (url.indexOf('?') < 0) {
return { fileId: url }
}
const urlStr = url.split('?')[1]
const obj = {}
const paramsArr = urlStr.split('&')
for (let i = 0; i < paramsArr.length; i++) {
const arr = paramsArr[i].split('=')
obj[arr[0]] = arr[1]
}
return obj
},
urlToBase64(url) {
const image = new Image()
image.setAttribute('crossOrigin', 'anonymous')
image.src = url
image.onload = () => {
const canvas = document.createElement('canvas')
canvas.width = image.width
canvas.height = image.height
const ctx = canvas.getContext('2d')
ctx.drawImage(image, 0, 0, image.width, image.height)
const base64 = canvas.toDataURL('image/png')
this.photos.push(base64)
}
|
0bf7e6a5
wuxw
加入问卷功能代码
|
156
157
158
159
160
161
162
163
164
165
|
}
}
}
</script>
<style lang="scss" scoped>
.upload-image-container {
display: flex;
flex-wrap: wrap;
gap: 10px;
|
a99eb7a5
wuxw
开发完成办公下功能
|
166
|
|
0bf7e6a5
wuxw
加入问卷功能代码
|
167
168
|
.image-item {
position: relative;
|
a99eb7a5
wuxw
开发完成办公下功能
|
169
|
|
0bf7e6a5
wuxw
加入问卷功能代码
|
170
171
|
.remove-icon {
position: absolute;
|
a99eb7a5
wuxw
开发完成办公下功能
|
172
173
174
175
|
top: 5px;
right: 5px;
color: #f56c6c;
font-size: 16px;
|
0bf7e6a5
wuxw
加入问卷功能代码
|
176
|
cursor: pointer;
|
a99eb7a5
wuxw
开发完成办公下功能
|
177
178
179
180
181
182
183
|
background: #fff;
border-radius: 50%;
padding: 2px;
&:hover {
color: #f00;
}
|
0bf7e6a5
wuxw
加入问卷功能代码
|
184
185
|
}
}
|
a99eb7a5
wuxw
开发完成办公下功能
|
186
|
|
0bf7e6a5
wuxw
加入问卷功能代码
|
187
|
.upload-button {
|
a99eb7a5
wuxw
开发完成办公下功能
|
188
189
190
|
width: 100px;
height: 100px;
border: 1px dashed #dcdfe6;
|
0bf7e6a5
wuxw
加入问卷功能代码
|
191
192
193
|
display: flex;
justify-content: center;
align-items: center;
|
0bf7e6a5
wuxw
加入问卷功能代码
|
194
|
cursor: pointer;
|
a99eb7a5
wuxw
开发完成办公下功能
|
195
196
197
|
color: #909399;
font-size: 24px;
|
0bf7e6a5
wuxw
加入问卷功能代码
|
198
199
|
&:hover {
border-color: #409eff;
|
a99eb7a5
wuxw
开发完成办公下功能
|
200
|
color: #409eff;
|
0bf7e6a5
wuxw
加入问卷功能代码
|
201
202
203
204
|
}
}
}
</style>
|