4 php7.4中开发一个websocket 聊天相关配置调用

时间:2024-09-29 15:54:27
<?php // 引入配置文件 require_once __DIR__ . '/../config/config.php'; // 获取 GET 参数并验证 $from_user_id = isset($_GET['from_user_id']) ? intval($_GET['from_user_id']) : 0; $to_user_id = isset($_GET['to_user_id']) ? intval($_GET['to_user_id']) : 0; $limit = isset($_GET['limit']) && is_numeric($_GET['limit']) ? intval($_GET['limit']) : 20; // 验证参数 //if ($from_user_id <= 0 || $to_user_id <= 0) { // exit(json_encode(['error' => '参数错误: from_user_id 和 to_user_id 是必需的且必须为有效数字'])); //} try { $pdo = getDbConfig(); $last_create_at = isset($_GET['last_create_at']) ? $_GET['last_create_at'] : null; $last_id = isset($_GET['last_id']) ? intval($_GET['last_id']) : null; $sql = " SELECT * FROM chat WHERE ( (from_userid = :from_user_id AND to_user_id = :to_user_id) OR (from_userid = :to_user_id AND to_user_id = :from_user_id) ) " . ($last_create_at ? "AND (create_at < :last_create_at OR (create_at = :last_create_at AND id < :last_id))" : "") . " ORDER BY create_at DESC, id DESC LIMIT :limit "; $stmt = $pdo->prepare($sql); $stmt->bindParam(':from_user_id', $from_user_id, PDO::PARAM_INT); $stmt->bindParam(':to_user_id', $to_user_id, PDO::PARAM_INT); if ($last_create_at) { $stmt->bindParam(':last_create_at', $last_create_at, PDO::PARAM_STR); $stmt->bindParam(':last_id', $last_id, PDO::PARAM_INT); } $stmt->bindParam(':limit', $limit, PDO::PARAM_INT); $stmt->execute(); $chatHistory = $stmt->fetchAll(PDO::FETCH_ASSOC); // 不需要反转顺序,因为前端已经处理 echo json_encode($chatHistory); } catch (Exception $e) { echo json_encode(['error' => '查询失败: ' . $e->getMessage()]); } ?> <?php require_once __DIR__ . '/../config/config.php'; $user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : 0; $search = isset($_GET['search']) ? $_GET['search'] : ''; if ($user_id <= 0) { echo json_encode(['status' => 'error', 'message' => '无效的用户ID']); exit; } try { $pdo = getDbConfig(); // 获取分页参数 $page = isset($_GET['page']) ? intval($_GET['page']) : 1; $limit = isset($_GET['limit']) ? intval($_GET['limit']) : 20; $offset = ($page - 1) * $limit; // 获取总记录数 $searchQuery = $search ? " AND friend_name LIKE :search" : ""; $countStmt = $pdo->prepare(" SELECT COUNT(*) as total FROM friend_list WHERE user_id = :user_id $searchQuery "); $countStmt->bindParam(':user_id', $user_id); $countStmt->execute(); $totalCount = $countStmt->fetch(PDO::FETCH_ASSOC)['total']; // 计算总页数 $totalPages = ceil($totalCount / $limit); $stmt = $pdo->prepare(" SELECT friend_id, friend_name, friend_header FROM friend_list WHERE user_id = :user_id $searchQuery LIMIT :limit OFFSET :offset "); $stmt->bindParam(':user_id', $user_id); $stmt->bindParam(':limit', $limit, PDO::PARAM_INT); $stmt->bindParam(':offset', $offset, PDO::PARAM_INT); $stmt->execute(); $friends = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_encode(['status' => 'success', 'data' => $friends]); } catch (PDOException $e) { echo json_encode(['status' => 'error', 'message' => '数据库错误: ' . $e->getMessage()]); } catch (Exception $e) { echo json_encode(['status' => 'error', 'message' => '未知错误: ' . $e->getMessage()]); } finally { if ($pdo) { $pdo = null; } } <?php // 引入配置文件 require_once __DIR__ . '/../config/config.php'; // Get parameters from GET request $from_user_id = $_GET['from_user_id'] ?? null; $to_user_id = $_GET['to_user_id'] ?? null; $user_name = $_GET['user_name'] ?? null; $to_user_name = $_GET['to_user_name'] ?? null; $userAvatar = $_GET['Avatar'] ?? $_GET['userAvatar'] ?? null; // Check both 'Avatar' and 'userAvatar' $to_user_avatar = $_GET['to_user_avatar'] ?? null; // Validate input if (!$from_user_id || !$to_user_id || !$user_name || !$to_user_name || !$userAvatar || !$to_user_avatar) { echo json_encode(['status' => 'error', 'message' => '参数错误']); exit; } try { // Close the database connection after all operations are complete // Start transaction $db = getDbConfig(); $db->beginTransaction(); // Check if already friends $stmt = $db->prepare("SELECT id FROM friend_list WHERE user_id = ? AND friend_id = ?"); $stmt->execute([$from_user_id, $to_user_id]); $result = $stmt->fetchAll(); if (count($result) > 0) { echo json_encode(['status' => 'info', 'message' => '已是好友']); $db->rollBack(); } else { // Add friend (from_user -> to_user) $stmt = $db->prepare("INSERT INTO friend_list (user_id, friend_id, friend_name, friend_header, create_at) VALUES (?, ?, ?, ?, NOW())"); $stmt->execute([$from_user_id, $to_user_id, $to_user_name, $to_user_avatar]); // Add friend (to_user -> from_user) $stmt = $db->prepare("INSERT INTO friend_list (user_id, friend_id, friend_name, friend_header, create_at) VALUES (?, ?, ?, ?, NOW())"); $stmt->execute([$to_user_id, $from_user_id, $user_name, $userAvatar]); // Commit transaction $db->commit(); echo json_encode(['status' => 'success', 'message' => '好友增加成功']); } } catch (Exception $e) { // Rollback transaction on error $db->rollback(); echo json_encode(['status' => 'error', 'message' => 'An error occurred: ' . $e->getMessage()]); } finally { // Close the database connection after all operations are complete if (isset($db)) { $db = null; } } // .. <?php require_once __DIR__ . '/../config/config.php'; $pdo = getDbConfig(); // 检查是否是POST请求 if ($_SERVER['REQUEST_METHOD'] === 'POST') { // 获取POST数据中的uniqueId $uniqueId = $_POST['uniqueId'] ?? null; // 检查uniqueId是否为空 if (empty($uniqueId)) { echo json_encode(['status' => 'error', 'message' => 'uniqueId不能为空']); exit; } try { // 更新withdraw字段的SQL语句 $sql = "UPDATE chat SET withdraw = 1 WHERE uniqueId = :uniqueId"; // 预处理SQL语句并执行 $stmt = $pdo->prepare($sql); $stmt->execute([':uniqueId' => $uniqueId]); echo json_encode(['status' => 'success', 'message' => '消息撤回成功']); } catch (PDOException $e) { echo json_encode(['status' => 'error', 'message' => '消息撤回失败']); } } else { echo json_encode(['status' => 'error', 'message' => '无效的请求方法']); } ?> <?php require_once __DIR__ . '/../config/config.php'; $pdo=getDbConfig(); // 检查是否是POST请求 if ($_SERVER['REQUEST_METHOD'] === 'POST') { // 获取POST数据中的uniqueId $uniqueId = $_POST['uniqueId'] ?? null; // 检查uniqueId是否为空 if ($uniqueId) { try { // 更新withdraw字段为false的SQL语句 $sql = "SELECT * FROM chat WHERE uniqueId = :uniqueId"; // 预处理SQL语句 $stmt = $pdo->prepare($sql); // 绑定参数 $stmt->bindParam(':uniqueId', $uniqueId, PDO::PARAM_STR); // 执行SQL语句 $stmt->execute(); // 获取查询结果 $result = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_encode ($result); } catch (PDOException $e) { echo json_encode(['status' => 'error', 'message' => '消息撤回失败']); } } else { echo json_encode(['status' => 'error', 'message' => 'id不能为空']); } } <?php require_once __DIR__ . '/../config/config.php'; $pdo = getDbConfig(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { // 获取POST数据 $postData = [ 'from_userid' => $_POST['from_userid'] ?? null, 'to_user_id' => $_POST['to_user_id'] ?? null, 'from_companyId' => $_POST['from_companyId'] ?? null, 'to_companyId' => $_POST['to_companyId'] ?? null, 'from_company_name' => $_POST['from_company_name'] ?? null, 'to_company_name' => $_POST['to_company_name'] ?? null, 'from_role' => $_POST['from_role'] ?? null, 'to_role' => $_POST['to_role'] ?? null, 'filetype' => $_POST['filetype'] ?? null, 'text' => $_POST['text'] ?? null, 'from_user_avatar' => $_POST['from_user_avatar'] ?? null, 'to_user_avatar' => $_POST['to_user_avatar'] ?? null, 'from_company_avatar' => $_POST['from_company_avatar'] ?? null, 'to_company_avatar' => $_POST['to_company_avatar'] ?? null, 'uniqueId' => $_POST['uniqueId'] ?? null, 'from_name' => $_POST['user_name'] ?? null, 'to_name' => $_POST['to_name'] ?? null ]; // 检查必填字段 if (!$postData['from_userid'] || !$postData['to_user_id']) { echo json_encode(['status' => 'error', 'message' => 'from_userid 和 to_user_id 是必需的']); exit; } var_dump($postData); // 准备SQL语句