|
@@ -3,8 +3,8 @@
|
|
|
namespace App\Services\Client;
|
|
|
|
|
|
use App\Models\CoachUser;
|
|
|
-use App\Models\MemberUser;
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
+use Illuminate\Support\Facades\Redis;
|
|
|
|
|
|
class CoachService
|
|
|
{
|
|
@@ -13,6 +13,8 @@ class CoachService
|
|
|
*/
|
|
|
public function getCoachList($latitude, $longitude)
|
|
|
{
|
|
|
+ $page = request()->get('page', 1);
|
|
|
+ $perPage = request()->get('per_page', 15);
|
|
|
// 获取当前用户
|
|
|
$user = Auth::user();
|
|
|
|
|
@@ -21,9 +23,24 @@ class CoachService
|
|
|
throw new \Exception('用户状态异常');
|
|
|
}
|
|
|
|
|
|
- // 获取附近的技师
|
|
|
+ // 使用 Redis 的 georadius 命令获取附近的技师 ID
|
|
|
+ $nearbyCoachIds = Redis::georadius('coach_locations', $longitude, $latitude, 40, 'km', ['WITHDIST']);
|
|
|
+
|
|
|
+ $coachData = array_map(function ($item) {
|
|
|
+ [$id, $type] = explode('_', $item[0]);
|
|
|
+
|
|
|
+ return ['id' => $id, 'type' => $type, 'distance' => $item[1]];
|
|
|
+ }, $nearbyCoachIds);
|
|
|
+
|
|
|
+ // 提取所有的id
|
|
|
+ $coachIds = array_unique(array_column($coachData, 'id'));
|
|
|
+
|
|
|
+ // 分页截取 coachIds
|
|
|
+ $paginatedCoachIds = array_slice($coachIds, ($page - 1) * $perPage, $perPage);
|
|
|
+
|
|
|
+ // 查询数据库获取技师信息
|
|
|
$coaches = CoachUser::query()
|
|
|
- ->where('state', 'enable')
|
|
|
+ ->whereIn('id', $paginatedCoachIds)
|
|
|
->whereHas('info', function ($query) {
|
|
|
$query->where('state', 'approved');
|
|
|
})
|
|
@@ -34,19 +51,14 @@ class CoachService
|
|
|
$query->where('state', 'approved');
|
|
|
})
|
|
|
->with(['info:id,nickname,avatar,gender'])
|
|
|
- ->with(['locations'])
|
|
|
- ->whereHas('locations', function ($query) use ($latitude, $longitude) {
|
|
|
- $query->where('state', 'enable')
|
|
|
- ->whereRaw(
|
|
|
- '(6371 * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?)) + sin(radians(?)) * sin(radians(latitude)))) < ?',
|
|
|
- [$latitude, $longitude, $latitude, 40]
|
|
|
- );
|
|
|
- })
|
|
|
- ->selectRaw(
|
|
|
- '*, (6371 * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?)) + sin(radians(?)) * sin(radians(latitude)))) as distance',
|
|
|
- [$latitude, $longitude, $latitude]
|
|
|
- )
|
|
|
- ->paginate(10);
|
|
|
+ ->paginate($perPage);
|
|
|
+
|
|
|
+ // 遍历技师并设置距离
|
|
|
+ foreach ($coaches as $coach) {
|
|
|
+ $coach->distance = $coachData[array_search($coach->id, array_column($coachData, 'id'))]['distance'] ?? null;
|
|
|
+ }
|
|
|
+ // 按 distance 升序排序
|
|
|
+ $coaches = $coaches->sortBy('distance')->values();
|
|
|
|
|
|
return $coaches;
|
|
|
}
|
|
@@ -57,8 +69,7 @@ class CoachService
|
|
|
public function getCoachDetail($coachId, $latitude, $longitude)
|
|
|
{
|
|
|
// 获取当前用户
|
|
|
- $userId = Auth::id();
|
|
|
- $user = MemberUser::findOrFail($userId);
|
|
|
+ $user = Auth::user();
|
|
|
|
|
|
// 检查用户状态
|
|
|
if ($user->state !== 'enable') {
|
|
@@ -66,16 +77,23 @@ class CoachService
|
|
|
}
|
|
|
|
|
|
// 获取技师信息
|
|
|
- $coach = CoachUser::where('id', $coachId)
|
|
|
- ->where('state', 'enable')
|
|
|
- ->where('auth_state', 'passed')
|
|
|
- ->with(['user:id,nickname,avatar,gender'])
|
|
|
- ->with(['location'])
|
|
|
- ->firstOrFail();
|
|
|
+ $coach = CoachUser::where('state', 'enable')
|
|
|
+ ->whereHas('info', function ($query) {
|
|
|
+ $query->where('state', 'approved');
|
|
|
+ })
|
|
|
+ ->whereHas('real', function ($query) {
|
|
|
+ $query->where('state', 'approved');
|
|
|
+ })
|
|
|
+ ->whereHas('qual', function ($query) {
|
|
|
+ $query->where('state', 'approved');
|
|
|
+ })
|
|
|
+ ->with(['info:id,nickname,avatar,gender'])
|
|
|
+ // ->with(['location'])
|
|
|
+ ->find($coachId);
|
|
|
|
|
|
// TODO: 计算距离
|
|
|
$distance = 0;
|
|
|
- if ($coach->location) {
|
|
|
+ if ($coach->locations) {
|
|
|
// 实现距离计算逻辑
|
|
|
}
|
|
|
|