freight.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * User: admin
  4. * Date Time: 2021/5/21 20:50
  5. */
  6. namespace common\components;
  7. //运费计算
  8. class freight
  9. {
  10. //模仿达达运费计算
  11. /**
  12. * 0km < d <=2 8元
  13. * 2km < d <=10 每新增1km加2
  14. * 10km < d <=50 每新增5km 加6
  15. *
  16. * 重量
  17. * 0 <w <=2 kg 不加价
  18. * 2kg < w <= 10 kg 每增1 kg 加2
  19. * 10 kg < w <= 50kg 每新增 5kg 加6
  20. */
  21. /**
  22. * @param $startLat
  23. * @param $starLng
  24. * @param $endLat
  25. * @param $endLng
  26. * @param $weight 重量kg
  27. */
  28. public static function getCost($startLat,$starLng,$endLat,$endLng,$weight)
  29. {
  30. if($weight > 50 ){
  31. return "";
  32. // util::fail("重量太重了哦!");
  33. }
  34. //后端计算距离 m
  35. $phpStatDistance = util::getDistance($startLat, $starLng, $endLat, $endLng);
  36. $phpStatDistance = $phpStatDistance/1000;
  37. if($phpStatDistance > 50) {
  38. return "";
  39. // util::fail("距离太远了哦!");
  40. }
  41. $cost = 8; //起步价
  42. //距离计算
  43. if($phpStatDistance > 10){
  44. //每新增5km 加6
  45. $diff = ceil(($phpStatDistance - 10) / 5);
  46. $diffCost = $diff*6;
  47. $cost += $diffCost;
  48. }elseif ($phpStatDistance <=10 && $phpStatDistance > 2){
  49. //每新增1km加2
  50. $diff = ceil(($phpStatDistance - 2));
  51. $diffCost = $diff*2;
  52. $cost += $diffCost;
  53. }
  54. //重量计算
  55. if($weight > 10){
  56. //每新增 5kg 加6
  57. $diff = ceil(($weight - 10) / 5);
  58. $diffCost = $diff*6;
  59. $cost += $diffCost;
  60. }elseif ($weight <=10 && $weight > 2){
  61. //每增1 kg 加2
  62. $diff = ceil(($weight - 2));
  63. $diffCost = $diff*2;
  64. $cost += $diffCost;
  65. }
  66. return $cost;
  67. }
  68. }