DeliveryController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. $ds->validateOrder($order); //对 $order 进行验证
  131. $platformQuotes = $ds->getAllPlatformPrice($order, $ghsShop, $orderTime);
  132. if (isset($platformQuotes['error'])) {
  133. util::fail($platformQuotes['error']);
  134. }
  135. $deliveryList = [];
  136. // 格式化各平台报价为前端展示格式
  137. $deliveryList = $ds->formatPlatformQuotesForDisplay($platformQuotes);
  138. // 按 price 从低到高排序
  139. ArrayHelper::multisort($deliveryList, 'price', SORT_ASC);
  140. $ret['deliveryList'] = $deliveryList;
  141. util::success($ret, "success");
  142. }
  143. // 创建订单(真实下单)
  144. public function actionCreateOrder()
  145. {
  146. $form = new CreateOrderForm();
  147. $form->loadAndValidate();
  148. $post = $form->getAttributes();
  149. $orderId = $post['orderId'] ?? 0; // xhOrder.id
  150. $params = $post['allParams'] ?? '';
  151. $platform = $params['en_name'] ?? '';// 平台英文名,$post['platform'] 是中文
  152. if (empty($platform)) {
  153. util::fail('请选择跑腿');
  154. }
  155. $pickupTime = $post['pickupTime'] ?? '';
  156. $params['mainId'] = $this->mainId;
  157. $params['ip'] = Yii::$app->request->userIP;
  158. $params['weight'] = $post['weight'] ?? 1;
  159. $params['remark'] = $post['remark'] ?? '';
  160. $params['total_price_fen'] = $post['price'] ?? '';
  161. $params['pickupTime'] = $pickupTime['value'] ?? '';
  162. $Order = OrderClass::getById($orderId, true);
  163. if (empty($Order)) {
  164. util::fail('没有找到订单');
  165. }
  166. if ($Order->mainId != $this->mainId) {
  167. util::fail('不是你的订单');
  168. }
  169. if (!in_array($Order['sendStatus'], [-4, -2, -1])) {
  170. Yii::error('订单已经发过跑腿');
  171. util::fail('订单已经发过跑腿');
  172. }
  173. //避免连续点击的重复提交 ssh
  174. $adminId = $this->adminId;
  175. util::checkRepeatCommit($adminId, 3);
  176. $ds = new DispatchService($this->mainId, $platform);
  177. $ret = $ds->createOrder($Order, 'xhOrder', $this->shopId, $params);
  178. if (isset($ret['code']) && $ret['code'] == 0) {
  179. $gdo = HdDeliveryOrderClass::getByCondition(['mainId' => $this->mainId, 'orderId' => $ret['data']['orderId']]);
  180. if (empty($gdo)) {
  181. $data = [
  182. 'mainId' => $this->mainId,
  183. 'shopId' => $this->shopId,
  184. 'deliveryId' => $ret['platform'],
  185. 'hdOrderId' => $orderId,
  186. 'orderId' => $ret['data']['orderId'],
  187. 'distance' => $ret['data']['distance'],
  188. 'fee' => $ret['data']['fee'],
  189. 'orderStatus' => 0
  190. ];
  191. // createdAt 中保存了滴滴跑腿订单的后半截数据
  192. if($platform == 'didi'){
  193. $data['createdAt'] = date('Y-m-d H:i:s', $ret['data']['timeStamp']);
  194. }
  195. HdDeliveryOrderClass::add($data);
  196. } else {
  197. Yii::info('重复创建订单');
  198. }
  199. //更新订单
  200. $Order->sendDistance = $ret['data']['distance'];
  201. $Order->sendStatus = 0;
  202. $Order->deliveryId = $platform;
  203. $Order->sendType = 2;// 2指跑腿
  204. $Order->status = 3;// 3配送中
  205. $save = $Order->save();
  206. if ($save) {
  207. util::success($ret, "success");
  208. } else {
  209. util::fail('创建跑腿失败(更新订单失败)');
  210. }
  211. } else {
  212. if (isset($ret['msg'])) {
  213. util::fail($ret['msg']);
  214. }
  215. util::fail('创建跑腿失败');
  216. }
  217. }
  218. // 取消订单
  219. public function actionCancelOrder()
  220. {
  221. $form = new CancelOrderForm();
  222. $form->loadAndValidate();
  223. $post = $form->getAttributes();
  224. $deliveryOrderId = $post['orderId'] ?? 0; // 平台的orderId,
  225. $platform = $post['platform'] ?? '';
  226. $hdOrderId = $post['hdOrderId'];
  227. // 先获取配送订单,确保 $gdo 始终可用
  228. $hdo = HdDeliveryOrderClass::getByCondition(['mainId' => $this->mainId, 'hdOrderId' => $hdOrderId, 'orderStatus' => ['not in', [-1, -2]]], true, 'id DESC');
  229. if (empty($hdo)) {
  230. util::fail('未找到对应订单: ' . $hdOrderId);
  231. }
  232. $orderId = $hdo->hdOrderId;
  233. // 如果没有提供平台和订单ID,从配送订单中获取
  234. if ($platform == '' || $deliveryOrderId == 0) {
  235. $deliveryOrderId = $hdo->orderId;
  236. $platform = $hdo->deliveryId;
  237. }
  238. $ds = new DispatchService($this->mainId, $platform);
  239. $cancelCostCent = 0;
  240. if ($platform == 'fengniao') {
  241. //请求订单预取消接口,获取是否可取消。如果运单状态不允许取消,则直接返回
  242. $cancelParams = ['order_id' => $deliveryOrderId, 'order_cancel_code' => $post['order_cancel_code']];
  243. $res = $ds->getAdapter()->preCancelOrder($cancelParams);
  244. if ($res['code'] != 0) {
  245. util::fail('运单状态不允许取消');
  246. }
  247. $cancelCostCent = $res['data']['actual_cancel_cost_cent']; //取消实际需金额(单位:分)
  248. }
  249. if ($platform == 'shunfeng') {
  250. //请求订单预取消接口,获取是否可取消。如果运单状态不允许取消,则直接返回
  251. $cancelParams = ['order_id' => $deliveryOrderId, 'order_cancel_code' => $post['order_cancel_code']];
  252. $res = $ds->getAdapter()->preCancelOrder($cancelParams);
  253. if ($res['code'] != 0) {
  254. util::fail('运单状态不允许取消');
  255. }
  256. $post['order_type'] = 1; //查询订单ID类型 1、顺丰订单号 2、商家订单号
  257. }
  258. if ($platform == 'dada') {
  259. $order = OrderClass::getById($orderId, false, 'id,orderSn');
  260. $deliveryOrderId = $order['orderSn'];
  261. }
  262. if ($platform == 'didi') {
  263. $order = OrderClass::getById($orderId, false, 'id,orderSn');
  264. $timeStamp = strtotime($hdo->createdAt);
  265. $post['outOrderNo'] = \common\components\delivery\platform\didi\Didi::generateUniOroder($order['orderSn'], $timeStamp);
  266. $post['cancelSource'] = 1; // TODO 要增加取消码判断
  267. }
  268. $post['order_cancel_role'] = 1; //1:商户取消, 2:用户取消
  269. $ret = $ds->cancelOrder($deliveryOrderId, $post);
  270. if ($ret['code'] == 0) {
  271. $Order = OrderClass::getById($hdOrderId, true, 'id, sendDistance, sendStatus, deliveryId');
  272. $Order->sendDistance = 0;
  273. if(in_array($platform, ['shunfeng', 'fengniao'])){ // 测试发现取消配送没回调的平台,统一在这主动更新配送状态
  274. $Order->sendStatus = -1;
  275. // 更新跑腿订单与批发订单状态
  276. $hdo->orderStatus = -1;
  277. HdNotifyClass::ptErrorNotify($Order->id, '门店主动取消');
  278. }
  279. $hdo->cancelCost = $ret['platform'] !== 'fengniao' ? $ret['data']['deductionFee'] : $cancelCostCent; // 由于蜂鸟平台返回的扣费不一样造成
  280. if (!$hdo->save()) {
  281. Yii::error('保存配送订单失败: ' . json_encode($hdo->errors));
  282. util::fail('保存配送订单失败');
  283. }
  284. $Order->status = 2;
  285. //$Order->deliveryId = '';
  286. //$Order->sendTip = 0; //零售xhOrder表没这个字段
  287. if (!$Order->save()) {
  288. Yii::error('保存批发订单失败: ' . json_encode($Order->errors));
  289. util::fail('保存批发订单失败');
  290. }
  291. util::success($ret['data'], "success");
  292. }
  293. util::fail('失败:'.$ret['msg']);
  294. }
  295. /**
  296. * 获取订单取消原因
  297. */
  298. public function actionCancelReason()
  299. {
  300. $post = Yii::$app->request->post();
  301. $hdOrderId = $post['hdOrderId'];
  302. $platform = $post['platform'];
  303. $hdo = HdDeliveryOrderClass::getByCondition(['mainId' => $this->mainId, 'hdOrderId' => $hdOrderId, 'orderStatus!=' => -1], true);
  304. if (!empty($hdo)) {
  305. $orderId = $hdo->orderId;
  306. //$platform = $gdo->deliveryId;
  307. } else {
  308. util::fail('未找到对应订单: ' . $hdOrderId);
  309. }
  310. $ds = new DispatchService($this->mainId, $platform);
  311. $ret = $ds->getCancelReasonList($orderId);
  312. if ($ret['code'] == 0) {
  313. util::success($ret['data']);
  314. } else {
  315. Yii::error('获取订单取消原因:' . json_encode($ret));
  316. util::fail($ret['msg']);
  317. }
  318. }
  319. public function actionAddress()
  320. {
  321. $post = Yii::$app->request->post();
  322. $adminId = $this->adminId;
  323. util::checkRepeatCommit($adminId, 4);
  324. $orderId = intval($post['orderId']);
  325. $order = OrderClass::getById($orderId, false, 'fullAddress');
  326. $shop = ShopClass::getById($this->shopId, false, 'fullAddress');
  327. $ret = [
  328. "send_address" => $shop['fullAddress'],
  329. "receive_address" => $order['fullAddress']
  330. ];
  331. util::success($ret, "success");
  332. }
  333. public function actionCancelAuth()
  334. {
  335. // 有平台有对应的取消授权接口,有的没有。
  336. // 有的就对接下,然后再删除数据。没有的直接删除数据就行。
  337. $platform = Yii::$app->request->get('platform');
  338. $delivery = DeliveryAuthTokenClass::getByCondition(['mainId' => $this->mainId, 'platform' => $platform]);
  339. if ($delivery) {
  340. if ($platform == 'shansong') {
  341. $auth = new \common\components\delivery\platform\shansong\Auth();
  342. $result = $auth->cancelAuthorization($delivery['accessToken']);
  343. if ($result['success']) {
  344. // 授权已取消
  345. } else {
  346. if ($result['message'] == '商户未授权,请授权后再次尝试') {
  347. Yii::info($this->mainId . '--商户未授权,请授权后再次尝试');
  348. } else {
  349. util::fail('取消授权失败:' . $result['message']);
  350. }
  351. }
  352. }
  353. } else {
  354. util::fail('取消授权失败,数据未找到');
  355. }
  356. $ret = DeliveryAuthTokenClass::deleteByCondition(['mainId' => $this->mainId, 'platform' => $platform]);
  357. if ($ret > 0) {
  358. util::success(['message' => '取消授权成功']);
  359. } else {
  360. util::fail('取消授权失败');
  361. }
  362. }
  363. public function actionGetAuthUrl()
  364. {
  365. $platform = Yii::$app->request->get('platformId');
  366. $Auth = new \common\components\delivery\services\AuthService();
  367. $mainId = $this->mainId;
  368. switch ($platform) {
  369. case 'shansong':
  370. $Auth->shansongAuth($mainId);
  371. break;
  372. case 'huolala':
  373. $Auth->huolalaAuth($mainId);
  374. break;
  375. case 'fengniao':
  376. $Auth->fengniaoAuth($mainId);
  377. break;
  378. case 'dada':
  379. $Auth->dadaAuth($mainId);
  380. break;
  381. case 'shunfeng':
  382. $Auth->shunfengAuth($mainId);
  383. break;
  384. case 'didi':
  385. break;
  386. default:
  387. util::error(-1, $platform . '不存在');
  388. break;
  389. }
  390. }
  391. }