DeliveryController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace hd\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use bizGhs\custom\classes\CustomClass;
  5. use Yii;
  6. use biz\shop\classes\ShopClass;
  7. use common\components\delivery\services\DispatchService;
  8. use common\components\util;
  9. use ghs\controllers\BaseController;
  10. use yii\helpers\ArrayHelper;
  11. class DeliveryController extends BaseController
  12. {
  13. // 获取多个平台报价
  14. public function actionAllDeliveryQuotes()
  15. {
  16. $post = Yii::$app->request->post();
  17. $orderTime = date('Y-m-d H:i:s');
  18. // --------- $ghsShopId ----------
  19. $ghsId = $post['ghsId'] ?? 0;
  20. $ghs = GhsClass::getById($ghsId, true);
  21. if (empty($ghs)) {
  22. util::fail('获取批发商失败');
  23. }
  24. $ghsShopId = $ghs->shopId;
  25. // --------- $customId
  26. $customId = $ghs->customId ?? 0;
  27. $custom = CustomClass::getById($customId, true);
  28. if (empty($custom)) {
  29. util::fail('获取custom数据失败');
  30. }
  31. $weight = $post['weight'] ?? 1;
  32. // 生成随机订单号
  33. //$snData = ['shopId' => $ghsShopId, 'mainId' => $ghs->mainId, 'ghsId' => $ghsId, 'customId' => $customId];
  34. //$orderSn = orderSn::getGhsOrderSn($snData);
  35. $prefix = 'XSD_CS-';
  36. $orderSn = $prefix . round(microtime(true) * 1000);
  37. //构建出 Order 数据
  38. $order = [
  39. 'orderSn' => $orderSn,
  40. 'customName' => $custom['name'],
  41. 'customMobile' => $custom['mobile'],
  42. 'fullAddress' => $custom['fullAddress'],
  43. 'floor' => $custom['floor'],
  44. 'dist' => $custom['dist'],
  45. 'lat' => $custom['lat'],
  46. 'long' => $custom['long'],
  47. 'address' => $custom['address'], //'toAddress' => $order['address'],
  48. 'city' => $custom['city'],
  49. 'weight' => $weight,
  50. 'remark' => $post['remark'] ?? '',
  51. 'prePrice' => 10, // TODO 要从前端获取
  52. 'actPrice' => 10, // TODO
  53. ];
  54. $ghsShop = ShopClass::getById($ghsShopId);
  55. $ds = new DispatchService($ghs->mainId);
  56. $platformQuotes = $ds->getAllPlatformPrice($order, $ghsShop, $orderTime);
  57. if(isset($platformQuotes['error'])){
  58. util::fail($platformQuotes['error']);
  59. }
  60. $deliveryList = [];
  61. // 格式化各平台报价为前端展示格式
  62. $deliveryList = $ds->formatPlatformQuotesForDisplay($platformQuotes);
  63. // 按 price 从低到高排序
  64. ArrayHelper::multisort($deliveryList, 'price', SORT_ASC);
  65. $ret['deliveryList'] = $deliveryList;
  66. util::success($ret, "success");
  67. }
  68. }