freight.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. util::fail("重量太重了哦!");
  32. }
  33. //后端计算距离 m
  34. $phpStatDistance = util::getDistance($startLat, $starLng, $endLat, $endLng);
  35. $phpStatDistance = $phpStatDistance/1000;
  36. if($phpStatDistance > 50) {
  37. util::fail("距离太远了哦!");
  38. }
  39. $cost = 8; //起步价
  40. //距离计算
  41. if($phpStatDistance > 10){
  42. //每新增5km 加6
  43. $diff = ceil(($phpStatDistance - 10) / 5);
  44. $diffCost = $diff*6;
  45. $cost += $diffCost;
  46. }elseif ($phpStatDistance <=10 && $phpStatDistance > 2){
  47. //每新增1km加2
  48. $diff = ceil(($phpStatDistance - 2));
  49. $diffCost = $diff*2;
  50. $cost += $diffCost;
  51. }
  52. //重量计算
  53. if($weight > 10){
  54. //每新增 5kg 加6
  55. $diff = ceil(($weight - 10) / 5);
  56. $diffCost = $diff*6;
  57. $cost += $diffCost;
  58. }elseif ($weight <=10 && $weight > 2){
  59. //每增1 kg 加2
  60. $diff = ceil(($weight - 2));
  61. $diffCost = $diff*2;
  62. $cost += $diffCost;
  63. }
  64. return $cost;
  65. }
  66. }