DeliveryController.php 25 KB

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