feat: 优化了店铺获取逻辑,根据经纬度获取最近的店铺,并更新了相关API接口。

This commit is contained in:
mkm 2024-08-19 11:10:41 +08:00
parent 86235aed08
commit 98abb78670
4 changed files with 27 additions and 28 deletions

View File

@ -207,8 +207,23 @@ class IndexController extends BaseApiController
*/ */
public function config() public function config()
{ {
//处理返回最近的店铺
$params=$this->request->get();
if ((isset($params['lat']) && $params['lat'] != '') && (isset($params['long']) && $params['long'] != '')) {
$latitude = $params['lat'];
$longitude = $params['long'];
// 计算距离的SQL表达式
$distanceSql = "SQRT(POW(69.1 * (latitude - {$latitude}), 2) +
POW(69.1 * ({$longitude} - longitude) * COS(latitude / 57.3), 2))";
$find = SystemStore::field("id, name, {$distanceSql} AS distance")
->where('latitude', '<>', '')
->where('longitude', '<>', '')
->order('distance', 'asc') // 根据距离排序
->find();
} else {
$store_id = getenv('STORE_ID') ?? 1; $store_id = getenv('STORE_ID') ?? 1;
$find = SystemStore::where('id', $store_id)->find(); $find = SystemStore::where('id', $store_id)->find();
}
$list = [ $list = [
'id' => $find['id'], 'id' => $find['id'],
'store_name' => $find['name'], 'store_name' => $find['name'],

View File

@ -35,7 +35,7 @@ class CartList extends BaseAdminDataLists implements ListsSearchInterface, Lists
*/ */
public function setSearch(): array public function setSearch(): array
{ {
return []; return ['='=>['store_id']];
} }

View File

@ -227,30 +227,14 @@ class OrderLogic extends BaseLogic
if (isset($params['source']) && $params['source'] > 0) { if (isset($params['source']) && $params['source'] > 0) {
$order['source'] = $params['source']; $order['source'] = $params['source'];
} }
//处理返回最近的店铺
if ((isset($params['lat']) && $params['lat'] != '') && (isset($params['long']) && $params['long'] != '')) {
$storeAll = SystemStore::field('id,name,phone,address,detailed_address,latitude,longitude')->select()->toArray();
$nearestStore = null;
$minDistance = PHP_FLOAT_MAX;
foreach ($storeAll as $value) {
$value['distance'] = haversineDistance($value['latitude'], $value['longitude'], $params['lat'], $params['long']);
if ($value['distance'] < $minDistance) {
$minDistance = $value['distance'];
$nearestStore = $value;
}
}
$store['near_store'] = [];
if ($nearestStore) {
$store['near_store'] = $nearestStore;
}
} else {
if (isset($params['store_id']) && $params['store_id'] > 0) { if (isset($params['store_id']) && $params['store_id'] > 0) {
$store_id = $params['store_id']; $store_id = $params['store_id'];
} else { } else {
$store_id = getenv('STORE_ID') ?? 1; $store_id = getenv('STORE_ID') ?? 1;
} }
$store['near_store'] = SystemStore::where('id', $store_id)->field('id,name,phone,address,detailed_address,latitude,longitude')->find() ?? []; $store['near_store'] = SystemStore::where('id', $store_id)->field('id,name,phone,address,detailed_address,latitude,longitude')->find() ?? [];
}
if ($user) { if ($user) {
$order['address_id'] = UserAddress::where('uid', $user['id'])->where('is_default', 1)->value('id') ?? 0; $order['address_id'] = UserAddress::where('uid', $user['id'])->where('is_default', 1)->value('id') ?? 0;
} }
@ -264,13 +248,13 @@ class OrderLogic extends BaseLogic
// 使用DateTime::format()方法获取时间并比较 // 使用DateTime::format()方法获取时间并比较
if ($currentTime->format('H:i') > $fourPM->format('H:i')) { if ($currentTime->format('H:i') > $fourPM->format('H:i')) {
$currentDate = date('Y-m-d'); $currentDate = date('Y-m-d');
$alert='当前时间超过配送截止时间16:00,若下单,物品送达时间为'.date('Y-m-d', strtotime($currentDate. '+2 days')); $alert = '当前时间超过配送截止时间16:00,若下单,物品送达时间为' . date('Y-m-d', strtotime($currentDate . '+2 days'));
} }
} catch (\Exception $e) { } catch (\Exception $e) {
self::setError($e->getMessage()); self::setError($e->getMessage());
return false; return false;
} }
return ['order' => $order, 'cart_list' => $cart_select, 'shopInfo' => $store['near_store'],'alert'=>$alert]; return ['order' => $order, 'cart_list' => $cart_select, 'shopInfo' => $store['near_store'], 'alert' => $alert];
} }
/** /**

View File

@ -11,7 +11,7 @@ class OrderDetail
{ {
public $warehouse='海吉星仓库'; public $warehouse='海吉星仓库';
public $company='投里海'; public $company='投里海';
public $address='泸州龙马潭区海吉星122栋'; public $address='泸州龙马潭区海吉星122栋';
public $phone='08302669767'; public $phone='08302669767';
public $tel='17309099881'; public $tel='17309099881';