where('status', $filters['status']); } else { // 默认只查询正常状态的好友 $query->where('status', FRIEND_STATUS::NORMAL); } // 应用分组过滤 if (isset($filters['group_id'])) { $query->where('group_id', $filters['group_id']); } // 应用关键词搜索(好友备注) if (isset($filters['keyword']) && !empty($filters['keyword'])) { $query->where('remark', 'like', '%' . $filters['keyword'] . '%'); } // 应用排序 if (isset($filters['sort_by']) && isset($filters['sort_order'])) { $query->orderBy($filters['sort_by'], $filters['sort_order']); } else { // 默认按亲密度降序排列 $query->orderBy('intimacy', 'desc'); } // 执行分页查询 $relations = $query->with('friend')->paginate($limit, ['*'], 'page', $page); // 格式化数据 $items = []; foreach ($relations as $relation) { if (!$relation->friend) { continue; } $items[] = [ 'user_id' => $relation->friend_id, 'nickname' => $relation->friend->nickname ?? '', 'avatar' => $relation->friend->avatar ?? '', 'level' => $relation->friend->level ?? 1, 'status' => $this->getUserOnlineStatus($relation->friend_id), 'last_login' => $relation->friend->last_login_at ?? 0, 'remark' => $relation->remark, 'intimacy' => $relation->intimacy, ]; } return [ 'total' => $relations->total(), 'per_page' => $relations->perPage(), 'current_page' => $relations->currentPage(), 'last_page' => $relations->lastPage(), 'items' => $items ]; } /** * 创建好友关系 * * @param int $userId 用户ID * @param int $friendId 好友ID * @param string|null $remark 备注名 * @return array 操作结果 */ public function createFriendRelation(int $userId, int $friendId, ?string $remark = null): array { try { // 检查是否已经是好友 $existingRelation = FriendRelation::where('user_id', $userId) ->where('friend_id', $friendId) ->first(); if ($existingRelation) { if ($existingRelation->status == FRIEND_STATUS::NORMAL) { return [ 'success' => false, 'code' => ERROR_CODE::ALREADY_FRIEND, 'message' => '已经是好友关系' ]; } else { // 如果是黑名单状态,则更新为正常状态 $existingRelation->status = FRIEND_STATUS::NORMAL; $existingRelation->save(); return [ 'success' => true, 'data' => $existingRelation ]; } } // 检查好友数量是否达到上限 $friendCount = FriendRelation::where('user_id', $userId) ->where('status', FRIEND_STATUS::NORMAL) ->count(); if ($friendCount >= $this->friendLimit) { return [ 'success' => false, 'code' => ERROR_CODE::FRIEND_LIMIT_REACHED, 'message' => '好友数量已达上限' ]; } // 创建好友关系(双向) DB::beginTransaction(); // 创建A->B的关系 $relationAB = FriendRelation::create([ 'user_id' => $userId, 'friend_id' => $friendId, 'remark' => $remark, 'group_id' => 0, 'intimacy' => 0, 'status' => FRIEND_STATUS::NORMAL, ]); // 创建B->A的关系 $relationBA = FriendRelation::create([ 'user_id' => $friendId, 'friend_id' => $userId, 'remark' => null, 'group_id' => 0, 'intimacy' => 0, 'status' => FRIEND_STATUS::NORMAL, ]); DB::commit(); return [ 'success' => true, 'data' => $relationAB ]; } catch (\Exception $e) { DB::rollBack(); Log::error('创建好友关系失败: ' . $e->getMessage()); return [ 'success' => false, 'code' => ERROR_CODE::PARAM_ERROR, 'message' => '创建好友关系失败' ]; } } /** * 删除好友关系 * * @param int $userId 用户ID * @param int $friendId 好友ID * @return array 操作结果 */ public function deleteFriend(int $userId, int $friendId): array { try { // 检查是否存在好友关系 $relation = FriendRelation::where('user_id', $userId) ->where('friend_id', $friendId) ->where('status', FRIEND_STATUS::NORMAL) ->first(); if (!$relation) { return [ 'success' => false, 'code' => ERROR_CODE::RELATION_NOT_EXIST, 'message' => '好友关系不存在' ]; } // 删除双向好友关系 DB::beginTransaction(); // 删除A->B的关系 FriendRelation::where('user_id', $userId) ->where('friend_id', $friendId) ->delete(); // 删除B->A的关系 FriendRelation::where('user_id', $friendId) ->where('friend_id', $userId) ->delete(); DB::commit(); return [ 'success' => true ]; } catch (\Exception $e) { DB::rollBack(); Log::error('删除好友关系失败: ' . $e->getMessage()); return [ 'success' => false, 'code' => ERROR_CODE::PARAM_ERROR, 'message' => '删除好友关系失败' ]; } } /** * 更新好友备注 * * @param int $userId 用户ID * @param int $friendId 好友ID * @param string $remark 备注名 * @return array 操作结果 */ public function updateFriendRemark(int $userId, int $friendId, string $remark): array { try { // 检查是否存在好友关系 $relation = FriendRelation::where('user_id', $userId) ->where('friend_id', $friendId) ->where('status', FRIEND_STATUS::NORMAL) ->first(); if (!$relation) { return [ 'success' => false, 'code' => ERROR_CODE::RELATION_NOT_EXIST, 'message' => '好友关系不存在' ]; } // 更新备注 $relation->remark = $remark; $relation->save(); return [ 'success' => true ]; } catch (\Exception $e) { Log::error('更新好友备注失败: ' . $e->getMessage()); return [ 'success' => false, 'code' => ERROR_CODE::PARAM_ERROR, 'message' => '更新好友备注失败' ]; } } /** * 获取用户在线状态 * * @param int $userId 用户ID * @return int 状态:1在线,2离线 */ protected function getUserOnlineStatus(int $userId): int { // TODO: 实现获取用户在线状态的逻辑 // 这里可以调用用户模块的相关服务或者查询缓存 return 2; // 默认离线 } }