ExpressController.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace mall\controllers;
  3. use bizMall\merchant\services\ExpressService;
  4. use bizMall\merchant\services\ShopService;
  5. use common\components\noticeUtil;
  6. use Yii;
  7. use yii\web\Controller;
  8. use common\components\util;
  9. use common\components\mapUtil;
  10. class ExpressController extends BaseController
  11. {
  12. //计算距离和运费【另外还有个地方要同步修改,关键词calc_freight_distance】 ssh 20221003
  13. public function actionGetUserDistance()
  14. {
  15. $get = Yii::$app->request->get();
  16. $lng = $get['lng'] ?? '';
  17. $lat = $get['lat'] ?? '';
  18. $weight = 1.5;
  19. if (empty($lng) || empty($lat)) {
  20. util::fail('请填写收花地址');
  21. }
  22. $shop = $this->shop;
  23. $shopLong = $shop->long ?? '';
  24. $shopLat = $shop->lat ?? '';
  25. if (empty($shopLong) || empty($shopLat)) {
  26. util::fail('门店地址有问题');
  27. }
  28. //基础配送费8元,0-5公里,每公里+1元,5-100公里,6元起,每公里+2元
  29. //5-10公斤,2元起,每公斤+2元,10-15公斤,+12元,超过15公斤,+20元
  30. //00:00 - 06:00 +6元,22:00-24:00 +4元
  31. //恶劣天气,下单高峰期会临时调整价格
  32. //起步价
  33. $baseFee = 8;
  34. $distance = mapUtil::calculateDistance($shopLong, $shopLat, $lng, $lat);
  35. //0-5公里,每公里+1元,5-100公里,6元起,每公里+2元
  36. $formatDistance = ceil($distance / 1000);
  37. $startKiloFee = 6;
  38. if ($formatDistance < 5) {
  39. $addDistanceFee = bcmul($formatDistance, 1);
  40. } elseif ($formatDistance == 5) {
  41. $addDistanceFee = $startKiloFee;
  42. } else {
  43. $overKilo = bcsub($formatDistance, 5);
  44. $overFee = bcmul($overKilo, 2);
  45. $addDistanceFee = bcadd($startKiloFee, $overFee);
  46. }
  47. //5-10公斤,2元起,每公斤+2元,10-15公斤,+12元,超过15公斤,+20元
  48. $formatWeight = ceil($weight);
  49. if ($formatWeight > 15) {
  50. $addWeightFee = 20;
  51. } elseif ($formatWeight >= 10 && $formatWeight <= 15) {
  52. $addWeightFee = 12;
  53. } elseif ($formatWeight >= 5 && $formatWeight < 10) {
  54. $overWeight = bcsub($formatWeight, 4);
  55. $addWeightFee = bcmul($overWeight, 2);
  56. } else {
  57. $addWeightFee = 0;
  58. }
  59. $addFee = bcadd($addDistanceFee, $addWeightFee, 2);
  60. $totalFee = bcadd($baseFee, $addFee, 2);
  61. //特殊时段费
  62. $timeFee = 0;
  63. $hour = date("G");
  64. //22:00 - 24:00 +4元
  65. if (in_array($hour, [22, 23])) {
  66. $timeFee = 4;
  67. }
  68. //00:00 - 06:00 +6元
  69. if (in_array($hour, [0, 1, 2, 3, 4, 5])) {
  70. $timeFee = 6;
  71. }
  72. $totalFee = bcadd($totalFee, $timeFee, 2);
  73. //恶劣天气和下单高峰费用
  74. $weatherFee = 0;
  75. $totalFee = bcadd($totalFee, $weatherFee, 2);
  76. $showDistance = sprintf("%.1f", ($distance / 1000));
  77. noticeUtil::push("商城订单 距离:{$distance} {$showDistance} 重量:{$weight} 费用:{$totalFee}", '15280215347');
  78. return ['distance' => $distance, 'showDistance' => $showDistance, 'fee' => $totalFee];
  79. }
  80. }