DeliveryController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use bizGhs\express\classes\GhsDeliveryOrderClass;
  5. use bizGhs\express\classes\DeliveryAuthTokenClass;
  6. use bizGhs\order\classes\OrderClass;
  7. use common\components\delivery\services\adapter\HuolalaAdapter;
  8. use common\components\util;
  9. use common\components\delivery\services\DispatchService;
  10. use ghs\models\delivery\CancelOrderForm;
  11. use ghs\models\delivery\CreateOrderForm;
  12. use ghs\models\delivery\OrderDetailForm;
  13. use Yii;
  14. /**
  15. * 聚合配送(自配 + 聚合动力)
  16. * Class DeliveryController
  17. * @package ghs\controllers
  18. */
  19. class DeliveryController extends BaseController
  20. {
  21. public $guestAccess = ['callback', 'shansong-auth-callback', 'huolala-auth-callback',
  22. 'fengniao-callback', 'shansong-callback', 'huolala-callback', 'shunfeng-callback'];
  23. public function actionList()
  24. {
  25. $platformList = [];
  26. $platformLogos = [
  27. 'data' => ['name'=>'达达', 'logo'=>'📦'],
  28. 'shunfeng' => ['name'=>'顺丰同城', 'logo'=>'🚚'],
  29. 'meituan' => ['name'=>'美团', 'logo'=>'🍱'] ,
  30. 'fengniao' => ['name'=>'蜂鸟', 'logo'=>'🍜'],
  31. 'huolala' => ['name'=>'货拉拉', 'logo'=>'🚛'],
  32. 'shansong' => ['name'=>'闪送', 'logo'=>'⚡'],
  33. 'didi' => ['name'=>'滴滴', 'logo'=>'🚗'],
  34. 'uu' => ['name'=>'UU跑腿', 'logo'=>'🛵']
  35. ];
  36. $mainId = $this->mainId;
  37. $allDeliveries = DeliveryAuthTokenClass::getAllByCondition(['mainId'=>$mainId]);
  38. foreach($allDeliveries as $d){
  39. $item = [
  40. 'id'=>$d['platform'],
  41. 'logo'=>$platformLogos[$d['platform']]['logo'],
  42. 'name'=>$platformLogos[$d['platform']]['name'],
  43. 'is_authorized' => true,
  44. 'balance' => 0,
  45. 'access_type' => 'oauth'
  46. ];
  47. $platformList[] = $item;
  48. }
  49. util::success(['platformList'=>$platformList]);
  50. }
  51. public function actionGetAuthUrl()
  52. {
  53. $platform = Yii::$app->request->get('platformId');
  54. switch($platform){
  55. case 'shansong':
  56. return $this->actionShansongAuth();
  57. break;
  58. case 'huolala':
  59. return $this->actionHuolalaAuth();
  60. break;
  61. case 'fengniao':
  62. return $this->actionFengniaoAuth();
  63. break;
  64. case 'dada':
  65. return $this->actionDadaAuth();
  66. break;
  67. case 'shunfeng':
  68. return $this->actionShunfengAuth();
  69. break;
  70. case 'didi':
  71. break;
  72. default:
  73. util::error(-1, $platform . '不存在');
  74. break;
  75. }
  76. }
  77. // ------------------ 授权(账号绑定) ---------------------
  78. //授权
  79. public function actionShansongAuth()
  80. {
  81. //闪送授权(商户授权:授权后能为商户下所有门店发单)
  82. $auth = new \common\components\delivery\platform\shansong\Auth();
  83. $redirectUrl = 'https://api.shop.hzghd.com/delivery/shansong-auth-callback';
  84. $authUrl = $auth->generateMerchantAuthUrl($this->mainId, $redirectUrl);
  85. // 重定向用户
  86. //header('Location: ' . $authUrl);
  87. util::success(['url'=>$authUrl]);
  88. }
  89. public function actionHuolalaAuth()
  90. {
  91. $huoLalaAuth = new \common\components\delivery\platform\huolala\Auth();
  92. $mainId = $this->mainId;
  93. $redirectUrl = 'https://api.shop.hzghd.com/delivery/huolala-auth-callback' . '?mainId=' . $mainId;
  94. $authUrl = $huoLalaAuth->generateAuthUrl($redirectUrl);
  95. //header('Location: ' . $authUrl);
  96. util::success(['url'=>$authUrl]);
  97. }
  98. public function actionFengniaoAuth()
  99. {
  100. $fengnaioAuth = new \common\components\delivery\platform\fengniao\Auth();
  101. $mainId = $this->mainId;
  102. $redirectUrl = 'https://api.shop.hzghd.com/delivery/fengniao-auth-callback' . '?mainId=' . $mainId;
  103. $authUrl = $fengnaioAuth->generateAuthUrl($redirectUrl);
  104. util::success(['url'=>$authUrl]);
  105. }
  106. public function actionDadaAuth()
  107. {
  108. $dadaAuth = new \common\components\delivery\platform\dada\Auth();
  109. $ticket = $dadaAuth->getTicket();
  110. $mainId = $this->mainId;
  111. $redirectUrl = 'https://api.shop.hzghd.com/delivery/dada-auth-callback' . '?mainId=' . $mainId;
  112. $shopNumber = '40d8391f9b05477b';
  113. $state = 'huidiao_biaoshi';
  114. $authUrl = $dadaAuth->generateAuthUrl($ticket, $state, $shopNumber);
  115. //header('Location: ' . $authUrl);
  116. util::success(['url'=>$authUrl]);
  117. }
  118. public function actionShunfengAuth()
  119. {
  120. $shunfengAuth = new \common\components\delivery\platform\shunfeng\Auth();
  121. $mainId = $this->mainId;
  122. $authUrl = $shunfengAuth->generateAuthUrl($mainId);
  123. util::success(['url'=>$authUrl]);
  124. }
  125. public function actionCancelAuth()
  126. {
  127. // 有平台有对应的取消授权接口,有的没有。
  128. // 有的就对接下,然后再删除数据。没有的直接删除数据就行。
  129. $platform = Yii::$app->request->get('platform');
  130. $ret = DeliveryAuthTokenClass::deleteByCondition(['mainId'=>$this->mainId, 'platform'=>$platform]);
  131. if($ret > 0) {
  132. util::success(['message'=>'取消授权成功']);
  133. } else {
  134. util::fail('取消授权失败');
  135. }
  136. }
  137. // --------------------------------------------------------------------------------------------------------------
  138. public function actionHuolalaVehicle()
  139. {
  140. $mainId = $this->mainId;
  141. $authPlatforms = DeliveryAuthTokenClass::getByCondition(['mainId'=>$mainId, 'platform'=>'huolala']);
  142. $huolalaAdapter = new HuolalaAdapter($authPlatforms['access_token']);
  143. $cities = $huolalaAdapter->getCityVehicleList(1006);
  144. return $this->asJson($cities);
  145. }
  146. // 查询开通城市
  147. public function actionOpenCitiesLists()
  148. {
  149. $platform = Yii::$app->request->get('platform');
  150. $ds = new DispatchService($this->mainId);
  151. $cities = $ds->openCitiesLists($platform);
  152. if ($platform == 'shansong') {
  153. $newCitiesArr = [];
  154. $citiesArr = $cities['data'];
  155. foreach($citiesArr as $block) {
  156. foreach ($block['cities'] as $city) {
  157. $newCitiesArr[$city['name']] = $city;
  158. }
  159. }
  160. // 把newCitiesArr保存到 platform/huolala/cities.php 文件中
  161. $citiesFile = Yii::getAlias('@common/components/delivery/platform/shansong/cities.php');
  162. // 如果文件不存在,则创建文件
  163. if (!file_exists($citiesFile)) {
  164. file_put_contents($citiesFile, '<?php return []; ?>');
  165. }
  166. // 文件内容要包含命名空间
  167. $content = '<?php return ' . var_export($newCitiesArr, true) . '; ?>';
  168. file_put_contents($citiesFile, $content);
  169. return $newCitiesArr;
  170. } else if ($platform == 'huolala') {
  171. $citiesArr = $cities['data']['city_list'];
  172. $newCitiesArr = [
  173. 'expire' => $cities['data']['expire'],
  174. ];
  175. foreach($citiesArr as $city) {
  176. $newCitiesArr[$city['name']] = [
  177. 'city_id' => $city['city_id'],
  178. 'city_name' => $city['name'],
  179. ];
  180. }
  181. // 把newCitiesArr保存到 platform/huolala/cities.php 文件中
  182. $citiesFile = Yii::getAlias('@common/components/delivery/platform/huolala/cities.php');
  183. // 如果文件不存在,则创建文件
  184. if (!file_exists($citiesFile)) {
  185. file_put_contents($citiesFile, '<?php namespace common\components\delivery\platform\huolala; return []; ?>');
  186. }
  187. // 文件内容要包含命名空间
  188. $content = '<?php namespace common\components\delivery\platform\huolala; return ' . var_export($newCitiesArr, true) . '; ?>';
  189. file_put_contents($citiesFile, $content);
  190. return $newCitiesArr;
  191. } else {
  192. return $cities;
  193. }
  194. return $cities;
  195. }
  196. public function actionAddress()
  197. {
  198. $post = Yii::$app->request->post();
  199. $adminId = $this->adminId;
  200. util::checkRepeatCommit($adminId, 4);
  201. $orderId = intval($post['orderId']);
  202. $order = OrderClass::getById($orderId, false, 'fullAddress');
  203. $shop = ShopClass::getById($this->shopId, false, 'fullAddress');
  204. $ret = [
  205. "send_address" => $shop['fullAddress'],
  206. "receive_address" => $order['fullAddress']
  207. ];
  208. util::success($ret, "success");
  209. }
  210. // 获取多个平台报价
  211. public function actionAllDeliveryQuotes()
  212. {
  213. $post = Yii::$app->request->post();
  214. $orderTime = $post['pickupTime'];
  215. $adminId = $this->adminId;
  216. util::checkRepeatCommit($adminId, 4);
  217. $orderId = intval($post['orderId']);
  218. $order = OrderClass::getById($orderId);
  219. $shop = ShopClass::getById($this->shopId);
  220. $ds = new DispatchService($this->mainId);
  221. $platforms = $ds->getAllPlatformPrice($order, $shop, $orderTime);
  222. $deliveryList = [];
  223. foreach ($platforms['quotes'] as $item) {
  224. switch ($item['platform']) {
  225. case 'shunfeng':
  226. $deliveryList[] = [
  227. 'name' => '顺丰',
  228. 'en_name' => 'shunfeng',
  229. 'price' => $item['real_pay_money'],
  230. 'distance' => $item['distance'],
  231. 'type' => '',
  232. 'isAble' => true
  233. ];
  234. break;
  235. case 'shansong':
  236. $deliveryList[] = [
  237. 'name' => '闪送',
  238. 'en_name' => 'shansong',
  239. 'price' => $item['total_amount'],
  240. 'distance' => $item['total_distance'],
  241. 'type' => '',
  242. 'issOrderNo' => $item['order_number'],
  243. 'isAble' => true
  244. ];
  245. break;
  246. case 'fengniao':
  247. // 循环遍历 goods_infos 数组,提取有效项的信息
  248. $validGoods = [];
  249. if (!empty($item['goods_infos']) && is_array($item['goods_infos'])) {
  250. foreach ($item['goods_infos'] as $good) {
  251. if (!empty($good['is_valid']) && $good['is_valid'] == 1) {
  252. $validGoods[] = [
  253. 'actual_delivery_amount_cent' => $good['actual_delivery_amount_cent'] ?? 0,
  254. 'service_goods_id' => $good['service_goods_id'] ?? ''
  255. ];
  256. $deliveryList[] = [
  257. 'name' => '蜂鸟'. ' (' .$good['base_goods_id'] . ')',
  258. 'en_name' => 'fengniao',
  259. 'price' => $good['actual_delivery_amount_cent'],
  260. 'distance' => $item['distance'],
  261. 'valid_goods' => $validGoods,
  262. 'type' => '',
  263. 'isAble' => true
  264. ];
  265. } else {
  266. $deliveryList[] = [
  267. 'name' => '蜂鸟'. ' (' .$good['base_goods_id'] . ')',
  268. 'en_name' => 'fengniao',
  269. 'price' => 0,
  270. 'distance' => $item['distance'],
  271. 'valid_goods' => $good,
  272. 'type' => $good['disable_reason'],
  273. 'isAble' => false
  274. ];
  275. }
  276. }
  277. }
  278. break;
  279. case 'huolala':
  280. $deliveryList = [];
  281. foreach($item['price_info_list'] as $arr) {
  282. $priceInfo = $arr['calculate_price_info'];
  283. $vehicleType = $arr['vehicle_type'];
  284. $deliveryList[] = [
  285. 'name' => '货拉拉' . ' (' . $vehicleType['_meta']['vehicle_type'] . ')',
  286. 'en_name' => 'huolala',
  287. 'price' => $priceInfo['price_conditions'][0]['price_info']['total_price'],
  288. 'distance' => $priceInfo['distance_info']['distance_total'],
  289. 'type' => $vehicleType['_meta']['vehicle_type'],
  290. 'isAble' => true,
  291. // ---------------------------------------------
  292. 'order_vehicle_id' => $vehicleType['order_vehicle_id'],
  293. 'city_id' => $item['city_id'],
  294. 'city_info_revision' => $vehicleType['city_info_revision'],
  295. // // 下单需要透传的数据
  296. 'vehicle_std' => $vehicleType['vehicle_std'],
  297. 'spec_req' => $vehicleType['spec_req'],
  298. 'price_calculate_id' => $priceInfo['price_calculate_id'],
  299. 'price_item_encryption' => $priceInfo['price_conditions'][0]['price_item_encryption'],
  300. 'vehicle_attr' => $priceInfo['vehicle_info']['vehicle_attr'],
  301. 'commodity_info' => $priceInfo['price_conditions'][0]['commodity_item'],
  302. ];
  303. }
  304. break;
  305. }
  306. }
  307. $ret['deliveryList'] = $deliveryList;
  308. util::success($ret, "success");
  309. }
  310. // 创建订单(真实下单)
  311. public function actionCreateOrder()
  312. {
  313. $form = new CreateOrderForm();
  314. $form->loadAndValidate();
  315. $post = $form->getAttributes();
  316. //$post = Yii::$app->request->post();
  317. $orderId = $post['orderId']; // xhGhsOrder.id
  318. $params = $post['allParams'];
  319. $platform = $params['en_name'];// $post['platform'] 是中文
  320. $pickupTime = $post['pickupTime'];
  321. $params['mainId'] = $this->mainId;
  322. $params['ip'] = Yii::$app->request->userIP;
  323. $params['remark'] = $post['remark'];
  324. $params['total_price_fen'] = $post['price'];
  325. $params['order_time'] = $pickupTime['value'];
  326. $shopId = $this->shopId;
  327. $ghsOrder = OrderClass::getById($orderId, true);
  328. // if ($ghsOrder['status'] != 2) {
  329. // util::fail('订单状态不是待配送');
  330. // // }
  331. if(!in_array($ghsOrder['sendStatus'], [-4, -1])) {
  332. Yii::error('订单已经发过跑腿');
  333. util::fail('订单已经发过跑腿');
  334. }
  335. $ds = new DispatchService($this->mainId, $platform);
  336. $ret = $ds->createOrder($ghsOrder, $shopId, $params);
  337. if (isset($ret['code']) && $ret['code'] == 0) {
  338. $gdo = GhsDeliveryOrderClass::getByCondition(['mainId'=>$this->mainId, 'orderId'=>$ret['data']['order_id']]);
  339. if (empty($gdo)) {
  340. $data = [
  341. 'mainId' => $this->mainId,
  342. 'shopId' => $this->shopId,
  343. 'deliveryId' => $ret['platform'],
  344. 'ghsOrderId' => $orderId,
  345. 'orderId' => $ret['data']['order_id'],
  346. 'distance' => $ret['data']['distance'],
  347. 'fee' => $ret['data']['fee'],
  348. 'orderStatus' => 0
  349. ];
  350. GhsDeliveryOrderClass::add($data);
  351. } else {
  352. Yii::info('重复创建订单');
  353. }
  354. //更新订单
  355. $ghsOrder->sendDistance = $ret['data']['distance'];
  356. $ghsOrder->sendStatus = 0;
  357. $ghsOrder->deliveryId = $ret['platform'];
  358. $ghsOrder->sendType = 2;// 2指跑腿
  359. $ghsOrder->save();
  360. util::success($ret, "success");
  361. } else {
  362. if(isset($ret['msg'])){
  363. util::fail($ret['msg']);
  364. }
  365. util::fail('创建跑腿失败');
  366. }
  367. }
  368. /**
  369. * 查询订单详情
  370. *
  371. * 1. 蜂鸟平台 orderId 可以使用 "XSD_CS26322799" (即:表 xhGhsOrder.orderSn)
  372. */
  373. public function actionOrderDetail()
  374. {
  375. $form = new OrderDetailForm();
  376. $form->loadAndValidate();
  377. $post = $form->getAttributes();
  378. $platform = $post['platform'];
  379. $orderId = $post['orderId'];
  380. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : '';
  381. $ds = new DispatchService($this->mainId, $platform);
  382. $ret = $ds->selectOrder($orderId, $orderSn);
  383. $return = isset($ret['ret']) ? $ret['ret'] : $ret['code']; // 各平台兼容处理
  384. if($return == 0) {
  385. util::success($ret, "success");
  386. }
  387. util::fail('查询失败');
  388. }
  389. // 取消订单
  390. public function actionCancelOrder()
  391. {
  392. $form = new CancelOrderForm();
  393. $form->loadAndValidate();
  394. $post = $form->getAttributes();
  395. $orderId = $post['orderId'] ?? 0; // 平台的orderId,
  396. $platform = $post['platform'] ?? '';
  397. $ghsOrderId = $post['ghsOrderId'];
  398. // 先获取配送订单,确保 $gdo 始终可用
  399. $gdo = GhsDeliveryOrderClass::getByCondition(['mainId'=>$this->mainId, 'ghsOrderId'=>$ghsOrderId, 'orderStatus!='=> -1], true);
  400. if(empty($gdo)) {
  401. util::fail('未找到对应订单: ' . $ghsOrderId);
  402. }
  403. // 如果没有提供平台和订单ID,从配送订单中获取
  404. if($platform == '' || $orderId == 0) {
  405. $orderId = $gdo->orderId;
  406. $platform = $gdo->deliveryId;
  407. }
  408. $ds = new DispatchService($this->mainId, $platform);
  409. $cancelCostCent = 0;
  410. if($platform == 'fengniao') {
  411. //请求订单预取消接口,获取是否可取消。如果运单状态不允许取消,则直接返回
  412. $cancelParams = ['order_id'=>$orderId, 'order_cancel_code'=>$post['order_cancel_code']];
  413. $res = $ds->getAdapter()->preCancelOrder($cancelParams);
  414. if($res['code'] != 0){
  415. util::fail('运单状态不允许取消');
  416. }
  417. $cancelCostCent = $res['data']['actual_cancel_cost_cent']; //取消实际需金额(单位:分)
  418. }
  419. $post['order_cancel_role'] = 1; //1:商户取消, 2:用户取消
  420. $ret = $ds->cancelOrder($orderId, $post);
  421. if($ret['code'] == 0) {
  422. // 更新跑腿订单与批发订单状态
  423. $gdo->orderStatus = -1;
  424. $gdo->cancelCost = $cancelCostCent;
  425. if(!$gdo->save()) {
  426. Yii::error('保存配送订单失败: ' . json_encode($gdo->errors));
  427. util::fail('保存配送订单失败');
  428. }
  429. $ghsOrder = OrderClass::getById($ghsOrderId, true, 'id, sendDistance, sendStatus, deliveryId');
  430. $ghsOrder->sendDistance = 0;
  431. $ghsOrder->sendStatus = -4;
  432. $ghsOrder->deliveryId = '';
  433. if(!$ghsOrder->save()) {
  434. Yii::error('保存批发订单失败: ' . json_encode($ghsOrder->errors));
  435. util::fail('保存批发订单失败');
  436. }
  437. util::success($ret['data'], "success");
  438. }
  439. util::fail('取消失败');
  440. }
  441. /**
  442. * 获取订单取消原因
  443. */
  444. public function actionCancelReason()
  445. {
  446. $post = Yii::$app->request->post();
  447. $ghsOrderId = $post['ghsOrderId'];
  448. $platform = $post['platform'];
  449. $gdo = GhsDeliveryOrderClass::getByCondition(['mainId'=>$this->mainId, 'ghsOrderId'=>$ghsOrderId, 'orderStatus!='=>-1], true);
  450. if(!empty($gdo)) {
  451. $orderId = $gdo->orderId;
  452. //$platform = $gdo->deliveryId;
  453. } else {
  454. util::fail('未找到对应订单: ' . $ghsOrderId);
  455. }
  456. $ds = new DispatchService($this->mainId, $platform);
  457. $ret = $ds->getCancelReasonList($orderId);
  458. util::success($ret);
  459. }
  460. // ---------------------------------------------------- 普通回调接口 ----------------------------------------------------------
  461. // 第三方回调入口
  462. /**
  463. * 回调接口
  464. */
  465. public function actionCallback()
  466. {
  467. $callbackData = Yii::$app->request->post();
  468. if (empty($callbackData)) {
  469. $postStr = file_get_contents('php://input');
  470. $callbackData = json_decode($postStr, true);
  471. if (empty($callbackData)) {
  472. util::fail('回调请求的数据为空');
  473. }
  474. }
  475. // 从配置中获取安全token
  476. // $token = Yii::$app->params['wx_intracity_token'] ?? 'your_token_here';
  477. // $result = IntraCityExpress::handleOrderCallback($callbackData, $token);
  478. // 记录回调日志
  479. Yii::info('聚合配送回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'delivery');
  480. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  481. }
  482. /**
  483. * 闪送回调接口
  484. * 回调返回的格式必须是{"status":200,"msg":"","data":null}格式,当status为200表示回调成功,否则,回调失败。当回调失败时,间隔一分钟重试一次,最多重试五次。
  485. */
  486. public function actionShansongCallback()
  487. {
  488. $callbackData = Yii::$app->request->post();
  489. if (empty($callbackData)) {
  490. $postStr = file_get_contents('php://input');
  491. $callbackData = json_decode($postStr, true);
  492. }
  493. Yii::info('shansong 回调 -- post: ' . json_encode($callbackData, JSON_UNESCAPED_UNICODE));
  494. $obj = new \common\components\delivery\platform\shansong\OrderStatusCallBack();
  495. $ret = $obj->handle($callbackData);
  496. if($ret) {
  497. return $this->asJson(['status' => 200, 'msg' => 'OK', "data"=> null]);
  498. } else {
  499. Yii::error('闪送回调失败: ' . json_encode($callbackData, JSON_UNESCAPED_UNICODE));
  500. return $this->asJson(['status' => 200, 'msg' => '回调失败', "data"=> null]);
  501. }
  502. }
  503. /**
  504. * 蜂鸟回调接口
  505. */
  506. public function actionFengniaoCallback()
  507. {
  508. $callbackData = Yii::$app->request->post();
  509. // 逆向回调接口结果的加签
  510. $param = [
  511. 'app_id' => $callbackData['app_id'],
  512. 'timestamp' => $callbackData['timestamp'],
  513. 'business_data' => $callbackData['business_data']
  514. ];
  515. $fa = new \common\components\delivery\services\adapter\FengniaoAdapter();
  516. $paramSign = $fa->generateSignature($param);
  517. if($paramSign == $callbackData['signature']){
  518. $businessData = isset($callbackData['business_data']) ? $callbackData['business_data'] : '';
  519. if(empty($businessData)){
  520. $this->asJson(['return_code' => -1, 'return_msg' => 'business data is empty']);
  521. }
  522. $businessData = json_decode($businessData, true);
  523. $cbnObj = new \common\components\delivery\platform\fengniao\CallBackNotify();
  524. $ret = $cbnObj->handle($businessData['callback_business_type'], $businessData['param']);
  525. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  526. }else{
  527. Yii::error('蜂鸟回调接口签名出错');
  528. }
  529. }
  530. /**
  531. * 货拉拉回调接口
  532. */
  533. public function actionHuolalaCallback()
  534. {
  535. $post = Yii::$app->request->post();
  536. Yii::info('货拉拉回调接口post:' . json_encode($post, JSON_UNESCAPED_UNICODE), 'delivery');
  537. $get = Yii::$app->request->get();
  538. Yii::info('货拉拉回调接口get:' . json_encode($get, JSON_UNESCAPED_UNICODE), 'delivery');
  539. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  540. }
  541. /**
  542. * 顺丰回调接口
  543. */
  544. public function actionShunfengCallback()
  545. {
  546. $post = Yii::$app->request->post();
  547. $cbh = new \common\components\delivery\platform\shunfeng\CallBackHandler();
  548. $ret = $cbh->handle($post);// 1.配送状态更改 2.订单完成 3.顺丰原因取消 4.订单配送异常 5.骑士撤单 6.自动建店回调
  549. if($ret) {
  550. return $this->asJson(['error_code' => 0, 'error_msg' => 'success']);
  551. } else {
  552. Yii::error('顺丰回调失败: ' . json_encode($post, JSON_UNESCAPED_UNICODE));
  553. return $this->asJson(['error_code' => -1, 'error_msg' => 'failed']);
  554. }
  555. }
  556. // ---------------------------------------------------- 授权回调接口 ----------------------------------------------------------
  557. /**
  558. * 闪送授权回调接口
  559. */
  560. public function actionShansongAuthCallback()
  561. {
  562. $code = Yii::$app->request->get('code');
  563. $state = Yii::$app->request->get('state');
  564. $shopId = Yii::$app->request->get('shopId');
  565. $isAllStoreAuth = Yii::$app->request->get('isAllStoreAuth');
  566. $thirdStoreId = Yii::$app->request->get('thirdStoreId');
  567. $storeId = Yii::$app->request->get('storeId');
  568. // 验证state参数(防止CSRF)
  569. //if ($state != $yourUserId) {
  570. //throw new \yii\web\BadRequestHttpException('Invalid state parameter');
  571. //}
  572. // 获取AccessToken
  573. $auth = new \common\components\delivery\platform\shansong\Auth();
  574. $result = $auth->getAccessToken($code);
  575. if (!$result['success']) {
  576. throw new \yii\web\HttpException(500, '获取授权失败:' . $result['error']);
  577. }
  578. // 保存授权信息到数据库
  579. $data = [
  580. 'mainId' => $state,
  581. 'shopId' => $shopId, //闪送商户ID
  582. 'accessToken' => $result['access_token'],
  583. 'refreshToken' => $result['refresh_token'],
  584. 'expiresAt' => time() + $result['expires_in'],
  585. 'authType' => $isAllStoreAuth ? 'all_store' : 'single_store',
  586. 'platform' => 'shansong'
  587. ];
  588. if (!$isAllStoreAuth) {
  589. $data['third_store_id'] = $thirdStoreId;
  590. $data['shans_store_id'] = $storeId;
  591. }
  592. DeliveryAuthTokenClass::add($data);
  593. if (false) {
  594. throw new \yii\web\HttpException(500, '保存授权信息失败');
  595. }
  596. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  597. }
  598. /**
  599. * 货拉拉授权回调接口
  600. */
  601. public function actionHuolalaAuthCallback()
  602. {
  603. $get = Yii::$app->request->get();
  604. $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
  605. Yii::info($getData);
  606. $code = $get['code'];
  607. $mainId = $get['mainId'];
  608. // 获取AccessToken
  609. $auth = new \common\components\delivery\platform\huolala\Auth();
  610. $result = $auth->getAccessToken($code);
  611. if (!$result['success']) {
  612. throw new \yii\web\HttpException(500, '获取授权失败:' . $result['error']);
  613. }
  614. // 保存授权信息到数据库
  615. $data = [
  616. 'mainId' => $mainId,
  617. 'shopId' => '',
  618. 'accessToken' => $result['access_token'],
  619. 'refreshToken' => $result['refresh_token'],
  620. 'expiresAt' => strtotime($result['auth_end_time']),
  621. 'authType' => 'all_store',
  622. 'platform' => 'huolala'
  623. ];
  624. DeliveryAuthTokenClass::add($data);
  625. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  626. }
  627. /**
  628. * 蜂鸟授权回调接口
  629. */
  630. public function actionFengniaoAuthCallback()
  631. {
  632. $get = Yii::$app->request->get();
  633. $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
  634. Yii::info($getData);
  635. $callbackData = Yii::$app->request->post();
  636. if (empty($callbackData)) {
  637. $postStr = file_get_contents('php://input');
  638. $callbackData = json_decode($postStr, true);
  639. if (empty($callbackData)) {
  640. Yii::info('回调请求的数据为空');
  641. }
  642. }
  643. Yii::info('蜂鸟授权回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'fengniao');
  644. $mainId = $get['mainId'];
  645. $code = $get['code'];
  646. $merchantId = $get['merchant_id'];
  647. $scope = $get['scope'];
  648. $state = $get['state'];
  649. // 获取AccessToken
  650. $auth = new \common\components\delivery\platform\fengniao\Auth();
  651. $result = $auth->getAccessToken($code, $merchantId);
  652. if (!$result['success']) {
  653. throw new \yii\web\HttpException(500, '获取授权失败:' . $result['error']);
  654. }
  655. $result = $result['data'];
  656. // 保存授权信息到数据库
  657. $data = [
  658. 'mainId' => $mainId,
  659. 'shopId' => '',
  660. 'accessToken' => $result['access_token'],
  661. 'refreshToken' => $result['refresh_token'],
  662. 'expiresAt' => $result['expire_in'],
  663. 'authType' => 'all_store',
  664. 'platform' => 'fengniao'
  665. ];
  666. DeliveryAuthTokenClass::add($data);
  667. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  668. }
  669. /**
  670. * 达达授权回调接口
  671. */
  672. public function actionDadaAuthCallback()
  673. {
  674. $get = Yii::$app->request->get();
  675. $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
  676. Yii::info($getData);
  677. $callbackData = Yii::$app->request->post();
  678. if (empty($callbackData)) {
  679. $postStr = file_get_contents('php://input');
  680. $callbackData = json_decode($postStr, true);
  681. if (empty($callbackData)) {
  682. Yii::info('回调请求的数据为空');
  683. }
  684. }
  685. Yii::info('达达授权回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'dada');
  686. }
  687. /**
  688. * 顺丰授权与取消授权回调接口
  689. */
  690. public function actionShunfengAuthCallback()
  691. {
  692. $get = Yii::$app->request->get();
  693. $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
  694. Yii::info($getData);
  695. $callbackData = Yii::$app->request->post();
  696. if (empty($callbackData)) {
  697. $postStr = file_get_contents('php://input');
  698. $callbackData = json_decode($postStr, true);
  699. if (empty($callbackData)) {
  700. Yii::info('回调请求的数据为空');
  701. }
  702. }
  703. Yii::info('顺丰授权与取消授权回调接口:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'dada');
  704. }
  705. }