绿满眶商城微信小程序-uniapp
peng
2025-10-30 c61f03cfe1fcf023c7128f77f3d692aef309f77f
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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
<template>
    <div class="wrapper">
        <u-popup class="popup" :value="buyMask" :height="setup.height" closeable :mode="setup.mode" :border-radius="setup.radius" @input="handlePopupInput" @close="closeMask()">
            <!-- 商品 -->
            <view class="goods-box bottom">
                <view class="goods-header">
                    <view class="goods-img">
                        <!-- <u-image width="200rpx" border-radius="20" class="uimage" height="200rpx" :src="selectedSpecImg ? selectedSpecImg : goodsDetail.thumbnail"></u-image> -->
                    </view>
                    <view class="goods-skus">
                        <!-- 有活动商品价格 -->
                        <view class="goods-price" v-if="goodsDetail.promotionPrice && ((isGroup && buyType === 'PINTUAN') || !isGroup)">
                            <span v-if="goodsDetail.promotionPrice && !pointDetail">
                                ¥
                                <span class="goods-price-promotionShow goods-price-bigshow">{{ $options.filters.goodsFormatPrice(goodsDetail.promotionPrice)[0] }}</span>
                                .{{ $options.filters.goodsFormatPrice(goodsDetail.promotionPrice)[1] }}
                            </span>
                            <span v-if="pointDetail.points">
                                <span class="goods-price-promotionShow goods-price-bigshow">{{ pointDetail.points }}</span>
                                积分
                            </span>
                            <div class="promotion-box">
                                ¥
                                <span class="goods-price-bigshow">{{ $options.filters.goodsFormatPrice(goodsDetail.price)[0] }}</span>
                                .{{ $options.filters.goodsFormatPrice(goodsDetail.price)[1] }}
                            </div>
                        </view>
                        <!-- 正常商品的价格 -->
                        <view v-else>
                            <!-- 批发价格 -->
                            <div class="price-row flex" v-if="goodsDetail.salesModel === 'WHOLESALE'">
                                <div class="goods-price" v-for="(item, index) in wholesaleList" :key="index">
                                    <span>
                                        ¥
                                        <span class="goods-price-bigshow">{{ $options.filters.goodsFormatPrice(item.price)[0] }}</span>
                                        .{{ $options.filters.goodsFormatPrice(item.price)[1] }}
                                    </span>
                                    <span class="wholesale-item">{{ item.num }}{{ goodsDetail.goodsUnit }}</span>
                                </div>
                            </div>
                            <div class="goods-price" v-else>
                                <span>
                                    ¥
                                    <span class="goods-price-bigshow">{{ $options.filters.goodsFormatPrice(goodsDetail.price)[0] }}</span>
                                    .{{ $options.filters.goodsFormatPrice(goodsDetail.price)[1] }}
                                </span>
                            </div>
                        </view>
                        <view class="goods-check-skus">
                            已选
                            <span class="goods-check-skus-name">
                                {{ selectName }}
                                <span>,{{ num }}个</span>
                            </span>
                        </view>
                        <view class="goods-check-skus">
                            库存
                            <span class="goods-check-skus-name">
                                <span>{{ goodsDetail.quantity }}</span>
                            </span>
                        </view>
                    </view>
                </view>
                <!-- 商品信息 -->
                <scroll-view class="goods-skus-box" :scroll-y="true">
                    <!-- 规格 -->
                    <view class="goods-skus-view" :key="specIndex" v-for="(spec, specIndex) in formatList">
                        <view class="skus-view-list">
                            <view class="view-class-title">{{ spec.name }}</view>
 
                            <!-- 正常逻辑 循环出sku -->
                            <view
                                v-if="!parentOrder"
                                :class="{ active: spec_val.value == currentSelected[specIndex] }"
                                class="skus-view-item"
                                v-for="(spec_val, spec_index) in spec.values"
                                :key="spec_index"
                                @click="handleClickSpec(spec, specIndex, spec_val)"
                            >
                                {{ spec_val.value }}
                            </view>
 
                            <!-- 拼团购买,仅筛选出当前拼团类型商品 -->
                            <view
                                v-if="parentOrder && spec_val.skuId == goodsDetail.id"
                                :class="{ active: spec_val.value == currentSelected[specIndex] }"
                                class="skus-view-item"
                                v-for="(spec_val, spec_index) in spec.values"
                                :key="spec_index"
                                @click="handleClickSpec(spec, specIndex, spec_val)"
                            >
                                {{ spec_val.value }}
                            </view>
                        </view>
                    </view>
                    <div class="soldout" v-if="goodsDetail.quantity === 0">
                        <u-alert-tips type="warning" title="商品已售罄" description="当前商品库存为0"></u-alert-tips>
                    </div>
                    <!-- 数量 -->
                    <view v-if="goodsDetail.quantity !== 0" class="goods-skus-number flex flex-a-c flex-j-sb">
                        <view class="view-class-title">数量</view>
                        <uni-number-box class="uNumber" :min="1" :max="999" :disabled="goodsDetail.quantity === 0"  v-model="num"></uni-number-box>
                    </view>
                    <view class="template">
                        <view class="template-title" v-if="consumizetemplateInfo && consumizetemplateInfo.name">
                            <text class="title-text">{{ consumizetemplateInfo.name }}</text>
                        </view>
                        
                        <!-- 图片选择区域 -->
                        <view class="template-images" v-if="consumizetemplateInfo && consumizetemplateInfo.templateImgs && consumizetemplateInfo.templateImgs.length > 0">
                            <view class="images-grid">
                                <view 
                                    v-for="(img, index) in consumizetemplateInfo.templateImgs" 
                                    :key="img.id"
                                    class="image-item"
                                    :class="{ selected: selectedImageObjects.some(selectedImg => selectedImg.id === img.id) }"
                                    @click="handleImageClick(img, index)"
                                >
                                    <image 
                                        :src="getFilePreviewUrlSync(img.imgUrl)" 
                                        class="image-preview"
                                        mode="aspectFill"
                                    />
                                    <view v-if="selectedImageObjects.some(selectedImg => selectedImg.id === img.id)" class="selected-overlay">
                                        <uni-icons type="checkmarkempty" size="30" color="#fff"></uni-icons>
                                    </view>
                                </view>
                            </view>
                            
                            <!-- 查看选中图片按钮 -->
                            <view class="view-selected-image" v-if="selectedImageObjects.length > 0" @click="viewSelectedImage">
                                查看选中模板图片
                            </view>
                        </view>
                        
                        <!-- 动态表单项 -->
                        <view class="template-form" v-if="consumizetemplateInfo && consumizetemplateInfo.templateConstomizeTitles">
                            <view 
                                v-for="(item, index) in consumizetemplateInfo.templateConstomizeTitles" 
                                :key="item.id"
                                class="form-item"
                            >
                                <text class="form-label">{{ item.templateTitle }}</text>
                                
                                <!-- 文本输入框 -->
                                <input 
                                    v-if="item.contentType === 'TEXT'"
                                    class="form-input"
                                    :value="getFormValue(item.id)"
                                    @input="updateFormValue(item.id, item.templateTitle, $event.target.value)"
                                    :placeholder="'请输入' + item.templateTitle"
                                />
                                
                                <!-- 图片上传(不使用u-upload组件) -->
                                <view v-if="item.contentType === 'IMAGE'" class="form-upload">
                                    <!-- 上传按钮 -->
                                    <view class="upload-btn" @click="chooseImage(item.id)">
                                        <uni-icons type="plusempty" size="40" color="#999"></uni-icons>
                                        <text class="upload-text">点击上传</text>
                                    </view>
                                    
                                    <!-- 图片预览区域 -->
                                    <view v-if="imagePreviewUrls[item.id]" class="image-preview-container">
                                        <image 
                                            :src="imagePreviewUrls[item.id]" 
                                            class="uploaded-image" 
                                            mode="aspectFill"
                                        />
                                        <uni-icons 
                                            type="closeempty" 
                                            class="delete-icon" 
                                            @click="deleteImage(item.id)"
                                            size="20"
                                            color="#fff"
                                        ></uni-icons>
                                    </view>
                                    
                                    <!-- 上传进度条 -->
                                    <progress 
                                        v-if="uploadProgress[item.id] && uploadProgress[item.id] > 0 && uploadProgress[item.id] < 100"
                                        :percent="uploadProgress[item.id]" 
                                        active-mode="forwards" 
                                        show-info 
                                        stroke-width="6" 
                                        :active="true" 
                                        active-color="#ff573e" 
                                        style="margin-top: 10rpx;"
                                    />
                                    
                                    <!-- 上传状态信息 -->
                                    <text v-if="uploadStatus[item.id]" class="upload-status">
                                        {{ uploadStatus[item.id] }}
                                    </text>
                                </view>
                            </view>
                        </view>
                    </view>
                </scroll-view>
                <!-- 按钮 -->
                <view class="btns" v-if="goodsDetail.quantity !== 0">
                    <view class="box-btn card" v-if="buyType != 'PINTUAN' && goodsDetail.goodsType != 'VIRTUAL_GOODS' && goodsDetail.customizeFlag!=='CUSTOMIZE'" @click="addToCartOrBuy('cart')">加入购物车</view>
                    <view class="box-btn buy" @click="addToCartOrBuy('buy')">立即购买</view>
                </view>
            </view>
        </u-popup>
    </div>
</template>
<script>
import '@/components/uview-components/uview-ui';
 
import * as API_trade from '@/api/trade.js';
import setup from './popup';
// import uniNumberBox from '@/components/uni-number-box'
import { getFilePreviewUrl } from "@/api/common.js";
import { getSTSToken } from "@/api/common.js";
import { getFileKey } from "@/utils/file.js";
 
export default {
    components: {
        // uniNumberBox
    },
    data() {
        return {
            setup,
            num: this.wholesaleList && this.wholesaleList.length > 0 ? this.wholesaleList[0].num : 1,
 
            selectName: '', //选中商品的昵称
            selectSkuList: '', //选中商铺sku,
            selectedSpecImg: '', //选中的图片路径
            buyType: '', //用于存储促销,拼团等活动类型
            parentOrder: '', //父级拼团活动的数据 - 如果是团员则有数据
            formatList: [],
            currentSelected: [],
            skuList: '',
            isClose: false, //是否可以点击遮罩关闭
            
            // 表单相关数据
            selectedImages: [], // 选中的模板图片
            selectedImageObjects: [], // 选中的模板图片对象(包含ID等完整信息)
            formValues: {
                templateId: "", // 模板ID
                templateName: "", // 模板名称
                chooseImage: "", // 选中的图片
                chooseImageId: "", // 选中图片的ID
                templateForm: [] // 表单数组 [{id, templateTitle, value}]
            }, // 表单值
            imagePreviewUrls: {}, // 图片预览URL
            uploadProgress: {}, // 上传进度
            uploadStatus: {}, // 上传状态信息
            cosClient: null, // COS客户端
            bucket: '', // 存储桶
            region: '', // 地域
            endpoint: '', // COS访问endpoint
            previewUrls: {} // 文件预览地址缓存
        };
    },
    props: {
        wholesaleList: {
            type: null,
            default: false
        },
        buyMask: {
            type: Boolean,
            default: false
        },
        isGroup: {
            type: Boolean,
            default: false
        },
        goodsDetail: {
            default: '',
            type: null
        },
        selectedSku: {
            default: '',
            type: null
        },
        goodsSpec: {
            default: '',
            type: null
        },
        addr: {
            default: '',
            type: null
        },
        pointDetail: {
            default: '',
            type: null
        },
        consumizetemplateInfo:{
            default:'',
            type: null
        }
    },
    computed: {
        wholesalePrice(key) {
            return this.wholesaleList.length
                ? this.wholesaleList.map(item => {
                        return item.price;
                  })
                : [];
        },
        wholesaleNum(key) {
            return this.wholesaleList.length
                ? this.wholesaleList.map(item => {
                        return item.num;
                  })
                : [];
        }
    },
    watch: {
        num(val) {
            
            val == 0 ? this.num = 1 : ''
            if (val) {
                
                //超过库存后修改回库存
                if (val > this.goodsDetail.quantity) {
                    this.$nextTick(function() {
                        this.num = this.goodsDetail.quantity;
                    });
                }
            }
        },
        buyType: {
            handler(val) {
                if (val) {
                    // 使用直接赋值而不是this.$set
                    this.buyType = val;
                }
            },
            immediate: true
        },
        selectSkuList: {
            handler(val, oldval) {
                // 使用setTimeout延迟触发事件,避免影响弹窗
                setTimeout(() => {
                    this.$emit('changed', val);
                }, 100);
            },
            deep: true
        },
        'goodsDetail.quantity': {
            handler(val) {
                if (val == 0) {
                    // 使用setTimeout延迟显示提示,避免影响弹窗
                    setTimeout(() => {
                        uni.showToast({
                            title: '商品已售罄',
                            duration: 2000,
                            icon: 'none'
                        });
                    }, 100);
                    this.num = 1;
                }
            }
        },
        // 监听模板信息变化
        consumizetemplateInfo: {
            handler(newVal, oldVal) {
                if (newVal) {
                    // 使用setTimeout延迟执行,避免影响弹窗
                    setTimeout(() => {
                        // 预加载模板图片的预览地址
                        this.preloadTemplateImages();
                        // 初始化模板表单数据
                        this.initTemplateFormData();
                        // 设置默认选中第一张模板图片
                        this.setDefaultSelectedImage();
                    }, 100);
                }
            },
            immediate: true,
            deep: true
        }
    },
 
    methods: {
        // 初始化腾讯云COS客户端
        initCOS() {
            getSTSToken().then(res => {
                const COS = require('@/lib/cos-wx-sdk-v5.js');
                // 使用直接赋值而不是this.$set
                this.cosClient = new COS({
                    SecretId: res.data.data.tmpSecretId,
                    SecretKey: res.data.data.tmpSecretKey,
                    SecurityToken: res.data.data.sessionToken,
                    StartTime: res.data.data.stsStartTime,
                    ExpiredTime: res.data.data.stsEndTime,
                    SimpleUploadMethod: 'putObject'
                });
                this.bucket = res.data.data.bucket;
                this.region = res.data.data.region;
                this.endpoint = res.data.data.endpoint; // 获取endpoint
            }).catch(err => {
                console.error('初始化COS失败', err);
                // 使用setTimeout延迟显示提示,避免影响弹窗
                setTimeout(() => {
                    uni.showToast({
                        title: '上传服务初始化失败',
                        icon: 'none'
                    });
                }, 100);
            });
        },
        
        // 获取文件预览地址
        async fetchFilePreviewUrl(fileKey) {
            // 如果是完整的URL,直接返回
            if (fileKey && (fileKey.startsWith('http://') || fileKey.startsWith('https://'))) {
                return fileKey;
            }
            
            // 如果已经缓存过,直接返回缓存值
            if (this.previewUrls[fileKey]) {
                return this.previewUrls[fileKey];
            }
            
            // 调用API获取预览地址
            try {
                const res = await getFilePreviewUrl(fileKey);
                const previewUrl = res.data.data;
                // 使用直接赋值而不是this.$set
                this.previewUrls[fileKey] = previewUrl;
                return previewUrl;
            } catch (error) {
                console.error('获取文件预览地址失败', error);
                return fileKey; // 如果获取失败,返回原始值
            }
        },
        
        // 选择模板图片(单选模式)
        selectImage(imgObject) {
            // 检查当前选中的图片是否就是点击的图片
            const selectedIndex = this.selectedImageObjects.findIndex(selectedImg => selectedImg.id === imgObject.id);
            
            if (selectedIndex > -1) {
                // 如果点击的是已选中的图片,则取消选择
                this.selectedImageObjects = [];
                this.selectedImages = [];
            } else {
                // 如果点击的是未选中的图片,则选中该图片(清空之前的选择)
                this.selectedImageObjects = [imgObject];
                this.selectedImages = [imgObject.imgUrl];
            }
            
            // 更新formValues中的chooseImage和chooseImageId
            this.formValues.chooseImage = this.selectedImages.join(',');
            // 将选中图片的ID也存储到formValues中
            this.formValues.chooseImageId = this.selectedImageObjects.map(img => img.id).join(',');
            
            // 强制更新视图以确保UI正确反映数据变化
            this.$forceUpdate();
        },
        
        // 选择图片(参考video.vue的实现)
        chooseImage(fieldId) {
            if (!this.cosClient) {
                // 使用setTimeout延迟显示提示,避免影响弹窗
                setTimeout(() => {
                    uni.showToast({
                        title: '上传服务未初始化',
                        icon: 'none'
                    });
                }, 100);
                // 重新初始化COS
                this.initCOS();
                return;
            }
            
            uni.chooseImage({
                count: 1,
                sizeType: ['compressed'],
                sourceType: ['album', 'camera'],
                success: (res) => {
                    const tempFilePath = res.tempFilePaths[0];
                    
                    // 获取文件名
                    let fileName = tempFilePath.substring(tempFilePath.lastIndexOf('/') + 1);
                    // 处理安卓可能的URI编码
                    if(fileName.indexOf('%') > -1) {
                        fileName = decodeURIComponent(fileName);
                    }
                    
                    // 生成文件key
                    const fileKey = getFileKey(fileName);
                    
                    // 显示预览图
                    this.$set(this.imagePreviewUrls, fieldId, tempFilePath);
                    
                    // 初始化上传状态
                    this.$set(this.uploadProgress, fieldId, 0);
                    this.$set(this.uploadStatus, fieldId, '上传中...');
                    
                    // 强制更新视图
                    this.$forceUpdate();
                    
                    // 执行上传
                    this.cosClient.uploadFile({
                        Bucket: this.bucket,
                        Region: this.region,
                        Key: fileKey,
                        FilePath: tempFilePath,
                        SliceSize: 1024 * 1024 * 5, // 5M
                        onProgress: (progressData) => {
                            // 更新上传进度
                            const progress = Math.round(progressData.percent * 100);
                            this.$set(this.uploadProgress, fieldId, progress);
                            
                            // 强制更新视图
                            this.$forceUpdate();
                        }
                    }, (err, data) => {
                        // 清除上传进度
                        // 使用Vue.delete或this.$delete可能会触发组件更新,改用直接删除
                        delete this.uploadProgress[fieldId];
                        
                        if (err) {
                            console.error('上传失败', err);
                            // 更新上传状态
                            this.$set(this.uploadStatus, fieldId, '上传失败');
                            // 清除预览图
                            this.$delete(this.imagePreviewUrls, fieldId);
                            // 注意:这里不再需要清除formValues中的字段,因为我们已经修改了数据结构
                            
                            // 强制更新视图
                            this.$forceUpdate();
                            
                            // 使用setTimeout延迟显示错误提示,避免影响弹窗
                            setTimeout(() => {
                                uni.showToast({
                                    title: '上传失败',
                                    icon: 'none'
                                });
                            }, 100);
                        } else {
                            // 获取当前表单项的信息
                            const currentItem = this.consumizetemplateInfo.templateConstomizeTitles.find(item => item.id === fieldId);
                            const templateTitle = currentItem ? currentItem.templateTitle : '';
                            
                            // 上传成功,更新表单值
                            this.updateFormValue(fieldId, templateTitle, fileKey);
                            
                            // 更新上传状态
                            this.$set(this.uploadStatus, fieldId, '上传成功');
                            
                            // 强制更新视图
                            this.$forceUpdate();
                            
                            // 使用setTimeout延迟显示成功提示,避免影响弹窗
                            setTimeout(() => {
                                uni.showToast({
                                    title: '上传成功',
                                    icon: 'success'
                                });
                            }, 100);
                        }
                    });
                },
                fail: (err) => {
                    console.error('选择图片失败', err);
                    // 区分用户取消操作和真正的错误
                    // 如果是用户主动取消,不显示提示
                    if (err.errMsg && err.errMsg.indexOf('cancel') === -1 && err.errMsg.indexOf('fail') !== -1) {
                        // 使用setTimeout延迟显示提示,避免影响弹窗
                        setTimeout(() => {
                            uni.showToast({
                                title: '未选择图片',
                                icon: 'none'
                            });
                        }, 100);
                    }
                }
            });
        },
        
        // 删除图片
        deleteImage(fieldId) {
            // 清除预览图
            this.$delete(this.imagePreviewUrls, fieldId);
            // 清除上传状态
            this.$delete(this.uploadStatus, fieldId);
            // 清除上传进度
            this.$delete(this.uploadProgress, fieldId);
            
            // 从formValues中移除对应的表单字段
            const index = this.formValues.templateForm.findIndex(item => item.id === fieldId);
            if (index > -1) {
                this.formValues.templateForm.splice(index, 1);
            }
            
            // 使用setTimeout延迟显示提示,避免影响弹窗
            setTimeout(() => {
                uni.showToast({
                    title: '删除成功',
                    icon: 'success'
                });
            }, 100);
            
            // 强制更新视图
            this.$forceUpdate();
        },
        
        // 获取表单值
        getFormValue(fieldId) {
            const field = this.formValues.templateForm.find(item => item.id === fieldId);
            return field ? field.value : '';
        },
        
        // 更新表单值
        updateFormValue(fieldId, templateTitle, value) {
            // 更新模板基本信息
            this.formValues.templateId = this.consumizetemplateInfo.id;
            this.formValues.templateName = this.consumizetemplateInfo.name;
            
            // 查找对应的模板字段以获取contentType
            const templateItem = this.consumizetemplateInfo.templateConstomizeTitles.find(item => item.id === fieldId);
            const contentType = templateItem ? templateItem.contentType : '';
            
            // 查找是否已存在该字段
            const existingIndex = this.formValues.templateForm.findIndex(item => item.id === fieldId);
            
            if (existingIndex > -1) {
                // 更新现有字段
                this.$set(this.formValues.templateForm, existingIndex, {
                    id: fieldId,
                    templateTitle: templateTitle,
                    contentType: contentType, // 添加contentType
                    value: value
                });
            } else {
                // 添加新字段
                this.formValues.templateForm.push({
                    id: fieldId,
                    templateTitle: templateTitle,
                    contentType: contentType, // 添加contentType
                    value: value
                });
            }
        },
        
        // 更新模板表单数据
        updateTemplateFormData(fieldId, templateTitle, value) {
            // 这个方法现在可以保持不变,因为我们已经在updateFormValue中处理了formValues的更新
            // 如果需要其他处理,可以在这里添加
        },
        
        // 表单提交处理
        handleFormSubmit() {
            // 确保选中的图片对象也被包含在提交数据中
            const formData = {
                ...this.formValues,
                selectedImageObjects: this.selectedImageObjects
            };
            
            console.log('表单数据:', formData);
            // 这里可以添加实际的表单提交逻辑
            // 使用setTimeout延迟显示提示,避免影响弹窗
            setTimeout(() => {
                uni.showToast({
                    title: '表单提交成功',
                    icon: 'success'
                });
            }, 100);
        },
        
        numCheck(val) {
            if (this.wholesaleList && this.wholesaleList.length > 0) {
                if (this.num <= this.wholesaleList[0].num) {
                    // 使用setTimeout延迟显示提示,避免影响弹窗
                    setTimeout(() => {
                        uni.showToast({
                            title: '批发商品购买数量不能小于起批数量!',
                            duration: 2000,
                            icon: 'none'
                        });
                    }, 100);
                    this.num = this.wholesaleList[0].num;
                }
            }
        },
        closeMask() {
            this.$emit('closeBuy', false);
        },
        
        // 处理弹窗输入事件,避免直接修改prop
        handlePopupInput(val) {
            this.$emit('closeBuy', val);
        },
        
        /**点击规格 */
        handleClickSpec(val, index, specValue) {
            this.currentSelected[index] = specValue.value;
            let selectedSkuId = this.goodsSpec.find(i => {
                let matched = true;
                let specValues = i.specValues.filter(j => j.specName !== 'images');
                for (let n = 0; n < specValues.length; n++) {
                    if (specValues[n].specValue !== this.currentSelected[n]) {
                        matched = false;
                        return;
                    }
                }
                if (matched) {
                    return i;
                }
            });
            if (selectedSkuId?.skuId) {
                this.$set(this.currentSelected, index, specValue.value);
                this.selectSkuList = {
                    spec: {
                        specName: val.name,
                        specValue: specValue.value
                    },
                    data: this.goodsDetail
                };
                this.selectName = specValue.value;
 
                this.$emit('handleClickSku', {
                    skuId: selectedSkuId.skuId,
                    goodsId: this.goodsDetail.goodsId
                });
            } else {
                // 使用setTimeout延迟显示提示,避免影响弹窗
                setTimeout(() => {
                    uni.showToast({
                        title: '暂无该商品!',
                        duration: 2000,
                        icon: 'none'
                    });
                }, 100);
            }
        },
 
        /**
         * 直接购买
         */
        buy(data) {
            API_trade.addToCart(data).then(res => {
                if (res.data.success) {
                    // 使用setTimeout延迟跳转,避免影响弹窗
                    setTimeout(() => {
                        uni.navigateTo({
                            url: `/pages/order/fillorder?way=${data.cartType}&addr=${''}&parentOrder=${encodeURIComponent(JSON.stringify(this.parentOrder))}`
                        });
                    }, 100);
                }
            });
        },
 
        /**
         * 添加到购物车或购买
         */
        addToCartOrBuy(val) {
            // 检查商品是否需要模板并且模板数据是否完整
            if (this.consumizetemplateInfo && this.consumizetemplateInfo.id) {
                const isValid = this.validateTemplateData();
                if (!isValid) {
                    return;
                }
            }
            
            console.log(JSON.stringify(this.formValues))
            if (!this.selectSkuList) {
                // 使用setTimeout延迟显示提示,避免影响弹窗
                setTimeout(() => {
                    uni.showToast({
                        title: '请选择规格商品',
                        icon: 'none'
                    });
                }, 100);
                return;
            }
            let data = {
                skuId: this.goodsDetail.id,
                num: this.num
            };
 
            if (val == 'cart') {
                API_trade.addToCart(data).then(res => {
                    if (res.data.code == 200) {
                        // 使用setTimeout延迟显示提示,避免影响弹窗
                        setTimeout(() => {
                            uni.showToast({
                                title: '商品已添加到购物车',
                                icon: 'none'
                            });
                        }, 100);
 
                        this.$emit('queryCart');
                        this.closeMask();
                    }
                });
            } else {
                // 判断是否拼团商品
                if (this.buyType) {
                    data.cartType = 'PINTUAN';
                } else if (this.goodsDetail.goodsType == 'VIRTUAL_GOODS') {
                    data.cartType = 'VIRTUAL';
                } else {
                    data.cartType = 'BUY_NOW';
                }
 
                // 创建包含图片ID的完整表单数据
                const templateData = {
                    ...this.formValues,
                    selectedImageObjects: this.selectedImageObjects
                };
 
                API_trade.addToCart(data).then(res => {
                    if (res.data.code == 200) {
                        // 使用setTimeout延迟跳转,避免影响弹窗
                        setTimeout(() => {
                            uni.navigateTo({
                                url: `/pages/order/fillorder?way=${data.cartType}&addr=${this.addr.id || ''}&template=${encodeURIComponent(JSON.stringify(templateData))}&parentOrder=${encodeURIComponent(JSON.stringify(this.parentOrder))}`
                            });
                        }, 100);
                    }
                });
            }
        },
        
        /**
         * 验证模板数据是否完整
         */
        validateTemplateData() {
            // 检查模板图片是否已选择(如果有模板图片)
            if (this.consumizetemplateInfo.templateImgs && 
                this.consumizetemplateInfo.templateImgs.length > 0 && 
                this.selectedImageObjects.length === 0) {
                setTimeout(() => {
                    uni.showToast({
                        title: '请选择模板图片',
                        icon: 'none'
                    });
                }, 100);
                return false;
            }
            
            // 检查动态表单项是否已填写
            if (this.consumizetemplateInfo.templateConstomizeTitles) {
                for (const item of this.consumizetemplateInfo.templateConstomizeTitles) {
                    const formItem = this.formValues.templateForm.find(formItem => formItem.id === item.id);
                    
                    // 如果表单项不存在或者值为空
                    if (!formItem || !formItem.value || formItem.value.trim() === '') {
                        setTimeout(() => {
                            uni.showToast({
                                title: `请填写${item.templateTitle}`,
                                icon: 'none'
                            });
                        }, 100);
                        return false;
                    }
                    
                    // 特别检查图片类型的表单项是否有上传
                    if (item.contentType === 'IMAGE' && 
                        (!this.imagePreviewUrls[item.id] || this.imagePreviewUrls[item.id].trim() === '')) {
                        setTimeout(() => {
                            uni.showToast({
                                title: `请上传${item.templateTitle}`,
                                icon: 'none'
                            });
                        }, 100);
                        return false;
                    }
                }
            }
            
            return true;
        },
        formatSku(list) {
            // 格式化数据
            let arr = [{}];
 
            if (!Array.isArray(list)) {
                return false;
            }
            list.forEach((item, index) => {
                item.specValues.forEach((spec, specIndex) => {
                    let name = spec.specName;
                    let values = {
                        value: spec.specValue,
                        quantity: item.quantity,
                        skuId: item.skuId
                    };
                    if (name === 'images') {
                        return;
                    }
 
                    arr.forEach((arrItem, arrIndex) => {
                        if (
                            arrItem.name == name &&
                            arrItem.values &&
                            !arrItem.values.find(i => {
                                return i.value === values.value;
                            })
                        ) {
                            // 使用直接赋值而不是this.$set
                            arrItem.values.push(values);
                        }
 
                        let keys = arr.map(key => {
                            return key.name;
                        });
                        if (!keys.includes(name)) {
                            // 使用直接赋值而不是this.$set
                            arr.push({
                                name: name,
                                values: [values]
                            });
                        }
                    });
                });
            });
 
            arr.shift();
            // 使用直接赋值而不是this.$set
            this.formatList = arr;
 
            list.forEach(item => {
                // 默认选中
                if (item.skuId === this.goodsDetail.id) {
                    item.specValues
                        .filter(i => i.specName !== 'images')
                        .forEach((value, _index) => {
                            // 使用直接赋值而不是this.$set
                            this.currentSelected[_index] = value.specValue;
 
                            this.selectName = value.specValue;
 
                            this.selectSkuList = {
                                spec: value,
                                data: this.goodsDetail
                            };
                        });
                }
            });
 
            // 使用直接赋值而不是this.$set
            this.skuList = list;
            // console.log(" this.skuList", this.skuList)
        },
        
        // 预加载模板图片的预览地址
        async preloadTemplateImages() {
            if (this.consumizetemplateInfo && this.consumizetemplateInfo.templateImgs) {
                for (const img of this.consumizetemplateInfo.templateImgs) {
                    await this.fetchFilePreviewUrl(img.imgUrl);
                }
            }
        },
        
        // 获取文件预览地址(同步版本)
        getFilePreviewUrlSync(fileKey) {
            // 如果是完整的URL,直接返回
            if (fileKey && (fileKey.startsWith('http://') || fileKey.startsWith('https://'))) {
                return fileKey;
            }
            // 如果有预览地址,返回预览地址
            if (this.previewUrls && this.previewUrls[fileKey]) {
                return this.previewUrls[fileKey];
            }
            // 如果没有http前缀,需要拼接endpoint进行显示
            if (fileKey && !fileKey.startsWith('http://') && !fileKey.startsWith('https://')) {
                // 使用endpoint拼接完整的URL
                if (this.endpoint) {
                    return this.endpoint + '/' + fileKey;
                }
            }
            // 否则返回原始值
            return fileKey;
        },
        
        // 初始化模板表单数据
        initTemplateFormData() {
            if (this.consumizetemplateInfo && this.consumizetemplateInfo.templateConstomizeTitles) {
                // 初始化formValues的基本信息
                this.formValues.templateId = this.consumizetemplateInfo.id;
                this.formValues.templateName = this.consumizetemplateInfo.name;
                this.formValues.chooseImage = "";
                this.formValues.chooseImageId = "";
                this.formValues.templateForm = [];
                
                // 初始化选中的图片对象数组和图片URL数组
                this.selectedImageObjects = [];
                this.selectedImages = [];
            }
        },
        
        // 预览模板图片
        previewImage(imgObject, index) {
            // 只预览当前选中的图片
            const urls = [this.getFilePreviewUrlSync(imgObject.imgUrl)];
            
            // 调用uniapp原生API预览图片
            uni.previewImage({
                current: 0, // 当前显示图片的索引(只有一张图片,所以是0)
                urls: urls, // 需要预览的图片链接列表
                indicator: 'default', // 显示索引指示器
                loop: false // 只有一张图片,不需要循环预览
            });
        },
        
        // 处理模板图片点击事件(既可以预览又可以选中)
        handleImageClick(imgObject, index) {
            // 检查当前图片是否已选中
            const isSelected = this.selectedImageObjects.some(selectedImg => selectedImg.id === imgObject.id);
            
            if (isSelected) {
                // 如果已选中,则预览图片
                this.previewImage(imgObject, index);
            } else {
                // 如果未选中,则选中图片
                this.selectImage(imgObject);
            }
        },
        
        // 查看选中的图片
        viewSelectedImage() {
            // 检查是否有选中的图片
            if (this.selectedImageObjects.length > 0) {
                // 获取第一个选中的图片(因为是单选模式)
                const selectedImage = this.selectedImageObjects[0];
                // 预览选中的图片
                this.previewImage(selectedImage, 0);
            }
        },
        
        // 设置默认选中第一张模板图片
        setDefaultSelectedImage() {
            // 检查是否有模板图片且当前没有选中任何图片
            if (this.consumizetemplateInfo && 
                this.consumizetemplateInfo.templateImgs && 
                this.consumizetemplateInfo.templateImgs.length > 0 && 
                this.selectedImageObjects.length === 0) {
                // 选中第一张图片
                const firstImage = this.consumizetemplateInfo.templateImgs[0];
                this.selectImage(firstImage);
            }
        }
    },
 
    mounted() {
        // 使用setTimeout延迟执行,避免影响弹窗
        setTimeout(() => {
            this.formatSku(this.goodsSpec);
            this.initCOS(); // 初始化COS客户端
            
            // 预加载模板图片的预览地址
            this.preloadTemplateImages();
 
            // 初始化模板表单数据
            this.initTemplateFormData();
            
            // 默认选中第一张模板图片
            this.setDefaultSelectedImage();
 
            console.log("goodsDetail",this.goodsDetail)
        }, 100);
    }
};
</script>
<style lang="scss" scoped>
@import './popup.scss';
 
.price-row {
    text-align: center;
}
 
.buy {
    background-image: linear-gradient(135deg, #ffba0d, #ffc30d 69%, #ffcf0d);
    box-shadow: 0 2px 6px 0 rgba(255, 65, 66, 0.2);
}
 
.card {
    background-image: linear-gradient(135deg, #f2140c, #f2270c 70%, #f24d0c);
    box-shadow: 0 2px 6px 0 rgba(255, 65, 66, 0.2);
}
 
/deep/.u-icon-plus,
.u-icon-minus,
.u-icon-disabled {
    height: 30rpx !important;
    background: #fff !important;
}
 
.goods-skus-number {
    justify-content: space-between;
    display: flex;
 
    > .view-class-title {
        flex: 8;
    }
 
    > .view-class-input {
        flex: 1;
    }
}
 
/deep/ .uni-scroll-view {
    overflow: hidden !important;
}
 
.active {
    background: $price-light-color !important;
    border: 2rpx solid $price-color;
    font-weight: bold;
    color: $price-color !important;
    box-sizing: border-box;
}
 
.goods-skus-box {
    overflow-y: auto;
    height: 610rpx;
    // #ifdef MP-WEIXIN
    height: 570rpx;
    // #endif
    margin-bottom: 10rpx;
}
.soldout{
    margin: 20rpx 0;
}
 
.goods-skus-view {
    overflow: hidden;
 
    .skus-view-list {
        > .skus-view-item {
            flex: 1;
            padding: 0 36rpx;
 
            overflow: hidden;
            height: 60rpx;
            line-height: 60rpx;
            float: left;
            text-align: center;
            margin-left: 24rpx;
            margin-bottom: 20rpx;
            font-size: 22rpx;
            color: #262626;
            background: #f2f2f2;
            border-radius: 30rpx;
        }
    }
}
 
.wholesale-item {
    color: #999 !important;
    font-size: 24rpx;
    margin: 0 20rpx;
    display: flex;
    justify-content: center;
}
 
.goods-header {
    height: 200rpx;
    display: flex;
    align-items: center;
    margin-bottom: 36rpx;
}
 
.goods-box {
    padding: 50rpx 36rpx 0 36rpx;
}
 
.goods-skus {
    padding: 0 20rpx;
}
 
.goods-price {
    color: $price-color;
    line-height: 80rpx;
    margin-right: 20rpx;
 
    > * {
        color: $price-color;
        line-height: 80rpx;
    }
}
 
.promotion-box {
    line-height: 1;
    display: flex;
    align-items: center;
    text-decoration: line-through;
    color: #999;
    margin-left: 10rpx;
 
    /deep/ span {
        font-size: 30rpx;
    }
}
 
.promotion {
    font-size: 30rpx;
}
 
.goods-price-promotionShow {
    font-size: 48rpx;
}
 
.goods-check-skus {
    font-size: 24rpx;
    color: #999;
 
    > .goods-check-skus-name {
        margin-left: 4rpx;
    }
 
    > span {
        color: #333;
    }
}
 
// 模板样式
.template {
    padding: 20rpx;
}
 
.template-title {
    margin-bottom: 30rpx;
    text-align: center;
}
 
.title-text {
    font-size: 36rpx;
    font-weight: bold;
}
 
// 查看选中图片按钮样式
.view-selected-image {
    margin-top: 20rpx;
    padding: 30rpx;
    text-align: center;
    background-color: #f0f0f0;
    border-radius: 10rpx;
    color: #333;
    font-size: 28rpx;
}
 
// 图片选择区域
.template-images {
    margin-bottom: 40rpx;
}
 
.images-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 20rpx;
}
 
.image-item {
    width: calc((100% - 40rpx) / 3);
    height: 200rpx;
    border: 2rpx solid #e0e0e0;
    border-radius: 10rpx;
    overflow: hidden;
    position: relative;
    
    &.selected {
        border-color: #ff6b00;
        box-shadow: 0 0 10rpx rgba(255, 107, 0, 0.5);
    }
}
 
.selected-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
}
 
.image-preview {
    width: 100%;
    height: 100%;
}
 
// 表单区域
.template-form {
    
}
 
.form-item {
    margin-bottom: 30rpx;
}
 
.form-label {
    display: block;
    margin-bottom: 15rpx;
    font-size: 28rpx;
    color: #333;
}
 
.form-input {
    width: 100%;
    height: 80rpx;
    padding: 0 20rpx;
    border: 2rpx solid #e0e0e0;
    border-radius: 10rpx;
    font-size: 28rpx;
    box-sizing: border-box;
}
 
.form-upload {
    margin-top: 15rpx;
}
 
// 图片上传样式
.upload-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 150rpx;
    height: 150rpx;
    border: 2rpx dashed #ccc;
    border-radius: 10rpx;
    background-color: #f8f8f8;
}
 
.upload-text {
    font-size: 24rpx;
    color: #999;
    margin-top: 10rpx;
}
 
.image-preview-container {
    position: relative;
    width: 150rpx;
    height: 150rpx;
    margin-top: 10rpx;
}
 
.uploaded-image {
    width: 100%;
    height: 100%;
    border-radius: 10rpx;
}
 
.delete-icon {
    position: absolute;
    top: -10rpx;
    right: -10rpx;
    background-color: #ff6b00;
    border-radius: 50%;
    width: 30rpx;
    height: 30rpx;
    display: flex;
    align-items: center;
    justify-content: center;
}
</style>