DeliveryController.php 24 KB

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