DeliveryController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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\OrderDetailForm;
  12. use Yii;
  13. /**
  14. * 聚合配送(自配 + 聚合动力)
  15. * Class DeliveryController
  16. * @package ghs\controllers
  17. */
  18. class DeliveryController extends BaseController
  19. {
  20. public $guestAccess = ['callback', 'shansong-auth-callback', 'huolala-auth-callback', 'fengniao-callback'];
  21. public function actionList()
  22. {
  23. $platformList = [];
  24. $platformLogos = [
  25. 'data' => ['name'=>'达达', 'logo'=>'📦'],
  26. 'shunfeng' => ['name'=>'顺丰同城', 'logo'=>'🚚'],
  27. 'meituan' => ['name'=>'美团', 'logo'=>'🍱'] ,
  28. 'fengniao' => ['name'=>'蜂鸟', 'logo'=>'🍜'],
  29. 'huolala' => ['name'=>'货拉拉', 'logo'=>'🚛'],
  30. 'shansong' => ['name'=>'闪送', 'logo'=>'⚡'],
  31. 'didi' => ['name'=>'滴滴', 'logo'=>'🚗'],
  32. 'uu' => ['name'=>'UU跑腿', 'logo'=>'🛵']
  33. ];
  34. $mainId = $this->mainId;
  35. $allDeliveries = DeliveryAuthTokenClass::getAllByCondition(['mainId'=>$mainId]);
  36. foreach($allDeliveries as $d){
  37. $item = [
  38. 'id'=>$d['platform'],
  39. 'logo'=>$platformLogos[$d['platform']]['logo'],
  40. 'name'=>$platformLogos[$d['platform']]['name'],
  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 actionCancelAuth()
  131. {
  132. $platform = Yii::$app->request->get('platform');
  133. $ret = DeliveryAuthTokenClass::deleteByCondition(['mainId'=>$this->mainId, 'platform'=>$platform]);
  134. if($ret > 0) {
  135. util::success(['message'=>'取消授权成功']);
  136. } else {
  137. util::fail('取消授权失败');
  138. }
  139. }
  140. public function actionHuolalaVehicle()
  141. {
  142. $mainId = $this->mainId;
  143. $authPlatforms = DeliveryAuthTokenClass::getByCondition(['mainId'=>$mainId, 'platform'=>'huolala']);
  144. $huolalaAdapter = new HuolalaAdapter($authPlatforms['access_token']);
  145. $cities = $huolalaAdapter->getCityVehicleList(1006);
  146. return $this->asJson($cities);
  147. }
  148. // 查询开通城市
  149. public function actionOpenCitiesLists()
  150. {
  151. $platform = Yii::$app->request->get('platform');
  152. $ds = new DispatchService($this->mainId);
  153. $cities = $ds->openCitiesLists($platform);
  154. if ($platform == 'shansong') {
  155. $newCitiesArr = [];
  156. $citiesArr = $cities['data'];
  157. foreach($citiesArr as $block) {
  158. foreach ($block['cities'] as $city) {
  159. $newCitiesArr[$city['name']] = $city;
  160. }
  161. }
  162. // 把newCitiesArr保存到 platform/huolala/cities.php 文件中
  163. $citiesFile = Yii::getAlias('@common/components/delivery/platform/shansong/cities.php');
  164. // 如果文件不存在,则创建文件
  165. if (!file_exists($citiesFile)) {
  166. file_put_contents($citiesFile, '<?php return []; ?>');
  167. }
  168. // 文件内容要包含命名空间
  169. $content = '<?php return ' . var_export($newCitiesArr, true) . '; ?>';
  170. file_put_contents($citiesFile, $content);
  171. return $newCitiesArr;
  172. } else if ($platform == 'huolala') {
  173. $citiesArr = $cities['data']['city_list'];
  174. $newCitiesArr = [
  175. 'expire' => $cities['data']['expire'],
  176. ];
  177. foreach($citiesArr as $city) {
  178. $newCitiesArr[$city['name']] = [
  179. 'city_id' => $city['city_id'],
  180. 'city_name' => $city['name'],
  181. ];
  182. }
  183. // 把newCitiesArr保存到 platform/huolala/cities.php 文件中
  184. $citiesFile = Yii::getAlias('@common/components/delivery/platform/huolala/cities.php');
  185. // 如果文件不存在,则创建文件
  186. if (!file_exists($citiesFile)) {
  187. file_put_contents($citiesFile, '<?php namespace common\components\delivery\platform\huolala; return []; ?>');
  188. }
  189. // 文件内容要包含命名空间
  190. $content = '<?php namespace common\components\delivery\platform\huolala; return ' . var_export($newCitiesArr, true) . '; ?>';
  191. file_put_contents($citiesFile, $content);
  192. return $newCitiesArr;
  193. } else {
  194. return $cities;
  195. }
  196. return $cities;
  197. }
  198. public function actionAddress()
  199. {
  200. $post = Yii::$app->request->post();
  201. $adminId = $this->adminId;
  202. util::checkRepeatCommit($adminId, 4);
  203. $orderId = intval($post['orderId']);
  204. $order = OrderClass::getById($orderId, false, 'fullAddress');
  205. $shop = ShopClass::getById($this->shopId, false, 'fullAddress');
  206. $ret = [
  207. "send_address" => $shop['fullAddress'],
  208. "receive_address" => $order['fullAddress']
  209. ];
  210. util::success($ret, "success");
  211. }
  212. // 获取多个平台报价
  213. public function actionAllDeliveryQuotes()
  214. {
  215. $post = Yii::$app->request->post();
  216. $orderTime = $post['pickupTime'];
  217. $adminId = $this->adminId;
  218. util::checkRepeatCommit($adminId, 4);
  219. $orderId = intval($post['orderId']);
  220. $order = OrderClass::getById($orderId);
  221. $shop = ShopClass::getById($this->shopId);
  222. $ds = new DispatchService($this->mainId);
  223. $platforms = $ds->getAllPlatformPrice($order, $shop, $orderTime);
  224. $deliveryList = [];
  225. foreach ($platforms['quotes'] as $item) {
  226. switch ($item['platform']) {
  227. case 'shansong':
  228. $deliveryList[] = [
  229. 'name' => '闪送',
  230. 'en_name' => 'shansong',
  231. 'price' => $item['total_amount'],
  232. 'distance' => $item['total_distance'],
  233. 'type' => '',
  234. 'issOrderNo' => $item['order_number'] //
  235. ];
  236. break;
  237. case 'fengniao':
  238. // 循环遍历 goods_infos 数组,提取有效项的信息
  239. $validGoods = [];
  240. if (!empty($item['goods_infos']) && is_array($item['goods_infos'])) {
  241. foreach ($item['goods_infos'] as $good) {
  242. if (!empty($good['is_valid']) && $good['is_valid'] == 1) {
  243. $validGoods[] = [
  244. 'actual_delivery_amount_cent' => $good['actual_delivery_amount_cent'] ?? 0,
  245. 'service_goods_id' => $good['service_goods_id'] ?? ''
  246. ];
  247. }
  248. }
  249. }
  250. $deliveryList[] = [
  251. 'name' => '蜂鸟',
  252. 'en_name' => 'fengniao',
  253. 'price' => !empty($validGoods) ? $validGoods[0]['actual_delivery_amount_cent'] : 0,
  254. 'distance' => $item['distance'],
  255. 'valid_goods' => $validGoods, // 返回所有有效的商品信息
  256. 'type' => ''
  257. ];
  258. break;
  259. case 'huolala':
  260. $deliveryList = [];
  261. foreach($item['price_info_list'] as $arr) {
  262. $priceInfo = $arr['calculate_price_info'];
  263. $vehicleType = $arr['vehicle_type'];
  264. $deliveryList[] = [
  265. 'name' => '货拉拉' . ' (' . $vehicleType['_meta']['vehicle_type'] . ')',
  266. 'en_name' => 'huolala',
  267. 'price' => $priceInfo['price_conditions'][0]['price_info']['total_price'],
  268. 'distance' => $priceInfo['distance_info']['distance_total'],
  269. 'type' => $vehicleType['_meta']['vehicle_type'],
  270. 'order_vehicle_id' => $vehicleType['order_vehicle_id'],
  271. 'city_id' => $item['city_id'],
  272. 'city_info_revision' => $vehicleType['city_info_revision'],
  273. // // 下单需要透传的数据
  274. 'vehicle_std' => $vehicleType['vehicle_std'],
  275. 'spec_req' => $vehicleType['spec_req'],
  276. 'price_calculate_id' => $priceInfo['price_calculate_id'],
  277. 'price_item_encryption' => $priceInfo['price_conditions'][0]['price_item_encryption'],
  278. 'vehicle_attr' => $priceInfo['vehicle_info']['vehicle_attr'],
  279. 'commodity_info' => $priceInfo['price_conditions'][0]['commodity_item'],
  280. ];
  281. }
  282. break;
  283. }
  284. }
  285. $ret['deliveryList'] = $deliveryList;
  286. util::success($ret, "success");
  287. }
  288. // 创建订单(真实下单)
  289. public function actionCreateOrder()
  290. {
  291. // $form = new CreateOrderForm();
  292. // $form->loadAndValidate();
  293. //
  294. // $post = $form->getAttributes();
  295. $post = Yii::$app->request->post();
  296. $orderId = $post['orderId']; // xhGhsOrder.id
  297. $params = $post['allParams'];
  298. $platform = $params['en_name'];// $post['platform'] 是中文
  299. $pickupTime = $post['pickupTime'];
  300. $params['mainId'] = $this->mainId;
  301. $params['ip'] = Yii::$app->request->userIP;
  302. $params['remark'] = $post['remark'];
  303. $params['total_price_fen'] = $post['price'];
  304. $params['order_time'] = $pickupTime['value'];
  305. $shopId = $this->shopId;
  306. $ghsOrder = OrderClass::getById($orderId, true);
  307. // if ($ghsOrder['status'] != 2) {
  308. // util::fail('订单状态不是待配送');
  309. // // }
  310. // if(!in_array($ghsOrder['sendStatus'], [-4, -1])) {
  311. // Yii::error('订单已经发过跑腿');
  312. // util::fail('订单已经发过跑腿');
  313. // }
  314. $ds = new DispatchService($this->mainId, $platform);
  315. $ret = $ds->createOrder($ghsOrder, $shopId, $params);
  316. if (isset($ret['code']) && $ret['code'] == 0) {
  317. $gdo = GhsDeliveryOrderClass::getByCondition(['mainId'=>$this->mainId, 'orderId'=>$ret['data']['order_id']]);
  318. if (empty($gdo)) {
  319. $data = [
  320. 'mainId' => $this->mainId,
  321. 'shopId' => $this->shopId,
  322. 'deliveryId' => $ret['platform'],
  323. 'ghsOrderId' => $orderId,
  324. 'orderId' => $ret['data']['order_id'],
  325. 'distance' => $ret['data']['distance'],
  326. 'fee' => $ret['data']['fee'],
  327. 'orderStatus' => 0
  328. ];
  329. GhsDeliveryOrderClass::add($data);
  330. } else {
  331. Yii::info('重复创建订单');
  332. }
  333. //更新订单
  334. $ghsOrder->sendDistance = $ret['data']['distance'];
  335. $ghsOrder->sendStatus = 0;
  336. $ghsOrder->deliveryId = $ret['platform'];
  337. $ghsOrder->sendType = 2;// 2指跑腿
  338. $ghsOrder->save();
  339. util::success($ret, "success");
  340. }
  341. util::fail('创建跑腿失败');
  342. }
  343. /**
  344. * 查询订单详情
  345. *
  346. * 1. 蜂鸟平台 orderId 可以使用 "XSD_CS26322799" (即:表 xhGhsOrder.orderSn)
  347. */
  348. public function actionOrderDetail()
  349. {
  350. $form = new OrderDetailForm();
  351. $form->loadAndValidate();
  352. $post = $form->getAttributes();
  353. $platform = $post['platform'];
  354. $orderId = $post['orderId'];
  355. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : '';
  356. $ds = new DispatchService($this->mainId, $platform);
  357. $ret = $ds->selectOrder($orderId, $orderSn);
  358. $return = isset($ret['ret']) ? $ret['ret'] : $ret['code']; // 各平台兼容处理
  359. if($return == 0) {
  360. util::success($ret, "success");
  361. }
  362. util::fail('查询失败');
  363. }
  364. // 取消订单
  365. public function actionCancelOrder()
  366. {
  367. $form = new CancelOrderForm();
  368. $form->loadAndValidate();
  369. $post = $form->getAttributes();
  370. $orderId = $post['orderId'] ?? 0; // 平台的orderId,
  371. $platform = $post['platform'] ?? '';
  372. $ghsOrderId = $post['ghsOrderId'];
  373. // 先获取配送订单,确保 $gdo 始终可用
  374. $gdo = GhsDeliveryOrderClass::getByCondition(['mainId'=>$this->mainId, 'ghsOrderId'=>$ghsOrderId, 'orderStatus!='=> -1], true);
  375. if(empty($gdo)) {
  376. util::fail('未找到对应订单: ' . $ghsOrderId);
  377. }
  378. // 如果没有提供平台和订单ID,从配送订单中获取
  379. if($platform == '' || $orderId == 0) {
  380. $orderId = $gdo->orderId;
  381. $platform = $gdo->deliveryId;
  382. }
  383. $ds = new DispatchService($this->mainId, $platform);
  384. $cancelCostCent = 0;
  385. if($platform == 'fengniao') {
  386. //请求订单预取消接口,获取是否可取消。如果运单状态不允许取消,则直接返回
  387. $cancelParams = ['order_id'=>$orderId, 'order_cancel_code'=>$post['order_cancel_code']];
  388. $res = $ds->getAdapter()->preCancelOrder($cancelParams);
  389. if($res['code'] != 0){
  390. util::fail('运单状态不允许取消');
  391. }
  392. $cancelCostCent = $res['data']['actual_cancel_cost_cent']; //取消实际需金额(单位:分)
  393. }
  394. $post['order_cancel_role'] = 1; //1:商户取消, 2:用户取消
  395. $ret = $ds->cancelOrder($orderId, $post);
  396. if($ret['code'] == 0) {
  397. // 更新跑腿订单与批发订单状态
  398. $gdo->orderStatus = -1;
  399. $gdo->cancelCost = $cancelCostCent;
  400. if(!$gdo->save()) {
  401. Yii::error('保存配送订单失败: ' . json_encode($gdo->errors));
  402. util::fail('保存配送订单失败');
  403. }
  404. $ghsOrder = OrderClass::getById($ghsOrderId, true, 'id, sendDistance, sendStatus, deliveryId');
  405. $ghsOrder->sendDistance = 0;
  406. $ghsOrder->sendStatus = -4;
  407. $ghsOrder->deliveryId = '';
  408. if(!$ghsOrder->save()) {
  409. Yii::error('保存批发订单失败: ' . json_encode($ghsOrder->errors));
  410. util::fail('保存批发订单失败');
  411. }
  412. util::success($ret['data'], "success");
  413. }
  414. util::fail('取消失败');
  415. }
  416. /**
  417. * 获取订单取消原因
  418. */
  419. public function actionCancelReason()
  420. {
  421. $post = Yii::$app->request->post();
  422. $ghsOrderId = $post['ghsOrderId'];
  423. $platform = $post['platform'];
  424. $gdo = GhsDeliveryOrderClass::getByCondition(['mainId'=>$this->mainId, 'ghsOrderId'=>$ghsOrderId, 'orderStatus!='=>-1], true);
  425. if(!empty($gdo)) {
  426. $orderId = $gdo->orderId;
  427. //$platform = $gdo->deliveryId;
  428. } else {
  429. util::fail('未找到对应订单: ' . $ghsOrderId);
  430. }
  431. $ds = new DispatchService($this->mainId, $platform);
  432. $ret = $ds->getCancelReasonList($orderId);
  433. util::success($ret);
  434. }
  435. // 第三方回调入口
  436. /**
  437. * 回调接口
  438. */
  439. public function actionCallback()
  440. {
  441. $callbackData = Yii::$app->request->post();
  442. if (empty($callbackData)) {
  443. $postStr = file_get_contents('php://input');
  444. $callbackData = json_decode($postStr, true);
  445. if (empty($callbackData)) {
  446. util::fail('回调请求的数据为空');
  447. }
  448. }
  449. // 从配置中获取安全token
  450. // $token = Yii::$app->params['wx_intracity_token'] ?? 'your_token_here';
  451. // $result = IntraCityExpress::handleOrderCallback($callbackData, $token);
  452. // 记录回调日志
  453. Yii::info('聚合配送回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'delivery');
  454. }
  455. /**
  456. * 闪送回调接口
  457. */
  458. public function actionShansongCallback()
  459. {
  460. $callbackData = Yii::$app->request->post();
  461. if (empty($callbackData)) {
  462. $postStr = file_get_contents('php://input');
  463. $callbackData = json_decode($postStr, true);
  464. }
  465. Yii::info('shansong 回调 -- post: ' . json_encode($callbackData));
  466. $get = Yii::$app->request->get();
  467. Yii::info('shansong 回调 -- get: ' . json_encode($get));
  468. }
  469. /**
  470. * 蜂鸟回调接口
  471. */
  472. public function actionFengniaoCallback()
  473. {
  474. $callbackData = Yii::$app->request->post();
  475. if (empty($callbackData)) {
  476. $postStr = file_get_contents('php://input');
  477. $callbackData = json_decode($postStr, true);
  478. }
  479. Yii::info('fengniao 回调 -- post: ' . json_encode($callbackData));
  480. $get = Yii::$app->request->get();
  481. Yii::info('fengniao 回调 -- get: ' . json_encode($get));
  482. $fa = new \common\components\delivery\services\adapter\FengniaoAdapter();
  483. $all_sign = $fa->second_geneSignature($callbackData);
  484. $arr = json_decode($callbackData['business_data'], true);
  485. $sec_sign = $fa->second_geneSignature($arr);
  486. Yii::info('fengniao sign: ' . $all_sign . ', $sec_sign: ' . $sec_sign);
  487. // 逆向回调接口结果的加签
  488. $param = [
  489. 'app_id' => $callbackData['app_id'],
  490. 'timestamp' => $callbackData['timestamp'],
  491. 'business_data' => $callbackData['business_data']
  492. ];
  493. $paramSign_1 = $fa->second_geneSignature($param);
  494. $paramSign_2 = $fa->generateSignature($param);
  495. Yii::info('$paramSign_1 sign: ' . $paramSign_1 . ', $paramSign_2: ' . $paramSign_2);
  496. util::complete('蜂鸟回调接口');
  497. }
  498. /**
  499. * 闪送授权回调接口
  500. */
  501. public function actionShansongAuthCallback()
  502. {
  503. $code = Yii::$app->request->get('code');
  504. $state = Yii::$app->request->get('state');
  505. $shopId = Yii::$app->request->get('shopId');
  506. $isAllStoreAuth = Yii::$app->request->get('isAllStoreAuth');
  507. $thirdStoreId = Yii::$app->request->get('thirdStoreId');
  508. $storeId = Yii::$app->request->get('storeId');
  509. // 验证state参数(防止CSRF)
  510. //if ($state != $yourUserId) {
  511. //throw new \yii\web\BadRequestHttpException('Invalid state parameter');
  512. //}
  513. // 获取AccessToken
  514. $auth = new \common\components\delivery\platform\shansong\Auth();
  515. $result = $auth->getAccessToken($code);
  516. if (!$result['success']) {
  517. throw new \yii\web\HttpException(500, '获取授权失败:' . $result['error']);
  518. }
  519. // 保存授权信息到数据库
  520. $data = [
  521. 'mainId' => $state,
  522. 'shopId' => $shopId, //闪送商户ID
  523. 'accessToken' => $result['access_token'],
  524. 'refreshToken' => $result['refresh_token'],
  525. 'expiresAt' => time() + $result['expires_in'],
  526. 'authType' => $isAllStoreAuth ? 'all_store' : 'single_store',
  527. 'platform' => 'shansong'
  528. ];
  529. if (!$isAllStoreAuth) {
  530. $data['third_store_id'] = $thirdStoreId;
  531. $data['shans_store_id'] = $storeId;
  532. }
  533. DeliveryAuthTokenClass::add($data);
  534. if (false) {
  535. throw new \yii\web\HttpException(500, '保存授权信息失败');
  536. }
  537. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  538. }
  539. /**
  540. * 货拉拉授权回调接口
  541. */
  542. public function actionHuolalaAuthCallback()
  543. {
  544. $get = Yii::$app->request->get();
  545. $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
  546. Yii::info($getData);
  547. $code = $get['code'];
  548. $mainId = $get['mainId'];
  549. // 获取AccessToken
  550. $auth = new \common\components\delivery\platform\huolala\Auth();
  551. $result = $auth->getAccessToken($code);
  552. if (!$result['success']) {
  553. throw new \yii\web\HttpException(500, '获取授权失败:' . $result['error']);
  554. }
  555. // 保存授权信息到数据库
  556. $data = [
  557. 'mainId' => $mainId,
  558. 'shopId' => '',
  559. 'accessToken' => $result['access_token'],
  560. 'refreshToken' => $result['refresh_token'],
  561. 'expiresAt' => strtotime($result['auth_end_time']),
  562. 'authType' => 'all_store',
  563. 'platform' => 'huolala'
  564. ];
  565. DeliveryAuthTokenClass::add($data);
  566. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  567. }
  568. /**
  569. * 蜂鸟授权回调接口
  570. */
  571. public function actionFengniaoAuthCallback()
  572. {
  573. $get = Yii::$app->request->get();
  574. $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
  575. Yii::info($getData);
  576. $callbackData = Yii::$app->request->post();
  577. if (empty($callbackData)) {
  578. $postStr = file_get_contents('php://input');
  579. $callbackData = json_decode($postStr, true);
  580. if (empty($callbackData)) {
  581. Yii::info('回调请求的数据为空');
  582. }
  583. }
  584. Yii::info('蜂鸟授权回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'fengniao');
  585. $mainId = $get['mainId'];
  586. $code = $get['code'];
  587. $merchantId = $get['merchant_id'];
  588. $scope = $get['scope'];
  589. $state = $get['state'];
  590. // 获取AccessToken
  591. $auth = new \common\components\delivery\platform\fengniao\Auth();
  592. $result = $auth->getAccessToken($code, $merchantId);
  593. if (!$result['success']) {
  594. throw new \yii\web\HttpException(500, '获取授权失败:' . $result['error']);
  595. }
  596. $result = $result['data'];
  597. // 保存授权信息到数据库
  598. $data = [
  599. 'mainId' => $mainId,
  600. 'shopId' => '',
  601. 'accessToken' => $result['access_token'],
  602. 'refreshToken' => $result['refresh_token'],
  603. 'expiresAt' => $result['expire_in'],
  604. 'authType' => 'all_store',
  605. 'platform' => 'fengniao'
  606. ];
  607. DeliveryAuthTokenClass::add($data);
  608. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  609. }
  610. /**
  611. * 达达授权回调接口
  612. */
  613. public function actionDadaAuthCallback()
  614. {
  615. $get = Yii::$app->request->get();
  616. $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
  617. Yii::info($getData);
  618. $callbackData = Yii::$app->request->post();
  619. if (empty($callbackData)) {
  620. $postStr = file_get_contents('php://input');
  621. $callbackData = json_decode($postStr, true);
  622. if (empty($callbackData)) {
  623. Yii::info('回调请求的数据为空');
  624. }
  625. }
  626. Yii::info('达达授权回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'dada');
  627. }
  628. }