$uid, 'name' => (string)($user['name'] ?? $uid), 'displayName' => (string)($user['displayName'] ?? $user['name'] ?? $uid), 'mobile' => (string)($user['mobile'] ?? ''), 'portrait' => (string)($user['portrait'] ?? ''), 'alias' => '', ]; } try { $target = tryImUserInfo(['userId' => $keyword]); if (!$target && preg_match('/^\+?\d{5,32}$/', $keyword)) { $target = tryImUserInfo(['mobile' => $keyword]); } if (!$target) { $target = tryImUserInfo(['name' => $keyword]); } if (!$target) { respond(404, '没有找到该用户'); } $contact = normalizeUserInfo($target); if ($contact['uid'] === '') { respond(404, '没有找到该用户'); } if ($contact['uid'] === $userId) { respond(400, '不能添加自己为联系人'); } $friendResult = requireImSuccess( imAdminPost('/admin/friend/list', ['userId' => $userId]), '好友列表检查失败' ); $friendIds = array_values(array_filter($friendResult['list'] ?? [], 'is_string')); if (in_array($contact['uid'], $friendIds, true)) { respond(409, '该用户已经是联系人', ['contact' => $contact]); } requireImSuccess( imAdminPost('/admin/friend/status', [ 'userId' => $userId, 'friendUid' => $contact['uid'], 'status' => 0, ]), '添加联系人失败' ); requireImSuccess( imAdminPost('/admin/friend/status', [ 'userId' => $contact['uid'], 'friendUid' => $userId, 'status' => 0, ]), '添加联系人失败' ); respond(0, 'success', ['contact' => $contact, 'reason' => $reason]); } catch (Throwable $error) { respond(500, $error->getMessage() ?: '添加联系人服务暂不可用'); }