index.vue
11.6 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<template>
<div>
<el-upload
:action="baseUrl ? baseUrl : baseApi + '/api/imageUpload'"
:auto-upload="true"
:data="uploadData"
:headers="headers"
:on-success="handleSuccess"
:on-error="handleError"
:on-exceed="handleExceed"
:before-upload="beforeUpload"
:file-list="fileList"
:disabled="isDisabled"
:limit="limit"
:class="{single:singleImage}"
list-type="picture-card"
accept="image/*"
>
<i slot="default" class="el-icon-plus" />
<div slot="file" slot-scope="{file}">
<!-- <span-->
<!-- style="position: absolute; z-index: 999; padding-left: 5px; font-size: 12px;color: #ffffff;-->
<!-- background-color: rgba(0, 0, 0, 0.5); padding-right: 5px; border-bottom-right-radius: 0.5em"-->
<!-- >-->
<!-- {{ file.hasOwnProperty("type") ? file.type : ((file.hasOwnProperty("response") ? file.response.type : -1))-->
<!-- | dictFilter(dictMap.image_type ? dictMap.image_type : [])-->
<!-- | dictFilter(dictMap.image_type_show ? dictMap.image_type_show : []) }}-->
<!-- </span>-->
<!-- 于2020/10/14为了ugc基地址进行修改 file.url-->
<img
:src="baseUrl ? baseUrl + file.fileUrl : file.url"
class="el-upload-list__item-thumbnail"
alt=""
>
<span class="el-upload-list__item-actions">
<span
class="el-upload-list__item-preview"
@click="handlePictureCardPreview(file)"
>
<i class="el-icon-zoom-in" />
</span>
<!-- <span-->
<!-- v-if="!readOnlyMode"-->
<!-- class="el-upload-list__item-delete"-->
<!-- @click="handleChangeType(file)"-->
<!-- >-->
<!-- <i class="el-icon-picture" />-->
<!-- </span>-->
<span
v-if="!readOnlyMode"
class="el-upload-list__item-delete"
@click="handleRemove(file)"
>
<i class="el-icon-delete" />
</span>
<span
v-if="isArticle"
class="el-upload-list__item-delete"
@click="handleAddDesc(file)"
>
<i class="el-icon-edit" />
</span>
</span>
</div>
</el-upload>
<el-dialog :visible.sync="dialogVisible" :append-to-body="true">
<img :src="dialogImageUrl" width="100%" alt="">
</el-dialog>
<!-- <el-dialog :visible.sync="dialogImageTypeVisible" :append-to-body="true" title="请选择图片类型" width="300px">-->
<!-- <el-select v-model="imageType" class="filter-item" style="width: 100%;">-->
<!-- <el-option v-for="item in dictMap.image_type" :key="item.value" :label="item.label | dictFilter(dictMap.image_type_show)" :value="item.value" />-->
<!-- </el-select>-->
<!-- <div slot="footer" class="dialog-footer">-->
<!-- <el-button @click="dialogImageTypeVisible = false">取消</el-button>-->
<!-- <el-button type="primary" @click="changeImageType">确认</el-button>-->
<!-- </div>-->
<!-- </el-dialog>-->
<el-dialog :visible.sync="imageDesc" :append-to-body="true" title="请输入图片描述" width="800px">
<el-input v-model="imgDesc.desc" type="textarea" />
<div slot="footer" class="dialog-footer">
<el-button @click="imageDesc = false">取消</el-button>
<el-button type="primary" @click="addDescription">确认</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import { getAttrByValueFromDict, convertImageArr2Json, evil, deepCopy } from '../../utils/common-util'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'
import initDict from '@/mixins/initDict'
import { Loading } from 'element-ui'
export default {
name: 'ImagesUpload',
mixins: [initDict],
props: {
isDisabled: {
type: Boolean,
required: false,
default() {
return false
}
},
isSubject: {
type: Boolean,
required: false,
default() {
return true
}
},
image: {
type: String,
default: '[]'
},
images: {
type: String,
default: '{"list":[],"map":{}}'
},
uploadEntity: {
type: String,
required: true
},
limit: {
type: Number,
default: 10
},
singleMode: {
type: Boolean,
default: false
},
readOnlyMode: {
type: Boolean,
default: false
},
isArticle: {
type: Boolean,
default: false
},
baseUrl: {
type: String,
default() {
return ''
}
}
},
data() {
return {
headers: {
'Authorization': 'Bearer ' + getToken()
},
fileList: [],
uploadData: { entity: this.uploadEntity },
dialogImageUrl: '',
dialogVisible: false,
dialogImageTypeVisible: false,
imageType: '-1',
imageSelect: {},
imgDesc: {
desc: ''
},
isUploading: false,
loading: null,
imageDesc: false
}
},
computed: {
...mapGetters(['baseApi']),
singleImage: function() {
return (this.singleMode && (this.fileList.length > 0 || this.isUploading)) || this.readOnlyMode
}
},
watch: {
image: {
immediate: true,
handler(val) {
if (val === null || val === '') {
val = '[]'
}
const arr = evil(val)
// 2020/12/31 00:04,为了专题管理覆盖数据的操作,增加了isSubject参数
if (this.fileList.length === arr.length && this.isSubject) {
// 当图片数量不变时,filelist不响应image的变更,避免2次刷新
return
}
this.fileList = arr
this.fileList.forEach(file => {
if (!file.hasOwnProperty('url') && file.fileUrl.indexOf('http') === -1) {
file.url = this.baseApi + '/' + file.fileUrl
} else {
file.url = file.fileUrl
}
})
this.fileList = deepCopy(this.fileList)
}
}
},
created() {
this.$nextTick(() => {
this.getDictMap('image_type,image_type_show,image_size_limit')
})
},
updated() {
if (!this.dictMap.image_type) {
// 图片类型数据字典未获取时,images无法计算,当获取到后,也会触发updated
return
}
const images = []
this.fileList.forEach(file => {
if (this.isArticle) {
if (file.hasOwnProperty('response')) {
images.push((({ id, type, width, height, fileUrl, desc }) =>
({ id, type, width, height, fileUrl, desc }))(file.response))
} else {
images.push((({ id, type, width, height, fileUrl, desc }) =>
({ id, type, width, height, fileUrl, desc }))(file))
}
} else {
if (file.hasOwnProperty('response')) {
images.push((({ id, type, width, height, fileUrl }) =>
({ id, type, width, height, fileUrl }))(file.response))
} else {
images.push((({ id, type, width, height, fileUrl }) =>
({ id, type, width, height, fileUrl }))(file))
}
}
})
this.$emit('update:image', JSON.stringify(images))
this.$emit('update:images', this.convertImageArr2Json(JSON.stringify(images), this.dictMap.image_type))
},
methods: {
getAttrByValueFromDict,
deepCopy,
convertImageArr2Json,
beforeUpload(file) {
const isValid = (file.type === 'image/jpeg' || file.type === 'image/gif' || file.type === 'image/png')
const dict = this.dictMap.image_size_limit.find(item => item.label === this.uploadEntity)
let maxSize = dict ? parseFloat(dict.value) : 20 * 1024 // 默认2M
const isLtMaxSize = parseFloat(file.size / 1024) < maxSize
maxSize = maxSize > 1024 ? (parseFloat(maxSize / 1024).toFixed(2) + 'MB') : (maxSize + 'KB')
this.isUploading = true
if (!isValid) {
this.$message.error('上传图片只能是 JPG/PNG/GIF 格式!')
this.isUploading = false
}
if (!isLtMaxSize) {
this.$message.error('上传图片大小不能超过' + maxSize + ' !')
this.isUploading = false
}
if (isValid && isLtMaxSize) {
this.loading = Loading.service({
lock: true,
text: '解析中Loading',
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.7)'
})
}
return isValid && isLtMaxSize
},
handleRemove(file) {
this.fileList.splice(this.fileList.findIndex(
item => !item.hasOwnProperty('response') ? item.id === file.id : item.response.id === file.response.id), 1)
},
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url
this.dialogVisible = true
},
handleChangeType(file) {
console.log(file)
this.imageSelect = file
// 2020/10/23上午,因为发现此处报错,注释
// this.imageType = file.hasOwnProperty('type') ? file.type.toString() : file.response.type.toString()
// 2020/11/10上午,需要标示上传图片类型,所以解锁,并置为-1
this.imageType = file.hasOwnProperty('type') ? file.type.toString() : file.hasOwnProperty('response') ? file.response.type.toString() : '-1'
// this.imageType = '-1'
this.dialogImageTypeVisible = true
},
// 增加描述
handleAddDesc(file) {
this.imgDesc = file
this.imageDesc = true
},
addDescription() {
// debugger
for (let i = 0; i < this.fileList.length; i++) {
const item = this.fileList[i]
if (item.id) {
if (this.imgDesc.id === item.id) {
item.desc = this.imgDesc.desc
this.imageDesc = false
break
}
} else {
if (this.imgDesc.response.id === item.response.id) {
item.response.desc = this.imgDesc.desc
this.imageDesc = false
break
}
}
}
},
handleError(err, file) {
this.isUploading = false
this.loading.close()
this.$message.error(`图片上传失败,失败信息 ${err}`)
console.log(err)
},
handleSuccess(res, file, fileList) {
// console.log(res, file, fileList)
this.loading.close()
this.isUploading = false
this.fileList = fileList
},
handleExceed(files, fileList) {
this.$message.warning('图片数量无法大于' + this.limit + '张')
},
generateImageFromFileList(item) {
const images = []
this.fileList.forEach(file => {
if (file.hasOwnProperty('response')) {
images.push((({ id, type, width, height, fileUrl, size }) =>
({ id, type, width, height, fileUrl, size }))(file.response))
} else {
images.push((({ id, type, width, height, fileUrl, size }) =>
({ id, type, width, height, fileUrl, size }))(file))
}
})
item.image = JSON.stringify(images)
item.images = this.convertImageArr2Json(item.image, this.dictMap.image_type)
},
changeImageType() {
const index = this.fileList.findIndex(
item => !item.hasOwnProperty('response') ? item.id === this.imageSelect.id : item.response.id === this.imageSelect.response.id)
if (this.fileList[index].hasOwnProperty('response')) {
this.fileList[index].response.type = parseInt(this.imageType)
} else {
this.fileList[index].type = parseInt(this.imageType)
}
this.dialogImageTypeVisible = false
}
}
}
</script>
<style>
.el-upload-list__item {
transition: none;
}
.single .el-upload--picture-card {
display: none;
}
</style>