DeliveryController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace mall\controllers;
  3. use Yii;
  4. use bizMall\shop\classes\ShopClass;
  5. use common\components\delivery\services\DispatchService;
  6. use common\components\util;
  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. $buyType = $post['buyType'] ?? 'huaCai'; //订单类型
  29. $order = $this->generateOrderData($buyType, $user, $post);
  30. $ghsShop = ShopClass::getById($ghsShopId);
  31. if (empty($ghsShop)) {
  32. util::fail('店铺信息不存在');
  33. }
  34. $ds = new DispatchService($mainId);
  35. $platformQuotes = $ds->getAllPlatformPrice($order, $ghsShop, $orderTime);
  36. if(isset($platformQuotes['error'])){
  37. util::fail($platformQuotes['error']);
  38. }
  39. // 格式化各平台报价为前端展示格式
  40. $deliveryList = $ds->formatPlatformQuotesForDisplay($platformQuotes);
  41. // 按 price 从低到高排序
  42. ArrayHelper::multisort($deliveryList, 'price', SORT_ASC);
  43. $ret['deliveryList'] = $deliveryList;
  44. util::success($ret, "success");
  45. }
  46. private function generateOrderData($buyType, $user, $post)
  47. {
  48. // 生成随机订单号,不要跟原来的混起来,跑腿-销售单-
  49. $prefix = 'PT-XSD-' . $this->mainId . '-';
  50. $orderSn = $prefix . round(microtime(true) * 1000);
  51. //构建出 Order 数据
  52. if($buyType == 'huaCai'){//订单类型是:花材
  53. $order = [
  54. 'orderSn' => $orderSn,
  55. 'customName' => $user['name'],
  56. 'customMobile' => $user['mobile'],
  57. 'fullAddress' => $user['fullAddress'],
  58. 'floor' => $user['floor'],
  59. 'dist' => $user['dist'],
  60. 'lat' => $user['lat'],
  61. 'long' => $user['long'],
  62. 'address' => $user['address'], //'toAddress' => $order['address'],
  63. 'city' => $user['city'],
  64. 'weight' => $post['weight'] ?? 1,
  65. 'remark' => $post['remark'] ?? '',
  66. 'prePrice' => floatval($post['totalPrice'] ?? 0),
  67. 'actPrice' => floatval($post['totalPrice'] ?? 0),
  68. ];
  69. }elseif($buyType == 'huaShu'){//订单类型是:花束
  70. $order = [
  71. 'orderSn' => $orderSn,
  72. 'customName' => $post['receiveUserName'],
  73. 'customMobile' => $post['receiveMobile'],
  74. 'fullAddress' => $post['address'],
  75. 'floor' => $post['floor'] ?? '',
  76. 'dist' => $post['dist'] ?? '',
  77. 'lat' => $post['region']['latitude'],
  78. 'long' => $post['region']['longitude'],
  79. 'address' => $post['address'], //'toAddress' => $order['address'],
  80. 'city' => $post['city'],
  81. 'weight' => $post['weight'] ?? 1,
  82. 'remark' => $post['remark'] ?? '',
  83. 'prePrice' => floatval($post['totalPrice'] ?? 0),
  84. 'actPrice' => floatval($post['totalPrice'] ?? 0),
  85. ];
  86. }else{
  87. util::fail('不存在此订单类型');
  88. }
  89. return $order;
  90. }
  91. }