freight.php 2.4 KB

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