DeliveryController.php 7.3 KB

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