DeliveryController.php 7.7 KB

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