DeliveryController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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\noticeUtil;
  9. use common\components\util;
  10. use common\components\delivery\services\DispatchService;
  11. use ghs\models\delivery\CancelOrderForm;
  12. use ghs\models\delivery\CreateOrderForm;
  13. use ghs\models\delivery\OrderDetailForm;
  14. use Yii;
  15. use yii\web\Response;
  16. use yii\helpers\ArrayHelper;
  17. /**
  18. * 聚合配送(自配 + 聚合动力)
  19. * Class DeliveryController
  20. * @package ghs\controllers
  21. */
  22. class DeliveryController extends BaseController
  23. {
  24. public $guestAccess = ['callback', 'shansong-auth-callback', 'huolala-auth-callback',
  25. 'fengniao-callback', 'shansong-callback', 'huolala-callback', 'shunfeng-callback'];
  26. public function actionList()
  27. {
  28. $platformList = [];
  29. $platformLogos = [
  30. 'data' => ['name'=>'达达', 'logo'=>'📦'],
  31. 'shunfeng' => ['name'=>'顺丰同城', 'logo'=>'🚚'],
  32. 'meituan' => ['name'=>'美团', 'logo'=>'🍱'] ,
  33. 'fengniao' => ['name'=>'蜂鸟', 'logo'=>'🍜'],
  34. 'huolala' => ['name'=>'货拉拉跑腿', 'logo'=>'🚛'],
  35. 'shansong' => ['name'=>'闪送', 'logo'=>'⚡'],
  36. 'didi' => ['name'=>'滴滴', 'logo'=>'🚗'],
  37. 'uu' => ['name'=>'UU跑腿', 'logo'=>'🛵']
  38. ];
  39. $mainId = $this->mainId;
  40. $allDeliveries = DeliveryAuthTokenClass::getAllByCondition(['mainId'=>$mainId]);
  41. foreach($allDeliveries as $d){
  42. $item = [
  43. 'id'=>$d['platform'],
  44. 'logo'=>$platformLogos[$d['platform']]['logo'],
  45. 'name'=>$platformLogos[$d['platform']]['name'],
  46. 'is_authorized' => true,
  47. 'balance' => $d['balance']/100.0,
  48. 'access_type' => 'oauth'
  49. ];
  50. $platformList[] = $item;
  51. }
  52. util::success(['platformList'=>$platformList]);
  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. return $this->actionShunfengAuth();
  72. break;
  73. case 'didi':
  74. break;
  75. default:
  76. util::error(-1, $platform . '不存在');
  77. break;
  78. }
  79. }
  80. // ------------------ 授权(账号绑定) ---------------------
  81. //授权
  82. public function actionShansongAuth()
  83. {
  84. //闪送授权(商户授权:授权后能为商户下所有门店发单)
  85. $auth = new \common\components\delivery\platform\shansong\Auth();
  86. if(getenv('YII_ENV') == 'production') {
  87. $apiHost = Yii::$app->params['ghsHost'];
  88. } else {
  89. $apiHost = Yii::$app->params['ghsHost'];
  90. }
  91. $redirectUrl = $apiHost . '/delivery/shansong-auth-callback';
  92. $authUrl = $auth->generateMerchantAuthUrl($this->mainId, $redirectUrl);
  93. // 重定向用户
  94. //header('Location: ' . $authUrl);
  95. util::success(['url'=>$authUrl]);
  96. }
  97. public function actionHuolalaAuth()
  98. {
  99. $huoLalaAuth = new \common\components\delivery\platform\huolala\Auth();
  100. $mainId = $this->mainId;
  101. $apiHost = Yii::$app->params['ghsHost'];
  102. $redirectUrl = $apiHost . '/delivery/huolala-auth-callback' . '?mainId=' . $mainId;
  103. $authUrl = $huoLalaAuth->generateAuthUrl($redirectUrl);
  104. //header('Location: ' . $authUrl);
  105. util::success(['url'=>$authUrl]);
  106. }
  107. public function actionFengniaoAuth()
  108. {
  109. $fengnaioAuth = new \common\components\delivery\platform\fengniao\Auth();
  110. $mainId = $this->mainId;
  111. $redirectUrl = Yii::$app->params['ghsHost'] . '/delivery/fengniao-auth-callback' . '?mainId=' . $mainId;
  112. $authUrl = $fengnaioAuth->generateAuthUrl($redirectUrl);
  113. util::success(['url'=>$authUrl]);
  114. }
  115. public function actionDadaAuth()
  116. {
  117. $dadaAuth = new \common\components\delivery\platform\dada\Auth();
  118. $ticket = $dadaAuth->getTicket();
  119. $mainId = $this->mainId;
  120. $redirectUrl = Yii::$app->params['ghsHost'] . '/delivery/dada-auth-callback' . '?mainId=' . $mainId;
  121. $shopNumber = '40d8391f9b05477b';
  122. $state = 'huidiao_biaoshi';
  123. $authUrl = $dadaAuth->generateAuthUrl($ticket, $state, $shopNumber);
  124. //header('Location: ' . $authUrl);
  125. util::success(['url'=>$authUrl]);
  126. }
  127. public function actionShunfengAuth()
  128. {
  129. $shunfengAuth = new \common\components\delivery\platform\shunfeng\Auth();
  130. $mainId = $this->mainId;
  131. $authUrl = $shunfengAuth->generateAuthUrl($mainId);
  132. util::success(['url'=>$authUrl]);
  133. }
  134. public function actionCancelAuth()
  135. {
  136. // 有平台有对应的取消授权接口,有的没有。
  137. // 有的就对接下,然后再删除数据。没有的直接删除数据就行。
  138. $platform = Yii::$app->request->get('platform');
  139. $delivery = DeliveryAuthTokenClass::getByCondition(['mainId'=>$this->mainId, 'platform'=>$platform]);
  140. if($delivery) {
  141. if($platform == 'shansong') {
  142. $auth = new \common\components\delivery\platform\shansong\Auth();
  143. $result = $auth->cancelAuthorization($delivery['accessToken']);
  144. if($result['success']) {
  145. // 授权已取消
  146. }else{
  147. if($result['message'] == '商户未授权,请授权后再次尝试'){
  148. Yii::info($this->mainId . '--商户未授权,请授权后再次尝试');
  149. }else{
  150. util::fail('取消授权失败:'.$result['message']);
  151. }
  152. }
  153. }
  154. }else{
  155. util::fail('取消授权失败,数据未找到');
  156. }
  157. $ret = DeliveryAuthTokenClass::deleteByCondition(['mainId'=>$this->mainId, 'platform'=>$platform]);
  158. if($ret > 0){
  159. util::success(['message'=>'取消授权成功']);
  160. } else {
  161. util::fail('取消授权失败');
  162. }
  163. }
  164. // --------------------------------------------------------------------------------------------------------------
  165. public function actionHuolalaVehicle()
  166. {
  167. $mainId = $this->mainId;
  168. $authPlatforms = DeliveryAuthTokenClass::getByCondition(['mainId'=>$mainId, 'platform'=>'huolala']);
  169. $huolalaAdapter = new HuolalaAdapter($authPlatforms['access_token']);
  170. $cities = $huolalaAdapter->getCityVehicleList(1006);
  171. return $this->asJson($cities);
  172. }
  173. // 查询开通城市
  174. public function actionOpenCitiesLists()
  175. {
  176. $platform = Yii::$app->request->get('platform');
  177. $ds = new DispatchService($this->mainId);
  178. $cities = $ds->openCitiesLists($platform);
  179. if ($platform == 'shansong') {
  180. $newCitiesArr = [];
  181. $citiesArr = $cities['data'];
  182. foreach($citiesArr as $block) {
  183. foreach ($block['cities'] as $city) {
  184. $newCitiesArr[$city['name']] = $city;
  185. }
  186. }
  187. return $newCitiesArr;
  188. } else if ($platform == 'huolala') {
  189. $citiesArr = $cities['data']['city_list'];
  190. $newCitiesArr = [
  191. 'expire' => $cities['data']['expire'],
  192. ];
  193. foreach($citiesArr as $city) {
  194. $newCitiesArr[$city['name']] = [
  195. 'city_id' => $city['city_id'],
  196. 'city_name' => $city['name'],
  197. ];
  198. }
  199. return $newCitiesArr;
  200. }
  201. return $cities;
  202. }
  203. public function actionAddress()
  204. {
  205. $post = Yii::$app->request->post();
  206. $adminId = $this->adminId;
  207. util::checkRepeatCommit($adminId, 4);
  208. $orderId = intval($post['orderId']);
  209. $order = OrderClass::getById($orderId, false, 'fullAddress');
  210. $shop = ShopClass::getById($this->shopId, false, 'fullAddress');
  211. $ret = [
  212. "send_address" => $shop['fullAddress'],
  213. "receive_address" => $order['fullAddress']
  214. ];
  215. util::success($ret, "success");
  216. }
  217. // 获取多个平台报价
  218. public function actionAllDeliveryQuotes()
  219. {
  220. $post = Yii::$app->request->post();
  221. $orderTime = $post['pickupTime'];
  222. $weight = $post['weight'];
  223. $remark = $post['remark'] ?? '';
  224. $adminId = $this->adminId;
  225. util::checkRepeatCommit($adminId, 4);
  226. $orderId = intval($post['orderId']);
  227. $order = OrderClass::getById($orderId);
  228. $shop = ShopClass::getById($this->shopId);
  229. $order['weight'] = $weight;
  230. $order['remark'] = $remark;
  231. $ds = new DispatchService($this->mainId);
  232. $platformQuotes = $ds->getAllPlatformPrice($order, $shop, $orderTime);
  233. if(isset($platformQuotes['error'])){
  234. util::fail($platformQuotes['error']);
  235. }
  236. // 格式化各平台报价为前端展示格式
  237. $deliveryList = $ds->formatPlatformQuotesForDisplay($platformQuotes);
  238. // 按 price 从低到高排序
  239. ArrayHelper::multisort($deliveryList, 'price', SORT_ASC);
  240. $ret['deliveryList'] = $deliveryList;
  241. util::success($ret, "success");
  242. }
  243. // 创建订单(真实下单)
  244. public function actionCreateOrder()
  245. {
  246. $form = new CreateOrderForm();
  247. $form->loadAndValidate();
  248. $post = $form->getAttributes();
  249. $orderId = $post['orderId']; // xhGhsOrder.id
  250. $params = $post['allParams'];
  251. $platform = $params['en_name'];// $post['platform'] 是中文
  252. $pickupTime = $post['pickupTime'];
  253. $params['mainId'] = $this->mainId;
  254. $params['ip'] = Yii::$app->request->userIP;
  255. $params['remark'] = $post['remark'];
  256. $params['total_price_fen'] = $post['price'];
  257. $params['pickupTime'] = $pickupTime['value'];
  258. $shopId = $this->shopId;
  259. $ghsOrder = OrderClass::getById($orderId, true);
  260. // if ($ghsOrder['status'] != 2) {
  261. // util::fail('订单状态不是待配送');
  262. // // }
  263. if(!in_array($ghsOrder['sendStatus'], [-4, -1])) {
  264. Yii::error('订单已经发过跑腿');
  265. util::fail('订单已经发过跑腿');
  266. }
  267. $ds = new DispatchService($this->mainId, $platform);
  268. $ret = $ds->createOrder($ghsOrder, $shopId, $params);
  269. if (isset($ret['code']) && $ret['code'] == 0) {
  270. $gdo = GhsDeliveryOrderClass::getByCondition(['mainId'=>$this->mainId, 'orderId'=>$ret['data']['order_id']]);
  271. if (empty($gdo)) {
  272. $data = [
  273. 'mainId' => $this->mainId,
  274. 'shopId' => $this->shopId,
  275. 'deliveryId' => $ret['platform'],
  276. 'ghsOrderId' => $orderId,
  277. 'orderId' => $ret['data']['order_id'],
  278. 'distance' => $ret['data']['distance'],
  279. 'fee' => $ret['data']['fee'],
  280. 'orderStatus' => 0
  281. ];
  282. GhsDeliveryOrderClass::add($data);
  283. } else {
  284. Yii::info('重复创建订单');
  285. }
  286. //更新订单
  287. $ghsOrder->sendDistance = $ret['data']['distance'];
  288. $ghsOrder->sendStatus = 0;
  289. $ghsOrder->deliveryId = $ret['platform'];
  290. $ghsOrder->sendType = 2;// 2指跑腿
  291. $ghsOrder->save();
  292. OrderClass::hasSend($ghsOrder);// 更新 status
  293. util::success($ret, "success");
  294. } else {
  295. if(isset($ret['msg'])){
  296. util::fail($ret['msg']);
  297. }
  298. util::fail('创建跑腿失败');
  299. }
  300. }
  301. /**
  302. * 查询订单详情
  303. *
  304. * 1. 蜂鸟平台 orderId 可以使用 "XSD_CS26322799" (即:表 xhGhsOrder.orderSn)
  305. */
  306. public function actionOrderDetail()
  307. {
  308. $form = new OrderDetailForm();
  309. $form->loadAndValidate();
  310. $post = $form->getAttributes();
  311. $platform = $post['platform'];
  312. $orderId = $post['orderId'];
  313. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : '';
  314. $ds = new DispatchService($this->mainId, $platform);
  315. $ret = $ds->selectOrder($orderId, $orderSn);
  316. $return = isset($ret['ret']) ? $ret['ret'] : $ret['code']; // 各平台兼容处理
  317. if($return == 0) {
  318. util::success($ret, "success");
  319. }
  320. util::fail('查询失败');
  321. }
  322. // 取消订单
  323. public function actionCancelOrder()
  324. {
  325. $form = new CancelOrderForm();
  326. $form->loadAndValidate();
  327. $post = $form->getAttributes();
  328. $orderId = $post['orderId'] ?? 0; // 平台的orderId,
  329. $platform = $post['platform'] ?? '';
  330. $ghsOrderId = $post['ghsOrderId'];
  331. // 先获取配送订单,确保 $gdo 始终可用
  332. $gdo = GhsDeliveryOrderClass::getByCondition(['mainId'=>$this->mainId, 'ghsOrderId'=>$ghsOrderId, 'orderStatus!='=> -1], true);
  333. if(empty($gdo)) {
  334. util::fail('未找到对应订单: ' . $ghsOrderId);
  335. }
  336. // 如果没有提供平台和订单ID,从配送订单中获取
  337. if($platform == '' || $orderId == 0) {
  338. $orderId = $gdo->orderId;
  339. $platform = $gdo->deliveryId;
  340. }
  341. $ds = new DispatchService($this->mainId, $platform);
  342. $cancelCostCent = 0;
  343. if($platform == 'fengniao') {
  344. //请求订单预取消接口,获取是否可取消。如果运单状态不允许取消,则直接返回
  345. $cancelParams = ['order_id'=>$orderId, 'order_cancel_code'=>$post['order_cancel_code']];
  346. $res = $ds->getAdapter()->preCancelOrder($cancelParams);
  347. if($res['code'] != 0){
  348. util::fail('运单状态不允许取消');
  349. }
  350. $cancelCostCent = $res['data']['actual_cancel_cost_cent']; //取消实际需金额(单位:分)
  351. }
  352. if($platform == 'shunfeng') {
  353. //请求订单预取消接口,获取是否可取消。如果运单状态不允许取消,则直接返回
  354. $cancelParams = ['order_id'=>$orderId, 'order_cancel_code'=>$post['order_cancel_code']];
  355. $res = $ds->getAdapter()->preCancelOrder($cancelParams);
  356. if($res['code'] != 0){
  357. util::fail('运单状态不允许取消');
  358. }
  359. $post['order_type'] = 1; //查询订单ID类型 1、顺丰订单号 2、商家订单号
  360. }
  361. $post['order_cancel_role'] = 1; //1:商户取消, 2:用户取消
  362. $ret = $ds->cancelOrder($orderId, $post);
  363. if($ret['code'] == 0) {
  364. // 更新跑腿订单与批发订单状态
  365. $gdo->orderStatus = -1;
  366. $gdo->cancelCost = $ret['platform'] !== 'fengniao' ? $ret['data']['deductionFee'] : $cancelCostCent; // 由于蜂鸟平台返回的扣费不一样造成
  367. if(!$gdo->save()) {
  368. Yii::error('保存配送订单失败: ' . json_encode($gdo->errors));
  369. util::fail('保存配送订单失败');
  370. }
  371. $ghsOrder = OrderClass::getById($ghsOrderId, true, 'id, sendDistance, sendStatus, deliveryId');
  372. $ghsOrder->sendDistance = 0;
  373. $ghsOrder->sendStatus = -4;
  374. $ghsOrder->deliveryId = '';
  375. $ghsOrder->sendTip = 0;
  376. if(!$ghsOrder->save()) {
  377. Yii::error('保存批发订单失败: ' . json_encode($ghsOrder->errors));
  378. util::fail('保存批发订单失败');
  379. }
  380. util::success($ret['data'], "success");
  381. }
  382. util::fail('取消失败');
  383. }
  384. /**
  385. * 获取订单取消原因
  386. */
  387. public function actionCancelReason()
  388. {
  389. $post = Yii::$app->request->post();
  390. $ghsOrderId = $post['ghsOrderId'];
  391. $platform = $post['platform'];
  392. $gdo = GhsDeliveryOrderClass::getByCondition(['mainId'=>$this->mainId, 'ghsOrderId'=>$ghsOrderId, 'orderStatus!='=>-1], true);
  393. if(!empty($gdo)) {
  394. $orderId = $gdo->orderId;
  395. //$platform = $gdo->deliveryId;
  396. } else {
  397. util::fail('未找到对应订单: ' . $ghsOrderId);
  398. }
  399. $ds = new DispatchService($this->mainId, $platform);
  400. $ret = $ds->getCancelReasonList($orderId);
  401. if($ret['code'] == 0){
  402. util::success($ret['data']);
  403. }else{
  404. Yii::error('获取订单取消原因:'.json_encode($ret));
  405. util::fail($ret['msg']);
  406. }
  407. }
  408. /**
  409. * 添加小费
  410. */
  411. public function actionAddTip()
  412. {
  413. $post = Yii::$app->request->post();
  414. $ghsOrderId = $post['ghsOrderId'];
  415. $platform = $post['platform'];
  416. $tips = intval($post['tips']);
  417. if($tips < 0) {
  418. util::fail('小费金额至少为1元');
  419. }
  420. $tips *= 100;
  421. $gdo = GhsDeliveryOrderClass::getByCondition(['mainId'=>$this->mainId, 'ghsOrderId'=>$ghsOrderId, 'orderStatus'=>['in',[0,1,2,10]]], true);
  422. if($gdo == null){
  423. util::fail('当前配送状态无法加小费');
  424. }
  425. $ds = new DispatchService($this->mainId, $platform);
  426. $ret = $ds->addTip(['orderId'=>$gdo->orderId, 'tips'=>$tips]);
  427. if($ret['code'] == 0){
  428. $gdo->tip = $tips + $gdo->tip;
  429. $gdo->save();
  430. $orderObj = OrderClass::getById($gdo->ghsOrderId, true, 'id,sendTip');
  431. $orderObj->sendTip = $tips/100 + $orderObj->sendTip;
  432. $orderObj->save();
  433. util::success($ret['data']);
  434. }else{
  435. Yii::error('添加小费请求失败,请求数据是:'.json_encode($ret));
  436. util::fail($ret['msg']);
  437. }
  438. }
  439. // ---------------------------------------------------- 普通回调接口 ----------------------------------------------------------
  440. // 第三方回调入口
  441. /**
  442. * 回调接口
  443. */
  444. public function actionCallback()
  445. {
  446. $callbackData = Yii::$app->request->post();
  447. if (empty($callbackData)) {
  448. $postStr = file_get_contents('php://input');
  449. $callbackData = json_decode($postStr, true);
  450. if (empty($callbackData)) {
  451. util::fail('回调请求的数据为空');
  452. }
  453. }
  454. // 从配置中获取安全token
  455. // $token = Yii::$app->params['wx_intracity_token'] ?? 'your_token_here';
  456. // $result = IntraCityExpress::handleOrderCallback($callbackData, $token);
  457. // 记录回调日志
  458. Yii::info('聚合配送回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'delivery');
  459. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  460. }
  461. /**
  462. * 闪送回调接口
  463. * 回调返回的格式必须是{"status":200,"msg":"","data":null}格式,当status为200表示回调成功,否则,回调失败。当回调失败时,间隔一分钟重试一次,最多重试五次。
  464. */
  465. public function actionShansongCallback()
  466. {
  467. $callbackData = Yii::$app->request->post();
  468. if (empty($callbackData)) {
  469. $postStr = file_get_contents('php://input');
  470. $callbackData = json_decode($postStr, true);
  471. }
  472. Yii::info('shansong 回调 -- post: ' . json_encode($callbackData, JSON_UNESCAPED_UNICODE));
  473. $obj = new \common\components\delivery\platform\shansong\OrderStatusCallBack();
  474. $ret = $obj->handle($callbackData);
  475. if($ret) {
  476. return $this->asJson(['status' => 200, 'msg' => 'OK', "data"=> null]);
  477. } else {
  478. Yii::error('闪送回调失败: ' . json_encode($callbackData, JSON_UNESCAPED_UNICODE));
  479. return $this->asJson(['status' => 200, 'msg' => '回调失败', "data"=> null]);
  480. }
  481. }
  482. /**
  483. * 蜂鸟回调接口
  484. */
  485. public function actionFengniaoCallback()
  486. {
  487. $callbackData = Yii::$app->request->post();
  488. // 逆向回调接口结果的加签
  489. $param = [
  490. 'app_id' => $callbackData['app_id'],
  491. 'timestamp' => $callbackData['timestamp'],
  492. 'business_data' => $callbackData['business_data']
  493. ];
  494. $fa = new \common\components\delivery\services\adapter\FengniaoAdapter();
  495. $paramSign = $fa->generateSignature($param);
  496. if($paramSign == $callbackData['signature']){
  497. $businessData = isset($callbackData['business_data']) ? $callbackData['business_data'] : '';
  498. if(empty($businessData)){
  499. $this->asJson(['return_code' => -1, 'return_msg' => 'business data is empty']);
  500. }
  501. $businessData = json_decode($businessData, true);
  502. $cbnObj = new \common\components\delivery\platform\fengniao\CallBackNotify();
  503. $ret = $cbnObj->handle($businessData['callback_business_type'], $businessData['param']);
  504. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  505. }else{
  506. Yii::error('蜂鸟回调接口签名出错');
  507. }
  508. }
  509. /**
  510. * 货拉拉回调接口
  511. */
  512. public function actionHuolalaCallback()
  513. {
  514. $post = Yii::$app->request->post();
  515. Yii::info('货拉拉回调接口post:' . json_encode($post, JSON_UNESCAPED_UNICODE), 'delivery');
  516. $action = $post['action'] ?? '';
  517. if($action != 'order_update') {
  518. Yii::error('当前接口只处理货拉拉订单状态变更事件');
  519. return $this->asJson(['return_code' => 0, 'msg' => '当前接口只处理货拉拉订单状态变更事件']);
  520. }
  521. $param = $post;
  522. unset($param['signature']);
  523. $huolala = new \common\components\delivery\platform\huolala\Huolala();
  524. $paramSign = $huolala->generateSignatureWithoutAppSecret($param); //注意:不用拼接secret,所有的参数都参与计算,包含空值。
  525. if($paramSign == $post['signature']){
  526. $cbh = new \common\components\delivery\platform\huolala\CallBackHandler();
  527. $ret = $cbh->handler($post);
  528. if($ret) {
  529. return $this->asJson(['serial_no' => $post['serial_no']]);
  530. } else {
  531. Yii::error('货拉拉回调失败: ' . json_encode($post, JSON_UNESCAPED_UNICODE));
  532. return $this->asJson(['return_code' => -1, 'return_msg' => 'failed']);
  533. }
  534. }else{
  535. Yii::error('蜂鸟回调接口签名出错');
  536. return $this->asJson(['return_code' => -1, 'return_msg' => 'signature error']);
  537. }
  538. }
  539. /**
  540. * 顺丰回调接口
  541. */
  542. public function actionShunfengCallback()
  543. {
  544. $post = Yii::$app->request->post();
  545. $cbh = new \common\components\delivery\platform\shunfeng\CallBackHandler();
  546. $ret = $cbh->handle($post);// 各回调处理
  547. if($ret) {
  548. return $this->asJson(['error_code' => 0, 'error_msg' => 'success']);
  549. } else {
  550. Yii::error('顺丰回调失败: ' . json_encode($post, JSON_UNESCAPED_UNICODE));
  551. return $this->asJson(['error_code' => -1, 'error_msg' => 'failed']);
  552. }
  553. }
  554. // ---------------------------------------------------- 授权回调接口 ----------------------------------------------------------
  555. /**
  556. * 闪送授权回调接口
  557. */
  558. public function actionShansongAuthCallback()
  559. {
  560. $code = Yii::$app->request->get('code');
  561. $state = Yii::$app->request->get('state');
  562. $shopId = Yii::$app->request->get('shopId');
  563. $isAllStoreAuth = Yii::$app->request->get('isAllStoreAuth');
  564. $thirdStoreId = Yii::$app->request->get('thirdStoreId');
  565. $storeId = Yii::$app->request->get('storeId');
  566. // 验证state参数(防止CSRF)
  567. //if ($state != $yourUserId) {
  568. //throw new \yii\web\BadRequestHttpException('Invalid state parameter');
  569. //}
  570. // 获取AccessToken
  571. $auth = new \common\components\delivery\platform\shansong\Auth();
  572. $result = $auth->getAccessToken($code);
  573. if (!$result['success']) {
  574. throw new \yii\web\HttpException(500, '获取授权失败:' . $result['error']);
  575. }
  576. // 保存授权信息到数据库
  577. $data = [
  578. 'mainId' => $state,
  579. 'shopId' => $shopId, //闪送商户ID
  580. 'accessToken' => $result['access_token'],
  581. 'refreshToken' => $result['refresh_token'],
  582. 'expiresAt' => time() + $result['expires_in'],
  583. 'authType' => $isAllStoreAuth ? 'all_store' : 'single_store',
  584. 'platform' => 'shansong'
  585. ];
  586. if (!$isAllStoreAuth) {
  587. $data['third_store_id'] = $thirdStoreId;
  588. $data['shans_store_id'] = $storeId;
  589. }
  590. DeliveryAuthTokenClass::add($data);
  591. if (false) {
  592. throw new \yii\web\HttpException(500, '保存授权信息失败');
  593. }
  594. //return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  595. $templatePath = Yii::getAlias('@common/components/delivery/html/auth-success-template.html');
  596. $template = '';
  597. if (is_file($templatePath) && is_readable($templatePath)) {
  598. $template = file_get_contents($templatePath);
  599. }
  600. if ($template === false || $template === '') {
  601. throw new \yii\web\HttpException(500, '返回h5页面不存在');
  602. }
  603. $content = strtr($template, [
  604. '{{title}}' => '授权成功',
  605. '{{heading}}' => '授权成功',
  606. '{{message}}' => '您的闪送账号已成功完成授权,现在可以返回系统继续使用聚合配送服务。',
  607. ]);
  608. $response = Yii::$app->response;
  609. $response->format = Response::FORMAT_HTML;
  610. $response->content = $content;
  611. return $response;
  612. }
  613. /**
  614. * 货拉拉授权回调接口
  615. */
  616. public function actionHuolalaAuthCallback()
  617. {
  618. $get = Yii::$app->request->get();
  619. $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
  620. Yii::info($getData);
  621. $code = $get['code'];
  622. $mainId = $get['mainId'];
  623. // 获取AccessToken
  624. $auth = new \common\components\delivery\platform\huolala\Auth();
  625. $result = $auth->getAccessToken($code);
  626. if (!$result['success']) {
  627. throw new \yii\web\HttpException(500, '获取授权失败:' . $result['error']);
  628. }
  629. // 保存授权信息到数据库
  630. $data = [
  631. 'mainId' => $mainId,
  632. 'shopId' => '',
  633. 'accessToken' => $result['access_token'],
  634. 'refreshToken' => $result['refresh_token'],
  635. 'expiresAt' => strtotime($result['auth_end_time']),
  636. 'authType' => 'all_store',
  637. 'platform' => 'huolala'
  638. ];
  639. DeliveryAuthTokenClass::add($data);
  640. $templatePath = Yii::getAlias('@common/components/delivery/html/auth-success-template.html');
  641. $template = '';
  642. if (is_file($templatePath) && is_readable($templatePath)) {
  643. $template = file_get_contents($templatePath);
  644. }
  645. if ($template === false || $template === '') {
  646. $template = '<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>{{title}}</title></head><body><h1>{{heading}}</h1><p>{{message}}</p></body></html>';
  647. }
  648. $content = strtr($template, [
  649. '{{title}}' => '授权成功',
  650. '{{heading}}' => '授权成功',
  651. '{{message}}' => '您的货拉拉账号已成功完成授权,现在可以返回系统继续使用聚合配送服务。',
  652. ]);
  653. $response = Yii::$app->response;
  654. $response->format = Response::FORMAT_HTML;
  655. $response->content = $content;
  656. return $response;
  657. }
  658. /**
  659. * 蜂鸟授权回调接口
  660. */
  661. public function actionFengniaoAuthCallback()
  662. {
  663. $callbackData = Yii::$app->request->post();
  664. if (empty($callbackData)) {
  665. Yii::error('回调请求的数据为空');
  666. }
  667. Yii::info('蜂鸟授权回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'fengniao');
  668. if(!isset($callbackData['main_id'])){
  669. Yii::error('蜂鸟授权回调的回调地址 authCallbackUrl 中没有带 main_id');
  670. noticeUtil::push('蜂鸟授权回调的回调地址 authCallbackUrl 中没有带 main_id,回调数据:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'fengniao');
  671. }
  672. $mainId = $callbackData['main_id'];
  673. $code = $callbackData['code'];
  674. $merchantId = $callbackData['merchant_id'];
  675. $scope = $callbackData['scope'];
  676. $state = $callbackData['state'];
  677. // 获取AccessToken
  678. $auth = new \common\components\delivery\platform\fengniao\Auth();
  679. $result = $auth->getAccessToken($code, $merchantId);
  680. if (!$result['success']) {
  681. throw new \yii\web\HttpException(500, '获取授权失败:' . $result['error']);
  682. }
  683. $result = $result['data'];
  684. // 保存授权信息到数据库
  685. $data = [
  686. 'mainId' => $mainId,
  687. 'shopId' => '',
  688. 'accessToken' => $result['access_token'],
  689. 'refreshToken' => $result['refresh_token'],
  690. 'expiresAt' => $result['expire_in'],
  691. 'authType' => 'all_store',
  692. 'platform' => 'fengniao'
  693. ];
  694. DeliveryAuthTokenClass::add($data);
  695. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  696. }
  697. /**
  698. * 达达授权回调接口
  699. */
  700. public function actionDadaAuthCallback()
  701. {
  702. $get = Yii::$app->request->get();
  703. $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
  704. Yii::info($getData);
  705. $callbackData = Yii::$app->request->post();
  706. if (empty($callbackData)) {
  707. $postStr = file_get_contents('php://input');
  708. $callbackData = json_decode($postStr, true);
  709. if (empty($callbackData)) {
  710. Yii::info('回调请求的数据为空');
  711. }
  712. }
  713. Yii::info('达达授权回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'dada');
  714. }
  715. /**
  716. * 顺丰授权与取消授权回调接口
  717. */
  718. public function actionShunfengAuthCallback()
  719. {
  720. $get = Yii::$app->request->get();
  721. $getData = json_encode($get, JSON_UNESCAPED_UNICODE);
  722. Yii::info($getData);
  723. $callbackData = Yii::$app->request->post();
  724. if (empty($callbackData)) {
  725. $postStr = file_get_contents('php://input');
  726. $callbackData = json_decode($postStr, true);
  727. if (empty($callbackData)) {
  728. Yii::info('回调请求的数据为空');
  729. }
  730. }
  731. Yii::info('顺丰授权与取消授权回调接口:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'dada');
  732. }
  733. }