DeliveryController.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace mall\controllers;
  3. use bizMall\shop\classes\ShopClass;
  4. use common\components\delivery\services\DispatchService;
  5. use common\components\util;
  6. use Yii;
  7. use yii\helpers\ArrayHelper;
  8. class DeliveryController extends BaseController
  9. {
  10. // 终端客户向花店买花材 -- 获取多个平台报价
  11. public function actionAllDeliveryQuotes()
  12. {
  13. $post = Yii::$app->request->post();
  14. $orderTime = date('Y-m-d H:i:s');
  15. // --------- $ghsShopId ----------
  16. $ghsShopId = $this->shopId;
  17. if (empty($ghsShopId)) {
  18. util::fail('店铺不存在');
  19. }
  20. $user = $this->user;
  21. if (empty($user)) {
  22. util::fail('用户不存在');
  23. }
  24. $mainId = $this->mainId;
  25. if (empty($mainId)) {
  26. util::fail('商户不存在');
  27. }
  28. $weight = $post['weight'] ?? 1;
  29. $buyType = $post['buyType'] ?? 'huaCai';
  30. $order = $this->generateOrderData($buyType, $user, $post);
  31. $ghsShop = ShopClass::getById($ghsShopId);
  32. $ds = new DispatchService($mainId);
  33. $platformQuotes = $ds->getAllPlatformPrice($order, $ghsShop, $orderTime);
  34. if(isset($platformQuotes['error'])){
  35. util::fail($platformQuotes['error']);
  36. }
  37. $deliveryList = [];
  38. // 格式化各平台报价为前端展示格式
  39. $deliveryList = $ds->formatPlatformQuotesForDisplay($platformQuotes);
  40. // 按 price 从低到高排序
  41. ArrayHelper::multisort($deliveryList, 'price', SORT_ASC);
  42. $ret['deliveryList'] = $deliveryList;
  43. util::success($ret, "success");
  44. }
  45. private function generateOrderData($buyType, $user, $post)
  46. {
  47. // 生成随机订单号
  48. $prefix = 'XSD_CS-' . $this->mainId . '-';
  49. $orderSn = $prefix . round(microtime(true) * 1000);
  50. //构建出 Order 数据
  51. if($buyType == 'huaCai'){//花材
  52. $order = [
  53. 'orderSn' => $orderSn,
  54. 'customName' => $user['name'],
  55. 'customMobile' => $user['mobile'],
  56. 'fullAddress' => $user['fullAddress'],
  57. 'floor' => $user['floor'],
  58. 'dist' => $user['dist'],
  59. 'lat' => $user['lat'],
  60. 'long' => $user['long'],
  61. 'address' => $user['address'], //'toAddress' => $order['address'],
  62. 'city' => $user['city'],
  63. 'weight' => $post['weight'] ?? 1,
  64. 'remark' => $post['remark'] ?? '',
  65. 'prePrice' => floatval($post['totalPrice'] ?? 0),
  66. 'actPrice' => floatval($post['totalPrice'] ?? 0),
  67. ];
  68. }elseif($buyType == 'huaShu'){//花束
  69. $order = [
  70. 'orderSn' => $orderSn,
  71. 'customName' => $post['receiveUserName'],
  72. 'customMobile' => $post['receiveMobile'],
  73. 'fullAddress' => $post['address'],
  74. 'floor' => $post['floor'] ?? '',
  75. 'dist' => $post['dist'] ?? '',
  76. 'lat' => $post['region']['latitude'],// TODO
  77. 'long' => $post['region']['longitude'],// TODO
  78. 'address' => $post['address'], //'toAddress' => $order['address'],
  79. 'city' => $post['city'],
  80. 'weight' => $post['weight'] ?? 1,
  81. 'remark' => $post['remark'] ?? '',
  82. 'prePrice' => floatval($post['totalPrice'] ?? 0),
  83. 'actPrice' => floatval($post['totalPrice'] ?? 0),
  84. ];
  85. }else{
  86. util::fail('不存在此订单类型');
  87. }
  88. return $order;
  89. }
  90. }