DeliveryFengniaoController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace ghs\controllers;
  3. use common\components\util;
  4. use Yii;
  5. use common\components\delivery\services\adapter\FengniaoAdapter;
  6. use common\components\delivery\platform\fengniao\Auth;
  7. /**
  8. * 蜂鸟配送 API 测试控制器
  9. *
  10. * ⚠️ 常见错误诊断:apiCode=B0112 -- 门店不存在
  11. *
  12. * 可能的原因:
  13. * 1. chain_store_id 填写错误或不存在 ← 最可能!
  14. * 2. chain_store_id 所属的城市不在蜂鸟配送范围内
  15. * 3. 收货地址超出该门店的配送范围
  16. * 4. 收货坐标系与接口要求不匹配(建议使用高德地图坐标系 position_source=3)
  17. * 5. 门店已被禁用或删除
  18. *
  19. * 解决步骤:
  20. * 1. 确认 chain_store_id = '14594092' 是否真实存在
  21. * 2. 使用 actionCityList() 验证收货城市是否在蜂鸟配送范围内
  22. * 3. 修改收货地址到该门店的配送范围内
  23. * 4. 确保使用高德地图坐标系(position_source = 3)
  24. * 5. 查看运行时日志(runtime/logs/app.log)了解更多错误细节
  25. */
  26. class DeliveryFengniaoController extends BaseController
  27. {
  28. private const TEST_SHOP_ID = '467788524';
  29. public function actionGetToken()
  30. {
  31. $auth = new Auth();
  32. $auth->setMerchantId('14594092');
  33. $accessToken = $auth->getAccessToken('H9IbEc4t5lf4w96KJ7yjQT');
  34. return $this->asJson($accessToken);
  35. }
  36. /**
  37. * 获取蜂鸟配送覆盖的城市列表
  38. * 用于诊断:收货地址所在城市是否在蜂鸟配送范围内
  39. */
  40. public function actionCityList()
  41. {
  42. $fap = new FengniaoAdapter('5e1577dc-487d-4f56-96e0-e4b3aba7326b');
  43. $fap->setMerchantId('14594092');
  44. Yii::info("=== 获取城市列表 ===");
  45. $re = $fap->cityList();
  46. Yii::info("=== 城市列表响应 ===");
  47. Yii::info(json_encode($re, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
  48. return $this->asJson($re);
  49. }
  50. /**
  51. * 门店批量查询接口
  52. */
  53. public function actionChainstoreQueryList()
  54. {
  55. $fap = new FengniaoAdapter('5e1577dc-487d-4f56-96e0-e4b3aba7326b');
  56. $ret = $fap->chainstoreQueryList(['merchant_id'=>14594092]);
  57. util::success($ret, "success");
  58. }
  59. public function actionCreateOrder()
  60. {
  61. $order = $this->getValidCreateOrderData();
  62. $fap = new FengniaoAdapter('5e1577dc-487d-4f56-96e0-e4b3aba7326b');
  63. $fap->setMerchantId('14594092');
  64. $fap->createOrder($order);
  65. }
  66. public function actionPreCreateOrder()
  67. {
  68. $order = $this->getValidCreateOrderData();
  69. $fap = new FengniaoAdapter('5e1577dc-487d-4f56-96e0-e4b3aba7326b');
  70. $fap->setMerchantId('14594092');
  71. // 添加调试日志
  72. Yii::info("=== 预下单接口请求 ===");
  73. Yii::info("门店ID: " . $order['chain_store_id']);
  74. Yii::info("商户ID: 14594092");
  75. Yii::info("收货地址: " . $order['receiver_address']);
  76. Yii::info("收货坐标: {$order['receiver_latitude']}, {$order['receiver_longitude']}");
  77. $re = $fap->getPrice($order);
  78. Yii::info("=== 预下单接口响应 ===");
  79. Yii::info(json_encode($re, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
  80. return $this->asJson($re);
  81. }
  82. public function actionPreCancelOrder()
  83. {
  84. $fap = new FengniaoAdapter('5e1577dc-487d-4f56-96e0-e4b3aba7326b');
  85. $fap->setMerchantId('14594092');
  86. $cancelOrder = [
  87. "order_id" => '100000000191491340',
  88. 'order_cancel_role' => 1,
  89. "order_cancel_code" => 6,
  90. 'order_cancel_other_reason' => '订单取消'
  91. ];
  92. $re = $fap->cancelOrder($cancelOrder);
  93. }
  94. /**
  95. * 获取有效的创建订单数据
  96. */
  97. private function getValidCreateOrderData(): array
  98. {
  99. return [
  100. 'partner_order_code' => 'TEST_ORDER_' . time() . '_' . uniqid(),
  101. 'receiver_primary_phone' => '13800000000',
  102. 'receiver_name' => '张三',
  103. 'receiver_latitude' => 39.9042,
  104. 'receiver_longitude' => 116.4074,
  105. 'receiver_address' => '北京市朝阳区建国门外大街1号',
  106. 'position_source' => 3,
  107. 'goods_count' => 2,
  108. 'goods_weight' => 1.5,
  109. 'goods_total_amount_cent' => 5000,
  110. 'goods_actual_amount_cent' => 4500,
  111. 'goods_item_list' => [
  112. [
  113. 'item_id' => 'ITEM_001',
  114. 'item_name' => '玫瑰花束',
  115. 'item_amount_cent' => 3000,
  116. 'item_actual_amount_cent' => 2700,
  117. 'item_quantity' => 1,
  118. 'item_size' => 2,
  119. ],
  120. [
  121. 'item_id' => 'ITEM_002',
  122. 'item_name' => '贺卡',
  123. 'item_amount_cent' => 2000,
  124. 'item_actual_amount_cent' => 1800,
  125. 'item_quantity' => 1,
  126. 'item_size' => 1,
  127. ],
  128. ],
  129. 'order_type' => 1,
  130. 'chain_store_id' => self::TEST_SHOP_ID,
  131. 'order_remark' => '请轻拿轻放',
  132. ];
  133. }
  134. }