DeliveryController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. if($platform='shunfeng') {
  420. //请求订单预取消接口,获取是否可取消。如果运单状态不允许取消,则直接返回
  421. $cancelParams = ['order_id'=>$orderId, 'order_cancel_code'=>$post['order_cancel_code']];
  422. $res = $ds->getAdapter()->preCancelOrder($cancelParams);
  423. if($res['code'] != 0){
  424. util::fail('运单状态不允许取消');
  425. }
  426. $post['order_type'] = 1; //查询订单ID类型 1、顺丰订单号 2、商家订单号
  427. }
  428. $post['order_cancel_role'] = 1; //1:商户取消, 2:用户取消
  429. $ret = $ds->cancelOrder($orderId, $post);
  430. if($ret['code'] == 0) {
  431. // 更新跑腿订单与批发订单状态
  432. $gdo->orderStatus = -1;
  433. $gdo->cancelCost = $ret['platform'] !== 'fengniao' ? $ret['data']['deductionFee'] : $cancelCostCent; // 由于蜂鸟平台返回的扣费不一样造成
  434. if(!$gdo->save()) {
  435. Yii::error('保存配送订单失败: ' . json_encode($gdo->errors));
  436. util::fail('保存配送订单失败');
  437. }
  438. $ghsOrder = OrderClass::getById($ghsOrderId, true, 'id, sendDistance, sendStatus, deliveryId');
  439. $ghsOrder->sendDistance = 0;
  440. $ghsOrder->sendStatus = -4;
  441. $ghsOrder->deliveryId = '';
  442. if(!$ghsOrder->save()) {
  443. Yii::error('保存批发订单失败: ' . json_encode($ghsOrder->errors));
  444. util::fail('保存批发订单失败');
  445. }
  446. util::success($ret['data'], "success");
  447. }
  448. util::fail('取消失败');
  449. }
  450. /**
  451. * 获取订单取消原因
  452. */
  453. public function actionCancelReason()
  454. {
  455. $post = Yii::$app->request->post();
  456. $ghsOrderId = $post['ghsOrderId'];
  457. $platform = $post['platform'];
  458. $gdo = GhsDeliveryOrderClass::getByCondition(['mainId'=>$this->mainId, 'ghsOrderId'=>$ghsOrderId, 'orderStatus!='=>-1], true);
  459. if(!empty($gdo)) {
  460. $orderId = $gdo->orderId;
  461. //$platform = $gdo->deliveryId;
  462. } else {
  463. util::fail('未找到对应订单: ' . $ghsOrderId);
  464. }
  465. $ds = new DispatchService($this->mainId, $platform);
  466. $ret = $ds->getCancelReasonList($orderId);
  467. util::success($ret);
  468. }
  469. // ---------------------------------------------------- 普通回调接口 ----------------------------------------------------------
  470. // 第三方回调入口
  471. /**
  472. * 回调接口
  473. */
  474. public function actionCallback()
  475. {
  476. $callbackData = Yii::$app->request->post();
  477. if (empty($callbackData)) {
  478. $postStr = file_get_contents('php://input');
  479. $callbackData = json_decode($postStr, true);
  480. if (empty($callbackData)) {
  481. util::fail('回调请求的数据为空');
  482. }
  483. }
  484. // 从配置中获取安全token
  485. // $token = Yii::$app->params['wx_intracity_token'] ?? 'your_token_here';
  486. // $result = IntraCityExpress::handleOrderCallback($callbackData, $token);
  487. // 记录回调日志
  488. Yii::info('聚合配送回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'delivery');
  489. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  490. }
  491. /**
  492. * 闪送回调接口
  493. * 回调返回的格式必须是{"status":200,"msg":"","data":null}格式,当status为200表示回调成功,否则,回调失败。当回调失败时,间隔一分钟重试一次,最多重试五次。
  494. */
  495. public function actionShansongCallback()
  496. {
  497. $callbackData = Yii::$app->request->post();
  498. if (empty($callbackData)) {
  499. $postStr = file_get_contents('php://input');
  500. $callbackData = json_decode($postStr, true);
  501. }
  502. Yii::info('shansong 回调 -- post: ' . json_encode($callbackData, JSON_UNESCAPED_UNICODE));
  503. $obj = new \common\components\delivery\platform\shansong\OrderStatusCallBack();
  504. $ret = $obj->handle($callbackData);
  505. if($ret) {
  506. return $this->asJson(['status' => 200, 'msg' => 'OK', "data"=> null]);
  507. } else {
  508. Yii::error('闪送回调失败: ' . json_encode($callbackData, JSON_UNESCAPED_UNICODE));
  509. return $this->asJson(['status' => 200, 'msg' => '回调失败', "data"=> null]);
  510. }
  511. }
  512. /**
  513. * 蜂鸟回调接口
  514. */
  515. public function actionFengniaoCallback()
  516. {
  517. $callbackData = Yii::$app->request->post();
  518. // 逆向回调接口结果的加签
  519. $param = [
  520. 'app_id' => $callbackData['app_id'],
  521. 'timestamp' => $callbackData['timestamp'],
  522. 'business_data' => $callbackData['business_data']
  523. ];
  524. $fa = new \common\components\delivery\services\adapter\FengniaoAdapter();
  525. $paramSign = $fa->generateSignature($param);
  526. if($paramSign == $callbackData['signature']){
  527. $businessData = isset($callbackData['business_data']) ? $callbackData['business_data'] : '';
  528. if(empty($businessData)){
  529. $this->asJson(['return_code' => -1, 'return_msg' => 'business data is empty']);
  530. }
  531. $businessData = json_decode($businessData, true);
  532. $cbnObj = new \common\components\delivery\platform\fengniao\CallBackNotify();
  533. $ret = $cbnObj->handle($businessData['callback_business_type'], $businessData['param']);
  534. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  535. }else{
  536. Yii::error('蜂鸟回调接口签名出错');
  537. }
  538. }
  539. /**
  540. * 货拉拉回调接口
  541. */
  542. public function actionHuolalaCallback()
  543. {
  544. $post = Yii::$app->request->post();
  545. Yii::info('货拉拉回调接口post:' . json_encode($post, JSON_UNESCAPED_UNICODE), 'delivery');
  546. $get = Yii::$app->request->get();
  547. Yii::info('货拉拉回调接口get:' . json_encode($get, JSON_UNESCAPED_UNICODE), 'delivery');
  548. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  549. }
  550. /**
  551. * 顺丰回调接口
  552. */
  553. public function actionShunfengCallback()
  554. {
  555. $post = Yii::$app->request->post();
  556. $cbh = new \common\components\delivery\platform\shunfeng\CallBackHandler();
  557. $ret = $cbh->handle($post);// 1.配送状态更改 2.订单完成 3.顺丰原因取消 4.订单配送异常 5.骑士撤单 6.自动建店回调
  558. if($ret) {
  559. return $this->asJson(['error_code' => 0, 'error_msg' => 'success']);
  560. } else {
  561. Yii::error('顺丰回调失败: ' . json_encode($post, JSON_UNESCAPED_UNICODE));
  562. return $this->asJson(['error_code' => -1, 'error_msg' => 'failed']);
  563. }
  564. }
  565. // ---------------------------------------------------- 授权回调接口 ----------------------------------------------------------
  566. /**
  567. * 闪送授权回调接口
  568. */
  569. public function actionShansongAuthCallback()
  570. {
  571. $code = Yii::$app->request->get('code');
  572. $state = Yii::$app->request->get('state');
  573. $shopId = Yii::$app->request->get('shopId');
  574. $isAllStoreAuth = Yii::$app->request->get('isAllStoreAuth');
  575. $thirdStoreId = Yii::$app->request->get('thirdStoreId');
  576. $storeId = Yii::$app->request->get('storeId');
  577. // 验证state参数(防止CSRF)
  578. //if ($state != $yourUserId) {
  579. //throw new \yii\web\BadRequestHttpException('Invalid state parameter');
  580. //}
  581. // 获取AccessToken
  582. $auth = new \common\components\delivery\platform\shansong\Auth();
  583. $result = $auth->getAccessToken($code);
  584. if (!$result['success']) {
  585. throw new \yii\web\HttpException(500, '获取授权失败:' . $result['error']);
  586. }
  587. // 保存授权信息到数据库
  588. $data = [
  589. 'mainId' => $state,
  590. 'shopId' => $shopId, //闪送商户ID
  591. 'accessToken' => $result['access_token'],
  592. 'refreshToken' => $result['refresh_token'],
  593. 'expiresAt' => time() + $result['expires_in'],
  594. 'authType' => $isAllStoreAuth ? 'all_store' : 'single_store',
  595. 'platform' => 'shansong'
  596. ];
  597. if (!$isAllStoreAuth) {
  598. $data['third_store_id'] = $thirdStoreId;
  599. $data['shans_store_id'] = $storeId;
  600. }
  601. DeliveryAuthTokenClass::add($data);
  602. if (false) {
  603. throw new \yii\web\HttpException(500, '保存授权信息失败');
  604. }
  605. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  606. }
  607. /**
  608. * 货拉拉授权回调接口
  609. */
  610. public function actionHuolalaAuthCallback()
  611. {
  612. $get = Yii::$app->request->get();
  613. $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
  614. Yii::info($getData);
  615. $code = $get['code'];
  616. $mainId = $get['mainId'];
  617. // 获取AccessToken
  618. $auth = new \common\components\delivery\platform\huolala\Auth();
  619. $result = $auth->getAccessToken($code);
  620. if (!$result['success']) {
  621. throw new \yii\web\HttpException(500, '获取授权失败:' . $result['error']);
  622. }
  623. // 保存授权信息到数据库
  624. $data = [
  625. 'mainId' => $mainId,
  626. 'shopId' => '',
  627. 'accessToken' => $result['access_token'],
  628. 'refreshToken' => $result['refresh_token'],
  629. 'expiresAt' => strtotime($result['auth_end_time']),
  630. 'authType' => 'all_store',
  631. 'platform' => 'huolala'
  632. ];
  633. DeliveryAuthTokenClass::add($data);
  634. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  635. }
  636. /**
  637. * 蜂鸟授权回调接口
  638. */
  639. public function actionFengniaoAuthCallback()
  640. {
  641. $get = Yii::$app->request->get();
  642. $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
  643. Yii::info($getData);
  644. $callbackData = Yii::$app->request->post();
  645. if (empty($callbackData)) {
  646. $postStr = file_get_contents('php://input');
  647. $callbackData = json_decode($postStr, true);
  648. if (empty($callbackData)) {
  649. Yii::info('回调请求的数据为空');
  650. }
  651. }
  652. Yii::info('蜂鸟授权回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'fengniao');
  653. $mainId = $get['mainId'];
  654. $code = $get['code'];
  655. $merchantId = $get['merchant_id'];
  656. $scope = $get['scope'];
  657. $state = $get['state'];
  658. // 获取AccessToken
  659. $auth = new \common\components\delivery\platform\fengniao\Auth();
  660. $result = $auth->getAccessToken($code, $merchantId);
  661. if (!$result['success']) {
  662. throw new \yii\web\HttpException(500, '获取授权失败:' . $result['error']);
  663. }
  664. $result = $result['data'];
  665. // 保存授权信息到数据库
  666. $data = [
  667. 'mainId' => $mainId,
  668. 'shopId' => '',
  669. 'accessToken' => $result['access_token'],
  670. 'refreshToken' => $result['refresh_token'],
  671. 'expiresAt' => $result['expire_in'],
  672. 'authType' => 'all_store',
  673. 'platform' => 'fengniao'
  674. ];
  675. DeliveryAuthTokenClass::add($data);
  676. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  677. }
  678. /**
  679. * 达达授权回调接口
  680. */
  681. public function actionDadaAuthCallback()
  682. {
  683. $get = Yii::$app->request->get();
  684. $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
  685. Yii::info($getData);
  686. $callbackData = Yii::$app->request->post();
  687. if (empty($callbackData)) {
  688. $postStr = file_get_contents('php://input');
  689. $callbackData = json_decode($postStr, true);
  690. if (empty($callbackData)) {
  691. Yii::info('回调请求的数据为空');
  692. }
  693. }
  694. Yii::info('达达授权回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'dada');
  695. }
  696. /**
  697. * 顺丰授权与取消授权回调接口
  698. */
  699. public function actionShunfengAuthCallback()
  700. {
  701. $get = Yii::$app->request->get();
  702. $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
  703. Yii::info($getData);
  704. $callbackData = Yii::$app->request->post();
  705. if (empty($callbackData)) {
  706. $postStr = file_get_contents('php://input');
  707. $callbackData = json_decode($postStr, true);
  708. if (empty($callbackData)) {
  709. Yii::info('回调请求的数据为空');
  710. }
  711. }
  712. Yii::info('顺丰授权与取消授权回调接口:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'dada');
  713. }
  714. }