|
@@ -4,8 +4,8 @@ namespace App\Services\Client;
|
|
|
|
|
|
use App\Models\CoachUser;
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
-use Illuminate\Support\Facades\Redis;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
+use Illuminate\Support\Facades\Redis;
|
|
|
|
|
|
class CoachService
|
|
|
{
|
|
@@ -26,7 +26,7 @@ class CoachService
|
|
|
]);
|
|
|
|
|
|
// 检查用户状态
|
|
|
- if (!$user) {
|
|
|
+ if (! $user) {
|
|
|
throw new \Exception('用户未登录');
|
|
|
}
|
|
|
|
|
@@ -85,7 +85,7 @@ class CoachService
|
|
|
Log::info('Redis connection test:', ['ping_result' => $pingResult]);
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('Redis connection error:', ['error' => $e->getMessage()]);
|
|
|
- throw new \Exception('Redis连接失败:' . $e->getMessage());
|
|
|
+ throw new \Exception('Redis连接失败:'.$e->getMessage());
|
|
|
}
|
|
|
|
|
|
// 检查Redis中的所有位置数据
|
|
@@ -99,11 +99,11 @@ class CoachService
|
|
|
'user' => $user ? $user->id : null,
|
|
|
'latitude' => $latitude,
|
|
|
'longitude' => $longitude,
|
|
|
- 'coach_id' => $coachId
|
|
|
+ 'coach_id' => $coachId,
|
|
|
]);
|
|
|
|
|
|
// 检查用户状态
|
|
|
- if (!$user) {
|
|
|
+ if (! $user) {
|
|
|
throw new \Exception('用户未登录');
|
|
|
}
|
|
|
|
|
@@ -132,68 +132,40 @@ class CoachService
|
|
|
$homeLocation = Redis::geopos('coach_locations', $coachId.'_home');
|
|
|
$workLocation = Redis::geopos('coach_locations', $coachId.'_work');
|
|
|
|
|
|
- Log::info('Coach locations from Redis:', [
|
|
|
- 'coach_id' => $coachId,
|
|
|
- 'home_location' => $homeLocation,
|
|
|
- 'work_location' => $workLocation,
|
|
|
- 'home_key' => $coachId.'_home',
|
|
|
- 'work_key' => $coachId.'_work'
|
|
|
- ]);
|
|
|
-
|
|
|
// 检查输入的经纬度是否有效
|
|
|
- if (!is_numeric($latitude) || !is_numeric($longitude)) {
|
|
|
+ if (! is_numeric($latitude) || ! is_numeric($longitude)) {
|
|
|
Log::error('Invalid coordinates:', ['latitude' => $latitude, 'longitude' => $longitude]);
|
|
|
throw new \Exception('无效的经纬度坐标');
|
|
|
}
|
|
|
|
|
|
// 临时存储用户当前位置用于计算距离
|
|
|
$tempKey = 'user_temp_'.$user->id;
|
|
|
- $addResult = Redis::geoadd('coach_locations', $longitude, $latitude, $tempKey);
|
|
|
-
|
|
|
- Log::info('User location added to Redis:', [
|
|
|
- 'temp_key' => $tempKey,
|
|
|
- 'latitude' => $latitude,
|
|
|
- 'longitude' => $longitude,
|
|
|
- 'add_result' => $addResult
|
|
|
- ]);
|
|
|
+ Redis::geoadd('coach_locations', $longitude, $latitude, $tempKey);
|
|
|
|
|
|
// 计算距离(单位:km)
|
|
|
$distanceHome = null;
|
|
|
$distanceWork = null;
|
|
|
|
|
|
- if ($homeLocation && !empty($homeLocation[0])) {
|
|
|
+ if ($homeLocation && ! empty($homeLocation[0])) {
|
|
|
$distanceHome = Redis::geodist('coach_locations', $tempKey, $coachId.'_home', 'km');
|
|
|
Log::info('Home distance calculation:', [
|
|
|
'from' => $tempKey,
|
|
|
'to' => $coachId.'_home',
|
|
|
'distance' => $distanceHome,
|
|
|
- 'home_location' => $homeLocation[0]
|
|
|
+ 'home_location' => $homeLocation[0],
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
- if ($workLocation && !empty($workLocation[0])) {
|
|
|
+ if ($workLocation && ! empty($workLocation[0])) {
|
|
|
$distanceWork = Redis::geodist('coach_locations', $tempKey, $coachId.'_work', 'km');
|
|
|
- Log::info('Work distance calculation:', [
|
|
|
- 'from' => $tempKey,
|
|
|
- 'to' => $coachId.'_work',
|
|
|
- 'distance' => $distanceWork,
|
|
|
- 'work_location' => $workLocation[0]
|
|
|
- ]);
|
|
|
}
|
|
|
|
|
|
- Log::info('Distance calculation results:', [
|
|
|
- 'distance_home' => $distanceHome,
|
|
|
- 'distance_work' => $distanceWork,
|
|
|
- 'temp_key' => $tempKey,
|
|
|
- 'coach_id' => $coachId
|
|
|
- ]);
|
|
|
-
|
|
|
// 删除临时位置点
|
|
|
Redis::zrem('coach_locations', $tempKey);
|
|
|
|
|
|
// 选择最近的距离
|
|
|
$distances = array_filter([$distanceHome, $distanceWork]);
|
|
|
- $coach->distance = !empty($distances) ? round(min($distances), 2) : null;
|
|
|
+ $coach->distance = ! empty($distances) ? round(min($distances), 2) : null;
|
|
|
|
|
|
return $coach;
|
|
|
}
|
|
@@ -201,29 +173,30 @@ class CoachService
|
|
|
/**
|
|
|
* 设置技师位置信息
|
|
|
*
|
|
|
- * @param int $coachId 技师ID
|
|
|
- * @param float $latitude 纬度
|
|
|
- * @param float $longitude 经度
|
|
|
- * @param string $type 位置类型 (home|work)
|
|
|
+ * @param int $coachId 技师ID
|
|
|
+ * @param float $latitude 纬度
|
|
|
+ * @param float $longitude 经度
|
|
|
+ * @param string $type 位置类型 (home|work)
|
|
|
* @return bool
|
|
|
+ *
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
public function setCoachLocation($coachId, $latitude, $longitude, $type = 'home')
|
|
|
{
|
|
|
- if (!is_numeric($latitude) || !is_numeric($longitude)) {
|
|
|
+ if (! is_numeric($latitude) || ! is_numeric($longitude)) {
|
|
|
Log::error('Invalid coordinates in setCoachLocation:', [
|
|
|
'coach_id' => $coachId,
|
|
|
'latitude' => $latitude,
|
|
|
- 'longitude' => $longitude
|
|
|
+ 'longitude' => $longitude,
|
|
|
]);
|
|
|
throw new \Exception('无效的经纬度坐标');
|
|
|
}
|
|
|
|
|
|
- if (!in_array($type, ['home', 'work'])) {
|
|
|
+ if (! in_array($type, ['home', 'work'])) {
|
|
|
throw new \Exception('无效的位置类型,必须是 home 或 work');
|
|
|
}
|
|
|
|
|
|
- $key = $coachId . '_' . $type;
|
|
|
+ $key = $coachId.'_'.$type;
|
|
|
$result = Redis::geoadd('coach_locations', $longitude, $latitude, $key);
|
|
|
|
|
|
Log::info('Coach location set:', [
|
|
@@ -232,14 +205,14 @@ class CoachService
|
|
|
'key' => $key,
|
|
|
'latitude' => $latitude,
|
|
|
'longitude' => $longitude,
|
|
|
- 'result' => $result
|
|
|
+ 'result' => $result,
|
|
|
]);
|
|
|
|
|
|
// 验证数据是否成功写入
|
|
|
$location = Redis::geopos('coach_locations', $key);
|
|
|
Log::info('Verify location after set:', [
|
|
|
'key' => $key,
|
|
|
- 'location' => $location
|
|
|
+ 'location' => $location,
|
|
|
]);
|
|
|
|
|
|
return $result;
|