ThirdStep.js
25 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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
import api from "~/api/api";
import { getSystemInfoSync } from '~/utils/systemInfo'
import { uploadFile, getImageDisplayUrl } from "~/packageUsedCar/utils/oss/ossUpload.js";
const IMG_URL =
"https://img02.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat/";
Component({
options: {
styleIsolation: "shared",
},
properties: {
imageData: {
type: Object,
value: () => ({}),
},
dataAttrName: {
type: String,
value: "formData.imageList",
},
},
lifetimes: {
attached() {
this.initImageList();
},
},
observers: {
imageData: function (newImageData) {
// 监听 imageData 变化,重新初始化图片列表
console.log("imageData 变化:", newImageData);
// 如果 newImageData 为 null 或 undefined,设置为空对象
if (!newImageData) {
this.setData({
imageList: {},
});
return;
}
// 处理回显数据:确保所有文件都有 status 字段和 isVideo 字段
const processedImageList = {};
const { uploadVideoFields } = this.data;
// 获取所有视频字段的 key 集合
const videoKeys = new Set(
uploadVideoFields.map((field) => String(field.key))
);
for (const key in newImageData) {
const fileList = newImageData[key];
if (Array.isArray(fileList) && fileList.length > 0) {
// 过滤掉 url 为空的文件,并处理每个文件
processedImageList[key] = fileList
.filter((file) => {
// 过滤掉没有 url 的文件
if (!file || !file.url) {
console.warn("过滤掉无效文件(url 为空):", file);
return false;
}
return true;
})
.map((file) => {
const processedFile = { ...file };
// 如果文件没有 status 字段,说明是编辑回显的数据,设置为 'done'
if (!processedFile.status) {
processedFile.status = "done";
}
// 如果是视频字段且没有 isVideo 字段,根据字段类型或 URL 判断
if (
videoKeys.has(String(key)) &&
processedFile.isVideo === undefined
) {
processedFile.isVideo = true;
} else if (processedFile.isVideo === undefined) {
// 如果不是视频字段,根据 URL 扩展名判断
const url = processedFile.url || processedFile.coverUrl || "";
const videoExtensions = [
".mp4",
".mov",
".avi",
".m4v",
".3gp",
".flv",
".wmv",
];
processedFile.isVideo = videoExtensions.some((ext) =>
url.toLowerCase().includes(ext)
);
}
return processedFile;
});
// 如果过滤后数组为空,不添加到 processedImageList
if (processedImageList[key].length === 0) {
delete processedImageList[key];
}
}
}
this.setData({
imageList: processedImageList,
});
console.log("已更新 imageList:", this.data.imageList);
},
},
data: {
IMG_URL,
// 控制各区域的折叠/展开
isShowObj: {
vehicle: true,
certificate: true,
video: true,
report: true,
other: true,
},
// 视频播放弹窗
showVideoPopup: false,
videoUrl: "",
// 车辆图片
uploadFields: [
{
key: 11,
label: "左前45度",
required: true,
maxCount: 1,
accept: "image",
},
{ key: 12, label: "主驾驶全景", maxCount: 1, accept: "image" },
{ key: 13, label: "仪表盘", maxCount: 1, accept: "image" },
{ key: 14, label: "后排座椅", maxCount: 1, accept: "image" },
{ key: 15, label: "中控台全景", maxCount: 1, accept: "image" },
{ key: 16, label: "右后45度", maxCount: 1, accept: "image" },
{ key: 17, label: "铭牌", maxCount: 1, accept: "image" },
],
// 证件图片
uploadCertificateFields: [
{ key: 21, label: "行驶证", maxCount: 1, accept: "image" },
{
key: 22,
label: "登记证",
maxCount: 3,
accept: "image",
fullwidth: true,
},
],
// 视频资料
uploadVideoFields: [
{ key: 31, label: "发动机工况", required: false, accept: "video" },
{ key: 32, label: "车况解说", required: false, accept: "video" },
],
// 检测报告
uploadReportFields: [
{
key: 51,
label: "车辆检测报告",
required: false,
accept: "image",
maxCount: 3,
fullwidth: true,
},
],
// 其他图片
uploadOtherFields: [
{
key: 41,
label: "其他图片",
required: false,
accept: "image",
maxCount: 30,
fullwidth: true,
},
],
// 图片列表存储 { key: [fileList] }
imageList: {},
},
methods: {
// 处理折叠/展开
handleUpDown(e) {
const { attr } = e.currentTarget.dataset;
const { isShowObj } = this.data;
isShowObj[attr] = !isShowObj[attr];
this.setData({
isShowObj,
});
},
// 初始化图片列表
initImageList() {
const { imageData, uploadVideoFields } = this.data;
// 如果 imageData 为空,设置为空对象
if (!imageData || Object.keys(imageData).length === 0) {
this.setData({
imageList: {},
});
return;
}
// 处理回显数据:确保所有文件都有 status 字段和 isVideo 字段
const processedImageList = {};
// 获取所有视频字段的 key 集合
const videoKeys = new Set(
uploadVideoFields.map((field) => String(field.key))
);
for (const key in imageData) {
const fileList = imageData[key];
if (Array.isArray(fileList) && fileList.length > 0) {
// 过滤掉 url 为空的文件,并处理每个文件
processedImageList[key] = fileList
.filter((file) => {
// 过滤掉没有 url 的文件
if (!file || !file.url) {
console.warn(
"initImageList - 过滤掉无效文件(url 为空):",
file
);
return false;
}
return true;
})
.map((file) => {
const processedFile = { ...file };
// 如果文件没有 status 字段,说明是编辑回显的数据,设置为 'done'
if (!processedFile.status) {
processedFile.status = "done";
}
// 如果是视频字段且没有 isVideo 字段,根据字段类型或 URL 判断
if (
videoKeys.has(String(key)) &&
processedFile.isVideo === undefined
) {
processedFile.isVideo = true;
} else if (processedFile.isVideo === undefined) {
// 如果不是视频字段,根据 URL 扩展名判断
const url = processedFile.url || processedFile.coverUrl || "";
const videoExtensions = [
".mp4",
".mov",
".avi",
".m4v",
".3gp",
".flv",
".wmv",
];
processedFile.isVideo = videoExtensions.some((ext) =>
url.toLowerCase().includes(ext)
);
}
return processedFile;
});
// 如果过滤后数组为空,不添加到 processedImageList
if (processedImageList[key].length === 0) {
delete processedImageList[key];
}
}
}
this.setData({
imageList: processedImageList,
});
},
// 处理上传
handleUpload(event) {
const { key, label } = event.currentTarget.dataset;
const { file, files } = event.detail;
console.log("handleUpload - event.detail:", event.detail);
console.log("handleUpload - file:", file);
console.log("handleUpload - files:", files);
// 支持批量上传:优先使用 files,如果 files 为空则使用 file
let filesToUpload = [];
// 递归展平数组的辅助函数
const flattenArray = (arr) => {
let result = [];
for (let item of arr) {
if (Array.isArray(item)) {
result = result.concat(flattenArray(item));
} else if (item && typeof item === "object") {
result.push(item);
}
}
return result;
};
// 优先处理 files(multiple 模式)
if (files && files.length > 0) {
filesToUpload = flattenArray(files);
console.log("handleUpload - 展平后的 files:", filesToUpload);
}
// 如果 files 为空或无效,尝试使用 file(single 模式)
if (filesToUpload.length === 0 && file) {
// 如果 file 是数组,展平它
if (Array.isArray(file)) {
filesToUpload = flattenArray(file);
} else if (file && typeof file === "object") {
filesToUpload = [file];
}
console.log("handleUpload - 使用 file,filesToUpload:", filesToUpload);
}
// 过滤掉无效的文件对象
filesToUpload = filesToUpload.filter(
(f) => f && typeof f === "object" && (f.url || f.path || f.tempFilePath)
);
console.log("开始上传,key:", key, "files:", filesToUpload);
if (filesToUpload.length === 0) {
console.warn("没有有效的文件需要上传");
return;
}
// 批量上传文件(顺序上传,避免索引冲突)
filesToUpload.forEach((fileItem) => {
this.uploadSingleFile(fileItem, key, label);
});
},
// 上传单个文件
async uploadSingleFile(file, key, label) {
// 如果 file 是数组,取第一个元素
if (Array.isArray(file)) {
if (file.length === 0) {
console.error("文件数组为空");
return;
}
file = file[0];
}
// 检查 file 是否为有效对象
if (!file || typeof file !== "object") {
console.error("文件对象无效:", file);
return wx.showToast({
title: "文件对象错误",
icon: "none",
});
}
// 先将文件添加到列表(显示上传中状态)
const { imageList } = this.data;
const currentList = imageList[key] || [];
// 兼容 van-uploader 的文件对象:可能使用 url、path 或 tempFilePath
const filePath = file.url || file.path || file.tempFilePath;
if (!filePath) {
console.error("文件路径不存在:", file);
console.error("文件对象详情:", JSON.stringify(file, null, 2));
return wx.showToast({
title: "文件路径错误",
icon: "none",
});
}
// 获取文件扩展名
const ext = filePath.split(".").pop()?.toLowerCase() || "unknown";
const fileType = ["mp4", "mov", "avi", "m4v", "3gp"].includes(ext)
? "video"
: "image";
let requestEvent =
fileType == "video" ? "uploadFileWithCover" : "uploadImg";
if (fileType == "video") {
if (file.size > 100 * 1024 * 1024) {
return wx.showToast({
title: "视频上传最大100MB",
icon: "none",
});
}
} else {
if (file.size > 10 * 1024 * 1024) {
return wx.showToast({
title: "图片上传最大10MB",
icon: "none",
});
}
}
let sysInfo = getSystemInfoSync();
// 添加上传中的文件到列表
const uploadingFile = {
...file,
url: filePath, // 确保 url 字段存在
status: "uploading",
message: "上传中...",
};
const tempList = [...currentList, uploadingFile];
const fileIndex = tempList.length - 1;
this.setData({
[`imageList.${key}`]: tempList,
});
const uploadType = ext === "unknown" ? "jpg" : ext;
console.log(file);
const url = await uploadFile(file);
console.log(url);
const newList = [...currentList];
// 对于视频,保存 videoUrl、coverUrl 和 isVideo 标识
// van-uploader 对于视频文件,需要使用 coverUrl 作为预览图
if (fileType == "video") {
newList[fileIndex] = {
url: url,
thumb: url + (url.indexOf("?") >= 0 ? "&" : "?") + "x-oss-process=video/snapshot,t_7000,f_jpg,w_800,h_600,m_fast",
coverUrl: url + (url.indexOf("?") >= 0 ? "&" : "?") + "x-oss-process=video/snapshot,t_7000,f_jpg,w_800,h_600,m_fast",
isVideo: true,
name: label,
key: key,
status: "done",
};
} else {
newList[fileIndex] = {
url: url,
isVideo: false,
name: label,
key: key,
status: "done",
};
}
console.log("更新后的列表:", newList);
// 使用完整路径更新 setData,确保视图更新
this.setData({
[`imageList.${key}`]: newList,
});
// 同步到页面数据
this.syncToPage();
wx.showToast({
title: "上传成功",
icon: "success",
duration: 2000,
});
},
// 处理删除
handleDelete(event) {
const { key, index } = event.currentTarget.dataset;
// 兼容 van-uploader 的删除事件(event.detail.index)和自定义删除按钮(dataset.index)
const deleteIndex =
event.detail?.index !== undefined
? event.detail.index
: index !== undefined
? parseInt(index)
: -1;
if (deleteIndex === -1 || !key) {
console.error("删除失败:缺少必要的参数", {
key,
index,
detail: event.detail,
});
return;
}
const { imageList } = this.data;
const currentList = imageList[key] || [];
if (deleteIndex < 0 || deleteIndex >= currentList.length) {
console.error("删除失败:索引超出范围", {
deleteIndex,
listLength: currentList.length,
});
return;
}
// 创建新数组,避免直接修改引用导致视图不更新
const newList = currentList.filter((_, i) => i !== deleteIndex);
// 使用完整路径更新 setData,确保视图更新
this.setData({
[`imageList.${key}`]: newList,
});
// 同步到页面数据
this.syncToPage();
wx.showToast({
title: "删除成功",
icon: "success",
duration: 1000,
});
},
// 同步数据到页面
syncToPage() {
const thisPage = wx.$router.getPageInstance();
const { dataAttrName, imageList } = this.data;
if (thisPage) {
thisPage.setData({
[dataAttrName]: imageList,
});
}
},
// 处理视频上传(自定义上传方式,类似 confirmCar)
handleVideoUpload(e) {
const { field } = e.currentTarget.dataset;
const { imageList } = this.data;
const currentList = imageList[field.key] || [];
const doneCount = currentList.filter(
(item) => item.status === "done"
).length;
const count = (field.maxCount || 1) - doneCount;
if (count <= 0) {
wx.showToast({
title: "已达到最大上传数量",
icon: "none",
});
return;
}
wx.chooseMedia({
count: count,
mediaType: ["video"], // 视频资料只能选择视频文件
maxDuration: 60,
success: (res) => {
const ext =
res.tempFiles?.length == 1 && res.tempFiles[0].tempFilePath
? res.tempFiles[0].tempFilePath.split(".").pop().toLowerCase()
: "";
const fileType = ["mp4", "mov", "avi", "m4v", "3gp"].includes(ext)
? "video"
: "image";
let requestEvent =
fileType == "video" ? "uploadFileWithCover" : "uploadImg";
// 验证文件大小
for (const file of res.tempFiles) {
if (fileType == "video") {
if (file.size > 100 * 1024 * 1024) {
return wx.showToast({
title: "视频上传最大100MB",
icon: "none",
});
}
} else {
if (file.size > 10 * 1024 * 1024) {
return wx.showToast({
title: "图片上传最大10MB",
icon: "none",
});
}
}
}
// 批量上传
this.uploadVideoFiles(res.tempFiles, field, requestEvent, fileType);
},
});
},
// 批量上传视频文件(使用 OSS uploadFile)
async uploadVideoFiles(tempFiles, field, requestEvent, fileType) {
wx.showLoading({
title: "上传中",
mask: true,
});
try {
const { imageList } = this.data;
let currentList = imageList[field.key] || [];
for (const item of tempFiles) {
const fileObj = {
tempFilePath: item.tempFilePath || item.path,
url: item.tempFilePath || item.path,
size: item.size,
};
const url = await uploadFile(fileObj);
const newFile =
fileType == "video"
? {
url,
thumb: url + (url.indexOf("?") >= 0 ? "&" : "?") + "x-oss-process=video/snapshot,t_7000,f_jpg,w_800,h_600,m_fast",
coverUrl: url + (url.indexOf("?") >= 0 ? "&" : "?") + "x-oss-process=video/snapshot,t_7000,f_jpg,w_800,h_600,m_fast",
isVideo: true,
name: field.label,
key: field.key,
status: "done",
}
: {
url,
isVideo: false,
name: field.label,
key: field.key,
status: "done",
};
currentList = [...currentList, newFile];
this.setData(
{
[`imageList.${field.key}`]: currentList,
},
() => {
this.syncToPage();
}
);
}
wx.hideLoading();
wx.showToast({
title: "上传成功",
icon: "success",
duration: 2000,
});
} catch (error) {
wx.hideLoading();
wx.showToast({
title: "上传失败,请重试",
icon: "none",
});
}
},
// 预览视频媒体(类似 confirmCar 的 previewMedia)
previewVideoMedia(e) {
const { key, fileUrl } = e.currentTarget.dataset;
const { imageList } = this.data;
const currentList = imageList[key] || [];
const doneFiles = currentList.filter((item) => item.status === "done");
if (doneFiles.length === 0) return;
// 找到当前文件
const currentFile = doneFiles.find((item) => item.url === fileUrl);
if (!currentFile) return;
// 判断是否为视频文件
const isVideo = currentFile.isVideo || false;
if (isVideo) {
// 视频预览:判断是否是企业微信环境
const sysInfo = getSystemInfoSync();
if (sysInfo.environment === "wxwork") {
// 企业微信环境,使用自定义弹窗播放视频
this.setData({
showVideoPopup: true,
videoUrl: currentFile.url,
});
} else {
// 微信环境,使用 wx.previewMedia
// 构建预览数据源
const media = doneFiles.map((item) => {
if (item.isVideo) {
return {
url: item.url,
type: "video",
poster: item.coverUrl || item.thumb || "",
};
} else {
return {
url: getImageDisplayUrl(item.url, false),
type: "image",
};
}
});
// 找到当前文件在 doneFiles 中的索引
const currentIndex = doneFiles.findIndex(
(item) => item.url === fileUrl
);
wx.previewMedia({
sources: media,
current: currentIndex >= 0 ? currentIndex : 0,
});
}
} else {
// 图片预览(展示用 URL 带 OSS 处理参数)
const imageUrls = doneFiles
.filter((item) => !item.isVideo)
.map((item) => getImageDisplayUrl(item.url, false));
const displayCurrent = getImageDisplayUrl(fileUrl, false);
const currentIndex = imageUrls.findIndex((url) => url === displayCurrent);
console.log(fileUrl, "fileUrl");
wx.previewImage({
current: displayCurrent,
urls: imageUrls,
success: () => {
console.log("success");
},
fail: () => {
console.log("fail");
},
});
}
},
// 关闭视频播放弹窗
onCloseVideoPopup() {
this.setData({
showVideoPopup: false,
videoUrl: "",
});
},
// 处理视频预览(使用 wx.previewMedia 预览视频)- 保留用于 van-uploader 的预览
handleVideoPreview(event) {
const { file, index } = event.detail;
const { key } = event.currentTarget.dataset;
if (!file || !key) return;
// 获取当前字段的所有文件
const { imageList } = this.data;
const currentList = imageList[key] || [];
// 判断是否为视频文件
// 兼容 van-uploader 的文件对象:可能使用 url、path 或 tempFilePath
const filePath = file.url || file.path || file.tempFilePath;
const isVideo =
file.isVideo ||
(filePath &&
["mp4", "mov", "avi", "m4v", "3gp"].includes(
filePath.split(".").pop()?.toLowerCase()
));
if (isVideo) {
// 判断是否是企业微信环境
const sysInfo = getSystemInfoSync();
if (sysInfo.environment === "wxwork") {
// 企业微信环境,使用自定义弹窗播放视频
const videoUrl = file.url || file.path || file.tempFilePath;
this.setData({
showVideoPopup: true,
videoUrl: videoUrl,
});
} else {
// 微信环境,使用 wx.previewMedia 预览
// 构建预览数据源(只包含已完成上传的文件)
const doneFiles = currentList.filter(
(item) => item.status === "done"
);
const sources = doneFiles.map((item) => {
if (item.isVideo) {
return {
url: item.url,
type: "video",
poster: item.coverUrl || item.thumb || "",
};
} else {
return {
url: getImageDisplayUrl(item.url, false),
type: "image",
};
}
});
// 找到当前文件在已完成文件列表中的索引
const currentIndex = doneFiles.findIndex(
(item) =>
item.url === file.url ||
(item.isVideo && file.isVideo && item.url === file.url)
);
if (sources.length > 0) {
wx.previewMedia({
sources: sources,
current: currentIndex >= 0 ? currentIndex : index || 0,
});
}
}
}
// 对于图片文件,让 van-uploader 自己处理预览
},
// 校验必填项
validate() {
const {
imageList,
uploadFields,
uploadCertificateFields,
uploadVideoFields,
uploadReportFields,
uploadOtherFields,
} = this.data;
// 合并所有字段
const allFields = [
...uploadFields,
...uploadCertificateFields,
...uploadVideoFields,
...uploadReportFields,
...uploadOtherFields,
];
// 1. 检查是否有正在上传的文件
for (let field of allFields) {
const list = imageList[field.key] || [];
const uploadingFiles = list.filter(
(file) => file.status === "uploading"
);
if (uploadingFiles.length > 0) {
return {
message: `${field.label}正在上传中,请稍候...`,
};
}
}
// 2. 检查必填的车辆图片(只检查状态为 done 的文件)
for (let field of uploadFields) {
if (field.required) {
console.log(imageList[field.key], "imageList[field.key]");
const list = imageList[field.key] || [];
// 只统计上传完成的文件
const doneFiles = list.filter(
(file) => !file.status || file.status === "done"
);
if (doneFiles.length === 0) {
return {
message: `请上传${field.label}`,
};
}
}
}
return null;
},
},
});