From a5baf4f85428e96dcbdbae23476378968821f8b2 Mon Sep 17 00:00:00 2001 From: Tiaotiao <65841827@qq.com> Date: Mon, 1 Dec 2025 19:43:04 +0800 Subject: [PATCH 1/7] Add sendMessages function for posting comments --- comment/send.php | 152 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 comment/send.php diff --git a/comment/send.php b/comment/send.php new file mode 100644 index 0000000..2cd5e02 --- /dev/null +++ b/comment/send.php @@ -0,0 +1,152 @@ + '缺少ID参数']); + exit; +} + +if (empty($content)) { + http_response_code(400); + echo json_encode(['error' => '缺少内容参数']); + exit; +} + +// 检查登录状态 +if (!isset($_SESSION['token']) || empty($_SESSION['token'])) { + http_response_code(401); + echo json_encode(['error' => '未登录或会话已过期']); + exit; +} + +function sendMessages( + $TargetID, + $Content, + $TargetType = "Discussion", + $ReplyID = "", + $token = null, + $authCode = null +) { + // 验证必需参数 + if (empty($TargetID)) { + throw new Exception("TargetID参数不能为空"); + } + + if (empty($Content)) { + throw new Exception("消息内容不能为空"); + } + + $baseUrl = "http://nlm-api-cn.turtlesim.com/"; + + // 准备请求数据 + $requestData = [ + 'TargetID' => $TargetID, + 'TargetType' => $TargetType, + 'Language' => "Chinese", + 'ReplyID' => $ReplyID, + 'Content' => $Content, + 'Special' => null, + ]; + + // 准备请求头 + $headers = [ + 'Content-Type: application/json', + 'Accept: application/json', + 'Accept-Language: zh-CN', + ]; + + // 添加认证头 + if ($token && !empty($token)) { + $headers[] = 'x-API-Token: ' . $token; + } + + if ($authCode && !empty($authCode)) { + $headers[] = 'x-API-AuthCode: ' . $authCode; + } + + // 发送POST请求 + $url = $baseUrl . 'Messages/PostComment'; + + $ch = curl_init(); + curl_setopt_array($ch, [ + CURLOPT_URL => $url, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => json_encode($requestData, JSON_UNESCAPED_UNICODE), + CURLOPT_HTTPHEADER => $headers, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_TIMEOUT => 30, + ]); + + $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $error = curl_error($ch); + curl_close($ch); + + // 错误处理 + if ($error) { + throw new Exception("请求失败: " . $error); + } + + if ($httpCode !== 200) { + // 尝试从响应中获取更多错误信息 + $errorInfo = ''; + if ($response) { + $responseData = json_decode($response, true); + if (isset($responseData['Message'])) { + $errorInfo = ' - ' . $responseData['Message']; + } + } + throw new Exception("API请求失败,HTTP状态码: " . $httpCode . $errorInfo); + } + + // 解析JSON响应 + $data = json_decode($response, true); + + if (json_last_error() !== JSON_ERROR_NONE) { + throw new Exception("JSON解析失败: " . json_last_error_msg()); + } + + // 检查API返回的业务状态码 + if (isset($data['Status']) && $data['Status'] !== 200) { + $errorMsg = $data['Message'] ?? '未知错误'; + throw new Exception("API返回错误: " . $errorMsg); + } + + return $data; +} + +try { + // 修复参数顺序问题 + $result = sendMessages( + $id, // TargetID + $content, // Content + $type, // TargetType + $rid, // ReplyID (空字符串) + $_SESSION['token'] ?? null, // token + $_SESSION['authCode'] ?? null // authCode + ); + + echo json_encode([ + 'success' => true, + 'data' => $result + ]); + +} catch (Exception $e) { + http_response_code(500); + echo json_encode([ + 'success' => false, + 'error' => $e->getMessage() + ]); +} +?> From 48602470447ce221818274bb747a42b52001bde1 Mon Sep 17 00:00:00 2001 From: Tiaotiao <65841827@qq.com> Date: Mon, 1 Dec 2025 19:43:44 +0800 Subject: [PATCH 2/7] Update button and script references in comment.html --- comment/comment.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/comment/comment.html b/comment/comment.html index 6fcaf77..aa9ec2f 100644 --- a/comment/comment.html +++ b/comment/comment.html @@ -1,3 +1,3 @@ - + - + From d3af13e734552ba88cbb2d19df4e5ad5d4edf5f3 Mon Sep 17 00:00:00 2001 From: Tiaotiao <65841827@qq.com> Date: Mon, 1 Dec 2025 19:44:42 +0800 Subject: [PATCH 3/7] Update comment.js --- scripts/comment.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/scripts/comment.js b/scripts/comment.js index fa43547..949bdd4 100644 --- a/scripts/comment.js +++ b/scripts/comment.js @@ -56,6 +56,17 @@ /* 示例sm函数 */ function sm(value) { - //向服务器通信 + var rid = document.getElementById('start').dataset.rid; + fetch('/comment/send.php?'+location.href.split('?')[1]+'&con='+value+'&rid='+rid) + .then(() => { + fetch('/comment/?'+location.href.split('?')[1]) + .then(response => response.text()) + .then(html => { + document.getElementById('comments').innerHTML = html; + }) + .catch(error => { + document.getElementById('comments').innerHTML = '