47 lines
1.5 KiB
PHP
47 lines
1.5 KiB
PHP
<?php
|
|
/**
|
|
* api/nav.php —— 导航数据 JSON 接口
|
|
* 用法:api/nav.php?cat=popular | red | blue
|
|
* 返回:[{ "title": "...", "items": [{"label":"...","url":"...","note":"..."}] }]
|
|
*/
|
|
require_once dirname(__DIR__) . '/includes/db.php';
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
$cat = (string)($_GET['cat'] ?? '');
|
|
if (!in_array($cat, ['popular', 'red', 'blue'], true)) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'cat 参数须为 popular/red/blue'], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
$pdo = db();
|
|
$st = $pdo->prepare('SELECT * FROM categories WHERE key = ? LIMIT 1');
|
|
$st->execute([$cat]);
|
|
$catRow = $st->fetch();
|
|
if (!$catRow) {
|
|
http_response_code(404);
|
|
echo json_encode(['error' => '分类不存在'], JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
$out = [];
|
|
$st = $pdo->prepare('SELECT * FROM nav_blocks WHERE cat_id = ? ORDER BY sort ASC, id ASC');
|
|
$st->execute([(int)$catRow['id']]);
|
|
$blocks = $st->fetchAll();
|
|
$itemSt = $pdo->prepare('SELECT * FROM nav_items WHERE block_id = ? ORDER BY sort ASC, id ASC LIMIT 15');
|
|
foreach ($blocks as $b) {
|
|
$itemSt->execute([(int)$b['id']]);
|
|
$items = [];
|
|
foreach ($itemSt->fetchAll() as $it) {
|
|
$items[] = [
|
|
'label' => (string)$it['label'],
|
|
'url' => (string)$it['url'],
|
|
'note' => (string)$it['note'],
|
|
];
|
|
}
|
|
$out[] = ['title' => (string)$b['title'], 'items' => $items];
|
|
}
|
|
|
|
echo json_encode($out, JSON_UNESCAPED_UNICODE);
|