Laravel 计算位置距离并顺序
原创 1,
city => $city
]);
$total = $model->count();
// 获取select字段
$model->select(self::getSelect($latitude, $longitude));
// orderby
if (isset($user\_distant) && $user\_distant) {
$model->orderBy("user\_distant", "asc");
} else {
$model->orderBy("shop\_id", "desc");
}
$data = $model->paginate($per\_page)->toArray();
return self::formatBody([
list => $data[data],
paged => self::formatPaged($page, $per\_page, $total)
]);
}
/**
* 门店搜索列表
*
* @param array $attributes
* [description]
* @return [type] [description]
*/
public static function getSearchList(array $attributes)
{
extract($attributes);
// 地区编码
if (! isset($city) || ! $city) {
return self::formatError(100001, 地区编码不能为空!);
}
if (! isset($keyword) || ! $keyword) {
return self::formatError(100001, 搜索内容不能为空!);
}
$model = self::where([
status => 1,
city => $city
])->where(function ($query) use ($keyword) {
// keywords
$query->where(shop\_name, like, % . strip\_tags($keyword) . %);
});
$total = $model->count();
// 获取select字段
$model->select(self::getSelect($latitude, $longitude));
// orderby
if (isset($user\_distant) && $user\_distant) {
$model->orderBy("user\_distant", "asc");
} else {
$model->orderBy("shop\_id", "desc");
}
$data = $model->paginate($per\_page)->toArray();
return self::formatBody([
list => $data[data],
paged => self::formatPaged($page, $per\_page, $total)
]);
}
/**
* 门店搜索地址列表
*
* @param array $attributes
* [description]
* @return [type] [description]
*/
public static function getSearchListAddress(array $attributes)
{
extract($attributes);
// 地区编码
if (! isset($goods\_id) || ! $goods\_id) {
return self::formatError(100001, 产品信息错误!);
}
if (! isset($keyword) || ! $keyword) {
$keyword = ;
}
$model = GoodsShop::leftJoin(shop, goods\_shop.shop\_id, =, shop.shop\_id)->where(goods\_shop.goods\_id, $goods\_id)
->where(shop.status, 1)
->where(function ($query) use ($keyword) {
// keywords
if ($keyword) {
$query->where(shop.shop\_name, like, % . strip\_tags($keyword) . %);
}
});
$total = $model->count();
// 获取select字段
$model->select(self::getSelectAddress($latitude, $longitude));
// orderby
if (isset($user\_distant) && $user\_distant) {
$model->orderBy("user\_distant", "asc");
} else {
$model->orderBy("shop.shop\_id", "desc");
}
$data = $model->paginate($per\_page)->toArray();
return self::formatBody([
list => $data[data],
paged => self::formatPaged($page, $per\_page, $total)
]);
}
/**
* 门店详情
*
* @param array $attributes
* [description]
* @return [type] [description]
*/
public static function getDetail(array $attributes)
{
extract($attributes);
$data = self::where(shop\_id, $shop\_id)->first()->toArray();
// 获取门店商品
$goods = GoodsShop::leftJoin(goods, goods\_shop.goods\_id, =, goods.goods\_id)->where(goods\_shop.shop\_id, $shop\_id)
->where(goods.is\_on\_sale, 1)
->select(goods\_shop.*, goods.goods\_name, goods.market\_price, goods.shop\_price, goods.goods\_thumb, goods.goods\_brief, goods.get\_method)
->get()
->toArray();
return self::formatBody([
info => $data,
goods => $goods
]);
}
/**
* 门店地址
*
* @param array $attributes
* [description]
* @return [type] [description]
*/
public static function getAddress(array $attributes)
{
extract($attributes);
$data = self::where(shop\_id, $shop\_id)->first([
shop\_id,
shop\_name,
address,
lng,
lat,
contact\_man,
contact\_phone
])->toArray();
return self::formatBody([
info => $data
]);
}
public static function getSelect($lat, $lng)
{
if ($lat && $lng) {
$select = [
*,
\DB::raw("ROUND(
6378.138 * 2 * ASIN(
SQRT(
POW(
SIN(
(
{$lat} * PI() / 180 - lat * PI() / 180
) / 2
),
2
) + COS({$lat} * PI() / 180) * COS(lat * PI() / 180) * POW(
SIN(
(
{$lng} * PI() / 180 - lng * PI() / 180
) / 2
),
2
)
)
) * 1000
) AS user\_distant")
];
} else {
$select = [
*,
\DB::raw("0 as user\_distant")
];
}
return $select;
}
public static function getSelectAddress($lat, $lng)
{
if ($lat && $lng) {
$select = [
goods\_shop.*,
shop.logo,
shop.shop\_name,
shop.address,
shop.lng,
shop.lat,
shop.contact\_man,
shop.contact\_phone,
\DB::raw("ROUND(
6378.138 * 2 * ASIN(
SQRT(
POW(
SIN(
(
{$lat} * PI() / 180 - (ecs\_shop.lat) * PI() / 180
) / 2
),
2
) + COS({$lat} * PI() / 180) * COS((ecs\_shop.lat) * PI() / 180) * POW(
SIN(
(
{$lng} * PI() / 180 - (ecs\_shop.lng) * PI() / 180
) / 2
),
2
)
)
) * 1000
) AS user\_distant")
];
} else {
$select = [
goods\_shop.*,
shop.logo,
shop.shop\_name,
shop.address,
shop.lng,
shop.lat,
shop.contact\_man,
shop.contact\_phone,
\DB::raw("0 as user\_distant")
];
}
return $select;
}
} 版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除
itfan123




