Files
leonapp/admin/addapp.php

382 lines
18 KiB
PHP
Raw Normal View History

<?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();
}
$success = '';
$error = '';
// 处理添加App请求
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_app'])) {
$name = $_POST['name'];
$description = $_POST['description'];
$ageRating = $_POST['age_rating'];
$platforms = isset($_POST['platforms']) ? json_encode($_POST['platforms']) : json_encode([]);
// 处理表单提交
// 验证必填字段
$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+或以上时,年龄分级说明不能为空';
}
// 处理应用图标上传
// 处理平台数据
$platforms = json_encode($_POST['platforms']);
// 插入应用数据
$stmt = $conn->prepare("INSERT INTO apps (name, description, age_rating, age_rating_description, platforms) VALUES (?, ?, ?, ?, ?)");
if (!$stmt) {
$error = "Database error: " . $conn->error;
}
if ($stmt) {
$stmt->bind_param("sssss", $name, $description, $ageRating, $_POST['age_rating_description'], $platforms);
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/';
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();
}
}
echo '<script>Swal.fire("成功", "App 添加成功", "success").then(() => { window.location.href = "index.php"; });</script>';
exit;
} else {
echo '<script>Swal.fire("错误", "App 添加失败: '. $conn->error .'", "error");</script>';
}
}
}
?>
<!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>
</head>
<body>
<div class="admin-header">
<h1>应用管理系统</h1>
<div class="admin-actions">
<span>欢迎, <?php echo htmlspecialchars($_SESSION['admin']['username']); ?></span>
<a href="#" onclick="confirmLogout()" class="logout-btn">登出</a>
</div>
</div>
<script>
function confirmLogout() {
Swal.fire({
title: '确定要登出吗?',
icon: 'question',
showCancelButton: true,
confirmButtonText: '确定',
cancelButtonText: '取消'
}).then((result) => {
if (result.isConfirmed) {
window.location.href = 'logout.php';
}
});
}
</script>
<!DOCTYPE html>\n<html lang="zh-CN">\n<head>\n <meta charset="UTF-8">\n <meta name="viewport" content="width=device-width, initial-scale=1.0">\n <title>添加App - <?php echo APP_STORE_NAME; ?></title>\n <!-- Bootstrap CSS -->\n <link href="../css/bootstrap.min.css" rel="stylesheet">\n <!-- 自定义CSS -->\n <link rel="stylesheet" href="../styles.css">\n <script src="/js/sweetalert.js"></script>\n <script>\n function confirmLogout() {\n Swal.fire({\n title: '确定要登出吗?',\n icon: 'question',\n showCancelButton: true,\n confirmButtonText: '确定',\n cancelButtonText: '取消'\n }).then((result) => {\n if (result.isConfirmed) {\n window.location.href = 'logout.php';\n }\n });\n }\n </script>\n <!-- Fluent Design 模糊效果 -->\n <style>\n .blur-bg {
backdrop-filter: blur(10px);
background-color: rgba(255, 255, 255, 0.5);
}
.page-transition {
animation: fadeIn 0.5s ease-in-out;
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
</style>
</head>
<body class="page-transition">
<?php if (isset($error)): ?>
<script>
document.addEventListener('DOMContentLoaded', function() {
document.body.classList.add('page-transition');
});
</script>
<div style='color: red; padding: 10px; background-color: #ffeeee; border-radius: 5px; margin-bottom: 20px;'>
<?php echo htmlspecialchars($error); ?>
</div>
<?php endif; ?>
<!-- 导航栏 -->
<nav class="navbar navbar-expand-lg navbar-light blur-bg">
<div class="container">
<a href="../index.php"><img src="/favicon.jpeg" alt="Logo" style="height: 30px; margin-right: 10px; border-radius: var(--border-radius);"></a>
<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 active" aria-current="page" href="addapp.php">添加App</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" onclick="confirmLogout()">退出登录</a>\n </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</h2>
<form method="post" enctype="multipart/form-data">
<div class="form-floating mb-3">
<input type="text" class="form-control" id="name" name="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></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+">3+</option>
<option value="7+">7+</option>
<option value="12+">12+</option>
<option value="17+">17+</option>
</select>
</div>
<div class="form-floating mb-3" id="ageRatingDescriptionGroup" style="display: none;">
<textarea class="form-control" id="age_rating_description" name="age_rating_description" rows="3" placeholder="请说明为何需要此年龄分级"></textarea>
<label for="age_rating_description">年龄分级说明</label>
<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[]">
<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[]">
<label class="form-check-label" for="ios">iOS</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="windows" id="windows" name="platforms[]">
<label class="form-check-label" for="windows">Windows</label>
</div>
<div id="windows_suboptions" class="ms-4 mt-2" style="display: none;">
<div class="form-check">
<input class="form-check-input" type="radio" name="windows_version" id="windows_xp" value="windows_xp">
<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">
<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[]">
<label class="form-check-label" for="macos">macOS</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="linux" id="linux" name="platforms[]">
<label class="form-check-label" for="linux">Linux</label>
</div>
<div id="linux_suboptions" class="ms-4 mt-2" style="display: none;">
<div class="form-check">
<input class="form-check-input" type="radio" name="linux_distribution" id="linux_ubuntu" value="linux_ubuntu">
<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">
<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">
<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" required>
<label for="version" class="form-label"><i class="fas fa-code-branch me-2"></i>版本号</label>
</div>
<div class="form-floating mb-3">
<textarea class="form-control" id="changelog" name="changelog" rows="3" required></textarea>
<label for="changelog" class="form-label"><i class="fas fa-history me-2"></i>更新日志</label>
</div>
<div class="form-floating mb-3">
<input class="form-control" type="file" id="app_file" name="app_file" required>
<label for="app_file" class="form-label"><i class="fas fa-file-archive me-2"></i>App文件</label>
</div>
<div class="form-floating mb-3">
<input class="form-control" type="file" id="images" name="images[]" multiple>
<label for="images" class="form-label"><i class="fas fa-upload me-2"></i>预览图片 (可多选)</label>
</div>
<button type="submit" name="add_app" class="btn btn-primary"><i class="fas fa-plus-circle me-2"></i>添加App</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>