index.vue
17 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
<template>
<div class="activity-img-box">
<Draggable :disabled="!picOptions.isDrag"
:value="imageUrlArr"
@end="dragEnd">
<transition-group class="activity-img-box">
<template v-if="imageUrlArr && imageUrlArr.length > 0">
<div v-for="(item, index) in imageUrlArr" :key="index">
<div v-if="picOptions.type == 'video'"
class="pic-box"
:style="{ width: picOptions.width, height: picOptions.height }">
<video id="video"
height="100%"
loop
muted
preload="none"
width="100%">
<source :src="item" type="video/mp4" />
</video>
<img class="video-poster"
:src="
videoImg && isAliyunOssUrl(item)
? item + videoStr
: 'https://img02.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat/usedCar/video_default.png'
" />
<div class="mask">
<div class="pic-pane-close"
@click="handleEnlarge(item, 'video')">
<img alt="" :src="require('@/assets/view.png')" />
</div>
<div v-if="picOptions.isClose"
class="pic-pane-close"
@click="close('video', index)">
<img alt="" :src="require('@/assets/icon-delete.png')" />
</div>
</div>
</div>
<div v-else
class="pic-box"
:style="{ width: picOptions.width, height: picOptions.height }">
<template v-if="picOptions.style == 'button'">
<div class="view-box">
<i class="el-icon-picture-outline"></i>
<div class="img-name">
{{ getName(item) }}
</div>
<div class="view-btn"
@click="handleEnlarge(item, 'img', index)">
预览
</div>
<i class="el-icon-close" @click="close('img', index)"></i>
</div>
</template>
<template v-else>
<img v-lazy="item"
alt=""
class="photo-activity" />
<div class="mask"
:style="{
width: picOptions.width,
height: picOptions.height,
}">
<div class="pic-pane-close"
@click="handleEnlarge(item, 'img', index)">
<img alt="" :src="require('@/assets/view.png')" />
</div>
<template v-if="picOptions.isClose">
<div class="pic-pane-close" @click="close('img', index)">
<img alt="" :src="require('@/assets/icon-delete.png')" />
</div>
</template>
</div>
</template>
</div>
</div>
</template>
</transition-group>
</Draggable>
<template v-if=" (imageUrlArr && imageUrlArr.length < 10 && limit > 1) || (imageUrlArr && imageUrlArr.length == 0 && limit == 1) ">
<template v-if="picOptions.style === 'button'">
<div class="upload-box">
<el-button icon="el-icon-plus"
size="small"
@click="selectFile()">
上传图片
</el-button>
<input ref="uploadImgRef"
:accept="
picOptions.noLimitType
? '*'
: picOptions.type === 'video'
? '.mp4'
: '.jpg, .jpeg, .png,.webp'
"
class="upload-img"
style="opacity: 0; z-index: -1"
:style="{ width: '100%', height: '100%' }"
type="file"
@change="(e) => handleUpload(e)" />
</div>
</template>
<template v-else>
<div class="upload-img"
:class="{
'reach-limit': imageUrlArr.length >= limit,
}"
:style="{ width: picOptions.width, height: picOptions.height }"
@click="selectFile()">
<i class="el-icon-plus avatar-uploader-desc-icon"></i>
<div class="avatar-uploader-desc-word">
上传{{
picOptions.noLimitType
? ""
: picOptions.type === "video"
? "视频"
: "图片"
}}
</div>
<input ref="uploadImgRef"
:accept="
picOptions.noLimitType
? '*'
: picOptions.type == 'video'
? '.mp4'
: '.jpg, .jpeg, .png,.webp'
"
class="upload-img"
style="opacity: 0; z-index: -1"
:style="{ width: picOptions.width, height: picOptions.height }"
type="file"
@change="(e) => handleUpload(e)" />
</div>
</template>
</template>
<el-dialog v-if="dialogType !== 'img'"
:append-to-body="true"
class="image-dialog"
:visible.sync="dialogVisibleImg"
width="50%">
<video autoplay
class="preVideo"
controls
:src="dialogSrc"
width="100%">
</video>
</el-dialog>
<ImageViewer v-if="dialogVisibleImg && dialogType == 'img'"
:initial-index="initialIndex"
:on-close="() => (dialogVisibleImg = false)"
:url-list="imageUrlArr" />
</div>
</template>
<script>
import GlOssApi from "glsk-oss-jssdk";
import draggable from "vuedraggable";
import { syncUploadVideo, uploadImg, uploadVideo } from "@/api/sys.base";
import upload from "@/common/upload";
import { baseConfig } from "@/main";
import ImageViewer from "./Image.vue";
// import GlOssApi from "@/common/oss"
export default {
components: {
Draggable: draggable,
ImageViewer,
},
props: {
picOptions: {
type: Object,
default: () => {
return {
width: "104px",
height: "104px",
isClose: true,
limitSize: 10,
};
},
},
imageUrlArr: {
type: Array,
default: () => [],
},
limit: {
type: String | Number,
default: "1",
},
index: {
type: Number,
default: -1,
},
videoImg: {
type: String | Boolean,
default: "",
},
},
data() {
return {
uploading: false,
dialogVisibleImg: false,
initialIndex: 0,
dialogSrc: "",
dialogType: "",
uploadInfo: null,
videoStr: "?x-oss-process=video/snapshot,t_7000,f_jpg,w_800,h_600,m_fast",
};
},
watch: {
dialogVisibleImg(val) {
if (!val) {
// 关闭所有视频播放
const videos = document.querySelectorAll(".preVideo");
videos.forEach((video) => {
if (video && !video.paused) {
video.pause();
video.currentTime = 0;
}
});
}
},
},
mounted() {
this.GlOssApi = new GlOssApi({
baseConfig,
});
},
methods: {
// 判断是否是阿里云OSS链接
isAliyunOssUrl(url) {
if (!url || typeof url !== "string") {
return false;
}
// 检查是否包含阿里云OSS的特征域名
// 常见的OSS域名格式:oss-cn-xxx.aliyuncs.com 或包含 oss- 的域名
return (
url.includes("aliyuncs.com")
|| url.includes("oss-")
|| /oss-cn-[a-z0-9-]+\.aliyuncs\.com/.test(url)
);
},
dragEnd(e) {
if (e.oldIndex != e.newIndex) {
const url = this.imageUrlArr[e.newIndex];
this.$set(this.imageUrlArr, e.newIndex, this.imageUrlArr[e.oldIndex]);
this.$set(this.imageUrlArr, e.oldIndex, url);
}
},
selectFile() {
if (this.uploading) {
return this.$message.error(`文件上传中,请耐心等待文件上传完成`);
}
this.$refs.uploadImgRef.click();
},
handleUpload(e) {
console.log(e.srcElement.files[0].type, this.picOptions.noLimitType);
if (this.picOptions.noLimitType) {
if (e.srcElement.files[0].type.includes("mp4")) {
this.$set(this.picOptions, "type", "video");
} else {
this.$set(this.picOptions, "type", "jpg");
}
}
console.log(this.picOptions.type);
this.handleImgChange(e);
},
// 上传图片
handleImgChange(e) {
let file = e.srcElement.files[0];
const fileName = file.name;
// var reader = new FileReader()
if (this.picOptions.type == "video" && file.type?.indexOf("mp4") == -1) {
this.$message.error(`上传文件不是mp4的格式!`);
this.$refs.uploadImgRef && (this.$refs.uploadImgRef.value = "");
return;
}
if (
this.picOptions.type != "video"
&& !/jpg|jpeg|png|webp/i.test(file.type)
) {
this.$message.error(`上传图片不是jpg、jpeg、webp或png的格式!`);
this.$refs.uploadImgRef && (this.$refs.uploadImgRef.value = "");
return;
}
// 默认1MB
const limitSize = this.picOptions.limitSize || 10;
const isLt = file.size / 1024 / 1024 < limitSize;
if (!isLt) {
this.$message.error(
`上传${
this.picOptions.type == "video" ? "视频" : "图片"
}大小不能超过${limitSize}MB!`,
);
this.$refs.uploadImgRef && (this.$refs.uploadImgRef.value = "");
return;
}
if (this.imageUrlArr.length >= this.limit) {
this.$message.error(
`最多上传${this.limit}${
this.picOptions.type == "video" ? "个视频" : "张图片"
}`,
);
this.$refs.uploadImgRef && (this.$refs.uploadImgRef.value = "");
return;
}
this.$notify({
title: "提示",
message: `${
this.picOptions.type == "video" ? "视频" : "图片"
}上传中,请稍后`,
});
this.$emit("beforeUpload");
this.uploading = true;
console.log(this.picOptions.type, 6666);
// if (this.picOptions.type == "video") {
// console.log(333333)
// this.uploadVideoFn(e)
// return
// }
// reader.readAsDataURL(file)
// reader.onload = (e) => {
// const imgFile = e.target.result
// const arrBase64 = imgFile.split(",")
// const splitStr = file.name.split(".")
// const fileUrl = splitStr[splitStr.length - 1] + "@" + arrBase64[1]
// const _api =
// this.picOptions.type == "video" ? syncUploadVideo : uploadImg
// _api({
// file: fileUrl,
// compress: this.picOptions.compress || 0
// })
// .then((res) => {
// if (res) {
// this.$notify({
// title: "提示",
// message: `${
// this.picOptions.type == "video" ? "视频" : "图片"
// }上传成功`,
// type: "success"
// })
// this.imageUrlArr.push(res)
// this.$emit("change", {
// arr: this.imageUrlArr,
// key: this.picOptions.key,
// index: this.index
// })
// }
// })
// .finally((end) => {
// this.uploadEnd()
// })
// }
const success = (res) => {
if (res.aliyunAddress) {
this.$notify({
title: "提示",
message: `${
this.picOptions.type == "video" ? "视频" : "图片"
}上传成功`,
type: "success",
});
this.imageUrlArr.push(res.aliyunAddress);
this.$emit("change", {
arr: this.imageUrlArr,
key: this.picOptions.key,
index: this.index,
});
this.uploadEnd();
}
};
const fail = () => {
this.uploadEnd();
};
this.handleOssUpload(file, fileName, success, fail);
},
handleOssUpload(file, fileName, success, fail) {
const bucketName = "jinrong-huidan-test"; // 桶名
const fileType = fileName.substr(fileName.lastIndexOf(".") + 1);
this.GlOssApi.multipartUpload(file, success, fail, bucketName, fileType);
},
getName(name) {
return name.split("/").pop();
},
uploadVideoFn(e) {
this.uploadInfo = new upload({
showprocess: true,
success: (res) => {
this.$notify({
title: "提示",
message: `视频上传成功`,
type: "success",
});
this.imageUrlArr.push(res);
this.$emit("change", {
arr: this.imageUrlArr,
key: this.picOptions.key,
index: this.index,
});
this.uploadEnd();
},
error: (err) => {
this.uploadEnd();
},
});
this.uploadInfo.clickEvent(e.srcElement.files);
},
uploadEnd() {
this.uploading = false;
this.$refs.uploadImgRef && (this.$refs.uploadImgRef.value = "");
this.$emit("afterUpload");
},
// 预览图文
handleEnlarge(src, type, index) {
this.dialogVisibleImg = true;
this.dialogSrc = src;
this.dialogType = type || "img";
this.initialIndex = index;
},
// 删除图片/视频
close(type, index) {
this.$confirm("是否确定删除?", "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
})
.then(() => {
this.imageUrlArr.splice(index, 1);
this.$emit("change", {
arr: this.imageUrlArr,
key: this.picOptions.key,
id: 1,
index: this.index,
});
})
.catch((err) => {
console.log(err);
});
},
},
};
</script>
<style scoped lang="scss">
.view-box {
width: 240px;
height: 32px;
border-radius: 4px;
opacity: 1;
background: #f4f6f7;
align-items: center;
display: flex;
padding: 0px 8px;
box-sizing: border-box;
font-size: 14px;
.el-icon-picture-outline {
color: #185efe;
}
.img-name {
width: 77px;
margin: 0px 4px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.view-btn {
color: #185efe;
cursor: pointer;
}
.el-icon-close {
margin-left: auto;
cursor: pointer;
}
}
.upload-box {
width: 95px;
height: 30px;
}
.activity-img-box {
display: flex;
flex-wrap: wrap;
max-width: 1200px;
.pic-box {
position: relative;
border-radius: 4px;
margin-right: 8px;
margin-bottom: 5px;
&:hover .mask {
opacity: 1;
}
.pic {
position: absolute;
object-fit: fill;
}
.photo-activity {
position: absolute;
width: 100%;
height: 100%;
}
.video-poster {
position: absolute;
width: 104px;
height: 104px;
object-fit: fill;
top: 0;
right: 0;
left: 0;
bottom: 0;
}
.mask {
display: flex;
align-items: center;
justify-content: center;
position: absolute;
background: rgba(0, 0, 0, 0.5);
color: #ffffff;
opacity: 0;
top: 0;
right: 0;
left: 0;
bottom: 0;
z-index: 999;
.pic-pane-close {
width: 16px;
height: 16px;
cursor: pointer;
margin-left: 24px;
&:first-child {
margin-left: 0;
}
img {
width: 100%;
display: block;
}
}
}
}
.upload-img {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
background: rgba(0, 0, 0, 0.04);
border: 1px dashed rgba(0, 0, 0, 0.15);
border-radius: 4px;
overflow: hidden;
input {
position: absolute;
// top: 0;
// left: 0;
}
}
.avatar-uploader-desc-word {
font-weight: 400;
font-size: 14px;
color: #666666;
line-height: 32px;
}
.image-dialog {
::v-deep {
.el-dialog__header {
border-bottom: 0px;
padding: 0px;
}
}
}
}
.preImg {
text-align: center;
display: inherit;
margin: 0 auto;
max-height: 60vh;
}
.preVideo {
max-height: 60vh;
}
</style>