index.vue
40.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
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
1358
1359
<!--
* @Date: 2022-09-19 14:11:06
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-07-28 12:48:34
* @FilePath: /jgdl/src/pages/myOrders/index.vue
* @Description: 订单管理页面
-->
<template>
<view class="order-management-page">
<nut-sticky>
<!-- 买车/卖车切换 -->
<view id="mode-toggle" class="view-mode-toggle">
<view class="toggle-container">
<view class="toggle-option" :class="{ active: viewMode === 'buy' }" @click="setViewMode('buy')">
我买的车
</view>
<view class="toggle-option" :class="{ active: viewMode === 'sell' }" @click="setViewMode('sell')">
我卖的车
</view>
</view>
</view>
<!-- 状态筛选标签 -->
<view id="status-tabs" class="status-tabs">
<view class="tab-item" :class="{ active: activeTab === '' }" @click="setActiveTab('')">
全部
</view>
<view v-if="viewMode === 'buy'" class="tab-item" :class="{ active: activeTab === 3 }" @click="setActiveTab(3)">
待支付
</view>
<view class="tab-item" :class="{ active: activeTab === 5 }" @click="setActiveTab(5)">
{{ viewMode === 'buy' ? '待发货' : '待发货' }}
</view>
<view class="tab-item" :class="{ active: activeTab === 9 }" @click="setActiveTab(9)">
{{ viewMode === 'buy' ? '待收货' : '已发货' }}
</view>
<view class="tab-item" :class="{ active: activeTab === 11 }" @click="setActiveTab(11)">
已完成
</view>
<view class="tab-item" :class="{ active: activeTab === 7 }" @click="setActiveTab(7)">
已取消
</view>
</view>
</nut-sticky>
<!-- 订单列表 -->
<view class="order-list">
<!-- 滚动列表 -->
<scroll-view ref="scrollViewRef" class="order-scroll-view" :style="scrollStyle" :scroll-y="true"
@scrolltolower="loadMore" @scroll="scroll" :lower-threshold="50" :enable-flex="false" :scroll-top="scrollTop">
<!-- 空状态 -->
<view v-if="filteredOrders.length === 0" class="empty-state">
<text class="empty-text">暂无订单</text>
</view>
<!-- 订单卡片 -->
<view v-else>
<view v-for="order in filteredOrders" :key="order.id" class="order-card">
<!-- 订单头部信息 -->
<view class="order-header">
<text class="order-date">{{ order.created_time }}</text>
<text class="order-status" :class="getStatusClass(order.status)">
{{ getStatusText(order.status) }}
</text>
</view>
<!-- 车辆信息 -->
<nut-row :gutter="12" class="vehicle-info" @click="goToProductDetail(order)">
<nut-col :span="6">
<image :src="order.details.vehicle.front_photo || DEFAULT_COVER_IMG"
:alt="order.details.vehicle.brand + ' ' + order.details.vehicle.model" class="vehicle-image"
mode="aspectFill" />
</nut-col>
<nut-col :span="18">
<view class="vehicle-details">
<text class="vehicle-name">{{ order.details.vehicle.brand }} {{ order.details.vehicle.model }}</text>
<text class="vehicle-specs">
{{ order.details.vehicle.manufacture_year }}年 | 续航{{ order.details.vehicle.range_km }}km
</text>
<text class="vehicle-battery">电池容量:{{ order.details.vehicle.battery_capacity_ah }}Ah</text>
<text class="vehicle-price">¥{{ order.details.vehicle.price }}</text>
</view>
</nut-col>
</nut-row>
<!-- 支付剩余时间 -->
<view v-if="viewMode === 'buy' && order.status === 3" class="payment-countdown">
<text class="countdown-text">剩余支付时间:{{ formatCountdown(order.remain_time) }}</text>
</view>
<!-- 操作按钮 -->
<view v-if="!order.is_sold" class="order-actions">
<!-- 买车模式:待支付状态 -->
<template v-if="viewMode === 'buy' && order.status === 3">
<nut-button type="default" size="small" @click="handleCancelOrder(order.id)"
:loading="cancelingOrderId === order.id" :disabled="cancelingOrderId === order.id">
{{ cancelingOrderId === order.id ? '取消中...' : '取消订单' }}
</nut-button>
<nut-button type="primary" size="small" @click="handlePayment(order)" color="orange" class="ml-2">
去支付
</nut-button>
</template>
<!-- 买车模式:待收货状态 -->
<template v-if="viewMode === 'buy' && order.status === 9">
<nut-button type="default" size="small" @click="viewOrderDetail(order.id)">
查看详情
</nut-button>
<nut-button type="primary" size="small" @click="handleConfirmReceive(order)" color="orange" class="ml-2"
:loading="receivingOrderId === order.id" :disabled="receivingOrderId === order.id">
{{ receivingOrderId === order.id ? '收货中...' : '确认收货' }}
</nut-button>
</template>
<!-- 卖车模式:待发货状态 -->
<template v-if="viewMode === 'sell' && order.status === 5">
<nut-button type="default" size="small" @click="viewOrderDetail(order.id)">
查看详情
</nut-button>
<nut-button type="primary" size="small" @click="handleConfirmShip(order)" color="orange" class="ml-2"
:loading="shippingOrderId === order.id" :disabled="shippingOrderId === order.id">
{{ shippingOrderId === order.id ? '发货中...' : '确认发货' }}
</nut-button>
</template>
<!-- 已完成状态 -->
<template v-if="order.status === 11">
<nut-button type="default" size="small" @click="viewOrderDetail(order.id)">
查看详情
</nut-button>
<nut-button type="primary" size="small" @click="rateOrder(order.id)" class="ml-2" color="orange" plain>
{{ order.details.review ? '查看评价' : '评价' }}
</nut-button>
</template>
<!-- 已取消状态 -->
<template v-if="order.status === 7">
<nut-button type="default" size="small" @click="deleteOrder(order.id)"
:loading="deletingOrderId === order.id" :disabled="deletingOrderId === order.id">
{{ deletingOrderId === order.id ? '删除中...' : '删除订单' }}
</nut-button>
</template>
</view>
</view>
</view>
<!-- 加载更多指示器 -->
<view v-if="loading" class="loading-container">
<text class="loading-text">加载中...</text>
</view>
<!-- 没有更多数据 -->
<view v-if="!hasMore && filteredOrders.length > 0" class="no-more">
<text class="no-more-text">没有更多订单了</text>
</view>
</scroll-view>
</view>
<!-- 支付组件 -->
<payCard :visible="show_pay" :data="payData" @close="onPayClose" @paySuccess="onPaySuccess" />
<!-- 评价弹窗 -->
<nut-popup v-model:visible="showRatePopup" position="right" :catch-move="true"
:style="{ width: '100%', height: '100%' }" closeable close-icon-position="top-right" @close="closeRatePopup">
<view class="rate-popup">
<view class="rate-header">
<text class="rate-title">{{ isReadOnlyMode ? '查看评价' : '商品评价' }}</text>
</view>
<view class="rate-content">
<!-- 商品信息展示 -->
<view class="product-info">
<image :src="currentRateOrder?.details?.vehicle?.front_photo || DEFAULT_COVER_IMG" class="product-image"
mode="aspectFill" />
<view class="product-details">
<text class="product-name">{{ currentRateOrder?.details?.vehicle?.brand }} {{
currentRateOrder?.details?.vehicle?.model }}</text>
<text class="product-specs">{{ currentRateOrder?.details?.vehicle?.manufacture_year }}年 · 里程: {{
currentRateOrder?.details?.vehicle?.range_km }}km</text>
<text class="product-price">¥ {{ currentRateOrder?.details?.vehicle?.price }}</text>
</view>
</view>
<!-- 评分组件 -->
<view class="rate-score-section">
<text class="score-label">{{ isReadOnlyMode ? '评分' : '请给商品评分' }}</text>
<nut-rate v-model="rateScore" :readonly="isReadOnlyMode" :size="isReadOnlyMode ? '20' : '24'"
active-color="#ff6b35" void-color="#e5e5e5" class="rate-stars" />
<!-- <text v-if="isReadOnlyMode" class="score-text">{{ rateScore }}/5分</text> -->
</view>
<!-- 评价输入框 -->
<view class="rate-input-section">
<text class="input-label">{{ isReadOnlyMode ? '评价内容' : '请输入评价内容' }}</text>
<view class="border border-gray-100">
<nut-textarea v-model="rateContent" :placeholder="isReadOnlyMode ? '' : '请输入您的评价内容...'" :max-length="200"
:cursorSpacing="100" :rows="4" :show-word-limit="!isReadOnlyMode" :readonly="isReadOnlyMode"
:class="{ 'readonly': isReadOnlyMode }" />
</view>
<!-- <text v-if="isReadOnlyMode && currentRateOrder?.details?.review?.date" class="review-date">
评价时间:{{ currentRateOrder?.details?.review?.date }}
</text> -->
</view>
</view>
<!-- 提交按钮 -->
<view class="rate-footer" v-if="!isReadOnlyMode">
<nut-button type="primary" size="large" @click="submitRate" :loading="submittingRate" block>
提交评价
</nut-button>
</view>
</view>
</nut-popup>
<!-- 订单详情弹窗 -->
<nut-popup v-model:visible="showOrderDetailPopup" position="right" :catch-move="true"
:style="{ width: '100%', height: '100%' }" @close="closeOrderDetailPopup">
<view class="order-detail-popup">
<view class="detail-header">
<text class="detail-title">订单详情</text>
</view>
<view class="detail-content">
<!-- 订单基本信息 -->
<view class="detail-section">
<text class="section-title">订单信息</text>
<view class="info-row">
<text class="info-label">订单编号</text>
<text class="info-value">{{ currentOrderDetail?.id }}</text>
</view>
<view class="info-row">
<text class="info-label">下单时间</text>
<text class="info-value">{{ currentOrderDetail?.created_time }}</text>
</view>
<view class="info-row">
<text class="info-label">支付时间</text>
<text class="info-value">{{ currentOrderDetail?.payment_time }}</text>
</view>
<view class="info-row">
<text class="info-label">订单状态</text>
<text class="info-value" :class="getStatusClass(currentOrderDetail?.status)">
{{ getStatusText(currentOrderDetail?.status) }}
</text>
</view>
<view class="info-row">
<text class="info-label">订单金额</text>
<text class="info-value price">¥ {{ currentOrderDetail?.total_amount }}</text>
</view>
</view>
<!-- 商品信息 -->
<view class="detail-section">
<text class="section-title">商品信息</text>
<view class="product-detail-info">
<image :src="currentOrderDetail?.details?.vehicle?.front_photo || DEFAULT_COVER_IMG"
class="product-detail-image" mode="aspectFill" />
<view class="product-detail-content">
<text class="product-detail-name">{{ currentOrderDetail?.details?.vehicle?.brand }} {{
currentOrderDetail?.details?.vehicle?.model }}</text>
<text class="product-detail-specs">{{ currentOrderDetail?.details?.vehicle?.manufacture_year }}年 · 续航:
{{
currentOrderDetail?.details?.vehicle?.range_km }}km/h</text>
<text class="product-detail-battery">电池容量: {{ currentOrderDetail?.details?.vehicle?.battery_capacity_ah
}}Ah</text>
<text class="product-detail-price">¥ {{ currentOrderDetail?.details?.vehicle?.price }}</text>
</view>
</view>
</view>
<!-- 交易信息 -->
<view class="detail-section">
<text class="section-title">交易信息</text>
<view class="info-row">
<text class="info-label">{{ viewMode === 'buy' ? '卖家' : '买家' }}</text>
<text class="info-value">{{ viewMode === 'buy' ? currentOrderDetail?.details?.vehicle?.seller?.nickname :
currentOrderDetail?.buyer?.nickname }}</text>
</view>
<view class="info-row">
<text class="info-label">联系电话</text>
<text class="info-value">{{ viewMode === 'buy' ? currentOrderDetail?.details?.vehicle?.seller?.phone :
currentOrderDetail?.buyer?.phone }}</text>
</view>
<!-- <view class="info-row">
<text class="info-label">交易地点</text>
<text class="info-value">北京市朝阳区望京SOHO</text>
</view> -->
</view>
<!-- 评价信息(如果有) -->
<view class="detail-section" v-if="currentOrderDetail?.details?.review">
<text class="section-title">评价信息</text>
<view class="review-info">
<view class="review-rating">
<text class="rating-label">评分:</text>
<nut-rate :model-value="currentOrderDetail?.details?.review?.rating" readonly size="20"
active-color="#ff6b35" void-color="#e5e5e5" />
<!-- <text class="rating-text">{{ currentOrderDetail?.details?.review?.rating }}/5分</text> -->
</view>
<view class="review-content">
<text class="content-label">评价内容:</text>
<text class="content-text">{{ currentOrderDetail?.details?.review?.note }}</text>
</view>
<view class="review-time">
<text class="time-text">评价时间:{{ currentOrderDetail?.details?.review?.created_time }}</text>
</view>
</view>
</view>
</view>
<!-- 关闭按钮 -->
<view class="detail-footer">
<nut-button type="primary" size="large" @click="closeOrderDetailPopup" block color="orange">
关闭
</nut-button>
</view>
</view>
</nut-popup>
<!-- Delete Confirmation Modal -->
<nut-dialog v-model:visible="showConfirmModal" title="温馨提示" cancel-text="取消" ok-text="确认"
@cancel="showConfirmModal = false" @ok="performDeleteOrder(pendingDeleteOrderId)">
<template #default>
<view>
<text style="font-size: 1rem;">是否确认删除订单?</text>
</view>
</template>
</nut-dialog>
<!-- Cancel Order Confirmation Modal -->
<nut-dialog v-model:visible="showCancelConfirmModal" title="温馨提示" cancel-text="我再想想" ok-text="确定取消"
@cancel="showCancelConfirmModal = false" @ok="performCancelOrder(pendingCancelOrderId)">
<template #default>
<view>
<text style="font-size: 1rem;">是否确认取消订单?</text>
</view>
</template>
</nut-dialog>
<!-- 确认发货弹窗 -->
<nut-dialog v-model:visible="showShipConfirmModal" title="确认发货" cancel-text="取消" ok-text="确认发货"
@cancel="showShipConfirmModal = false" @ok="performConfirmShip(pendingShipOrderId)">
<template #default>
<view>
<text style="font-size: 1rem;">确认已将车辆发货给买家?</text>
</view>
</template>
</nut-dialog>
<!-- 确认收货弹窗 -->
<nut-dialog v-model:visible="showReceiveConfirmModal" title="确认收货" cancel-text="取消" ok-text="确认收货"
@cancel="showReceiveConfirmModal = false" @ok="performConfirmReceive(pendingReceiveOrderId)">
<template #default>
<view>
<text style="font-size: 1rem;">确认已收到车辆?</text>
</view>
</template>
</nut-dialog>
</view>
</template>
<script setup>
import { ref, computed, onMounted, onUnmounted, nextTick } from 'vue'
import Taro from '@tarojs/taro'
import './index.less'
import { $ } from '@tarojs/extend'
import payCard from '@/components/payCard.vue'
// 导入接口
import { getOrderListAPI, getOrderDetailAPI, reviewOrderAPI, cancelOrderAPI, deleteOrderAPI, shipOrderAPI, receiptOrderStatusAPI } from '@/api/orders'
import { DEFAULT_COVER_IMG } from '@/utils/config'
const scrollStyle = ref({
height: ''
})
/**
* 跳转到商品详情页
* @param {string} orderId - 订单ID
*/
const goToProductDetail = ({ details }) => {
Taro.navigateTo({
url: `/pages/productDetail/index?id=${details.vehicle.id}`
})
}
// 滚动相关
const scrollViewRef = ref(null)
const scrollTop = ref(0)
// 页面状态
const activeTab = ref('')
const viewMode = ref('buy')
const loading = ref(false)
const hasMore = ref(true)
const show_pay = ref(false)
const payData = ref({
id: '',
price: 0,
remain_time: 0
})
// 评价相关状态
const showRatePopup = ref(false)
const currentRateOrder = ref(null)
const rateContent = ref('')
const rateScore = ref(5)
const submittingRate = ref(false)
const isReadOnlyMode = ref(false)
// 订单详情相关状态
const showOrderDetailPopup = ref(false)
const currentOrderDetail = ref(null)
// 订单数据 - 我买的车
const boughtOrders = ref([])
// 订单数据 - 我卖的车
const soldOrders = ref([])
// 分页相关状态
const currentPage = ref(0)
const pageLimit = ref(10)
// 倒计时相关状态
const countdownIntervals = ref(new Map()) // 存储每个订单的倒计时定时器
/**
* 根据当前视图模式和筛选条件获取过滤后的订单列表
*/
const filteredOrders = computed(() => {
const orders = viewMode.value === 'buy' ? boughtOrders.value : soldOrders.value
if (activeTab.value === '') {
return orders
}
return orders.filter(order => {
if (activeTab.value === 3) return order.status === 3
if (activeTab.value === 5) return order.status === 5
if (activeTab.value === 9) return order.status === 9
if (activeTab.value === 11) return order.status === 11
if (activeTab.value === 7) return order.status === 7
return true
})
})
/**
* 设置视图模式(买车/卖车)
*/
const setViewMode = (mode) => {
viewMode.value = mode
// 重置状态筛选标签到全部
activeTab.value = ''
// 重置列表状态和数据
resetListState(true)
}
/**
* 设置活跃的状态标签
*/
const setActiveTab = (tab) => {
activeTab.value = tab
// 重置列表加载状态并重新加载数据
currentPage.value = 0
loadOrderData(false)
}
/**
* 加载订单数据
* @param {boolean} isLoadMore - 是否为加载更多
*/
const loadOrderData = async (isLoadMore = false) => {
if (loading.value) return
try {
loading.value = true
const type = viewMode.value === 'buy' ? 'buy' : 'sell'
const status = activeTab.value || undefined
const page = isLoadMore ? currentPage.value + 1 : 0
// TAG: 添加mock数据用于测试
// if (page === 0) {
// const mockOrder = {
// id: `mock_${type}_${Date.now()}`,
// status: 9, // 已发货/待收货状态
// created_time: new Date().toLocaleString('zh-CN'),
// total_amount: 15000,
// details: {
// id: `detail_${Date.now()}`,
// vehicle: {
// id: `vehicle_${Date.now()}`,
// brand: '雅迪',
// model: 'DE2',
// manufacture_year: 2023,
// range_km: 60,
// battery_capacity_ah: 48,
// price: 15000,
// front_photo: DEFAULT_COVER_IMG,
// seller: {
// nickname: '测试卖家',
// phone: '138****8888'
// }
// }
// },
// buyer: {
// nickname: '测试买家',
// phone: '139****9999'
// },
// is_sold: false
// }
// const targetOrders = viewMode.value === 'buy' ? boughtOrders : soldOrders
// targetOrders.value = [mockOrder]
// }
const response = await getOrderListAPI({
type,
status,
page,
limit: pageLimit.value
})
if (response.code && response.data && response.data.list && response.data.list.length > 0) {
// 处理多个订单数据
const newOrders = response.data.list.map(orderData => {
// 处理details数组,取第一个元素作为details对象
const processedOrder = {
...orderData,
details: orderData.details && orderData.details.length > 0 ? orderData.details[0] : null
}
// 为待支付订单添加倒计时时间
if (processedOrder.status === 3) {
// 计算剩余时间(毫秒)
const current_date = new Date(processedOrder.server_time);
const end_date = new Date(processedOrder.pay_deadline_time);
let time_left = end_date - current_date;
// 将毫秒转换为秒,如果时间已过期或计算失败则默认30分钟(1800秒)
processedOrder.remain_time = time_left > 0 ? Math.floor(time_left / 1000) : 1800
}
return processedOrder
})
const targetOrders = viewMode.value === 'buy' ? boughtOrders : soldOrders
if (isLoadMore) {
targetOrders.value = [...targetOrders.value, ...newOrders]
currentPage.value = page
} else {
targetOrders.value = newOrders
currentPage.value = 0
}
// 为待支付订单启动倒计时
newOrders.forEach(order => {
if (order.status === 3 && order.remain_time) {
startCountdown(order)
}
})
// 判断是否还有更多数据
hasMore.value = newOrders.length === pageLimit.value
} else {
hasMore.value = false
}
} catch (error) {
console.error('加载订单数据失败:', error)
Taro.showToast({
title: '加载失败,请重试',
icon: 'error',
duration: 2000
})
hasMore.value = false
} finally {
loading.value = false
}
}
/**
* 重置列表状态
* @param {boolean} resetData - 是否重置已加载的数据
*/
const resetListState = (resetData = false) => {
loading.value = false
hasMore.value = true
currentPage.value = 0
// 重置滚动位置到顶部
scrollTop.value = Math.random() // 使用随机数触发scroll-view重新渲染
if (resetData) {
// 清空订单数据
boughtOrders.value = []
soldOrders.value = []
// 重新加载数据
loadOrderData(false)
}
// 延迟重置scrollTop为0,确保滚动位置正确
setTimeout(() => {
scrollTop.value = 0
}, 50)
}
/**
* 滚动事件处理
*/
const scroll = (e) => {
// 可以在这里处理滚动事件
}
/**
* 加载更多数据
*/
const loadMore = () => {
if (loading.value || !hasMore.value) return
loadOrderData(true)
}
/**
* 获取订单状态文本
*/
const getStatusText = (status) => {
switch (status) {
case 3:
return '待支付'
case 5:
return '待发货'
case 9:
return viewMode.value === 'buy' ? '待收货' : '已发货'
case 11:
return '已完成'
case 7:
return '已取消'
default:
return '未知状态'
}
}
/**
* 获取订单状态样式类
*/
const getStatusClass = (status) => {
switch (status) {
case 3:
return 'status-pending'
case 5:
return 'status-shipping'
case 9:
return 'status-shipping'
case 11:
return 'status-completed'
case 7:
return 'status-cancelled'
default:
return ''
}
}
/**
* 格式化倒计时显示
* @param {number} seconds - 剩余秒数
* @returns {string} 格式化的时间字符串 HH:MM:SS
*/
const formatCountdown = (seconds) => {
if (!seconds || seconds <= 0) {
return '00:00:00'
}
const hours = Math.floor(seconds / 3600)
const minutes = Math.floor((seconds % 3600) / 60)
const secs = seconds % 60
return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`
}
/**
* 启动订单倒计时
* @param {Object} order - 订单对象
*/
const startCountdown = (order) => {
if (!order || order.status !== 3 || !order.remain_time) {
return
}
// 清除已存在的定时器
if (countdownIntervals.value.has(order.id)) {
clearInterval(countdownIntervals.value.get(order.id))
}
const timer = setInterval(async () => {
const orders = viewMode.value === 'buy' ? boughtOrders : soldOrders
const targetOrderIndex = orders.value.findIndex(o => o.id === order.id)
if (targetOrderIndex !== -1) {
const targetOrder = orders.value[targetOrderIndex]
if (targetOrder.remain_time > 0) {
// 创建新的订单对象来触发响应式更新
const updatedOrder = {
...targetOrder,
remain_time: targetOrder.remain_time - 1
}
// 替换数组中的订单对象
orders.value.splice(targetOrderIndex, 1, updatedOrder)
// 使用nextTick确保DOM更新
await nextTick()
} else {
// 时间到,取消订单
clearInterval(timer)
countdownIntervals.value.delete(order.id)
handleTimeoutCancel(targetOrder)
}
} else {
// 订单不存在,清除定时器
clearInterval(timer)
countdownIntervals.value.delete(order.id)
}
}, 1000)
countdownIntervals.value.set(order.id, timer)
}
/**
* 处理超时取消订单
* @param {Object} order - 订单对象
*/
const handleTimeoutCancel = async (order) => {
// 清除该订单的倒计时定时器
if (countdownIntervals.value.has(order.id)) {
clearInterval(countdownIntervals.value.get(order.id))
countdownIntervals.value.delete(order.id)
}
// 更新订单状态为已取消
const orders = viewMode.value === 'buy' ? boughtOrders : soldOrders
const targetOrderIndex = orders.value.findIndex(o => o.id === order.id)
if (targetOrderIndex !== -1) {
// 创建新的订单对象来触发响应式更新
const updatedOrder = {
...orders.value[targetOrderIndex],
status: 7
}
// 替换数组中的订单对象
orders.value.splice(targetOrderIndex, 1, updatedOrder)
// 使用nextTick确保DOM更新
await nextTick()
}
Taro.showToast({
title: '订单已超时取消',
icon: 'none',
duration: 2000
})
}
/**
* 清除所有倒计时定时器
*/
const clearAllCountdowns = () => {
countdownIntervals.value.forEach((timer) => {
clearInterval(timer)
})
countdownIntervals.value.clear()
}
/**
* 处理支付
*/
const handlePayment = ({ id, total_amount, remain_time }) => {
onPay({
id,
remain_time, // 30分钟
price: total_amount
})
}
/**
* 发送订单支付信息到支付组件
* @param {Object} payInfo - 支付信息
* @param {string} payInfo.id - 订单ID
* @param {number} payInfo.remain_time - 剩余时间
* @param {number} payInfo.price - 价格
*/
const onPay = ({ id, remain_time, price }) => {
show_pay.value = true
payData.value.id = id
payData.value.price = price
payData.value.remain_time = remain_time
}
/**
* 关闭支付弹框
*/
const onPayClose = () => {
show_pay.value = false
}
/**
* 处理支付成功事件
* @param {Object} data - 支付成功数据
* @param {string} data.orderId - 订单ID
*/
const onPaySuccess = ({ orderId }) => {
// 找到对应的订单并更新状态
const orders = viewMode.value === 'buy' ? boughtOrders.value : soldOrders.value
const order = orders.find(o => o.id === orderId)
if (order) {
// 更新订单状态为已完成
order.status = 5
Taro.showToast({
title: '支付成功,订单已更新',
icon: 'success',
duration: 2000
})
}
}
/**
* 查看订单详情
*/
const viewOrderDetail = async (orderId) => {
// 找到对应的订单
const orders = viewMode.value === 'buy' ? boughtOrders.value : soldOrders.value
const order = orders.find(o => o.id === orderId)
if (order) {
const { code, data } = await getOrderDetailAPI({ id: orderId })
if (code) {
currentOrderDetail.value = { ...data, details: data.details[0] }
showOrderDetailPopup.value = true
}
}
}
/**
* 关闭订单详情弹窗
*/
const closeOrderDetailPopup = () => {
showOrderDetailPopup.value = false
currentOrderDetail.value = null
}
/**
* 评价订单
*/
const rateOrder = (orderId) => {
// 找到对应的订单
const orders = viewMode.value === 'buy' ? boughtOrders.value : soldOrders.value
const order = orders.find(o => o.id === orderId)
if (order) {
currentRateOrder.value = order
// 检查是否已有评价
if (order.details.review) {
// 已评价,显示只读模式
isReadOnlyMode.value = true
rateContent.value = order.details.review.note
rateScore.value = order.details.review.rating
} else {
// 未评价,显示编辑模式
isReadOnlyMode.value = false
rateContent.value = ''
rateScore.value = 5
}
showRatePopup.value = true
}
}
/**
* 关闭评价弹窗
*/
const closeRatePopup = () => {
showRatePopup.value = false
currentRateOrder.value = null
rateContent.value = ''
rateScore.value = 5
isReadOnlyMode.value = false
}
/**
* 提交评价
*/
const submitRate = async () => {
if (!rateContent.value.trim()) {
Taro.showToast({
title: '请输入评价内容',
icon: 'error',
duration: 2000
})
return
}
try {
submittingRate.value = true
const response = await reviewOrderAPI({
detail_id: currentRateOrder.value.details.id,
rating: rateScore.value,
note: rateContent.value
})
if (response.code) {
// API提交成功后的处理
const currentOrders = viewMode.value === 'buy' ? boughtOrders : soldOrders
const order = currentOrders.value.find(o => o.id === currentRateOrder.value.id)
if (order) {
// 创建评价数据对象
const reviewData = {
rating: rateScore.value,
note: rateContent.value,
created_time: new Date().toLocaleString('zh-CN')
}
// 同时更新order.review和order.details.review,确保状态一致
order.review = reviewData
if (order.details) {
order.details.review = reviewData
}
// 确保订单状态为已完成
order.status = 11
}
Taro.showToast({
title: response.msg || '评价提交成功',
icon: 'success',
duration: 2000
})
closeRatePopup()
} else {
throw new Error(response.msg || '提交失败')
}
} catch (error) {
console.error('提交评价失败:', error)
} finally {
submittingRate.value = false
}
}
// 取消订单相关状态
const cancelingOrderId = ref('')
// 删除订单相关状态
const deletingOrderId = ref('')
/**
* 确认弹窗显示状态
*/
const showConfirmModal = ref(false)
/**
* 待删除的订单ID
*/
const pendingDeleteOrderId = ref('')
/**
* 取消订单确认弹窗显示状态
*/
const showCancelConfirmModal = ref(false)
/**
* 待取消的订单ID
*/
const pendingCancelOrderId = ref('')
/**
* 确认发货弹窗显示状态
*/
const showShipConfirmModal = ref(false)
/**
* 待发货的订单ID
*/
const pendingShipOrderId = ref('')
/**
* 发货中的订单ID
*/
const shippingOrderId = ref('')
/**
* 确认收货弹窗显示状态
*/
const showReceiveConfirmModal = ref(false)
/**
* 待收货的订单ID
*/
const pendingReceiveOrderId = ref('')
/**
* 收货中的订单ID
*/
const receivingOrderId = ref('')
/**
* 取消订单
* @param {string} orderId - 订单ID
*/
const handleCancelOrder = async (orderId) => {
try {
// 保存待取消的订单ID
pendingCancelOrderId.value = orderId
showCancelConfirmModal.value = true
} catch (error) {
// 用户取消操作或其他错误
// console.log('取消订单操作被取消或出错:', error)
}
}
/**
* 执行取消订单操作
* @param {string} orderId - 订单ID
*/
const performCancelOrder = async (orderId) => {
// 关闭确认弹窗
showCancelConfirmModal.value = false
// 设置取消状态,用于显示加载效果
cancelingOrderId.value = orderId
try {
// 调用取消订单API
const response = await cancelOrderAPI({ id: orderId })
if (response.code) {
// API取消成功后的处理
const orders = viewMode.value === 'buy' ? boughtOrders : soldOrders
const order = orders.value.find(o => o.id === orderId)
if (order) {
// 清除该订单的倒计时定时器
if (countdownIntervals.value.has(orderId)) {
clearInterval(countdownIntervals.value.get(orderId))
countdownIntervals.value.delete(orderId)
}
// 更新订单状态为已取消
const orders = viewMode.value === 'buy' ? boughtOrders : soldOrders
const targetOrderIndex = orders.value.findIndex(o => o.id === orderId)
if (targetOrderIndex !== -1) {
// 创建新的订单对象来触发响应式更新
const updatedOrder = {
...orders.value[targetOrderIndex],
status: 7
}
// 替换数组中的订单对象
orders.value.splice(targetOrderIndex, 1, updatedOrder)
}
Taro.showToast({
title: '订单已取消',
icon: 'success',
duration: 2000
})
}
} else {
throw new Error(response.msg || '取消失败')
}
} catch (error) {
// console.error('取消订单失败:', error)
Taro.showToast({
title: error.msg || '取消订单失败,请重试',
icon: 'error',
duration: 2000
})
} finally {
// 清除取消状态
cancelingOrderId.value = ''
// 清除待取消订单ID
pendingCancelOrderId.value = ''
}
}
/**
* 删除订单
* @param {string} orderId - 订单ID
*/
const deleteOrder = async (orderId) => {
try {
// 保存待删除的订单ID
pendingDeleteOrderId.value = orderId
showConfirmModal.value = true
} catch (error) {
// 用户取消删除或其他错误
// console.log('删除操作被取消或出错:', error)
}
}
/**
* 确认收货处理
* @param {Object} order - 订单对象
*/
const handleConfirmReceive = async (order) => {
try {
// 保存待收货的订单ID
pendingReceiveOrderId.value = order.id
showReceiveConfirmModal.value = true
} catch (error) {
Taro.showToast({
title: '操作失败',
icon: 'error',
duration: 2000
})
}
}
/**
* 执行确认收货操作
* @param {string} orderId - 订单ID
*/
const performConfirmReceive = async (orderId) => {
// 关闭确认弹窗
showReceiveConfirmModal.value = false
// 设置收货状态,用于显示加载效果
receivingOrderId.value = orderId
try {
const order = boughtOrders.value.find(o => o.id === orderId)
if (wx.openBusinessView) {
wx.openBusinessView({
businessType: 'weappOrderConfirm',
extraData: {
// merchant_id: '1230000109',
// merchant_trade_no: order.id,
transaction_id: order.transaction_id || ''
},
success() {
// 确认收货组件调用成功
},
fail(err) {
Taro.showToast({
title: '确认收货失败',
icon: 'error',
duration: 2000
})
},
complete() {
receivingOrderId.value = ''
}
});
} else {
// 引导用户升级微信版本
Taro.showModal({
title: '提示',
content: '当前微信版本过低,请升级微信版本后重试',
showCancel: false
})
}
} catch (error) {
Taro.showToast({
title: '操作失败',
icon: 'error',
duration: 2000
})
} finally {
// 清除收货状态
receivingOrderId.value = ''
// 清除待收货订单ID
pendingReceiveOrderId.value = ''
}
}
/**
* 确认发货处理
* @param {Object} order - 订单对象
*/
const handleConfirmShip = async (order) => {
try {
// 保存待发货的订单ID
pendingShipOrderId.value = order.id
showShipConfirmModal.value = true
} catch (error) {
// 确认发货失败处理
}
}
/**
* 执行确认发货操作
* @param {string} orderId - 订单ID
*/
const performConfirmShip = async (orderId) => {
// 关闭确认弹窗
showShipConfirmModal.value = false
// 设置发货状态,用于显示加载效果
shippingOrderId.value = orderId
try {
// 调用确认发货API
const response = await shipOrderAPI({ order_id: orderId })
if (response.code) {
// API发货成功后的处理
const orders = viewMode.value === 'buy' ? boughtOrders : soldOrders
const order = orders.value.find(o => o.id === orderId)
if (order) {
// 更新订单状态为已发货/待收货
order.status = 9
Taro.showToast({
title: response.msg || '发货成功',
icon: 'success',
duration: 2000
})
}
} else {
throw new Error(response.msg || '发货失败')
}
} catch (error) {
// 确认发货失败处理
Taro.showToast({
title: error.msg || '确认发货失败,请重试',
icon: 'error',
duration: 2000
})
} finally {
// 清除发货状态
shippingOrderId.value = ''
// 清除待发货订单ID
pendingShipOrderId.value = ''
}
}
/**
* 执行删除订单操作
* @param {string} orderId - 订单ID
*/
const performDeleteOrder = async (orderId) => {
// 关闭确认弹窗
showConfirmModal.value = false
// 设置删除状态,用于显示加载效果
deletingOrderId.value = orderId
try {
// 调用删除订单API
const response = await deleteOrderAPI({ id: orderId })
if (response.code) {
// API删除成功后的处理
const orders = viewMode.value === 'buy' ? boughtOrders : soldOrders
const orderIndex = orders.value.findIndex(order => order.id === orderId)
if (orderIndex !== -1) {
orders.value.splice(orderIndex, 1)
Taro.showToast({
title: response.msg || '订单删除成功',
icon: 'success',
duration: 2000
})
}
} else {
throw new Error(response.msg || '删除失败')
}
} catch (error) {
// console.error('删除订单失败:', error)
Taro.showToast({
title: '删除订单失败',
icon: 'error',
duration: 2000
})
} finally {
// 清除删除状态
deletingOrderId.value = ''
// 清除待删除订单ID
pendingDeleteOrderId.value = ''
}
}
/**
* 处理确认收货成功事件
* @param {Object} data - 事件数据
*/
const handleConfirmReceiveSuccess = async ({ merchantId, transactionId, merchantTradeNo }) => {
const orders = boughtOrders.value
const order = orders.find(o => o.id === merchantTradeNo)
if (order) {
try {
// 调用买家查询收货状态接口
const response = await receiptOrderStatusAPI({ id: merchantTradeNo })
if (response.code && response.data && response.data.status === 11) {
// 接口返回状态为11,确认收货成功
order.status = 11 // 更新为已完成状态
Taro.showToast({
title: '确认收货成功',
icon: 'success',
duration: 2000
})
} else {
// 状态不是11,可能还在处理中
// 可以根据需要添加其他处理逻辑
}
} catch (error) {
// 查询收货状态失败,即使接口调用失败,也更新本地状态
order.status = 11
}
}
}
// 页面加载时的初始化
onMounted(async () => {
// 加载订单数据
loadOrderData(false)
// 设置滚动列表可视高度
const windowHeight = wx.getWindowInfo().windowHeight;
setTimeout(async () => {
const headerHeight = await $('#mode-toggle').height();
const navHeight = await $('#status-tabs').height();
scrollStyle.value = {
height: windowHeight - headerHeight - navHeight + 'px'
}
}, 500);
// 监听确认收货成功事件
Taro.eventCenter.on('confirmReceiveSuccess', handleConfirmReceiveSuccess)
})
// 页面卸载时清理定时器和事件监听器
onUnmounted(() => {
clearAllCountdowns()
// 移除确认收货成功事件监听器
Taro.eventCenter.off('confirmReceiveSuccess', handleConfirmReceiveSuccess)
})
</script>
<script>
export default {
name: "OrderManagementPage",
};
</script>