DeliveryController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\express\classes\ShansAuthTokenClass;
  4. use common\components\util;
  5. use common\components\delivery\services\DispatchService;
  6. use Yii;
  7. /**
  8. * 聚合配送(自配 + 聚合动力)
  9. * Class DeliveryController
  10. * @package ghs\controllers
  11. */
  12. class DeliveryController extends BaseController
  13. {
  14. // ------------------ 授权(账号绑定) ---------------------
  15. //授权
  16. public function actionMerchantAuth()
  17. {
  18. //闪送授权(商户授权:授权后能为商户下所有门店发单)
  19. $auth = new \common\components\delivery\platform\shanSong\Auth();
  20. $redirectUrl = 'https://api.shop.hzghd.com/delivery/callback';
  21. $authUrl = $auth->generateMerchantAuthUrl($this->mainId, $redirectUrl);
  22. // 重定向用户
  23. header('Location: ' . $authUrl);
  24. }
  25. // 获取多个平台报价
  26. public function actionAllPlatformPrice()
  27. {
  28. $ds = new DispatchService();
  29. $orderData = [
  30. 'city_name' => '北京市',
  31. 'sender' => [
  32. 'from_address' => '东升科技国际园',
  33. 'from_address_detail' => '2层202',
  34. 'from_sender_name' => '小闪',
  35. 'from_mobile' => '13800000000',
  36. 'from_latitude' => '40.047858',
  37. 'from_longitude' => '116.378424',
  38. ],
  39. 'receiver_list' => [
  40. [
  41. 'order_no' => 'C1119A000013053981',
  42. 'to_address' => '永泰庄地铁站',
  43. 'to_address_detail' => '1楼',
  44. 'to_receiver_name' => '小送',
  45. 'to_mobile' => '13800000001',
  46. 'to_latitude' => '40.043612',
  47. 'to_longitude' => '116.361199',
  48. 'good_type' => 5, // 物品类型
  49. 'weight' => 2, // 物品重量(kg,整数)
  50. 'remarks' => '', // 备注
  51. ]
  52. ],
  53. 'appoint_type' => 0, // 0: 立即单,1: 预约单
  54. 'appointment_date' => '', // 预约时间 yyyy-MM-dd HH:mm
  55. 'store_id' => 393549, // 店铺ID
  56. 'travel_way' => 0, // 指定交通工具,0: 不限交通方式
  57. 'delivery_type' => 1, // 1: 帮我送,2: 帮我取
  58. 'expect_start_time' => null, // 期望送达时间起始(毫秒级时间戳)
  59. 'expect_end_time' => null, // 期望送达时间终止(毫秒级时间戳)
  60. ];
  61. $re = $ds->getBestPlatformByPrice($orderData);
  62. return $re;
  63. }
  64. // 创建订单(发单)
  65. // 查询订单(查单)
  66. // 第三方回调入口
  67. /**
  68. * 回调接口
  69. */
  70. public function actionCallback()
  71. {
  72. $callbackData = Yii::$app->request->post();
  73. if (empty($callbackData)) {
  74. $postStr = file_get_contents('php://input');
  75. $callbackData = json_decode($postStr, true);
  76. if (empty($callbackData)) {
  77. util::fail('回调请求的数据为空');
  78. }
  79. }
  80. // 从配置中获取安全token
  81. // $token = Yii::$app->params['wx_intracity_token'] ?? 'your_token_here';
  82. // $result = IntraCityExpress::handleOrderCallback($callbackData, $token);
  83. // 记录回调日志
  84. Yii::info('聚合配送回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'delivery');
  85. $code = $callbackData['code'];
  86. $state = $callbackData['state'];
  87. $shopId = $callbackData['shopId'];
  88. $isAllStoreAuth = $callbackData['isAllStoreAuth'];
  89. $thirdStoreId = $callbackData['thirdStoreId'];
  90. $storeId = $callbackData['storeId'];
  91. // 验证state参数(防止CSRF)
  92. //if ($state != $yourUserId) {
  93. //throw new \yii\web\BadRequestHttpException('Invalid state parameter');
  94. //}
  95. // 获取AccessToken
  96. $auth = new \common\components\delivery\platform\shanSong\Auth();
  97. $result = $auth->getAccessToken($code);
  98. if (!$result['success']) {
  99. throw new \yii\web\HttpException(500, '获取授权失败:' . $result['error']);
  100. }
  101. // 保存授权信息到数据库
  102. $data['user_id'] = $state;
  103. $data['shop_id'] = $shopId;
  104. $data['access_token'] = $result['access_token'];
  105. $data['refresh_token'] = $result['refresh_token'];
  106. $data['expires_at'] = time() + $result['expires_in'];
  107. $data['auth_type'] = $isAllStoreAuth ? 'all_store' : 'single_store';
  108. if (!$isAllStoreAuth) {
  109. $data['third_store_id'] = $thirdStoreId;
  110. $data['shans_store_id'] = $storeId;
  111. }
  112. ShansAuthTokenClass::add($data);
  113. if (false) {
  114. throw new \yii\web\HttpException(500, '保存授权信息失败');
  115. }
  116. //return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  117. return $this->redirect(['success']);
  118. }
  119. }