0) { $st = $db->prepare('DELETE FROM messages WHERE id = ?'); $st->bindValue(1, $id, SQLITE3_INTEGER); $st->execute(); admin_log('delete', $id, '删除留言'); $notice = '留言已删除'; } elseif ($action === 'toggle_status' && $id > 0) { $q = $db->prepare('SELECT status FROM messages WHERE id = ?'); $q->bindValue(1, $id, SQLITE3_INTEGER); $row = $q->execute()->fetchArray(SQLITE3_ASSOC); if ($row) { $new = ((int)$row['status'] === 1) ? 0 : 1; $u = $db->prepare('UPDATE messages SET status = ? WHERE id = ?'); $u->bindValue(1, $new, SQLITE3_INTEGER); $u->bindValue(2, $id, SQLITE3_INTEGER); $u->execute(); admin_log('toggle_status', $id, '新状态=' . $new); $notice = $new === 1 ? '已显示' : '已隐藏'; } } elseif ($action === 'reply' && $id > 0) { $reply = get_post('reply'); if (mb_len($reply) > MAX_CONTENT_LEN) { $error = '回复内容过长'; } elseif ($reply !== '') { $u = $db->prepare('UPDATE messages SET reply = ?, replied_at = CURRENT_TIMESTAMP WHERE id = ?'); $u->bindValue(1, $reply, SQLITE3_TEXT); $u->bindValue(2, $id, SQLITE3_INTEGER); $u->execute(); admin_log('reply', $id, '回复留言'); $notice = '回复成功'; } } } catch (Throwable $e) { log_error('后台操作失败: ' . $e->getMessage()); $error = '操作失败,请稍后再试'; } } /* ---------------- 列表 ---------------- */ $messages = []; $total_pages = 1; $page = max(1, (int)get_get('page', '1')); try { $db = get_db(); $total = (int)$db->querySingle('SELECT COUNT(*) FROM messages'); $total_pages = max(1, (int)ceil($total / PAGE_SIZE)); if ($page > $total_pages) { $page = $total_pages; } $offset = PAGE_SIZE * ($page - 1); $st = $db->prepare('SELECT * FROM messages ORDER BY created_at DESC, id DESC LIMIT ? OFFSET ?'); $st->bindValue(1, PAGE_SIZE, SQLITE3_INTEGER); $st->bindValue(2, $offset, SQLITE3_INTEGER); $res = $st->execute(); while ($row = $res->fetchArray(SQLITE3_ASSOC)) { $messages[] = $row; } } catch (Throwable $e) { log_error('后台列表读取失败: ' . $e->getMessage()); } ?>