DeliveryController.php 8.6 KB

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