feat: 实现应用标签管理功能

- 新增标签管理页面,支持标签的增删改查
- 在应用添加/编辑页面增加标签选择功能
- 在首页和应用详情页展示标签信息
- 新增标签筛选功能,支持按标签搜索应用
- 新增标签展示页面,展示所有标签及相关应用
This commit is contained in:
2025-07-06 21:27:50 +08:00
parent accf0760bb
commit e8e4f6c269
13 changed files with 572 additions and 21 deletions

View File

@@ -47,6 +47,16 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_app'])) {
if ($stmt->execute() === TRUE) {
$appId = $stmt->insert_id;
// 保存标签关联
if (!empty($_POST['tags'])) {
$stmt = $conn->prepare("INSERT INTO app_tags (app_id, tag_id) VALUES (?, ?)");
foreach ($_POST['tags'] as $tagId) {
$stmt->bind_param("ii", $appId, $tagId);
$stmt->execute();
}
$stmt->close();
}
// 处理上传的预览图片
if (!empty($_FILES['images']['name'][0])) {
$uploadDir = '../images/';
@@ -146,6 +156,18 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_app'])) {
<label for="name" class="form-label">App名称</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="mb-3">
<label for="tags" class="form-label">标签</label>
<select id="tags" name="tags[]" multiple class="form-control">
<?php
$tagResult = $conn->query("SELECT id, name FROM tags");
while ($tag = $tagResult->fetch_assoc()):
?>
<option value="<?php echo $tag['id']; ?>"><?php echo htmlspecialchars($tag['name']); ?></option>
<?php endwhile; ?>
</select>
<small class="form-text text-muted">按住Ctrl键可选择多个标签</small>
</div>
<div class="mb-3">
<label for="description" class="form-label">描述</label>
<textarea class="form-control" id="description" name="description" rows="3" required></textarea>

View File

@@ -84,11 +84,26 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_app'])) {
}
}
header('Location: index.php?success=App 更新成功');
exit;
} else {
$error = 'App 更新失败: '. $conn->error;
}
// 更新标签关联
$stmt = $conn->prepare("DELETE FROM app_tags WHERE app_id = ?");
$stmt->bind_param("i", $appId);
$stmt->execute();
$stmt->close();
if (!empty($_POST['tags'])) {
$stmt = $conn->prepare("INSERT INTO app_tags (app_id, tag_id) VALUES (?, ?)");
foreach ($_POST['tags'] as $tagId) {
$stmt->bind_param("ii", $appId, $tagId);
$stmt->execute();
}
$stmt->close();
}
header('Location: index.php?success=App 更新成功');
exit;
} else {
$error = 'App 更新失败: '. $conn->error;
}
}
?>
<!DOCTYPE html>
@@ -150,7 +165,29 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_app'])) {
<label for="name" class="form-label">App名称</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($app['name']); ?>" required>
</div>
<div class="mb-3">
<div class="mb-3">
<label for="tags" class="form-label">标签</label>
<select id="tags" name="tags[]" multiple class="form-control">
<?php
$selectedTags = [];
$tagQuery = $conn->prepare("SELECT tag_id FROM app_tags WHERE app_id = ?");
$tagQuery->bind_param("i", $appId);
$tagQuery->execute();
$tagResult = $tagQuery->get_result();
while ($tag = $tagResult->fetch_assoc()) {
$selectedTags[] = $tag['tag_id'];
}
$allTags = $conn->query("SELECT id, name FROM tags");
while ($tag = $allTags->fetch_assoc()):
$selected = in_array($tag['id'], $selectedTags) ? 'selected' : '';
?>
<option value="<?php echo $tag['id']; ?>" <?php echo $selected; ?>><?php echo htmlspecialchars($tag['name']); ?></option>
<?php endwhile; ?>
</select>
<div class="form-text">按住Ctrl键可选择多个标签</div>
</div>
<div class="mb-3">
<label for="description" class="form-label">描述</label>
<textarea class="form-control" id="description" name="description" rows="3" required><?php echo htmlspecialchars($app['description']); ?></textarea>
</div>

View File

@@ -75,6 +75,9 @@ if (!$resultApps) {
<?php endif; ?>
<h2>App列表</h2>
<div class="mb-3">
<a href="manage_tags.php" class="btn btn-info">标签管理</a>
</div>
<table class="table table-striped">
<thead>
<tr>

155
admin/manage_tags.php Normal file
View File

@@ -0,0 +1,155 @@
<?php
require_once '../config.php';
require_once 'login.php'; // 确保管理员已登录
// 处理标签添加
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_tag'])) {
$name = trim($_POST['tag_name']);
if (!empty($name)) {
$stmt = $conn->prepare("INSERT INTO tags (name) VALUES (?)");
$stmt->bind_param("s", $name);
if ($stmt->execute()) {
header('Location: manage_tags.php?success=标签添加成功');
exit;
} else {
$error = '添加失败: ' . $conn->error;
}
} else {
$error = '标签名称不能为空';
}
}
// 处理标签删除
if (isset($_GET['delete'])) {
$tagId = intval($_GET['delete']);
$stmt = $conn->prepare("DELETE FROM tags WHERE id = ?");
$stmt->bind_param("i", $tagId);
if ($stmt->execute()) {
header('Location: manage_tags.php?success=标签删除成功');
exit;
} else {
$error = '删除失败: ' . $conn->error;
}
}
// 处理标签编辑
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_tag'])) {
$tagId = intval($_POST['tag_id']);
$name = trim($_POST['tag_name']);
if (!empty($name)) {
$stmt = $conn->prepare("UPDATE tags SET name = ? WHERE id = ?");
$stmt->bind_param("si", $name, $tagId);
if ($stmt->execute()) {
header('Location: manage_tags.php?success=标签更新成功');
exit;
} else {
$error = '更新失败: ' . $conn->error;
}
} else {
$error = '标签名称不能为空';
}
}
// 获取所有标签
$tagsResult = $conn->query("SELECT * FROM tags ORDER BY created_at DESC");
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>标签管理 - 应用商店后台</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<h1 class="mb-4">标签管理</h1>
<a href="index.php" class="btn btn-secondary mb-3">返回应用列表</a>
<?php if (isset($_GET['success'])): ?>
<div class="alert alert-success"><?php echo $_GET['success']; ?></div>
<?php endif; ?>
<?php if (isset($error)): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<!-- 添加标签表单 -->
<div class="card mb-4">
<div class="card-header">
<h5 class="mb-0">添加新标签</h5>
</div>
<div class="card-body">
<form method="post">
<div class="mb-3">
<label for="tag_name" class="form-label">标签名称</label>
<input type="text" class="form-control" id="tag_name" name="tag_name" required>
</div>
<button type="submit" name="add_tag" class="btn btn-primary">添加标签</button>
</form>
</div>
</div>
<!-- 标签列表 -->
<div class="card">
<div class="card-header">
<h5 class="mb-0">现有标签</h5>
</div>
<div class="card-body">
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>标签名称</th>
<th>创建时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php while ($tag = $tagsResult->fetch_assoc()): ?>
<tr>
<td><?php echo $tag['id']; ?></td>
<td><?php echo htmlspecialchars($tag['name']); ?></td>
<td><?php echo $tag['created_at']; ?></td>
<td>
<!-- 编辑按钮触发模态框 -->
<button type="button" class="btn btn-sm btn-outline-primary" data-bs-toggle="modal" data-bs-target="#editModal<?php echo $tag['id']; ?>">
编辑
</button>
<a href="manage_tags.php?delete=<?php echo $tag['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('确定要删除这个标签吗?关联的应用标签也会被删除。');">删除</a>
</td>
</tr>
<!-- 编辑标签模态框 -->
<div class="modal fade" id="editModal<?php echo $tag['id']; ?>" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">编辑标签</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="post">
<input type="hidden" name="tag_id" value="<?php echo $tag['id']; ?>">
<div class="mb-3">
<label for="edit_tag_name<?php echo $tag['id']; ?>" class="form-label">标签名称</label>
<input type="text" class="form-control" id="edit_tag_name<?php echo $tag['id']; ?>" name="tag_name" value="<?php echo htmlspecialchars($tag['name']); ?>" required>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
<button type="submit" name="edit_tag" class="btn btn-primary">保存修改</button>
</div>
</form>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
</tbody>
</table>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

32
api.php
View File

@@ -42,6 +42,14 @@ if (isset($_GET['action'])) {
$stmtParams[] = &$ageRating;
$paramTypes .= 's';
}
// 标签过滤
if (isset($_GET['tag'])) {
$tag = $_GET['tag'];
$conditions[] = "apps.id IN (SELECT app_id FROM app_tags JOIN tags ON app_tags.tag_id = tags.id WHERE tags.name = ?)";
$stmtParams[] = &$tag;
$paramTypes .= 's';
}
// 分页参数处理
$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
@@ -155,6 +163,18 @@ if (isset($_GET['action'])) {
}
$app['reviews'] = $reviews;
// 获取应用标签
$sqlTags = "SELECT tags.id, tags.name FROM app_tags JOIN tags ON app_tags.tag_id = tags.id WHERE app_tags.app_id = ?";
$stmtTags = $conn->prepare($sqlTags);
$stmtTags->bind_param("i", $appId);
$stmtTags->execute();
$resultTags = $stmtTags->get_result();
$tags = [];
while ($tag = $resultTags->fetch_assoc()) {
$tags[] = $tag;
}
$app['tags'] = $tags;
echo json_encode($app);
} else {
http_response_code(404);
@@ -198,6 +218,18 @@ if (isset($_GET['action'])) {
echo json_encode($favorites);
exit;
}
// 获取所有标签
elseif ($action === 'tags' && $requestMethod === 'GET') {
$sql = "SELECT id, name FROM tags ORDER BY name";
$result = $conn->query($sql);
$tags = [];
while ($row = $result->fetch_assoc()) {
$tags[] = $row;
}
echo json_encode($tags);
exit;
}
// 获取应用推荐列表
elseif ($action === 'recommendations' && $requestMethod === 'GET') {

28
app.php
View File

@@ -100,7 +100,33 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['rating'])) {
<p><?php echo nl2br(htmlspecialchars($app['age_rating_description'])); ?></p>
</div>
<?php endif; ?>
<p>适用平台: <?php echo implode(', ', json_decode($app['platforms'], true) ?? []); ?></p>
<p>适用平台: <?php
$platforms = json_decode($app['platforms'], true) ?? [];
$platformIcons = [
'Windows' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16"><path d="M2.5 1.5v10.5h10.5V1.5H2.5zm0 12v10.5h10.5V13.5H2.5zm11 0v10.5H21.5V13.5h-8zm0-12v10.5H21.5V1.5h-8z" fill="currentColor"/></svg>',
'Mac' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16"><path d="M20 18.5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0zm-16 0a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0zm14.5-17A2.5 2.5 0 0 1 23 4.5v13a2.5 2.5 0 0 1-2.5 2.5h-13A2.5 2.5 0 0 1 5 17.5V4.5A2.5 2.5 0 0 1 7.5 2h13zm-13 15A1.5 1.5 0 0 0 6 17.5v9h12v-9a1.5 1.5 0 0 0-1.5-1.5h-9z" fill="currentColor"/></svg>',
'Linux' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16"><path d="M12 2a10 10 0 1 0 10 10A10 10 0 0 0 12 2zm0 18a8 8 0 0 1-5.66-2.34l1.41-1.41A6 6 0 0 0 12 18a6 6 0 0 0 4.24-1.76l1.41 1.41A8 8 0 0 1 12 20zm0-14a2 2 0 1 1-2 2 2 2 0 0 1 2-2zm0 4a2 2 0 1 1 2 2 2 2 0 0 1-2-2z" fill="currentColor"/></svg>',
'Android' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16"><path d="M19.92 13.93a1 1 0 0 0-.84-.53h-.67a1 1 0 0 0-.93.62l-1.26 2.71a14.2 14.2 0 0 1-4.74 0l-1.26-2.71a1 1 0 0 0-.93-.62h-.67a1 1 0 0 0-.84.53l-1.6 3.46a1 1 0 0 0 .84 1.41h1.62a1 1 0 0 0 .93-.62l1.26-2.71a12.24 12.24 0 0 0 3.7 0l1.26 2.71a1 1 0 0 0 .93.62h1.62a1 1 0 0 0 .84-1.41zM7.5 10.5a1 1 0 1 1 0-2 1 1 0 0 1 0 2zm9 0a1 1 0 1 1 0-2 1 1 0 0 1 0 2zm4.5 4.5v-3a1 1 0 0 0-1-1h-1.5a1 1 0 0 0-1 1v3a1 1 0 0 0 1 1H20a1 1 0 0 0 1-1zm-18 0v-3a1 1 0 0 1 1-1H5a1 1 0 0 1 1 1v3a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1z" fill="currentColor"/></svg>',
'iOS' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16"><path d="M15.5 2h-7A3.5 3.5 0 0 0 5 5.5v13A3.5 3.5 0 0 0 8.5 22h7A3.5 3.5 0 0 0 19 18.5v-13A3.5 3.5 0 0 0 15.5 2zm-7 1A1.5 1.5 0 0 1 10 4.5v13A1.5 1.5 0 0 1 8.5 19h-1A1.5 1.5 0 0 1 6 17.5v-13A1.5 1.5 0 0 1 7.5 3zm7 1A1.5 1.5 0 0 1 17 4.5v13a1.5 1.5 0 0 1-1.5 1.5h-1A1.5 1.5 0 0 1 13 17.5v-13A1.5 1.5 0 0 1 14.5 3zm-3.5 11a1 1 0 0 1 1-1h1a1 1 0 0 1 0 2h-1a1 1 0 0 1-1-1zm0-4a1 1 0 0 1 1-1h1a1 1 0 0 1 0 2h-1a1 1 0 0 1-1-1z" fill="currentColor"/></svg>'
];
$platformMap = [
'android' => 'Android',
'ios' => 'iOS',
'windows_win7' => 'WindowsWindows 7以上',
'windows_xp' => 'Windows XP',
'macos' => 'MacOS',
'linux_arch' => 'Linux适用于Arch Linux',
'linux_ubuntu' => 'Linux适用于Ubuntu',
];
$platformTexts = [];
foreach ($platforms as $platform) {
$icon = $platformIcons[$platform] ?? $platformIcons[ucfirst($platform)] ?? '';
$readableName = $platformMap[strtolower($platform)] ?? ucfirst($platform);
$platformTexts[] = $icon . ' ' . $readableName;
}
echo implode(', ', $platformTexts);
?></p>
<p>评分: <?php echo round($app['avg_rating'], 1); ?>/5</p>
</div>
<div class="col-md-6">

View File

@@ -64,6 +64,22 @@ CREATE TABLE IF NOT EXISTS users (
last_login TIMESTAMP NULL
);
-- 创建标签表
CREATE TABLE IF NOT EXISTS tags (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 创建应用标签关联表
CREATE TABLE IF NOT EXISTS app_tags (
app_id INT NOT NULL,
tag_id INT NOT NULL,
PRIMARY KEY (app_id, tag_id),
FOREIGN KEY (app_id) REFERENCES apps(id) ON DELETE CASCADE,
FOREIGN KEY (tag_id) REFERENCES tags(id) ON DELETE CASCADE
);
-- 创建应用分类表
CREATE TABLE IF NOT EXISTS categories (
id INT AUTO_INCREMENT PRIMARY KEY,

BIN
files/cloudmusic.exe Normal file

Binary file not shown.

Binary file not shown.

BIN
images/139649.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 456 KiB

BIN
images/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

118
index.php
View File

@@ -1,7 +1,10 @@
<?php
session_start();
require_once 'config.php';
?>
if (!isset($conn) || !$conn instanceof mysqli) {
die('数据库连接失败,请检查配置文件。');
}?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
@@ -31,23 +34,53 @@ require_once 'config.php';
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">首页</a>
<a class="nav-link" href="index.php">首页</a>
</li>
<?php if (isset($_SESSION['admin'])): ?>
<li class="nav-item">
<a class="nav-link" href="/admin/">管理</a>
</li>
<?php endif; ?>
<li class="nav-item">
<a class="nav-link" href="tags.php">标签</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-4">
<form method="get" action="index.php" class="mb-4">
<div class="input-group">
<input type="text" name="search" class="form-control" placeholder="搜索应用..." value="<?php echo isset($_GET['search']) ? htmlspecialchars($_GET['search']) : ''; ?>">
<button class="btn btn-primary" type="submit">搜索</button>
<form method="get" action="index.php" class="mb-4" onsubmit="return validateSearch();">
<script>
function validateSearch() {
const searchInput = document.querySelector('input[name="search"]');
if (searchInput.value.trim() === '') {
alert('请填写搜索名称后再进行搜索!');
return false;
}
return true;
}
</script>
<div class="row g-3">
<div class="col-md-6">
<input type="text" name="search" class="form-control" placeholder="搜索应用..." value="<?php echo isset($_GET['search']) ? htmlspecialchars($_GET['search']) : ''; ?>">
</div>
<div class="col-md-4">
<select name="tag" class="form-select">
<option value="">所有标签</option>
<?php
$tagResult = $conn->query("SELECT id, name FROM tags ORDER BY name");
$selectedTag = isset($_GET['tag']) ? $_GET['tag'] : '';
while ($tag = $tagResult->fetch_assoc()):
$selected = ($tag['id'] == $selectedTag) ? 'selected' : '';
?>
<option value="<?php echo $tag['id']; ?>" <?php echo $selected; ?>><?php echo htmlspecialchars($tag['name']); ?></option>
<?php endwhile; ?>
</select>
</div>
<div class="col-md-2">
<button class="btn btn-primary w-100" type="submit">搜索</button>
</div>
</div>
</form>
<h1>最新应用</h1>
@@ -55,26 +88,83 @@ require_once 'config.php';
<!-- 这里将通过PHP动态加载应用列表 -->
<?php
$search = isset($_GET['search']) ? $_GET['search'] : '';
$limit = isset($_GET['limit']) ? intval($_GET['limit']) : 12;
$offset = isset($_GET['page']) ? (intval($_GET['page']) - 1) * $limit : 0;
$sql = "SELECT apps.id, apps.name, apps.description, apps.age_rating, AVG(reviews.rating) as avg_rating
FROM apps
LEFT JOIN reviews ON apps.id = reviews.app_id ";
if (!empty($search)) {
$sql .= "WHERE apps.name LIKE ? OR apps.description LIKE ? ";
$conditions = [];
$params = [];
$paramTypes = '';
// 标签筛选
if (!empty($_GET['tag'])) {
$sql .= "JOIN app_tags ON apps.id = app_tags.app_id
JOIN tags ON app_tags.tag_id = tags.id ";
$conditions[] = "app_tags.tag_id = ?";
$tagId = $_GET['tag'];
$params[] = &$tagId;
$paramTypes .= 'i';
}
// 平台筛选
if (!empty($_GET['platform'])) {
$conditions[] = "apps.platform = ?";
$platform = $_GET['platform'];
$params[] = &$platform;
$paramTypes .= 's';
}
// 年龄分级筛选
if (!empty($_GET['age_rating'])) {
$conditions[] = "apps.age_rating = ?";
$ageRating = $_GET['age_rating'];
$params[] = &$ageRating;
$paramTypes .= 's';
}
// 搜索关键词筛选
if (!empty($search)) {
$conditions[] = "(apps.name LIKE ? OR apps.description LIKE ?)";
$searchTerm1 = "%$search%";
$searchTerm2 = "%$search%";
$params[] = &$searchTerm1;
$params[] = &$searchTerm2;
$paramTypes .= 'ss';
}
// 添加条件
if (!empty($conditions)) {
$sql .= "WHERE " . implode(" AND ", $conditions);
}
$sql .= "GROUP BY apps.id
ORDER BY apps.created_at DESC
LIMIT 12";
if (!empty($search)) {
LIMIT ? OFFSET ?";
$limitVal = $limit;
$offsetVal = $offset;
$params[] = &$limitVal;
$params[] = &$offsetVal;
// 添加分页参数类型
$paramTypes .= 'ii';
// 执行查询
if (!empty($params)) {
$stmt = $conn->prepare($sql);
$searchTerm = "%$search%";
$stmt->bind_param("ss", $searchTerm, $searchTerm);
$stmt->execute();
if (!$stmt) {
die('预处理语句失败: ' . $conn->error);
}
call_user_func_array([$stmt, 'bind_param'], array_merge([$paramTypes], $params));
if (!$stmt->execute()) {
die('执行语句失败: ' . $stmt->error);
}
$result = $stmt->get_result();
} else {
$result = $conn->query($sql);
if (!$result) {
die('查询失败: ' . $conn->error);
}
}
if ($result->num_rows > 0) {

170
tags.php Normal file
View File

@@ -0,0 +1,170 @@
<?php
session_start();
require_once 'config.php';
if (!isset($conn) || !$conn instanceof mysqli) {
die('数据库连接失败,请检查配置文件。');
}
$tagId = isset($_GET['tag']) ? intval($_GET['tag']) : 0;
$search = isset($_GET['search']) ? $_GET['search'] : '';
$limit = isset($_GET['limit']) ? intval($_GET['limit']) : 12;
$offset = isset($_GET['page']) ? (intval($_GET['page']) - 1) * $limit : 0;
$sql = "SELECT apps.id, apps.name, apps.description, apps.age_rating, AVG(reviews.rating) as avg_rating
FROM apps
LEFT JOIN reviews ON apps.id = reviews.app_id ";
$conditions = [];
$params = [];
$paramTypes = '';
if ($tagId) {
$sql .= "JOIN app_tags ON apps.id = app_tags.app_id
JOIN tags ON app_tags.tag_id = tags.id ";
$conditions[] = "app_tags.tag_id = ?";
$params[] = $tagId;
$paramTypes .= 'i';
}
if (!empty($search)) {
$conditions[] = "(apps.name LIKE ? OR apps.description LIKE ?)";
$searchTerm1 = "%$search%";
$searchTerm2 = "%$search%";
$params[] = $searchTerm1;
$params[] = $searchTerm2;
$paramTypes .= 'ss';
}
if (!empty($conditions)) {
$sql .= " WHERE " . implode(" AND ", $conditions);
}
$sql .= " GROUP BY apps.id
ORDER BY apps.created_at DESC
LIMIT ? OFFSET ?";
$params[] = $limit;
$params[] = $offset;
$paramTypes .= 'ii';
if (!empty($params)) {
$stmt = $conn->prepare($sql);
if (!$stmt) {
die('预处理语句失败: ' . $conn->error);
}
$stmt->bind_param($paramTypes, ...$params);
if (!$stmt->execute()) {
die('执行语句失败: ' . $stmt->error);
}
$result = $stmt->get_result();
} else {
$result = $conn->query($sql);
if (!$result) {
die('查询失败: ' . $conn->error);
}
}
$tagResult = $conn->query("SELECT id, name FROM tags ORDER BY name");
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>应用标签</title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- 自定义CSS -->
<link rel="stylesheet" href="styles.css">
<!-- Fluent Design 模糊效果 -->
<style>
.blur-bg {
backdrop-filter: blur(10px);
background-color: rgba(255, 255, 255, 0.5);
}
</style>
</head>
<body>
<!-- 导航栏 -->
<nav class="navbar navbar-expand-lg navbar-light blur-bg">
<div class="container">
<a class="navbar-brand" href="index.php"><?php echo APP_STORE_NAME; ?></a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="index.php">首页</a>
</li>
<?php if (isset($_SESSION['admin'])): ?>
<li class="nav-item">
<a class="nav-link" href="/admin/">管理</a>
</li>
<?php endif; ?>
<li class="nav-item">
<a class="nav-link active" href="tags.php">标签</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-4">
<h1>应用标签</h1>
<div class="mb-4">
<?php $tagResult = $conn->query("SELECT id, name FROM tags ORDER BY name"); ?>
<?php while ($tag = $tagResult->fetch_assoc()): ?>
<a href="tags.php?tag=<?php echo $tag['id']; ?>" class="btn btn-outline-primary me-2 mb-2">
<?php echo htmlspecialchars($tag['name']); ?>
</a>
<?php endwhile; ?>
</div>
<form method="get" action="tags.php" class="mb-4">
<div class="row g-3">
<div class="col-md-6">
<input type="text" name="search" class="form-control" placeholder="搜索应用..." value="<?php echo isset($_GET['search']) ? htmlspecialchars($_GET['search']) : ''; ?>">
</div>
<div class="col-md-2">
<button class="btn btn-primary w-100" type="submit">搜索</button>
</div>
</div>
</form>
<div class="row">
<?php if ($result->num_rows > 0): ?>
<?php while ($row = $result->fetch_assoc()): ?>
<div class="col-md-3 mb-4">
<div class="card blur-bg">
<img src="images/default.png" class="card-img-top" alt="<?php echo $row['name']; ?>">
<div class="card-body">
<h5 class="card-title"><?php echo $row['name']; ?></h5>
<p class="card-text"><?php echo substr($row['description'], 0, 100); ?>...</p>
<p class="card-text">评分: <?php echo round($row['avg_rating'], 1); ?>/5</p>
<a href="app.php?id=<?php echo $row['id']; ?>" class="btn btn-primary">查看详情</a>
</div>
</div>
</div>
<?php endwhile; ?>
<?php else: ?>
<div class="col-12">
<p class="text-center">未找到相关应用。</p>
</div>
<?php endif; ?>
</div>
</div>
<!-- Bootstrap JS Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
// 导航栏滚动效果
window.addEventListener('scroll', function() {
const navbar = document.querySelector('.navbar');
if (window.scrollY > 10) {
navbar.classList.add('scrolled');
} else {
navbar.classList.remove('scrolled');
}
});
</script>
</body>
</html>