$userId, 'status' => $filters['status'] ?? FRIEND_STATUS::NORMAL ]; if (isset($filters['group_id'])) { $where['group_id'] = $filters['group_id']; } // 设置排序 if (isset($filters['sort_by']) && isset($filters['sort_order'])) { $where['_sort_field'] = $filters['sort_by']; $where['_sort_order'] = $filters['sort_order']; } else { $where['_sort_field'] = 'intimacy'; $where['_sort_order'] = 'desc'; } // 查询好友关系 $relations = FriendRelation::where($where) ->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 string $keyword 搜索关键词 * @param int $page 页码 * @param int $limit 每页数量 * @return array 搜索结果 */ public function searchUsers(int $userId, string $keyword, int $page = 1, int $limit = 20): array { // 构建查询条件 $query = User::query(); // 排除当前用户 $query->where('id', '!=', $userId); // 根据关键词搜索 if (is_numeric($keyword)) { // 如果是数字,则按ID搜索 $query->where('id', $keyword); } else { // 否则按昵称搜索 $query->where('nickname', 'like', "%{$keyword}%"); } // 执行分页查询 $users = $query->paginate($limit, ['id', 'nickname', 'avatar', 'level'], 'page', $page); // 获取当前用户的好友ID列表 $friendIds = FriendRelation::where('user_id', $userId) ->where('status', FRIEND_STATUS::NORMAL) ->pluck('friend_id') ->toArray(); // 获取当前用户已发送申请的用户ID列表 $requestLogic = new RequestLogic(); $requestedIds = $requestLogic->getSentRequestUserIds($userId); // 格式化数据 $items = []; foreach ($users as $user) { $relation = 0; // 0:无关系, 1:已是好友, 2:已申请 if (in_array($user->id, $friendIds)) { $relation = 1; } elseif (in_array($user->id, $requestedIds)) { $relation = 2; } $items[] = [ 'user_id' => $user->id, 'nickname' => $user->nickname ?? '', 'avatar' => $user->avatar ?? '', 'level' => $user->level ?? 1, 'relation' => $relation ]; } return [ 'total' => $users->total(), 'per_page' => $users->perPage(), 'current_page' => $users->currentPage(), 'last_page' => $users->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' => '好友数量已达上限' ]; } // 检查事务是否已开启 \UCore\Db\Helper::check_tr(); // 创建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, ]); return [ 'success' => true, 'data' => $relationAB ]; } catch (\Exception $e) { 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' => '好友关系不存在' ]; } // 检查事务是否已开启 \UCore\Db\Helper::check_tr(); // 删除A->B的关系 FriendRelation::where('user_id', $userId) ->where('friend_id', $friendId) ->delete(); // 删除B->A的关系 FriendRelation::where('user_id', $friendId) ->where('friend_id', $userId) ->delete(); return [ 'success' => true ]; } catch (\Exception $e) { 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; // 默认离线 } }