DeliveryController.php 25 KB

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