DeliveryController.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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'];
  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. ];
  233. break;
  234. case 'shansong':
  235. $deliveryList[] = [
  236. 'name' => '闪送',
  237. 'en_name' => 'shansong',
  238. 'price' => $item['total_amount'],
  239. 'distance' => $item['total_distance'],
  240. 'type' => '',
  241. 'issOrderNo' => $item['order_number'] //
  242. ];
  243. break;
  244. case 'fengniao':
  245. // 循环遍历 goods_infos 数组,提取有效项的信息
  246. $validGoods = [];
  247. if (!empty($item['goods_infos']) && is_array($item['goods_infos'])) {
  248. foreach ($item['goods_infos'] as $good) {
  249. if (!empty($good['is_valid']) && $good['is_valid'] == 1) {
  250. $validGoods[] = [
  251. 'actual_delivery_amount_cent' => $good['actual_delivery_amount_cent'] ?? 0,
  252. 'service_goods_id' => $good['service_goods_id'] ?? ''
  253. ];
  254. }
  255. }
  256. }
  257. $deliveryList[] = [
  258. 'name' => '蜂鸟',
  259. 'en_name' => 'fengniao',
  260. 'price' => !empty($validGoods) ? $validGoods[0]['actual_delivery_amount_cent'] : 0,
  261. 'distance' => $item['distance'],
  262. 'valid_goods' => $validGoods, // 返回所有有效的商品信息
  263. 'type' => ''
  264. ];
  265. break;
  266. case 'huolala':
  267. $deliveryList = [];
  268. foreach($item['price_info_list'] as $arr) {
  269. $priceInfo = $arr['calculate_price_info'];
  270. $vehicleType = $arr['vehicle_type'];
  271. $deliveryList[] = [
  272. 'name' => '货拉拉' . ' (' . $vehicleType['_meta']['vehicle_type'] . ')',
  273. 'en_name' => 'huolala',
  274. 'price' => $priceInfo['price_conditions'][0]['price_info']['total_price'],
  275. 'distance' => $priceInfo['distance_info']['distance_total'],
  276. 'type' => $vehicleType['_meta']['vehicle_type'],
  277. 'order_vehicle_id' => $vehicleType['order_vehicle_id'],
  278. 'city_id' => $item['city_id'],
  279. 'city_info_revision' => $vehicleType['city_info_revision'],
  280. // // 下单需要透传的数据
  281. 'vehicle_std' => $vehicleType['vehicle_std'],
  282. 'spec_req' => $vehicleType['spec_req'],
  283. 'price_calculate_id' => $priceInfo['price_calculate_id'],
  284. 'price_item_encryption' => $priceInfo['price_conditions'][0]['price_item_encryption'],
  285. 'vehicle_attr' => $priceInfo['vehicle_info']['vehicle_attr'],
  286. 'commodity_info' => $priceInfo['price_conditions'][0]['commodity_item'],
  287. ];
  288. }
  289. break;
  290. }
  291. }
  292. $ret['deliveryList'] = $deliveryList;
  293. util::success($ret, "success");
  294. }
  295. // 创建订单(真实下单)
  296. public function actionCreateOrder()
  297. {
  298. $form = new CreateOrderForm();
  299. $form->loadAndValidate();
  300. $post = $form->getAttributes();
  301. //$post = Yii::$app->request->post();
  302. $orderId = $post['orderId']; // xhGhsOrder.id
  303. $params = $post['allParams'];
  304. $platform = $params['en_name'];// $post['platform'] 是中文
  305. $pickupTime = $post['pickupTime'];
  306. $params['mainId'] = $this->mainId;
  307. $params['ip'] = Yii::$app->request->userIP;
  308. $params['remark'] = $post['remark'];
  309. $params['total_price_fen'] = $post['price'];
  310. $params['order_time'] = $pickupTime['value'];
  311. $shopId = $this->shopId;
  312. $ghsOrder = OrderClass::getById($orderId, true);
  313. // if ($ghsOrder['status'] != 2) {
  314. // util::fail('订单状态不是待配送');
  315. // // }
  316. if(!in_array($ghsOrder['sendStatus'], [-4, -1])) {
  317. Yii::error('订单已经发过跑腿');
  318. util::fail('订单已经发过跑腿');
  319. }
  320. $ds = new DispatchService($this->mainId, $platform);
  321. $ret = $ds->createOrder($ghsOrder, $shopId, $params);
  322. if (isset($ret['code']) && $ret['code'] == 0) {
  323. $gdo = GhsDeliveryOrderClass::getByCondition(['mainId'=>$this->mainId, 'orderId'=>$ret['data']['order_id']]);
  324. if (empty($gdo)) {
  325. $data = [
  326. 'mainId' => $this->mainId,
  327. 'shopId' => $this->shopId,
  328. 'deliveryId' => $ret['platform'],
  329. 'ghsOrderId' => $orderId,
  330. 'orderId' => $ret['data']['order_id'],
  331. 'distance' => $ret['data']['distance'],
  332. 'fee' => $ret['data']['fee'],
  333. 'orderStatus' => 0
  334. ];
  335. GhsDeliveryOrderClass::add($data);
  336. } else {
  337. Yii::info('重复创建订单');
  338. }
  339. //更新订单
  340. $ghsOrder->sendDistance = $ret['data']['distance'];
  341. $ghsOrder->sendStatus = 0;
  342. $ghsOrder->deliveryId = $ret['platform'];
  343. $ghsOrder->sendType = 2;// 2指跑腿
  344. $ghsOrder->save();
  345. util::success($ret, "success");
  346. }
  347. util::fail('创建跑腿失败');
  348. }
  349. /**
  350. * 查询订单详情
  351. *
  352. * 1. 蜂鸟平台 orderId 可以使用 "XSD_CS26322799" (即:表 xhGhsOrder.orderSn)
  353. */
  354. public function actionOrderDetail()
  355. {
  356. $form = new OrderDetailForm();
  357. $form->loadAndValidate();
  358. $post = $form->getAttributes();
  359. $platform = $post['platform'];
  360. $orderId = $post['orderId'];
  361. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : '';
  362. $ds = new DispatchService($this->mainId, $platform);
  363. $ret = $ds->selectOrder($orderId, $orderSn);
  364. $return = isset($ret['ret']) ? $ret['ret'] : $ret['code']; // 各平台兼容处理
  365. if($return == 0) {
  366. util::success($ret, "success");
  367. }
  368. util::fail('查询失败');
  369. }
  370. // 取消订单
  371. public function actionCancelOrder()
  372. {
  373. $form = new CancelOrderForm();
  374. $form->loadAndValidate();
  375. $post = $form->getAttributes();
  376. $orderId = $post['orderId'] ?? 0; // 平台的orderId,
  377. $platform = $post['platform'] ?? '';
  378. $ghsOrderId = $post['ghsOrderId'];
  379. // 先获取配送订单,确保 $gdo 始终可用
  380. $gdo = GhsDeliveryOrderClass::getByCondition(['mainId'=>$this->mainId, 'ghsOrderId'=>$ghsOrderId, 'orderStatus!='=> -1], true);
  381. if(empty($gdo)) {
  382. util::fail('未找到对应订单: ' . $ghsOrderId);
  383. }
  384. // 如果没有提供平台和订单ID,从配送订单中获取
  385. if($platform == '' || $orderId == 0) {
  386. $orderId = $gdo->orderId;
  387. $platform = $gdo->deliveryId;
  388. }
  389. $ds = new DispatchService($this->mainId, $platform);
  390. $cancelCostCent = 0;
  391. if($platform == 'fengniao') {
  392. //请求订单预取消接口,获取是否可取消。如果运单状态不允许取消,则直接返回
  393. $cancelParams = ['order_id'=>$orderId, 'order_cancel_code'=>$post['order_cancel_code']];
  394. $res = $ds->getAdapter()->preCancelOrder($cancelParams);
  395. if($res['code'] != 0){
  396. util::fail('运单状态不允许取消');
  397. }
  398. $cancelCostCent = $res['data']['actual_cancel_cost_cent']; //取消实际需金额(单位:分)
  399. }
  400. $post['order_cancel_role'] = 1; //1:商户取消, 2:用户取消
  401. $ret = $ds->cancelOrder($orderId, $post);
  402. if($ret['code'] == 0) {
  403. // 更新跑腿订单与批发订单状态
  404. $gdo->orderStatus = -1;
  405. $gdo->cancelCost = $cancelCostCent;
  406. if(!$gdo->save()) {
  407. Yii::error('保存配送订单失败: ' . json_encode($gdo->errors));
  408. util::fail('保存配送订单失败');
  409. }
  410. $ghsOrder = OrderClass::getById($ghsOrderId, true, 'id, sendDistance, sendStatus, deliveryId');
  411. $ghsOrder->sendDistance = 0;
  412. $ghsOrder->sendStatus = -4;
  413. $ghsOrder->deliveryId = '';
  414. if(!$ghsOrder->save()) {
  415. Yii::error('保存批发订单失败: ' . json_encode($ghsOrder->errors));
  416. util::fail('保存批发订单失败');
  417. }
  418. util::success($ret['data'], "success");
  419. }
  420. util::fail('取消失败');
  421. }
  422. /**
  423. * 获取订单取消原因
  424. */
  425. public function actionCancelReason()
  426. {
  427. $post = Yii::$app->request->post();
  428. $ghsOrderId = $post['ghsOrderId'];
  429. $platform = $post['platform'];
  430. $gdo = GhsDeliveryOrderClass::getByCondition(['mainId'=>$this->mainId, 'ghsOrderId'=>$ghsOrderId, 'orderStatus!='=>-1], true);
  431. if(!empty($gdo)) {
  432. $orderId = $gdo->orderId;
  433. //$platform = $gdo->deliveryId;
  434. } else {
  435. util::fail('未找到对应订单: ' . $ghsOrderId);
  436. }
  437. $ds = new DispatchService($this->mainId, $platform);
  438. $ret = $ds->getCancelReasonList($orderId);
  439. util::success($ret);
  440. }
  441. // ---------------------------------------------------- 普通回调接口 ----------------------------------------------------------
  442. // 第三方回调入口
  443. /**
  444. * 回调接口
  445. */
  446. public function actionCallback()
  447. {
  448. $callbackData = Yii::$app->request->post();
  449. if (empty($callbackData)) {
  450. $postStr = file_get_contents('php://input');
  451. $callbackData = json_decode($postStr, true);
  452. if (empty($callbackData)) {
  453. util::fail('回调请求的数据为空');
  454. }
  455. }
  456. // 从配置中获取安全token
  457. // $token = Yii::$app->params['wx_intracity_token'] ?? 'your_token_here';
  458. // $result = IntraCityExpress::handleOrderCallback($callbackData, $token);
  459. // 记录回调日志
  460. Yii::info('聚合配送回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'delivery');
  461. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  462. }
  463. /**
  464. * 闪送回调接口
  465. * 回调返回的格式必须是{"status":200,"msg":"","data":null}格式,当status为200表示回调成功,否则,回调失败。当回调失败时,间隔一分钟重试一次,最多重试五次。
  466. */
  467. public function actionShansongCallback()
  468. {
  469. $callbackData = Yii::$app->request->post();
  470. if (empty($callbackData)) {
  471. $postStr = file_get_contents('php://input');
  472. $callbackData = json_decode($postStr, true);
  473. }
  474. Yii::info('shansong 回调 -- post: ' . json_encode($callbackData, JSON_UNESCAPED_UNICODE));
  475. $obj = new \common\components\delivery\platform\shansong\OrderStatusCallBack();
  476. $ret = $obj->handle($callbackData);
  477. if($ret) {
  478. return $this->asJson(['status' => 200, 'msg' => 'OK', "data"=> null]);
  479. } else {
  480. Yii::error('闪送回调失败: ' . json_encode($callbackData, JSON_UNESCAPED_UNICODE));
  481. return $this->asJson(['status' => 200, 'msg' => '回调失败', "data"=> null]);
  482. }
  483. }
  484. /**
  485. * 蜂鸟回调接口
  486. */
  487. public function actionFengniaoCallback()
  488. {
  489. $callbackData = Yii::$app->request->post();
  490. if (empty($callbackData)) {
  491. $postStr = file_get_contents('php://input');
  492. $callbackData = json_decode($postStr, true);
  493. }
  494. Yii::info('fengniao 回调 -- post: ' . json_encode($callbackData));
  495. // 逆向回调接口结果的加签
  496. $param = [
  497. 'app_id' => $callbackData['app_id'],
  498. 'timestamp' => $callbackData['timestamp'],
  499. 'business_data' => $callbackData['business_data']
  500. ];
  501. $fa = new \common\components\delivery\services\adapter\FengniaoAdapter();
  502. $paramSign = $fa->generateSignature($param);
  503. if($paramSign == $callbackData['signature']){
  504. $businessData = isset($callbackData['business_data']) ? $callbackData['business_data'] : '';
  505. if(empty($businessData)){
  506. $this->asJson(['return_code' => -1, 'return_msg' => 'business data is empty']);
  507. }
  508. $businessData = json_decode($businessData, true);
  509. $cbnObj = new \common\components\delivery\platform\fengniao\CallBackNotify();
  510. $ret = $cbnObj->handle($businessData['callback_business_type'], $businessData['param']);
  511. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  512. }else{
  513. Yii::error('蜂鸟回调接口签名出错');
  514. }
  515. }
  516. /**
  517. * 货拉拉回调接口
  518. */
  519. public function actionHuolalaCallback()
  520. {
  521. }
  522. /**
  523. * 顺丰回调接口
  524. */
  525. public function actionShunfengCallback()
  526. {
  527. // 1.配送状态更改 2.订单完成 3.顺丰原因取消 4.订单配送异常 5.骑士撤单 6.自动建店回调
  528. }
  529. // ---------------------------------------------------- 授权回调接口 ----------------------------------------------------------
  530. /**
  531. * 闪送授权回调接口
  532. */
  533. public function actionShansongAuthCallback()
  534. {
  535. $code = Yii::$app->request->get('code');
  536. $state = Yii::$app->request->get('state');
  537. $shopId = Yii::$app->request->get('shopId');
  538. $isAllStoreAuth = Yii::$app->request->get('isAllStoreAuth');
  539. $thirdStoreId = Yii::$app->request->get('thirdStoreId');
  540. $storeId = Yii::$app->request->get('storeId');
  541. // 验证state参数(防止CSRF)
  542. //if ($state != $yourUserId) {
  543. //throw new \yii\web\BadRequestHttpException('Invalid state parameter');
  544. //}
  545. // 获取AccessToken
  546. $auth = new \common\components\delivery\platform\shansong\Auth();
  547. $result = $auth->getAccessToken($code);
  548. if (!$result['success']) {
  549. throw new \yii\web\HttpException(500, '获取授权失败:' . $result['error']);
  550. }
  551. // 保存授权信息到数据库
  552. $data = [
  553. 'mainId' => $state,
  554. 'shopId' => $shopId, //闪送商户ID
  555. 'accessToken' => $result['access_token'],
  556. 'refreshToken' => $result['refresh_token'],
  557. 'expiresAt' => time() + $result['expires_in'],
  558. 'authType' => $isAllStoreAuth ? 'all_store' : 'single_store',
  559. 'platform' => 'shansong'
  560. ];
  561. if (!$isAllStoreAuth) {
  562. $data['third_store_id'] = $thirdStoreId;
  563. $data['shans_store_id'] = $storeId;
  564. }
  565. DeliveryAuthTokenClass::add($data);
  566. if (false) {
  567. throw new \yii\web\HttpException(500, '保存授权信息失败');
  568. }
  569. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  570. }
  571. /**
  572. * 货拉拉授权回调接口
  573. */
  574. public function actionHuolalaAuthCallback()
  575. {
  576. $get = Yii::$app->request->get();
  577. $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
  578. Yii::info($getData);
  579. $code = $get['code'];
  580. $mainId = $get['mainId'];
  581. // 获取AccessToken
  582. $auth = new \common\components\delivery\platform\huolala\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' => $mainId,
  590. 'shopId' => '',
  591. 'accessToken' => $result['access_token'],
  592. 'refreshToken' => $result['refresh_token'],
  593. 'expiresAt' => strtotime($result['auth_end_time']),
  594. 'authType' => 'all_store',
  595. 'platform' => 'huolala'
  596. ];
  597. DeliveryAuthTokenClass::add($data);
  598. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  599. }
  600. /**
  601. * 蜂鸟授权回调接口
  602. */
  603. public function actionFengniaoAuthCallback()
  604. {
  605. $get = Yii::$app->request->get();
  606. $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
  607. Yii::info($getData);
  608. $callbackData = Yii::$app->request->post();
  609. if (empty($callbackData)) {
  610. $postStr = file_get_contents('php://input');
  611. $callbackData = json_decode($postStr, true);
  612. if (empty($callbackData)) {
  613. Yii::info('回调请求的数据为空');
  614. }
  615. }
  616. Yii::info('蜂鸟授权回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'fengniao');
  617. $mainId = $get['mainId'];
  618. $code = $get['code'];
  619. $merchantId = $get['merchant_id'];
  620. $scope = $get['scope'];
  621. $state = $get['state'];
  622. // 获取AccessToken
  623. $auth = new \common\components\delivery\platform\fengniao\Auth();
  624. $result = $auth->getAccessToken($code, $merchantId);
  625. if (!$result['success']) {
  626. throw new \yii\web\HttpException(500, '获取授权失败:' . $result['error']);
  627. }
  628. $result = $result['data'];
  629. // 保存授权信息到数据库
  630. $data = [
  631. 'mainId' => $mainId,
  632. 'shopId' => '',
  633. 'accessToken' => $result['access_token'],
  634. 'refreshToken' => $result['refresh_token'],
  635. 'expiresAt' => $result['expire_in'],
  636. 'authType' => 'all_store',
  637. 'platform' => 'fengniao'
  638. ];
  639. DeliveryAuthTokenClass::add($data);
  640. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  641. }
  642. /**
  643. * 达达授权回调接口
  644. */
  645. public function actionDadaAuthCallback()
  646. {
  647. $get = Yii::$app->request->get();
  648. $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
  649. Yii::info($getData);
  650. $callbackData = Yii::$app->request->post();
  651. if (empty($callbackData)) {
  652. $postStr = file_get_contents('php://input');
  653. $callbackData = json_decode($postStr, true);
  654. if (empty($callbackData)) {
  655. Yii::info('回调请求的数据为空');
  656. }
  657. }
  658. Yii::info('达达授权回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'dada');
  659. }
  660. /**
  661. * 顺丰授权与取消授权回调接口
  662. */
  663. public function actionShunfengAuthCallback()
  664. {
  665. $get = Yii::$app->request->get();
  666. $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
  667. Yii::info($getData);
  668. $callbackData = Yii::$app->request->post();
  669. if (empty($callbackData)) {
  670. $postStr = file_get_contents('php://input');
  671. $callbackData = json_decode($postStr, true);
  672. if (empty($callbackData)) {
  673. Yii::info('回调请求的数据为空');
  674. }
  675. }
  676. Yii::info('顺丰授权与取消授权回调接口:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'dada');
  677. }
  678. }