Просмотр исходного кода

feat(order): 添加1688订单售后退款原因查询功能

- 在admin路由中新增refundReasonList接口用于查询退款原因
- 实现AlibabaOrder控制器中的refundReasonList方法,简化参数传递逻辑
- 新增getOrderDetailBuyerView方法获取订单详情(买家视角)
- 定义REFUND_GOOD_STATUS常量映射货物状态
- 重构getRefundReasonList方法自动获取子订单号和货物状态
- 更新formatRefundReasonListResult方法返回完整的退款信息
- 在RefundOrder控制器中添加1688订单退款原因查询接口
shichen месяцев назад: 2
Родитель
Сommit
ec7c720534

+ 26 - 0
app/controller/admin/order/RefundOrder.php

@@ -10,6 +10,7 @@
10
 namespace app\controller\admin\order;
10
 namespace app\controller\admin\order;
11
 
11
 
12
 use app\common\repositories\user\UserRepository;
12
 use app\common\repositories\user\UserRepository;
13
+use app\services\ThirdParty\AlibabaAgent\OrderService;
13
 use crmeb\basic\BaseController;
14
 use crmeb\basic\BaseController;
14
 use app\common\repositories\store\order\MerchantReconciliationorderRepository;
15
 use app\common\repositories\store\order\MerchantReconciliationorderRepository;
15
 use app\common\repositories\store\order\MerchantReconciliationRepository;
16
 use app\common\repositories\store\order\MerchantReconciliationRepository;
@@ -64,6 +65,31 @@ class RefundOrder extends BaseController
64
         $where = $this->request->params(['refund_order_sn','status','refund_type','date','mer_id','order_sn']);
65
         $where = $this->request->params(['refund_order_sn','status','refund_type','date','mer_id','order_sn']);
65
         return app('json')->success($this->repository->getAllList($where, $page, $limit));
66
         return app('json')->success($this->repository->getAllList($where, $page, $limit));
66
     }
67
     }
68
+
69
+    /**
70
+     * @return mixed
71
+     * @author 史晨
72
+     * @date 2026/5/8 17:27
73
+     * 1688订单获取退款原因
74
+     */
75
+    public function refundReasonList()
76
+    {
77
+        $orderId = request()->param('order_id');
78
+
79
+        if (empty($orderId)) {
80
+            return app('json')->fail('订单号不能为空');
81
+        }
82
+
83
+        $orderService = new OrderService();
84
+        $params = ['orderId' => $orderId];
85
+        $result = $orderService->getRefundReasonList($params);
86
+        if ($result === false) {
87
+            return app('json')->fail('获取退款原因失败');
88
+        }
89
+
90
+        return app('json')->success($result);
91
+    }
92
+
67
     public function detail($id)
93
     public function detail($id)
68
     {
94
     {
69
         $data=$this->repository->getOne($id);
95
         $data=$this->repository->getOne($id);

+ 4 - 11
app/controller/api/store/order/AlibabaOrder.php

@@ -60,6 +60,9 @@ class AlibabaOrder
60
     /**
60
     /**
61
      * 查询退款退货原因列表
61
      * 查询退款退货原因列表
62
      *
62
      *
63
+     * 客户端只需传入 order_id,服务端自动调用 alibaba.trade.get.buyerView
64
+     * 获取订单详情,提取子订单号和货物状态后,再调用 alibaba.trade.getRefundReasonList
65
+     *
63
      * API: alibaba.trade.getRefundReasonList-1
66
      * API: alibaba.trade.getRefundReasonList-1
64
      *
67
      *
65
      * @return \think\Response
68
      * @return \think\Response
@@ -67,23 +70,13 @@ class AlibabaOrder
67
     public function refundReasonList()
70
     public function refundReasonList()
68
     {
71
     {
69
         $orderId = request()->param('order_id');
72
         $orderId = request()->param('order_id');
70
-        $orderEntryIds = request()->param('order_entry_ids', '');
71
-        $goodsStatus = request()->param('goods_status', '');
72
 
73
 
73
         if (empty($orderId)) {
74
         if (empty($orderId)) {
74
             return app('json')->fail('订单号不能为空');
75
             return app('json')->fail('订单号不能为空');
75
         }
76
         }
76
 
77
 
77
         $orderService = new OrderService();
78
         $orderService = new OrderService();
78
-        $params = ['orderId' => $orderId];
79
-        if (!empty($orderEntryIds)) {
80
-            $params['orderEntryIds'] = $orderEntryIds;
81
-        }
82
-        if (!empty($goodsStatus)) {
83
-            $params['goodsStatus'] = $goodsStatus;
84
-        }
85
-
86
-        $result = $orderService->getRefundReasonList($params);
79
+        $result = $orderService->getRefundReasonList(['orderId' => $orderId]);
87
         if ($result === false) {
80
         if ($result === false) {
88
             return app('json')->fail('获取退款原因失败');
81
             return app('json')->fail('获取退款原因失败');
89
         }
82
         }

+ 98 - 36
app/services/ThirdParty/AlibabaAgent/OrderService.php

@@ -27,6 +27,20 @@ class OrderService extends AlibabaAgentBaseService
27
      */
27
      */
28
     const LOGISTICS_NAMESPACE = 'com.alibaba.logistics';
28
     const LOGISTICS_NAMESPACE = 'com.alibaba.logistics';
29
 
29
 
30
+    const REFUND_GOOD_STATUS = [
31
+        // 售中等待卖家发货
32
+        'waitsellersend' => 'refundWaitSellerSend',
33
+        // 售中等待买家收货
34
+        'waitbuyerreceive'=>'refundWaitBuyerReceive',
35
+        // // 售中已收货(未确认完成交易)
36
+        // 'BuyerReceived'=>'refundBuyerReceived',
37
+        // // 售后未收货
38
+        // 'saleBuyerNotReceived'=>'aftersaleBuyerNotReceived',
39
+        // // 售后已收到货
40
+        // 'saleBuyerReceived'=>'aftersaleBuyerReceived',
41
+
42
+    ];
43
+
30
     /**
44
     /**
31
      * 预下单
45
      * 预下单
32
      *
46
      *
@@ -509,33 +523,51 @@ class OrderService extends AlibabaAgentBaseService
509
     }
523
     }
510
 
524
 
511
     /**
525
     /**
512
-     * 查询退款退货原因列表
526
+     * 获取订单详情(买家视角)
513
      *
527
      *
514
-     * API: com.alibaba.trade:alibaba.trade.getRefundReasonList-1
528
+     * API: com.alibaba.trade:alibaba.trade.get.buyerView-1
515
      *
529
      *
516
-     * 接口文档: https://open.1688.com/api/apidocdetail.htm?id=com.alibaba.trade%3Aalibaba.trade.getRefundReasonList-1
530
+     * @param string $orderId 1688订单号
531
+     * @return array|false
532
+     */
533
+    public function getOrderDetailBuyerView(string $orderId)
534
+    {
535
+        if (empty($orderId)) {
536
+            Log::channel('alibaba')->error('[获取订单详情] orderId不能为空');
537
+            return false;
538
+        }
539
+
540
+        $businessParams = [
541
+            'orderId' => $orderId,
542
+            'webSite' => 1688,
543
+        ];
544
+
545
+        $result = $this->executeParam2(
546
+            self::NAMESPACE,
547
+            'alibaba.trade.get.buyerView',
548
+            1,
549
+            $businessParams
550
+        );
551
+
552
+        if ($result === false) {
553
+            Log::channel('alibaba')->error('[获取订单详情] API请求失败, orderId:' . $orderId);
554
+            return false;
555
+        }
556
+
557
+        Log::channel('alibaba')->info('[获取订单详情] API请求成功, orderId:' . $orderId . ', result:' . json_encode($result, JSON_UNESCAPED_UNICODE));
558
+
559
+        return $result;
560
+    }
561
+
562
+    /**
563
+     * 查询退款退货原因列表
517
      *
564
      *
518
-     * 请求参数:
519
-     *   - orderId: 订单号(必填)
520
-     *   - orderEntryIds: 子订单号(可选,多个用逗号分隔)
565
+     * 流程:
566
+     * 1. 先调用 alibaba.trade.get.buyerView 获取订单详情
567
+     * 2. 从订单详情中提取子订单号(orderEntryIds)、货物状态、可退款金额、可退运费
568
+     * 3. 再调用 alibaba.trade.getRefundReasonList 获取退款原因
521
      *
569
      *
522
-     * 返回示例:
523
-     * {
524
-     *   "result": [
525
-     *     {
526
-     *       "refundId": "REFUND123456",
527
-     *       "refundType": "退货退款",
528
-     *       "refundStatus": "waitSellerAgree",
529
-     *       "refundReasonList": [
530
-     *         {
531
-     *           "reasonId": 1,
532
-     *           "reasonText": "质量问题"
533
-     *         }
534
-     *       ]
535
-     *     }
536
-     *   ],
537
-     *   "success": true
538
-     * }
570
+     * API: com.alibaba.trade:alibaba.trade.getRefundReasonList-1
539
      *
571
      *
540
      * @param array $params 请求参数(orderId 必填)
572
      * @param array $params 请求参数(orderId 必填)
541
      * @return array|false
573
      * @return array|false
@@ -548,20 +580,38 @@ class OrderService extends AlibabaAgentBaseService
548
             return false;
580
             return false;
549
         }
581
         }
550
 
582
 
551
-        $businessParams = [
552
-            'orderId' => (string)$params['orderId'],
553
-        ];
583
+        $orderId = (string)$params['orderId'];
584
+
585
+        // 1. 先获取订单详情,提取子订单号和货物状态
586
+        $orderDetail = $this->getOrderDetailBuyerView($orderId);
554
 
587
 
555
-        // 子订单号
556
-        if (!empty($params['orderEntryIds'])) {
557
-            $businessParams['orderEntryIds'] = '[' . (string)$params['orderEntryIds'] . ']';
588
+        if ($orderDetail === false) {
589
+            Log::channel('alibaba')->error('[获取退款原因] 获取订单详情失败, orderId:' . $orderId);
590
+            return false;
558
         }
591
         }
559
 
592
 
560
-        // 货物状态
561
-        if (!empty($params['goodsStatus'])) {
562
-            $businessParams['goodsStatus'] = (string)$params['goodsStatus'];
593
+        // 从订单详情中提取子订单ID列表
594
+        $orderEntryIds = $orderDetail['result']['productItems'][0]['subItemID'] ?: '';
595
+        $goodsStatus = self::REFUND_GOOD_STATUS[$orderDetail['result']['productItems'][0]['status']] ?: '';
596
+        if (!$goodsStatus) {
597
+            Log::channel('alibaba')->error('[获取退款原因] 获取货物状态失败, orderId:' . $orderId);
598
+            return false;
563
         }
599
         }
564
 
600
 
601
+        // 提取可退款金额和可退运费(从订单详情中)
602
+        // totalSuccessAmount: 实付款(分),carriage: 运费(分)
603
+        $applyPayment = (string)$orderDetail['result']['baseInfo']['totalAmount'] ?? 0;
604
+        $applyCarriage = (string)$orderDetail['result']['baseInfo']['shippingFee'] ?? 0;
605
+
606
+        // 2. 调用退款原因查询API
607
+        $businessParams = [
608
+            'orderId' => $orderId,
609
+            'orderEntryIds' => '[' . $orderEntryIds . ']',
610
+            'goodsStatus' => $goodsStatus,
611
+        ];
612
+
613
+        Log::channel('alibaba')->info('[获取退款原因] 准备请求, orderId:' . $orderId . ', orderEntryIds:' . json_encode($orderEntryIds) . ', goodsStatus:' . $goodsStatus);
614
+
565
         // --- 调用API ---
615
         // --- 调用API ---
566
         $result = $this->executeParam2(
616
         $result = $this->executeParam2(
567
             self::NAMESPACE,
617
             self::NAMESPACE,
@@ -577,7 +627,7 @@ class OrderService extends AlibabaAgentBaseService
577
 
627
 
578
         Log::channel('alibaba')->info('[获取退款原因] API请求成功, result:' . json_encode($result, JSON_UNESCAPED_UNICODE));
628
         Log::channel('alibaba')->info('[获取退款原因] API请求成功, result:' . json_encode($result, JSON_UNESCAPED_UNICODE));
579
 
629
 
580
-        return $this->formatRefundReasonListResult($result);
630
+        return $this->formatRefundReasonListResult($result, $orderId, $orderEntryIds, $goodsStatus, $applyPayment, $applyCarriage);
581
     }
631
     }
582
 
632
 
583
     /**
633
     /**
@@ -595,10 +645,15 @@ class OrderService extends AlibabaAgentBaseService
595
      *   }
645
      *   }
596
      * }
646
      * }
597
      *
647
      *
598
-     * @param array $result API原始返回数据
648
+     * @param array  $result        API原始返回数据
649
+     * @param string $orderId       1688订单号
650
+     * @param string $orderEntryIds 子订单ID列表
651
+     * @param string $goodsStatus   货物状态
652
+     * @param int    $applyPayment  可退款金额(分)
653
+     * @param int    $applyCarriage 可退运费(分)
599
      * @return array
654
      * @return array
600
      */
655
      */
601
-    protected function formatRefundReasonListResult(array $result): array
656
+    protected function formatRefundReasonListResult(array $result, string $orderId = '', string $orderEntryIds = '', string $goodsStatus = '', string $applyPayment = '0', string $applyCarriage = '0'): array
602
     {
657
     {
603
         $reasons = [];
658
         $reasons = [];
604
         if (isset($result['result']['result']['reasons']) && is_array($result['result']['result']['reasons'])) {
659
         if (isset($result['result']['result']['reasons']) && is_array($result['result']['result']['reasons'])) {
@@ -610,6 +665,13 @@ class OrderService extends AlibabaAgentBaseService
610
             }
665
             }
611
         }
666
         }
612
 
667
 
613
-        return $reasons;
668
+        return [
669
+            'orderId'       => $orderId,
670
+            'orderEntryIds' => $orderEntryIds,
671
+            'goodsStatus'   => $goodsStatus,
672
+            'applyPayment'  => $applyPayment,
673
+            'applyCarriage' => $applyCarriage,
674
+            'reasons'       => $reasons,
675
+        ];
614
     }
676
     }
615
 }
677
 }

+ 3 - 0
route/admin.php

@@ -1004,6 +1004,9 @@ Route::group(config('admin.api_admin_prefix') . '/', function () {
1004
             Route::get('refund/express/:id', 'RefundOrder/express')->name('systemStoreRefundExpress');//查询用户退货物流
1004
             Route::get('refund/express/:id', 'RefundOrder/express')->name('systemStoreRefundExpress');//查询用户退货物流
1005
 
1005
 
1006
 
1006
 
1007
+            // 1688订单售后
1008
+            Route::get('refundReasonList','RefundOrder/refundReasonList');
1009
+
1007
             Route::get('dhm_list/lst', 'RefundOrder/dhm_list')->name('systemRefundOrderDhmLst');
1010
             Route::get('dhm_list/lst', 'RefundOrder/dhm_list')->name('systemRefundOrderDhmLst');
1008
             Route::get('dhm_status', 'RefundOrder/dhm_status')->name('systemRefundOrderDhmStatus');
1011
             Route::get('dhm_status', 'RefundOrder/dhm_status')->name('systemRefundOrderDhmStatus');
1009
             Route::get('excel', 'Order/Excel')->name('systemOrderExcel');
1012
             Route::get('excel', 'Order/Excel')->name('systemOrderExcel');