From 24c0f748936c5652e07eaaa4e5156d6131237077 Mon Sep 17 00:00:00 2001 From: cody <648753004@qq.com> Date: Thu, 28 Aug 2025 15:22:22 +0800 Subject: [PATCH] update --- .../Controllers/Mobile/OtherController.php | 53 +++++++++++-------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/app/Http/Controllers/Mobile/OtherController.php b/app/Http/Controllers/Mobile/OtherController.php index cefa431..f341017 100755 --- a/app/Http/Controllers/Mobile/OtherController.php +++ b/app/Http/Controllers/Mobile/OtherController.php @@ -140,32 +140,19 @@ class OtherController extends CommonController $sortName = $all['sort_name'] ?? 'distance'; $sortType = $all['sort_type'] ?? 'asc'; - // 使用简单稳定的距离计算公式 + // 使用简化的距离计算公式 $distanceFormula = " (6371 * acos( - GREATEST(-1, LEAST(1, - cos(radians(?)) * - cos(radians(CAST(company_latitude AS DECIMAL(10,8)))) * - cos(radians(CAST(company_longitude AS DECIMAL(10,8))) - radians(?)) + - sin(radians(?)) * - sin(radians(CAST(company_latitude AS DECIMAL(10,8)))) - )) + cos(radians(?)) * + cos(radians(CAST(company_latitude AS DECIMAL(10,8)))) * + cos(radians(CAST(company_longitude AS DECIMAL(10,8))) - radians(?)) + + sin(radians(?)) * + sin(radians(CAST(company_latitude AS DECIMAL(10,8)))) )) AS distance "; - $query = Company::with([ - 'users' => function ($query) { - $query->with([ - 'courseSigns' => function ($query) { - $query->with('course')->orderBy('fee_status', 'desc'); - } - ]); - } - ]) - ->whereHas('users', function ($query) { - $query->where('is_schoolmate', 1); - }) - ->select('*') + // 先构建基础查询 + $query = Company::select('*') ->selectRaw($distanceFormula, [$latitude, $longitude, $latitude]) ->where(function ($query) use ($all) { if (isset($all['company_name'])) { @@ -179,7 +166,19 @@ class OtherController extends CommonController ->whereRaw('company_longitude REGEXP \'^-?[0-9]+\.?[0-9]*$\'') ->whereRaw('company_latitude REGEXP \'^-?[0-9]+\.?[0-9]*$\'') ->whereRaw('CAST(company_longitude AS DECIMAL(10,8)) BETWEEN -180 AND 180') - ->whereRaw('CAST(company_latitude AS DECIMAL(10,8)) BETWEEN -90 AND 90'); + ->whereRaw('CAST(company_latitude AS DECIMAL(10,8)) BETWEEN -90 AND 90') + ->whereHas('users', function ($query) { + $query->where('is_schoolmate', 1); + }) + ->with([ + 'users' => function ($query) { + $query->with([ + 'courseSigns' => function ($query) { + $query->with('course')->orderBy('fee_status', 'desc'); + } + ]); + } + ]); // 根据排序字段进行排序 if ($sortName === 'distance') { @@ -191,6 +190,16 @@ class OtherController extends CommonController $result = $query->paginate($pageSize, ['*'], 'page', $page); + // 添加调试信息 + \Log::info('Distance calculation debug', [ + 'input_lat' => $latitude, + 'input_lon' => $longitude, + 'target_lat' => '31.326810', + 'target_lon' => '120.421618', + 'expected_distance' => 13.22, + 'actual_distance' => $result->first() ? $result->first()->distance : 'N/A' + ]); + return $this->success($result); }