| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- /**
- * User: admin
- * Date Time: 2021/5/21 20:50
- */
- namespace common\components;
- //运费计算
- class freight
- {
- //模仿达达运费计算
- /**
- * 0km < d <=2 8元
- * 2km < d <=10 每新增1km加2
- * 10km < d <=50 每新增5km 加6
- *
- * 重量
- * 0 <w <=2 kg 不加价
- * 2kg < w <= 10 kg 每增1 kg 加2
- * 10 kg < w <= 50kg 每新增 5kg 加6
- */
- /**
- * @param $startLat
- * @param $starLng
- * @param $endLat
- * @param $endLng
- * @param $weight 重量kg
- */
- public static function getCost($startLat,$starLng,$endLat,$endLng,$weight)
- {
- if($weight > 50 ){
- util::fail("重量太重了哦!");
- }
- //后端计算距离 m
- $phpStatDistance = util::getDistance($startLat, $starLng, $endLat, $endLng);
- $phpStatDistance = $phpStatDistance/1000;
- if($phpStatDistance > 50) {
- util::fail("距离太远了哦!");
- }
- $cost = 8; //起步价
- //距离计算
- if($phpStatDistance > 10){
- //每新增5km 加6
- $diff = ceil(($phpStatDistance - 10) / 5);
- $diffCost = $diff*6;
- $cost += $diffCost;
- }elseif ($phpStatDistance <=10 && $phpStatDistance > 2){
- //每新增1km加2
- $diff = ceil(($phpStatDistance - 2));
- $diffCost = $diff*2;
- $cost += $diffCost;
- }
- //重量计算
- if($weight > 10){
- //每新增 5kg 加6
- $diff = ceil(($weight - 10) / 5);
- $diffCost = $diff*6;
- $cost += $diffCost;
- }elseif ($weight <=10 && $weight > 2){
- //每增1 kg 加2
- $diff = ceil(($weight - 2));
- $diffCost = $diff*2;
- $cost += $diffCost;
- }
- return $cost;
- }
- }
|