绿满眶商城微信小程序-uniapp
peng
2025-10-11 f1d0c08bbef3ffef0b9bbd0c181d2047c9866a7f
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
<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: selectedImages.includes(img.imgUrl) }"
                                    @click="selectImage(img.imgUrl)"
                                >
                                    <image 
                                        :src="getFilePreviewUrlSync(img.imgUrl)" 
                                        class="image-preview"
                                        mode="aspectFill"
                                    />
                                    <view v-if="selectedImages.includes(img.imgUrl)" class="selected-overlay">
                                        <uni-icons type="checkmarkempty" size="30" color="#fff"></uni-icons>
                                    </view>
                                </view>
                            </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: [], // 选中的模板图片
            formValues: {
                templateId: "", // 模板ID
                templateName: "", // 模板名称
                chooseImage: "", // 选中的图片
                templateForm: [] // 表单数组 [{id, templateTitle, value}]
            }, // 表单值
            imagePreviewUrls: {}, // 图片预览URL
            uploadProgress: {}, // 上传进度
            uploadStatus: {}, // 上传状态信息
            cosClient: null, // COS客户端
            bucket: '', // 存储桶
            region: '', // 地域
            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();
                    }, 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;
            }).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(imgUrl) {
            const index = this.selectedImages.indexOf(imgUrl);
            if (index > -1) {
                // 如果已选中,则取消选择
                this.selectedImages.splice(index, 1);
            } else {
                // 否则添加到选中列表
                this.selectedImages.push(imgUrl);
            }
            // 更新formValues中的chooseImage
            this.formValues.chooseImage = this.selectedImages.join(',');
        },
        
        // 选择图片(参考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;
            
            // 查找是否已存在该字段
            const existingIndex = this.formValues.templateForm.findIndex(item => item.id === fieldId);
            
            if (existingIndex > -1) {
                // 更新现有字段
                this.$set(this.formValues.templateForm, existingIndex, {
                    id: fieldId,
                    templateTitle: templateTitle,
                    value: value
                });
            } else {
                // 添加新字段
                this.formValues.templateForm.push({
                    id: fieldId,
                    templateTitle: templateTitle,
                    value: value
                });
            }
        },
        
        // 更新模板表单数据
        updateTemplateFormData(fieldId, templateTitle, value) {
            // 这个方法现在可以保持不变,因为我们已经在updateFormValue中处理了formValues的更新
            // 如果需要其他处理,可以在这里添加
        },
        
        // 表单提交处理
        handleFormSubmit() {
            // 使用新的formValues数据结构
            const formData = this.formValues;
            
            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) {
            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';
                }
 
                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(this.formValues))}&parentOrder=${encodeURIComponent(JSON.stringify(this.parentOrder))}`
                            });
                        }, 100);
                    }
                });
            }
        },
        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];
            }
            // 否则返回原始值
            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.templateForm = [];
            }
        }
    },
 
    mounted() {
        // 使用setTimeout延迟执行,避免影响弹窗
        setTimeout(() => {
            this.formatSku(this.goodsSpec);
            this.initCOS(); // 初始化COS客户端
            
            // 预加载模板图片的预览地址
            this.preloadTemplateImages();
 
            // 初始化模板表单数据
            this.initTemplateFormData();
 
            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;
    color: #333;
}
 
// 图片选择区域
.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>