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 = '
加载评论失败
'; + }); + }); document.querySelector('dialog').close(); } From 726f1b8cbccf8836fa3df3acae8f34d4923d599a Mon Sep 17 00:00:00 2001 From: Tiaotiao <65841827@qq.com> Date: Mon, 1 Dec 2025 19:45:35 +0800 Subject: [PATCH 4/7] Update index.php --- comment/index.php | 45 +++++++-------------------------------------- 1 file changed, 7 insertions(+), 38 deletions(-) diff --git a/comment/index.php b/comment/index.php index 0f9698e..c02e0cf 100644 --- a/comment/index.php +++ b/comment/index.php @@ -184,8 +184,8 @@ function formatCommentTime($timestamp) { // 处理回复内容中的@用户标签 function processReplyContent($content) { // 匹配 @用户名 格式 - $pattern = '/@([^<]+)/'; - $replacement = '@$2'; + $pattern = '/([^<]+)<\/user>/'; + $replacement = '$2'; return preg_replace($pattern, $replacement, $content); } @@ -202,13 +202,13 @@ function processReplyContent($content) { background-color: #f5f7fa; color: #333; line-height: 1.6; - }*/ + } .scroll-container { max-width: 80vh; margin: 0 auto; - /*padding: 16px;*/ - } + padding: 16px; + }*/ #notification_container { display: flex; @@ -324,7 +324,7 @@ function processReplyContent($content) { $comment): ?>
-
+
- +
@@ -355,34 +355,3 @@ function processReplyContent($content) {
- - From 145c873520b8aa41f298f6e454eaebe2919c28b0 Mon Sep 17 00:00:00 2001 From: Tiaotiao <65841827@qq.com> Date: Mon, 1 Dec 2025 19:46:16 +0800 Subject: [PATCH 5/7] Update med.php --- med.php | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/med.php b/med.php index d27151b..98295fb 100644 --- a/med.php +++ b/med.php @@ -102,7 +102,8 @@ function formatDate($timestamp) { <?= htmlspecialchars($content['LocalizedSubject']['Chinese'] ?? $pageTitle) ?> - Turtle Universe Web - + + @@ -133,7 +134,7 @@ function formatDate($timestamp) {
-
+
 
@@ -270,5 +271,39 @@ function remixExperiment() { document.getElementById('comments').innerHTML = '
加载评论失败
'; }); + From d5d4c4bf610cdca6a228ee869fd86b3b39b19631 Mon Sep 17 00:00:00 2001 From: Tiaotiao <65841827@qq.com> Date: Tue, 2 Dec 2025 13:35:01 +0800 Subject: [PATCH 6/7] Update comment.html --- comment/comment.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/comment/comment.html b/comment/comment.html index aa9ec2f..c1858dc 100644 --- a/comment/comment.html +++ b/comment/comment.html @@ -1,3 +1,3 @@ 发一条友善的评论吧 - + From 34441d1bedaa16eab0d4adf6f0e362b20955c30a Mon Sep 17 00:00:00 2001 From: Tiaotiao <65841827@qq.com> Date: Tue, 2 Dec 2025 13:37:44 +0800 Subject: [PATCH 7/7] Update med.php --- med.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/med.php b/med.php index 98295fb..e09d144 100644 --- a/med.php +++ b/med.php @@ -103,7 +103,7 @@ function formatDate($timestamp) { <?= htmlspecialchars($content['LocalizedSubject']['Chinese'] ?? $pageTitle) ?> - Turtle Universe Web - +