diff --git a/comment/index.php b/comment/index.php
index 4beff28..14b15b9 100644
--- a/comment/index.php
+++ b/comment/index.php
@@ -1,22 +1,19 @@
authCode = $authCode;
}
- /**
- * 获取留言/评论信息
- */
public function getMessages(
$ID,
$type = "Discussion",
@@ -37,7 +31,6 @@ public function getMessages(
$from = null,
$skip = 0
) {
- // 验证必需参数
if (empty($ID)) {
throw new Exception("ID参数不能为空");
}
@@ -59,7 +52,6 @@ public function getMessages(
'Skip' => $skip,
];
- // 只有在提供了from参数时才添加CommentID字段
if ($from !== null) {
$requestData['CommentID'] = $from;
}
@@ -117,7 +109,6 @@ public function getMessages(
throw new Exception("JSON解析失败: " . json_last_error_msg());
}
- // 检查API返回的业务状态码
if (isset($data['Status']) && $data['Status'] !== 200) {
$errorMsg = $data['Message'] ?? '未知错误';
throw new Exception("API返回错误: " . $errorMsg);
@@ -126,7 +117,6 @@ public function getMessages(
return $data;
}
- // 设置认证信息
public function setAuth($token, $authCode) {
$this->token = $token;
$this->authCode = $authCode;
@@ -134,7 +124,7 @@ public function setAuth($token, $authCode) {
}
}
-// 类形式的使用示例:
+// 初始化评论服务
$messageService = new MessageService($_SESSION['token'] ?? null, $_SESSION['authCode'] ?? null);
try {
$messages = $messageService->getMessages($contentId, $category, 20, null, $skp);
@@ -145,128 +135,64 @@ public function setAuth($token, $authCode) {
$error = $e->getMessage();
}
-// 生成头像URL函数
+// ---------- 辅助函数 ----------
function getAvatarUrl($userID, $avatarNumber) {
- // 根据用户ID构建头像路径
$part1 = substr($userID, 0, 4);
$part2 = substr($userID, 4, 2);
$part3 = substr($userID, 6, 2);
$part4 = substr($userID, 8, 16);
-
return "http://netlogo-static-cn.turtlesim.com/users/avatars/{$part1}/{$part2}/{$part3}/{$part4}/{$avatarNumber}.jpg!full";
}
-// 格式化时间函数
function formatCommentTime($timestamp) {
if (!$timestamp) return '未知时间';
-
- // 如果是毫秒时间戳,转换为秒
- if ($timestamp > 9999999999) {
- $timestamp = $timestamp / 1000;
- }
-
+ if ($timestamp > 9999999999) $timestamp = $timestamp / 1000;
$date = date('n/j', $timestamp);
$hour = date('G', $timestamp);
$minute = date('i', $timestamp);
-
- // 转换为12小时制并添加上午/下午
- if ($hour < 12) {
- $period = '上午';
- $displayHour = $hour == 0 ? 12 : $hour;
- } else {
- $period = '下午';
- $displayHour = $hour > 12 ? $hour - 12 : $hour;
- }
-
+ $period = $hour < 12 ? '上午' : '下午';
+ $displayHour = $hour == 0 ? 12 : ($hour > 12 ? $hour - 12 : $hour);
return "{$date} {$period}{$displayHour}:{$minute}";
}
-// 自定义标签解析器类
class CustomTagParser {
-
- /**
- * 解析包含自定义标签的文本
- */
public static function parse(string $text): string {
- if (empty($text)) {
- return '';
- }
-
- // 先转义HTML特殊字符,防止XSS攻击
+ if (empty($text)) return '';
$text = htmlspecialchars($text);
-
- // 将换行符转换为
$text = nl2br($text);
- // 解析自定义标签
$patterns = [
- // user标签 - 保持原有格式
'/<user=([a-f0-9]+)>(.*?)<\/user>/i',
- // experiment标签 - 跳转链接
'/<experiment=([a-f0-9]+)>(.*?)<\/experiment>/i',
- // discussion标签 - 跳转链接
'/<discussion=([a-f0-9]+)>(.*?)<\/discussion>/i',
- // model标签 - 跳转链接
'/<model=([a-f0-9]+)>(.*?)<\/model>/i',
- // external标签 - 外部链接
'/<external=([^&]+)>(.*?)<\/external>/i',
- // size标签
'/<size=([^&]+)>(.*?)<\/size>/i',
- // color标签 - 新增
'/<color=([^&]+)>(.*?)<\/color>/i',
- // b标签
'/<b>(.*?)<\/b>/i',
- // i标签
'/<i>(.*?)<\/i>/i',
- // a标签 - 深蓝色标签
'/<a>(.*?)<\/a>/i'
];
$replacements = [
- // user标签
'$2',
- // experiment标签
'$2',
- // discussion标签
'$2',
- // model标签
'$2',
- // external标签
'$2',
- // size标签
'$2',
- // color标签 - 新增
'$2',
- // b标签
'$1',
- // i标签
'$1',
- // a标签
'$1'
];
$text = preg_replace($patterns, $replacements, $text);
- // 解析简单的Markdown格式
- $text = self::parseSimpleMarkdown($text);
-
- return $text;
- }
-
- /**
- * 解析简单的Markdown格式
- */
- private static function parseSimpleMarkdown(string $text): string {
- // 粗体
+ // 简单 Markdown
$text = preg_replace('/\*\*(.*?)\*\*/', '$1', $text);
-
- // 斜体
$text = preg_replace('/\*(.*?)\*/', '$1', $text);
-
- // 删除线
$text = preg_replace('/~~(.*?)~~/', '$1', $text);
-
- // 行内代码
$text = preg_replace('/`([^`]+)`/', '$1', $text);
return $text;
@@ -274,172 +200,133 @@ private static function parseSimpleMarkdown(string $text): string {
}
?>
-
暂无评论
-暂无评论