diff --git a/app/Http/Controllers/Mobile/OtherController.php b/app/Http/Controllers/Mobile/OtherController.php index 994cbad..cefa431 100755 --- a/app/Http/Controllers/Mobile/OtherController.php +++ b/app/Http/Controllers/Mobile/OtherController.php @@ -140,26 +140,33 @@ class OtherController extends CommonController $sortName = $all['sort_name'] ?? 'distance'; $sortType = $all['sort_type'] ?? 'asc'; - // 使用更稳定的距离计算公式 + // 使用简单稳定的距离计算公式 $distanceFormula = " - (6371 * 2 * asin( - sqrt( - pow(sin(radians((CAST(company_latitude AS DECIMAL(10,8)) - ?) / 2)), 2) + - cos(radians(?)) * cos(radians(CAST(company_latitude AS DECIMAL(10,8)))) * - pow(sin(radians((CAST(company_longitude AS DECIMAL(10,8)) - ?) / 2)), 2) - ) + (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)))) + )) )) 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('*') - ->selectRaw($distanceFormula, [$latitude, $latitude, $longitude]) + $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('*') + ->selectRaw($distanceFormula, [$latitude, $longitude, $latitude]) ->where(function ($query) use ($all) { if (isset($all['company_name'])) { $query->where('company_name', 'like', '%' . $all['company_name'] . '%');