This commit is contained in:
mei 2024-10-30 12:59:20 +08:00
parent 07ab18a47b
commit 59537c566f
7 changed files with 63 additions and 7 deletions

View File

@ -1,7 +1,23 @@
<?php
if (isset($_SESSION['user_id'])) {
passer();
} else {
header("application/json");
// 管理界面路由
if ($api_class == 'admin') {
if ($_SESSION['user_id'] == 'admin') {
// 处理管理员请求
// ...
} else {
echo json_encode(['error' => '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']);
}

View File

View File

View File

@ -0,0 +1,37 @@
<?php
// TODO: 点赞数
$sql = "SELECT lw.id, u.username AS author, lw.content, COUNT(c.id) AS likes
FROM love_wall lw
LEFT JOIN users u ON lw.user_id = u.id
LEFT JOIN comments c ON lw.id = c.love_wall_id
GROUP BY lw.id, u.username, lw.content
ORDER BY lw.created_at DESC";
$stmt = $pdo->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);
?>

View File

@ -1,6 +1,8 @@
<?php
include 'core/config.php';
include 'core/clean.php';
// TODO: cookie manage
session_start();
// 获取参数
@ -12,5 +14,6 @@ if ($api_class != 'public') {
include 'core/login_router.php';
}
include '/includes/' . $api_class . '/' . $api . '.php';
header("Content-Type: application/json");
include 'includes/' . $api_class . '/' . $api . '.php';

View File

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