diff --git a/app/Constants/v3/ErrorCode.php b/app/Constants/v3/ErrorCode.php index 9cfcb43..75df14e 100644 --- a/app/Constants/v3/ErrorCode.php +++ b/app/Constants/v3/ErrorCode.php @@ -267,4 +267,19 @@ class ErrorCode extends AbstractConstants * @Message("可提现余额不足") */ const STORE_WITHDRAW_INSUFFICIENT_BALANCE = 1252; + + /************************************/ + /* 定位相关 1301-1350 */ + /************************************/ + /** + * 定位失败 + * @Message("定位失败:") + */ + const LOCATION_ERROR = 1301; + + /** + * 定位失败 + * @Message("超出配送范围") + */ + const LOCATION_LONG_DISTANCE= 1302; } \ No newline at end of file diff --git a/app/Controller/v3/OrderOnlineController.php b/app/Controller/v3/OrderOnlineController.php index 30760ec..5e544e6 100644 --- a/app/Controller/v3/OrderOnlineController.php +++ b/app/Controller/v3/OrderOnlineController.php @@ -15,6 +15,7 @@ use App\Service\v3\Implementations\PaymentService; use App\Service\v3\Interfaces\CouponRecServiceInterface; use App\Service\v3\Interfaces\SeparateAccountsServiceInterface; use App\Service\v3\Interfaces\ShopCartServiceInterface; +use App\Service\v3\Interfaces\UserAddressServiceInterface; use Hyperf\DbConnection\Db; use Hyperf\Di\Annotation\Inject; use App\Service\v3\Interfaces\OrderOnlineServiceInterface; @@ -59,6 +60,12 @@ class OrderOnlineController extends BaseController */ protected $separateAccountsService; + /** + * @Inject + * @var UserAddressServiceInterface + */ + protected $userAddressService; + /* * 如果没有绑手机号去绑定页面 * 收货地址接口 @@ -74,12 +81,18 @@ class OrderOnlineController extends BaseController $marketId = $this->request->input('market_id'); $shopcartIds = $this->request->input('shopcart_ids'); //判断用户有没有绑定手机 - // $telExists = $this->userBindTelService->check($params['user_id']); - // if(!$telExists){ - // throw new ErrorCodeException(ErrorCode::NOT_BIND_TEL_ERROR); - // } + $telExists = $this->userBindTelService->check($userId); + if(!$telExists){ + throw new ErrorCodeException(ErrorCode::NOT_BIND_TEL_ERROR); + } //获取用户收货地址 - $res['address'] = UserAddress::query()->find(15); + $address = UserAddress::query()->where([ + ['user_id','=',$userId], + ['is_default','=',1], + ]) + ->select('id') + ->first(); + $res['location'] = $this->userAddressService->getAddressAndDistributionRrice($address->id,$marketId); //返回预约送达时间 数组 $res['appointment_time'] = $this->appointmentTimeService->do(); // diff --git a/app/Controller/v3/UserAddressController.php b/app/Controller/v3/UserAddressController.php index f95d097..3687345 100644 --- a/app/Controller/v3/UserAddressController.php +++ b/app/Controller/v3/UserAddressController.php @@ -59,4 +59,13 @@ class UserAddressController extends BaseController $res = $this->userAddressService->setDefault($userId,$userAddressId); return $this->success($res); } + + public function getAddressAndDistributionRrice() + { + $userAddressId = $this->request->input('user_address_id'); + $marketId = $this->request->input('market_id'); + $res = $this->userAddressService->getAddressAndDistributionRrice($userAddressId,$marketId); + return $this->success(['location' => $res]); + } + } \ No newline at end of file diff --git a/app/Service/v3/Implementations/LocationService.php b/app/Service/v3/Implementations/LocationService.php index f34afb6..a5af342 100644 --- a/app/Service/v3/Implementations/LocationService.php +++ b/app/Service/v3/Implementations/LocationService.php @@ -48,7 +48,7 @@ class LocationService implements LocationServiceInterface return $res; } - function getDistance($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2) + public function getDistance($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2) { $EARTH_RADIUS = 6370.996; // 地球半径系数 @@ -78,4 +78,16 @@ class LocationService implements LocationServiceInterface { return Market::where('status',1)->get(); } + + //获取骑行距离 + public function getDistanceByTencent($lng1, $lat1, $lng2, $lat2) + { + $url = 'https://apis.map.qq.com/ws/direction/v1/'; + /** + * $tyep driving 驾车 walking 步行 bicycling 骑行 transit 公交 + */ + $type = 'bicycling'; + $key = 'GB3BZ-7W2CU-LW3VH-B7ZIF-XRKSF-D3FOL'; + return file_get_contents($url.$type.'/?from='.$lat1.','.$lng1.'&to='.$lat2.','.$lng2.'&output=json&key='.$key); + } } \ No newline at end of file diff --git a/app/Service/v3/Implementations/UserAddressService.php b/app/Service/v3/Implementations/UserAddressService.php index 9937df2..cc6912b 100644 --- a/app/Service/v3/Implementations/UserAddressService.php +++ b/app/Service/v3/Implementations/UserAddressService.php @@ -2,11 +2,21 @@ namespace App\Service\v3\Implementations; +use App\Constants\v3\ErrorCode; +use App\Exception\ErrorCodeException; +use App\Model\v3\Market; use App\Model\v3\UserAddress; +use App\Service\v3\Interfaces\LocationServiceInterface; use App\Service\v3\Interfaces\UserAddressServiceInterface; +use Hyperf\Di\Annotation\Inject; class UserAddressService implements UserAddressServiceInterface { + /** + * @Inject + * @var LocationServiceInterface + */ + protected $locationService; public function do($userAddressId,$user_id,$user_name,$address,$doorplate,$gender,$lat,$lng,$tel,$tags) { @@ -55,4 +65,54 @@ class UserAddressService implements UserAddressServiceInterface { return UserAddress::query()->where('user_id',$userId)->get(); } + + /** + * @param $userAddressId + * @param $marketId + * @return false|float + */ + public function getAddressAndDistributionRrice($userAddressId,$marketId) + { + $address = $this->get($userAddressId); + $market = Market::query()->select('lng','lat')->find($marketId); + $result = $this->locationService->getDistanceByTencent($address->lng,$address->lat,$market->lng,$market->lat); + $resultArr = json_decode($result,true); + if($resultArr['status'] > 0) + { + throw new ErrorCodeException(ErrorCode::LOCATION_ERROR,$resultArr['message']); + } + $distance = $resultArr['result']['routes'][0]['distance']; + $distributionRrice = $this->calculateDistributionRrice($distance); + $res['address'] = $address; + $res['distribution_price'] = $distributionRrice; + return $res; + } + + /** + * @param $distance + * @return false|float + */ + function calculateDistributionRrice($distance) + { + $km = ceil($distance/1000); + switch ($km){ + case ($km > 3 && $km < 5) : + $distributionRrice = bcmul(0.70,($km-3),2); + break; + case ($km >= 5 && $km < 7) : + $distributionRrice = bcmul(1.00,($km-3),2); + break; + case ($km >= 7 && $km < 10) : + $distributionRrice = bcmul(1.50,($km-3),2); + break; + case ($km >= 10) : + throw new ErrorCodeException(ErrorCode::LOCATION_LONG_DISTANCE); + break; + default: + $distributionRrice = 0; + break; + } + $distributionRrice = bcadd($distributionRrice,3.50,2); + return $distributionRrice; + } } \ No newline at end of file diff --git a/app/Service/v3/Interfaces/LocationServiceInterface.php b/app/Service/v3/Interfaces/LocationServiceInterface.php index 688ae98..68e659e 100644 --- a/app/Service/v3/Interfaces/LocationServiceInterface.php +++ b/app/Service/v3/Interfaces/LocationServiceInterface.php @@ -12,4 +12,6 @@ interface LocationServiceInterface public function getMarketListByLocation($cityIds); public function searchMarket($key,$cityId = 2163); public function getMarketsInfo(); + public function getDistance($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2); + public function getDistanceByTencent($lng1, $lat1, $lng2, $lat2); } \ No newline at end of file diff --git a/app/Service/v3/Interfaces/UserAddressServiceInterface.php b/app/Service/v3/Interfaces/UserAddressServiceInterface.php index df5bc36..4b2b5dc 100644 --- a/app/Service/v3/Interfaces/UserAddressServiceInterface.php +++ b/app/Service/v3/Interfaces/UserAddressServiceInterface.php @@ -12,4 +12,5 @@ interface UserAddressServiceInterface public function get($userAddressId); public function getList($userId); public function setDefault($userId,$userAddressId); + public function getAddressAndDistributionRrice($userAddressId,$marketId); } \ No newline at end of file diff --git a/config/routes.php b/config/routes.php index 1e7005a..876a8f4 100644 --- a/config/routes.php +++ b/config/routes.php @@ -138,6 +138,7 @@ Router::addGroup('/v3/', function () { Router::post('user/oflOrders', 'App\Controller\v3\OrderListController@offlineForUser'); Router::post('shopCart/delete', 'App\Controller\v3\ShopCartUpdateController@delete'); Router::post('withdraw/pageByStore', 'App\Controller\v3\WithdrawController@pageByStore'); + Router::post('userAddress/getAddressAndDistributionRrice', 'App\Controller\v3\UserAddressController@getAddressAndDistributionRrice'); Router::post('withdraw/applyByStore', 'App\Controller\v3\WithdrawController@applyByStore'); },['middleware' => [\App\Middleware\Auth\ApiMiddleware::class, \App\Middleware\Auth\UserMiddleware::class]]);