From 59537c566f9f8c9de7d2a9b93f62f490aa3aa5ec Mon Sep 17 00:00:00 2001 From: mei Date: Wed, 30 Oct 2024 12:59:20 +0800 Subject: [PATCH] update --- core/router.php | 26 +++++++++++++++++---- includes/{ => account}/account.php | 0 includes/lovewall/submit_love.php | 0 includes/public/comments.php | 0 includes/public/lovewall.php | 37 ++++++++++++++++++++++++++++++ index.php | 5 +++- rewrite.conf | 2 +- 7 files changed, 63 insertions(+), 7 deletions(-) rename includes/{ => account}/account.php (100%) create mode 100644 includes/lovewall/submit_love.php create mode 100644 includes/public/comments.php create mode 100644 includes/public/lovewall.php diff --git a/core/router.php b/core/router.php index 4d6d493..5e4aa5c 100644 --- a/core/router.php +++ b/core/router.php @@ -1,7 +1,23 @@ 'Unauthorized', 'code' => 401]); + exit; + } } +// 非管理需登陆界面路由 +elseif ($api_class != 'admin') { + if (isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])) { + // 处理已登录用户请求 + // ... + } else { + echo json_encode(['error' => 'Unauthorized', 'code' => 401]); + exit; + } +} else { + echo json_encode(['error' => 'unknown error']); +} \ No newline at end of file diff --git a/includes/account.php b/includes/account/account.php similarity index 100% rename from includes/account.php rename to includes/account/account.php diff --git a/includes/lovewall/submit_love.php b/includes/lovewall/submit_love.php new file mode 100644 index 0000000..e69de29 diff --git a/includes/public/comments.php b/includes/public/comments.php new file mode 100644 index 0000000..e69de29 diff --git a/includes/public/lovewall.php b/includes/public/lovewall.php new file mode 100644 index 0000000..d303b7d --- /dev/null +++ b/includes/public/lovewall.php @@ -0,0 +1,37 @@ +query($sql); +$posts = $stmt->fetchAll(); + +// 为每个post添加评论 +foreach ($posts as &$post) { + $commentSql = "SELECT c.id, u.username AS author, c.content + FROM comments c + JOIN users u ON c.user_id = u.id + WHERE c.love_wall_id = :love_wall_id"; + $commentStmt = $pdo->prepare($commentSql); + $commentStmt->execute([':love_wall_id' => $post['id']]); + $post['comments'] = $commentStmt->fetchAll(PDO::FETCH_ASSOC); // 确保是关联数组 + // TODO: 头像 + $post['avatar'] = '/placeholder.svg?height=40&width=40'; + + // 清理不需要的索引 + $post = [ + 'id' => $post['id'], + 'author' => $post['author'], + 'avatar' => $post['avatar'], + 'content' => $post['content'], + 'likes' => (int)$post['likes'], // 转换为整数 + 'comments' => $post['comments'] + ]; +} + +echo json_encode($posts, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); +?> diff --git a/index.php b/index.php index e8ca6bb..d7fce03 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,8 @@