Browse Source

购物车 获取最近市场

master
Lemon 5 years ago
parent
commit
4ecf195d3c
  1. 4
      app/Controller/v3/LocationController.php
  2. 26
      app/Service/v3/Implementations/LocationService.php
  3. 12
      app/Service/v3/Implementations/ShopCartService.php
  4. 1
      app/Service/v3/Interfaces/LocationServiceInterface.php

4
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()

26
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)
{

12
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) {

1
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);
}
Loading…
Cancel
Save