DeliveryController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. namespace hd\controllers;
  3. use biz\common\classes\HdNotifyClass;
  4. use Yii;
  5. use biz\ghs\classes\GhsClass;
  6. use bizGhs\custom\classes\CustomClass;
  7. use bizGhs\express\classes\DeliveryAuthTokenClass;
  8. use bizHd\order\classes\OrderClass;
  9. use bizHd\express\classes\HdDeliveryOrderClass;
  10. use hd\models\delivery\CancelOrderForm;
  11. use hd\models\delivery\CreateOrderForm;
  12. use biz\shop\classes\ShopClass;
  13. use common\components\delivery\services\DispatchService;
  14. use common\components\util;
  15. use yii\helpers\ArrayHelper;
  16. class DeliveryController extends BaseController
  17. {
  18. public function actionList()
  19. {
  20. $platformList = [];
  21. $platformLogos = [
  22. 'dada' => ['name' => '达达', 'logo' => '📦'],
  23. 'shunfeng' => ['name' => '顺丰同城', 'logo' => '🚚'],
  24. 'meituan' => ['name' => '美团', 'logo' => '🍱'],
  25. 'fengniao' => ['name' => '蜂鸟', 'logo' => '🍜'],
  26. 'huolala' => ['name' => '货拉拉跑腿', 'logo' => '🚛'],
  27. 'shansong' => ['name' => '闪送', 'logo' => '⚡'],
  28. 'didi' => ['name' => '滴滴', 'logo' => '🚗'],
  29. 'uu' => ['name' => 'UU跑腿', 'logo' => '🛵']
  30. ];
  31. $mainId = $this->mainId;
  32. $allDeliveries = DeliveryAuthTokenClass::getAllByCondition(['mainId' => $mainId]);
  33. foreach ($allDeliveries as $d) {
  34. $item = [
  35. 'id' => $d['platform'],
  36. 'logo' => $platformLogos[$d['platform']]['logo'],
  37. 'name' => $platformLogos[$d['platform']]['name'],
  38. 'is_authorized' => true,
  39. 'balance' => $d['balance'] / 100.0,
  40. 'access_type' => 'oauth'
  41. ];
  42. $platformList[] = $item;
  43. }
  44. util::success(['platformList' => $platformList]);
  45. }
  46. // 花店发货时 -- 获取多个平台报价
  47. public function actionHdAllDeliveryQuotes()
  48. {
  49. $post = Yii::$app->request->post();
  50. $orderTime = $post['pickupTime'];
  51. $weight = $post['weight'];
  52. $remark = $post['remark'] ?? '';
  53. $adminId = $this->adminId;
  54. util::checkRepeatCommit($adminId, 3);
  55. $orderId = intval($post['orderId']);
  56. $order = OrderClass::getById($orderId);
  57. if (empty($order)) {
  58. util::fail('没有找到订单');
  59. }
  60. if ($order['mainId'] != $this->mainId) {
  61. util::fail('不是你的订单');
  62. }
  63. if (empty($order['address'])) {
  64. util::fail('客户地址缺失');
  65. }
  66. if (empty($order['long']) || empty($order['lat'])) {
  67. util::fail('客户地址信息缺失');
  68. }
  69. if (empty($order['receiveMobile'])) {
  70. util::fail('收货人手机为空');
  71. }
  72. if (empty($order['receiveUserName'])) {
  73. util::fail('收货人名称为空');
  74. }
  75. $shop = ShopClass::getById($this->shopId);
  76. $order['weight'] = $weight;
  77. $order['remark'] = $remark;
  78. $ds = new DispatchService($this->mainId);
  79. $platformQuotes = $ds->getAllPlatformPrice($order, $shop, $orderTime);
  80. if (isset($platformQuotes['error'])) {
  81. util::fail($platformQuotes['error']);
  82. }
  83. // 格式化各平台报价为前端展示格式
  84. $deliveryList = $ds->formatPlatformQuotesForDisplay($platformQuotes);
  85. // 按 price 从低到高排序
  86. ArrayHelper::multisort($deliveryList, 'price', SORT_ASC);
  87. $ret['deliveryList'] = $deliveryList;
  88. util::success($ret, "success");
  89. }
  90. // 花店向批发买花时 -- 获取多个平台报价
  91. public function actionAllDeliveryQuotes()
  92. {
  93. $post = Yii::$app->request->post();
  94. $orderTime = date('Y-m-d H:i:s');
  95. // --------- $ghsShopId ----------
  96. $ghsId = $post['ghsId'] ?? 0;
  97. $ghs = GhsClass::getById($ghsId, true);
  98. if (empty($ghs)) {
  99. util::fail('获取批发商失败');
  100. }
  101. $ghsShopId = $ghs->shopId;
  102. // --------- $customId
  103. $customId = $ghs->customId ?? 0;
  104. $custom = CustomClass::getById($customId, true); // 批发的 custom
  105. if (empty($custom)) {
  106. util::fail('获取custom数据失败');
  107. }
  108. // 生成随机订单号,不要跟原来的混起来,跑腿-销售单-
  109. $prefix = 'PT-XSD-' . $ghs->mainId . '-';
  110. $orderSn = $prefix . round(microtime(true) * 1000);
  111. //构建出 Order 数据
  112. $order = [
  113. 'orderSn' => $orderSn,
  114. 'customName' => $custom['name'],
  115. 'customMobile' => $custom['mobile'],
  116. 'fullAddress' => $custom['fullAddress'],
  117. 'floor' => $custom['floor'],
  118. 'dist' => $custom['dist'],
  119. 'lat' => $custom['lat'],
  120. 'long' => $custom['long'],
  121. 'address' => $custom['address'], //'toAddress' => $order['address'],
  122. 'city' => $custom['city'],
  123. 'weight' => $post['weight'] ?? 1,
  124. 'remark' => $post['remark'] ?? '',
  125. 'prePrice' => floatval($post['totalPrice'] ?? 0),
  126. 'actPrice' => floatval($post['totalPrice'] ?? 0),
  127. ];
  128. $ghsShop = ShopClass::getById($ghsShopId);
  129. $ds = new DispatchService($ghs->mainId);
  130. $platformQuotes = $ds->getAllPlatformPrice($order, $ghsShop, $orderTime);
  131. if (isset($platformQuotes['error'])) {
  132. util::fail($platformQuotes['error']);
  133. }
  134. $deliveryList = [];
  135. // 格式化各平台报价为前端展示格式
  136. $deliveryList = $ds->formatPlatformQuotesForDisplay($platformQuotes);
  137. // 按 price 从低到高排序
  138. ArrayHelper::multisort($deliveryList, 'price', SORT_ASC);
  139. $ret['deliveryList'] = $deliveryList;
  140. util::success($ret, "success");
  141. }
  142. // 创建订单(真实下单)
  143. public function actionCreateOrder()
  144. {
  145. $form = new CreateOrderForm();
  146. $form->loadAndValidate();
  147. $post = $form->getAttributes();
  148. $orderId = $post['orderId'] ?? 0; // xhOrder.id
  149. $params = $post['allParams'] ?? '';
  150. $platform = $params['en_name'] ?? '';// 平台英文名,$post['platform'] 是中文
  151. if (empty($platform)) {
  152. util::fail('请选择跑腿');
  153. }
  154. $pickupTime = $post['pickupTime'] ?? '';
  155. $params['mainId'] = $this->mainId;
  156. $params['ip'] = Yii::$app->request->userIP;
  157. $params['weight'] = $post['weight'] ?? 1;
  158. $params['remark'] = $post['remark'] ?? '';
  159. $params['total_price_fen'] = $post['price'] ?? '';
  160. $params['pickupTime'] = $pickupTime['value'] ?? '';
  161. $Order = OrderClass::getById($orderId, true);
  162. if (empty($Order)) {
  163. util::fail('没有找到订单');
  164. }
  165. if ($Order->mainId != $this->mainId) {
  166. util::fail('不是你的订单');
  167. }
  168. if (!in_array($Order['sendStatus'], [-4, -2, -1])) {
  169. Yii::error('订单已经发过跑腿');
  170. util::fail('订单已经发过跑腿');
  171. }
  172. //避免连续点击的重复提交 ssh
  173. $adminId = $this->adminId;
  174. util::checkRepeatCommit($adminId, 3);
  175. $ds = new DispatchService($this->mainId, $platform);
  176. $ret = $ds->createOrder($Order, 'xhOrder', $this->shopId, $params);
  177. if (isset($ret['code']) && $ret['code'] == 0) {
  178. $gdo = HdDeliveryOrderClass::getByCondition(['mainId' => $this->mainId, 'orderId' => $ret['data']['orderId']]);
  179. if (empty($gdo)) {
  180. $data = [
  181. 'mainId' => $this->mainId,
  182. 'shopId' => $this->shopId,
  183. 'deliveryId' => $ret['platform'],
  184. 'hdOrderId' => $orderId,
  185. 'orderId' => $ret['data']['orderId'],
  186. 'distance' => $ret['data']['distance'],
  187. 'fee' => $ret['data']['fee'],
  188. 'orderStatus' => 0
  189. ];
  190. // createdAt 中保存了滴滴跑腿订单的后半截数据
  191. if($platform == 'didi'){
  192. $data['createdAt'] = date('Y-m-d H:i:s', $ret['data']['timeStamp']);
  193. }
  194. HdDeliveryOrderClass::add($data);
  195. } else {
  196. Yii::info('重复创建订单');
  197. }
  198. //更新订单
  199. $Order->sendDistance = $ret['data']['distance'];
  200. $Order->sendStatus = 0;
  201. $Order->deliveryId = $platform;
  202. $Order->sendType = 2;// 2指跑腿
  203. $Order->status = 3;// 3配送中
  204. $save = $Order->save();
  205. if ($save) {
  206. util::success($ret, "success");
  207. } else {
  208. util::fail('创建跑腿失败(更新订单失败)');
  209. }
  210. } else {
  211. if (isset($ret['msg'])) {
  212. util::fail($ret['msg']);
  213. }
  214. util::fail('创建跑腿失败');
  215. }
  216. }
  217. // 取消订单
  218. public function actionCancelOrder()
  219. {
  220. $form = new CancelOrderForm();
  221. $form->loadAndValidate();
  222. $post = $form->getAttributes();
  223. $deliveryOrderId = $post['orderId'] ?? 0; // 平台的orderId,
  224. $platform = $post['platform'] ?? '';
  225. $hdOrderId = $post['hdOrderId'];
  226. // 先获取配送订单,确保 $gdo 始终可用
  227. $hdo = HdDeliveryOrderClass::getByCondition(['mainId' => $this->mainId, 'hdOrderId' => $hdOrderId, 'orderStatus' => ['not in', [-1, -2]]], true, 'id DESC');
  228. if (empty($hdo)) {
  229. util::fail('未找到对应订单: ' . $hdOrderId);
  230. }
  231. $orderId = $hdo->hdOrderId;
  232. // 如果没有提供平台和订单ID,从配送订单中获取
  233. if ($platform == '' || $deliveryOrderId == 0) {
  234. $deliveryOrderId = $hdo->orderId;
  235. $platform = $hdo->deliveryId;
  236. }
  237. $ds = new DispatchService($this->mainId, $platform);
  238. $cancelCostCent = 0;
  239. if ($platform == 'fengniao') {
  240. //请求订单预取消接口,获取是否可取消。如果运单状态不允许取消,则直接返回
  241. $cancelParams = ['order_id' => $deliveryOrderId, 'order_cancel_code' => $post['order_cancel_code']];
  242. $res = $ds->getAdapter()->preCancelOrder($cancelParams);
  243. if ($res['code'] != 0) {
  244. util::fail('运单状态不允许取消');
  245. }
  246. $cancelCostCent = $res['data']['actual_cancel_cost_cent']; //取消实际需金额(单位:分)
  247. }
  248. if ($platform == 'shunfeng') {
  249. //请求订单预取消接口,获取是否可取消。如果运单状态不允许取消,则直接返回
  250. $cancelParams = ['order_id' => $deliveryOrderId, 'order_cancel_code' => $post['order_cancel_code']];
  251. $res = $ds->getAdapter()->preCancelOrder($cancelParams);
  252. if ($res['code'] != 0) {
  253. util::fail('运单状态不允许取消');
  254. }
  255. $post['order_type'] = 1; //查询订单ID类型 1、顺丰订单号 2、商家订单号
  256. }
  257. if ($platform == 'dada') {
  258. $order = OrderClass::getById($orderId, false, 'id,orderSn');
  259. $deliveryOrderId = $order['orderSn'];
  260. }
  261. if ($platform == 'didi') {
  262. $order = OrderClass::getById($orderId, false, 'id,orderSn');
  263. $timeStamp = strtotime($hdo->createdAt);
  264. $post['outOrderNo'] = \common\components\delivery\platform\didi\Didi::generateUniOroder($order['orderSn'], $timeStamp);
  265. $post['cancelSource'] = 1; // TODO 要增加取消码判断
  266. }
  267. $post['order_cancel_role'] = 1; //1:商户取消, 2:用户取消
  268. $ret = $ds->cancelOrder($deliveryOrderId, $post);
  269. if ($ret['code'] == 0) {
  270. $Order = OrderClass::getById($hdOrderId, true, 'id, sendDistance, sendStatus, deliveryId');
  271. $Order->sendDistance = 0;
  272. if(in_array($platform, ['shunfeng'])){ // 测试发现取消配送没回调的平台,统一在这主动更新配送状态
  273. $Order->sendStatus = -1;
  274. // 更新跑腿订单与批发订单状态
  275. $hdo->orderStatus = -1;
  276. HdNotifyClass::ptErrorNotify($Order->id, '商家主动取消');
  277. }
  278. $hdo->cancelCost = $ret['platform'] !== 'fengniao' ? $ret['data']['deductionFee'] : $cancelCostCent; // 由于蜂鸟平台返回的扣费不一样造成
  279. if (!$hdo->save()) {
  280. Yii::error('保存配送订单失败: ' . json_encode($hdo->errors));
  281. util::fail('保存配送订单失败');
  282. }
  283. $Order->status = 2;
  284. //$Order->deliveryId = '';
  285. //$Order->sendTip = 0;
  286. if (!$Order->save()) {
  287. Yii::error('保存批发订单失败: ' . json_encode($Order->errors));
  288. util::fail('保存批发订单失败');
  289. }
  290. util::success($ret['data'], "success");
  291. }
  292. util::fail('失败:'.$ret['msg']);
  293. }
  294. /**
  295. * 获取订单取消原因
  296. */
  297. public function actionCancelReason()
  298. {
  299. $post = Yii::$app->request->post();
  300. $hdOrderId = $post['hdOrderId'];
  301. $platform = $post['platform'];
  302. $hdo = HdDeliveryOrderClass::getByCondition(['mainId' => $this->mainId, 'hdOrderId' => $hdOrderId, 'orderStatus!=' => -1], true);
  303. if (!empty($hdo)) {
  304. $orderId = $hdo->orderId;
  305. //$platform = $gdo->deliveryId;
  306. } else {
  307. util::fail('未找到对应订单: ' . $hdOrderId);
  308. }
  309. $ds = new DispatchService($this->mainId, $platform);
  310. $ret = $ds->getCancelReasonList($orderId);
  311. if ($ret['code'] == 0) {
  312. util::success($ret['data']);
  313. } else {
  314. Yii::error('获取订单取消原因:' . json_encode($ret));
  315. util::fail($ret['msg']);
  316. }
  317. }
  318. public function actionAddress()
  319. {
  320. $post = Yii::$app->request->post();
  321. $adminId = $this->adminId;
  322. util::checkRepeatCommit($adminId, 4);
  323. $orderId = intval($post['orderId']);
  324. $order = OrderClass::getById($orderId, false, 'fullAddress');
  325. $shop = ShopClass::getById($this->shopId, false, 'fullAddress');
  326. $ret = [
  327. "send_address" => $shop['fullAddress'],
  328. "receive_address" => $order['fullAddress']
  329. ];
  330. util::success($ret, "success");
  331. }
  332. public function actionCancelAuth()
  333. {
  334. // 有平台有对应的取消授权接口,有的没有。
  335. // 有的就对接下,然后再删除数据。没有的直接删除数据就行。
  336. $platform = Yii::$app->request->get('platform');
  337. $delivery = DeliveryAuthTokenClass::getByCondition(['mainId' => $this->mainId, 'platform' => $platform]);
  338. if ($delivery) {
  339. if ($platform == 'shansong') {
  340. $auth = new \common\components\delivery\platform\shansong\Auth();
  341. $result = $auth->cancelAuthorization($delivery['accessToken']);
  342. if ($result['success']) {
  343. // 授权已取消
  344. } else {
  345. if ($result['message'] == '商户未授权,请授权后再次尝试') {
  346. Yii::info($this->mainId . '--商户未授权,请授权后再次尝试');
  347. } else {
  348. util::fail('取消授权失败:' . $result['message']);
  349. }
  350. }
  351. }
  352. } else {
  353. util::fail('取消授权失败,数据未找到');
  354. }
  355. $ret = DeliveryAuthTokenClass::deleteByCondition(['mainId' => $this->mainId, 'platform' => $platform]);
  356. if ($ret > 0) {
  357. util::success(['message' => '取消授权成功']);
  358. } else {
  359. util::fail('取消授权失败');
  360. }
  361. }
  362. public function actionGetAuthUrl()
  363. {
  364. $platform = Yii::$app->request->get('platformId');
  365. $Auth = new \common\components\delivery\services\AuthService();
  366. $mainId = $this->mainId;
  367. switch ($platform) {
  368. case 'shansong':
  369. $Auth->shansongAuth($mainId);
  370. break;
  371. case 'huolala':
  372. $Auth->huolalaAuth($mainId);
  373. break;
  374. case 'fengniao':
  375. $Auth->fengniaoAuth($mainId);
  376. break;
  377. case 'dada':
  378. $Auth->dadaAuth($mainId);
  379. break;
  380. case 'shunfeng':
  381. $Auth->shunfengAuth($mainId);
  382. break;
  383. case 'didi':
  384. break;
  385. default:
  386. util::error(-1, $platform . '不存在');
  387. break;
  388. }
  389. }
  390. }