freight.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. $diffCost = $phpStatDistance*2;
  50. $cost += $diffCost;
  51. }else{
  52. //8*2
  53. $cost += 16;
  54. }
  55. if($phpStatDistance > 10){
  56. //每新增5km 加6
  57. $diff = ceil(($phpStatDistance - 10) / 5);
  58. $diffCost = $diff*6;
  59. $cost += $diffCost;
  60. }
  61. }
  62. //重量计算
  63. Yii::info("重量为:".$weight);
  64. $weight = 5;
  65. $weight = $weight -2;
  66. if($weight > 0){
  67. if ($weight <=10){
  68. //每增1 kg 加2
  69. $diffCost = $weight*2;
  70. $cost += $diffCost;
  71. $weight = $weight -2;
  72. }else{
  73. //8*2
  74. $cost += 16;
  75. }
  76. if($weight > 10){
  77. //每新增 5kg 加6
  78. $diff = ceil(($weight - 10) / 5);
  79. $diffCost = $diff*6;
  80. $cost += $diffCost;
  81. }
  82. }
  83. return $cost;
  84. }
  85. }