This commit is contained in:
mei 2024-11-02 12:58:24 +08:00
parent ce3c31aae4
commit 75b049f6d3
6 changed files with 100 additions and 5 deletions

View File

@ -0,0 +1,49 @@
<?php
// 需要获取的参数
$articleId = isset($_GET['id']) ? sanitizeInput($_GET['id']) : ''; // 文章id
$sql = "SELECT id, title, content, DATE_FORMAT(created_at, '%Y-%m-%d') AS publishDate
FROM articles
WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute([':id' => $articleId]);
$article = $stmt->fetch();
if ($article) {
// 查询评论
$commentSql = "SELECT c.id, u.username AS author, u.email AS avatarUrl, c.content
FROM comments c
JOIN users u ON c.user_id = u.id
WHERE c.article_id = :article_id
ORDER BY c.created_at ASC";
$commentStmt = $pdo->prepare($commentSql);
$commentStmt->execute([':article_id' => $articleId]);
$comments = $commentStmt->fetchAll();
// 构建最终的JSON数据
$result = [
'article' => [
'title' => $article['title'],
'author' => 'admin', // TODO:
'publishDate' => $article['publishDate'],
'content' => $article['content']
],
'comments' => array_map(function ($comment) {
return [
'id' => $comment['id'],
'author' => $comment['author'],
'avatarUrl' => $comment['avatarUrl'] ? $comment['avatarUrl'] : '/placeholder.svg?height=40&width=40',
'content' => $comment['content']
];
}, $comments)
];
// 输出JSON
header('Content-Type: application/json');
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
} else {
echo json_encode(['error' => 'Article not found'], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}

View File

@ -0,0 +1,9 @@
<?php
$sql = "SELECT id, title, content, DATE_FORMAT(created_at, '%Y-%m-%d') AS created_at, type
FROM articles
ORDER BY created_at DESC";
$stmt = $pdo->query($sql);
$articles = $stmt->fetchAll();
echo json_encode($articles, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
?>

View File

@ -0,0 +1,33 @@
<?php
$quotes = [
"这个年龄抽烟,不是装逼就是二逼" => "秦涛",
"天涯何处无芳草,何必要在身边找,本来数量就不多,质量还不咋地" => "秦涛",
"弱小和无知,不是生存的障碍,傲慢才是" => "《三体》",
"你的无畏来源于无知" => "《三体》",
"要想逃避现实,最好的方式就是深深介入现实之中" => "《三体》",
"我爱你,与你有何相干?毁灭你,又与你有何相干?" => "《三体》",
"宇宙很大,生活更大,也许以后还有缘相见" => "《三体》",
"大多数人到死都没有向尘世之外瞥一眼" => "《三体》",
"碑是那么小,与其说是为了纪念,更像是为了忘却" => "《三体》",
"人们习惯将凡事分出黑与白,但很遗憾,现实全是灰色的" => "《三体》",
"没有不散的宴席,一切都有个尽头" => "《三体》",
"编程本身虽然是一种智力活动,但是中国的现实却更像一种体力劳动" => "《未来世界的幸存者》",
"我们只是让某些局部变得更有秩序,把混乱转移到另一些领域。" => "《未来世界的幸存者》",
"苦难就是苦难,苦难不会带来成功" => "《活着》",
"我的职业建议是任何工作要么让你学习learn要么让你赚钱earn。如果既学不到新东西又赚不到钱你就应该走了" => "Garry Tan",
"编程既不是短跑,也不是马拉松,而是日记。在日复一日的累积当中,完成你的事业" => "《四十年编程感想》"
];
$randomKey = array_rand($quotes);
$quote = $randomKey;
$source = $quotes[$randomKey];
// 创建一个数组来存储名言和作者信息
$response = [
'quote' => $quote,
'source' => $source
];
// 将数组转换为 JSON 格式并输出
echo json_encode($response);

View File

@ -9,16 +9,16 @@ session_start();
$api_class = isset($_GET['class']) ? sanitizeInput($_GET['class']) : '';
$api = isset($_GET['api']) ? sanitizeInput($_GET['api']) : '';
// 设置响应头
header("Content-Type: application/json");
if (empty($api_class)) {
http_response_code(200); // Bad Request
http_response_code(200);
echo json_encode(array('status' => 'Home page'));
exit();
}
// 验证输入是否符合预期格式
elseif (!preg_match('/^[a-zA-Z0-9_]+$/', $api_class) || !preg_match('/^[a-zA-Z0-9_]+$/', $api)) {
header("Content-Type: application/json");
http_response_code(400); // Bad Request
echo json_encode(array('error' => 'Invalid input'));
exit();
@ -29,6 +29,7 @@ $file_path = 'includes/' . $api_class . '/' . $api . '.php';
// 检查文件是否存在
if (!file_exists($file_path)) {
header("Content-Type: application/json");
http_response_code(404); // Not Found
echo json_encode(array('error' => '404 Not Found', 'message' => 'The requested resource could not be found'));
exit();
@ -36,7 +37,10 @@ if (!file_exists($file_path)) {
// 如果不是 public API则启用路由
if ($api_class != 'public') {
include 'core/login_router.php';
header("Content-Type: application/json");
include 'core/router.php';
} else {
header("Content-Type: application/json");
}
// 包含文件

View File

@ -1 +1 @@
rewrite ^/([^.]+)/([^.]+)(/|)$ /?class=$1&api=$2 last;
rewrite ^/([^.]+)/([^.]+)$ /?class=$1&api=$2 last;