本地生活商品列表添加经纬度排序

This commit is contained in:
wangxiaowei 2024-10-19 10:56:04 +08:00
parent 65b22bd2aa
commit de8cbd732a

@ -135,7 +135,12 @@ class LocalStoreLogic extends Logic
}
// 经纬度排序
$distance_sql = "6371 * acos(cos(radians(:user_latitude)) * cos(radians(latitude)) * cos(radians(longitude) - radians(:user_longitude)) + sin(radians(:user_latitude)) * sin(radians(latitude))) AS distance";
if (isset($data['distance_sort'])) {
$order = ['distance' => $data['distance_sort']];
}
// 经纬度排序
$distance_sql = "6371 * acos(cos(radians(" . $data['latitude'] . ")) * cos(radians(latitude)) * cos(radians(longitude) - radians(" . $data['longitude'] . ")) + sin(radians(" . $data['latitude'] . ")) * sin(radians(latitude))) AS distance";
// 数量统计
$count = LocalStore::alias('ls')
@ -146,38 +151,21 @@ class LocalStoreLogic extends Logic
->count();
// 列表
if (isset($data['distance_sort'])) {
// 经纬度排序
$lists = LocalStore::alias('ls')
->join('local_store_goods lsg', 'ls.lstore_id = lsg.lstore_id')
->where(['ls.is_delete' => 0, 'ls.store_state' => 1, 'lsg.state' => 1, 'lsg.audit_status' => 1, 'lsg.delete_time' => 0])
->where($where)
->whereNotNull('ls.shopLogo')
->order($order)
->page($data['page_no'], $data['page_size'])
->field('ls.shopName, ls.longitude, ls.latitude, lsg.id, lsg.lstore_id, lsg.title, lsg.price, ' . $distance_sql)
->bind([
'user_latitude' => $data['latitude'],
'user_longitude' => $data['longitude'],
])
->order('distance', $data['distance_sort'])
->select();
} else {
$lists = LocalStore::alias('ls')
->join('local_store_goods lsg', 'ls.lstore_id = lsg.lstore_id')
->where(['ls.is_delete' => 0, 'ls.store_state' => 1, 'lsg.state' => 1, 'lsg.audit_status' => 1, 'lsg.delete_time' => 0])
->where($where)
->whereNotNull('ls.shopLogo')
->order($order)
->page($data['page_no'], $data['page_size'])
->field('ls.shopName, ls.longitude, ls.latitude, lsg.id, lsg.lstore_id, lsg.title, lsg.price')
->select();
}
$lists = LocalStore::alias('ls')
->join('local_store_goods lsg', 'ls.lstore_id = lsg.lstore_id')
->where(['ls.is_delete' => 0, 'ls.store_state' => 1, 'lsg.state' => 1, 'lsg.audit_status' => 1, 'lsg.delete_time' => 0])
->where($where)
->whereNotNull('ls.shopLogo')
->order($order)
->page($data['page_no'], $data['page_size'])
->field('ls.shopName, ls.longitude, ls.latitude, lsg.id, lsg.lstore_id, lsg.title, lsg.price, ' . $distance_sql)
->select();
foreach ($lists as $key => &$value) {
$pic = LocalStoreGoodsPic::where('lsg_id', $value['id'])->field('url')->find();
$lists[$key]['url'] = UrlServer::getFileUrl($pic['url']);
$lists[$key]['distance'] = round($this->distance($data['latitude'], $data['longitude'], $value['latitude'], $value['longitude']) / 1000, 2) . "km";
$lists[$key]['distance'] = number_format($value['distance'], 2, '.', '') . "km";
// $lists[$key]['distance'] = round($this->distance($data['latitude'], $data['longitude'], $value['latitude'], $value['longitude']) / 1000, 2) . "km";
$lists[$key]['store_nickname'] = $value["shopName"];
}