Files
leonapp/admin/editapp.php
2025-09-20 22:20:08 +08:00

402 lines
19 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require_once '../config.php';
session_start();
// 检查是否已登录
if (!isset($_SESSION['admin'])) {
header('Location: login.php');
exit();
}
// 非全部权限管理员重定向到对应权限页面
if ($_SESSION['admin']['permission'] != 'all') {
$redirect = $_SESSION['admin']['permission'] == 'say' ? 'announcements.php' : 'review_apps.php';
header("Location: $redirect");
exit();
}
// 验证App ID
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
echo '<script>Swal.fire("错误", "无效的App ID", "error").then(() => { window.location.href = "index.php"; });</script>';
exit;
}
$appId = $_GET['id'];
// 获取App信息
$app = null;
$getAppSql = "SELECT * FROM apps WHERE id = ?";
$stmt = $conn->prepare($getAppSql);
$stmt->bind_param("i", $appId);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
echo '<script>Swal.fire("错误", "App不存在", "error").then(() => { window.location.href = "index.php"; });</script>';
exit;
}
$app = $result->fetch_assoc();
$platforms = json_decode($app['platforms'], true);
$success = '';
$error = '';
// 处理编辑App请求
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_app'])) {
$name = $_POST['name'];
$description = $_POST['description'];
$ageRating = $_POST['age_rating'];
$newPlatforms = json_encode($_POST['platforms'] ?? []);
// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// 验证必填字段
$required = ['name', 'description', 'age_rating', 'platforms'];
$errors = [];
foreach ($required as $field) {
if (empty($_POST[$field])) {
$errors[] = ucfirst($field) . ' 不能为空';
}
}
// 年龄分级验证
if (($_POST['age_rating'] === '12+' || $_POST['age_rating'] === '17+') && empty($_POST['age_rating_description'])) {
$errors[] = '年龄分级为12+或以上时,年龄分级说明不能为空';
}
// 处理应用图标上传(如果有新上传)
if (!empty($_FILES['images']['name'][0])) {
$uploadDir = '../images/';
foreach ($_FILES['images']['tmp_name'] as $key => $tmpName) {
$fileName = basename($_FILES['images']['name'][$key]);
$targetPath = $uploadDir . $fileName;
if (move_uploaded_file($tmpName, $targetPath)) {
$insertImageSql = "INSERT INTO app_images (app_id, image_path) VALUES (?, ?)";
$imgStmt = $conn->prepare($insertImageSql);
$imgStmt->bind_param("is", $appId, $targetPath);
$imgStmt->execute();
}
}
}
// 处理新上传的App文件
if (!empty($_FILES['app_file']['name'])) {
$uploadDir = '../files/';
$fileName = basename($_FILES['app_file']['name']);
$targetPath = $uploadDir . $fileName;
if (move_uploaded_file($_FILES['app_file']['tmp_name'], $targetPath)) {
$version = $_POST['version'];
$changelog = $_POST['changelog'];
$insertVersionSql = "INSERT INTO app_versions (app_id, version, changelog, file_path) VALUES (?, ?, ?, ?)";
$verStmt = $conn->prepare($insertVersionSql);
$verStmt->bind_param("isss", $appId, $version, $changelog, $targetPath);
$verStmt->execute();
}
}
// 更新标签关联
$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();
}
$updateAppSql = "UPDATE apps SET name = ?, description = ?, age_rating = ?, platforms = ? WHERE id = ?";
$updateStmt = $conn->prepare($updateAppSql);
$updateStmt->bind_param("ssssi", $name, $description, $ageRating, $newPlatforms, $appId);
if ($updateStmt->execute() === TRUE) {
echo '<script>Swal.fire("成功", "App 更新成功", "success").then(() => { window.location.href = "index.php"; });</script>';
exit;
} else {
echo '<script>Swal.fire("错误", "App 更新失败: '. $conn->error .'", "error");</script>';
}
$updateStmt->close();
}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>编辑App - <?php echo APP_STORE_NAME; ?></title>
<!-- Bootstrap CSS -->
<link href="../css/bootstrap.min.css" rel="stylesheet">
<!-- Font Awesome -->
<link rel="stylesheet" href="/css/all.min.css">
<!-- 自定义CSS -->
<link rel="stylesheet" href="../styles.css">
<script src="/js/sweetalert.js"></script>
<script>
function confirmLogout() {
Swal.fire({
title: '确定要登出吗?',
icon: 'question',
showCancelButton: true,
confirmButtonText: '确定',
cancelButtonText: '取消'
}).then((result) => {
if (result.isConfirmed) {
window.location.href = 'logout.php';
}
});
}
</script>
<!-- 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">App列表</a>
</li>
<li class="nav-item">
<a class="nav-link" href="addapp.php">添加App</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="editapp.php?id=<?php echo $appId; ?>">编辑App</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" onclick="confirmLogout()">退出登录</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-4">
<?php if (!empty($success)): ?>
<div class="alert alert-success"><?php echo $success; ?></div>
<?php endif; ?>
<?php if (!empty($error)): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<h2>编辑App: <?php echo htmlspecialchars($app['name']); ?></h2>
<form method="post" enctype="multipart/form-data">
<div class="form-floating mb-3">
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($app['name']); ?>" required>
<label for="name"><i class="fas fa-file-signature me-2"></i>App名称</label>
</div>
<div class="mb-3">
<label for="description" class="form-label"><i class="fas fa-align-left me-2"></i>描述</label>
<textarea class="form-control" id="description" name="description" rows="3" required><?php echo htmlspecialchars($app['description']); ?></textarea>
</div>
<div class="mb-3">
<label for="age_rating" class="form-label"><i class="fas fa-shield-alt me-2"></i>年龄分级</label>
<select class="form-select" id="age_rating" name="age_rating" required>
<option value="3+" <?php echo $app['age_rating'] === '3+' ? 'selected' : ''; ?>>3+</option>
<option value="7+" <?php echo $app['age_rating'] === '7+' ? 'selected' : ''; ?>>7+</option>
<option value="12+" <?php echo $app['age_rating'] === '12+' ? 'selected' : ''; ?>>12+</option>
<option value="17+" <?php echo $app['age_rating'] === '17+' ? 'selected' : ''; ?>>17+</option>
</select>
</div>
<div class="mb-3" id="ageRatingDescriptionGroup" style="display: none;">
<label for="age_rating_description" class="form-label">年龄分级说明</label>
<textarea class="form-control" id="age_rating_description" name="age_rating_description" rows="3" placeholder="请说明为何需要此年龄分级"><?php echo htmlspecialchars($app['age_rating_description'] ?? ''); ?></textarea>
<div class="form-text">当年龄分级为12+或以上时,此项为必填</div>
</div>
<div class="mb-3">
<label class="form-label">适用平台</label>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="android" id="android" name="platforms[]" <?php echo in_array('android', $platforms) ? 'checked' : ''; ?>>
<label class="form-check-label" for="android">Android</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="ios" id="ios" name="platforms[]" <?php echo in_array('ios', $platforms) ? 'checked' : ''; ?>>
<label class="form-check-label" for="ios">iOS</label>
</div>
<?php
$windowsChecked = false;
$windowsVersion = '';
foreach ($platforms as $p) {
if (strpos($p, 'windows_') === 0) {
$windowsChecked = true;
$windowsVersion = $p;
break;
}
}
?>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="windows" id="windows" name="platforms[]" <?php echo $windowsChecked ? 'checked' : ''; ?>>
<label class="form-check-label" for="windows">Windows</label>
</div>
<div id="windows_suboptions" class="ms-4 mt-2" style="display: <?php echo $windowsChecked ? 'block' : 'none'; ?>">
<div class="form-check">
<input class="form-check-input" type="radio" name="windows_version" id="windows_xp" value="windows_xp" <?php echo $windowsVersion === 'windows_xp' ? 'checked' : ''; ?>>
<label class="form-check-label" for="windows_xp">XP以前</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="windows_version" id="windows_win7" value="windows_win7" <?php echo $windowsVersion === 'windows_win7' ? 'checked' : ''; ?>>
<label class="form-check-label" for="windows_win7">Win7以后</label>
</div>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="macos" id="macos" name="platforms[]" <?php echo in_array('macos', $platforms) ? 'checked' : ''; ?>>
<label class="form-check-label" for="macos">macOS</label>
</div>
<?php
$linuxChecked = false;
$linuxVersion = '';
foreach ($platforms as $p) {
if (strpos($p, 'linux_') === 0) {
$linuxChecked = true;
$linuxVersion = $p;
break;
}
}
?>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="linux" id="linux" name="platforms[]" <?php echo $linuxChecked ? 'checked' : ''; ?>>
<label class="form-check-label" for="linux">Linux</label>
</div>
<div id="linux_suboptions" class="ms-4 mt-2" style="display: <?php echo $linuxChecked ? 'block' : 'none'; ?>">
<div class="form-check">
<input class="form-check-input" type="radio" name="linux_distribution" id="linux_ubuntu" value="linux_ubuntu" <?php echo $linuxVersion === 'linux_ubuntu' ? 'checked' : ''; ?>>
<label class="form-check-label" for="linux_ubuntu">Ubuntu</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="linux_distribution" id="linux_arch" value="linux_arch" <?php echo $linuxVersion === 'linux_arch' ? 'checked' : ''; ?>>
<label class="form-check-label" for="linux_arch">Arch Linux</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="linux_distribution" id="linux_centos" value="linux_centos" <?php echo $linuxVersion === 'linux_centos' ? 'checked' : ''; ?>>
<label class="form-check-label" for="linux_centos">CentOS</label>
</div>
</div>
</div>
<div class="form-floating mb-3">
<input type="text" class="form-control" id="version" name="version" placeholder="如: 1.0.1">
<label for="version" class="form-label"><i class="fas fa-code-branch me-2"></i>版本号</label>
<div class="form-text">仅在上传新安装包时填写</div>
</div>
<div class="form-floating mb-3">
<textarea class="form-control" id="changelog" name="changelog" rows="3" placeholder="描述本次更新内容"></textarea>
<label for="changelog" class="form-label"><i class="fas fa-history me-2"></i>更新日志</label>
</div>
<div class="mb-3">
<label for="app_file" class="form-label">新App文件 (可选)</label>
<input class="form-control" type="file" id="app_file" name="app_file">
</div>
<div class="mb-3">
<label for="images" class="form-label"><i class="fas fa-upload me-2"></i>预览图片 (可多选)</label>
<input class="form-control" type="file" id="images" name="images[]" multiple>
</div>
<button type="submit" name="edit_app" class="btn btn-primary"><i class="fas fa-save me-2"></i>保存修改</button>
<a href="index.php" class="btn btn-secondary ms-2"><i class="fas fa-times me-1"></i>取消</a>
</form>
</div>
<!-- Bootstrap JS Bundle with Popper -->
<script src="/js/bootstrap.bundle.js"></script>
<script>
// 年龄分级说明显示控制
// 年龄分级说明显示控制
const ageRatingSelect = document.getElementById('age_rating');
const descriptionGroup = document.getElementById('ageRatingDescriptionGroup');
const descriptionInput = document.getElementById('age_rating_description');
function toggleAgeDescription() {
const selectedRating = ageRatingSelect.value;
if (selectedRating === '12+' || selectedRating === '17+') {
descriptionGroup.style.display = 'block';
descriptionInput.required = true;
} else {
descriptionGroup.style.display = 'none';
descriptionInput.required = false;
}
}
ageRatingSelect.addEventListener('change', toggleAgeDescription);
// 初始加载时检查
toggleAgeDescription();
// 导航栏滚动效果
window.addEventListener('scroll', function() {
const navbar = document.querySelector('.navbar');
if (window.scrollY > 10) {
navbar.classList.add('scrolled');
} else {
navbar.classList.remove('scrolled');
}
});
// 平台子选项显示控制
document.getElementById('windows').addEventListener('change', function() {
const suboptions = document.getElementById('windows_suboptions');
suboptions.style.display = this.checked ? 'block' : 'none';
if (!this.checked) {
document.querySelectorAll('input[name="windows_version"]').forEach(radio => radio.checked = false);
}
});
document.getElementById('linux').addEventListener('change', function() {
const suboptions = document.getElementById('linux_suboptions');
suboptions.style.display = this.checked ? 'block' : 'none';
if (!this.checked) {
document.querySelectorAll('input[name="linux_distribution"]').forEach(radio => radio.checked = false);
}
});
// 表单提交验证
document.querySelector('form').addEventListener('submit', function(e) {
// 验证Windows子选项
if (document.getElementById('windows').checked && !document.querySelector('input[name="windows_version"]:checked')) {
e.preventDefault();
Swal.fire({
title: '提示',
text: '请选择Windows版本XP以前或Win7以后',
icon: 'warning',
confirmButtonText: '确定'
});
return;
}
// 验证Linux子选项
if (document.getElementById('linux').checked && !document.querySelector('input[name="linux_distribution"]:checked')) {
e.preventDefault();
Swal.fire({
title: '提示',
text: '请选择Linux发行版Ubuntu、Arch Linux或CentOS',
icon: 'warning',
confirmButtonText: '确定'
});
return;
}
// 更新平台值包含子选项信息
if (document.getElementById('windows').checked) {
document.getElementById('windows').value = document.querySelector('input[name="windows_version"]:checked').value;
}
if (document.getElementById('linux').checked) {
document.getElementById('linux').value = document.querySelector('input[name="linux_distribution"]:checked').value;
}
});
</script>
</body>
</html>
<?php
// 更新应用数据
$stmt = $conn->prepare("UPDATE apps SET name=?, description=?, age_rating=?, age_rating_description=?, platforms=?, updated_at=NOW() WHERE id=?");
$stmt->bind_param("sssssi", $name, $description, $age_rating, $_POST['age_rating_description'], $platformsJson, $appId);
?>