freight.php 2.4 KB

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