diff --git a/app/Controller/v3/LocationController.php b/app/Controller/v3/LocationController.php index 18ab8f6..05d84a1 100644 --- a/app/Controller/v3/LocationController.php +++ b/app/Controller/v3/LocationController.php @@ -27,7 +27,9 @@ class LocationController extends BaseController */ public function getNearestMarket() { - return $this->success(['market' => Market::query()->find(1)]); + $lng = $this->request->input('lng',0); + $lat = $this->request->input('lat',0); + return $this->success($this->locationService->getNearestMarket($lng,$lat)); } public function getMarketListByLocation() diff --git a/app/Service/v3/Implementations/LocationService.php b/app/Service/v3/Implementations/LocationService.php index d6181a6..89ff26c 100644 --- a/app/Service/v3/Implementations/LocationService.php +++ b/app/Service/v3/Implementations/LocationService.php @@ -4,6 +4,7 @@ namespace App\Service\v3\Implementations; use App\Constants\v3\ErrorCode; use App\Exception\ErrorCodeException; +use App\Model\v3\UserCollection; use App\Service\v3\Interfaces\LocationServiceInterface; use App\Model\v3\Area; use App\Model\v3\Market; @@ -59,6 +60,31 @@ class LocationService implements LocationServiceInterface return $res; } + public function getNearestMarket($lng,$lat) + { + $markets = Market::query()->get()->toArray(); + $result = []; + $distinctMin = 0; + foreach ($markets as &$market){ + $distinct = $this->getDistance($lng,$lat,$market['lng'],$market['lat'],1); + $market['distinct'] = round(($distinct/1000),2).' km'; + if($distinct < $distinctMin || $distinctMin === 0) { + $distinctMin = $distinct; + $result = $market; + } + } + return $result; + } + + /** + * @param $lng1 + * @param $lat1 + * @param $lng2 + * @param $lat2 + * @param int $unit 单位 1:米 2:公里 + * @param int $decimal 精度 保留小数位数 + * @return false|float + */ public function getDistance($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2) { diff --git a/app/Service/v3/Implementations/ShopCartService.php b/app/Service/v3/Implementations/ShopCartService.php index 478308c..1915ab1 100644 --- a/app/Service/v3/Implementations/ShopCartService.php +++ b/app/Service/v3/Implementations/ShopCartService.php @@ -231,7 +231,11 @@ class ShopCartService implements ShopCartServiceInterface bcscale(6); // 查询当前用户的市场下的购物车数据 $carts = ShoppingCart::query() - ->with(['store', 'goods']) + ->with(['store', + 'goods' => function($query) { + $query->withoutGlobalScope('normal'); + } + ]) ->where(['user_id' => $userId]) ->where(['market_id' => $marketId]) ->get(); @@ -267,11 +271,9 @@ class ShopCartService implements ShopCartServiceInterface // 商品是否失效 if ($cart->activity_type == 2) { - $goods = GoodsActivity::query()->find($cart->goods_id); - $res = $this->goodsActivityService->check($goods, $cart->num, $cart->user_id); + $res = $this->goodsActivityService->check($cart->goods, $cart->num, $cart->user_id); } else { - $goods = Goods::query()->find($cart->goods_id); - $res = $this->goodsService->check($goods, $cart->num); + $res = $this->goodsService->check($cart->goods, $cart->num); } if ($res === true) { diff --git a/app/Service/v3/Interfaces/LocationServiceInterface.php b/app/Service/v3/Interfaces/LocationServiceInterface.php index 6acab97..9b7b6a1 100644 --- a/app/Service/v3/Interfaces/LocationServiceInterface.php +++ b/app/Service/v3/Interfaces/LocationServiceInterface.php @@ -14,4 +14,5 @@ interface LocationServiceInterface public function getMarketsInfo(); public function getDistance($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2); public function getDistanceByTencent($lng1, $lat1, $lng2, $lat2); + public function getNearestMarket($lng,$lat); } \ No newline at end of file