prepare($getAppSql); $stmt->bind_param("i", $appId); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows === 0) { header('Location: index.php?error=App不存在'); exit; } $app = $result->fetch_assoc(); // 获取所有版本 $versions = []; $getVersionsSql = "SELECT * FROM app_versions WHERE app_id = ? ORDER BY created_at DESC"; $stmt = $conn->prepare($getVersionsSql); $stmt->bind_param("i", $appId); $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { $versions[] = $row; } $success = ''; $error = ''; // 处理添加版本 if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_version'])) { $version = $_POST['version']; $changelog = $_POST['changelog']; if (empty($version)) { $error = '版本号不能为空'; } elseif (empty($_FILES['app_file']['name'])) { $error = '请上传App文件'; } else { $uploadDir = '../files/'; $fileName = basename($_FILES['app_file']['name']); $targetPath = $uploadDir . $fileName; if (move_uploaded_file($_FILES['app_file']['tmp_name'], $targetPath)) { $insertVersionSql = "INSERT INTO app_versions (app_id, version, changelog, file_path, created_at) VALUES (?, ?, ?, ?, NOW())"; $stmt = $conn->prepare($insertVersionSql); $stmt->bind_param("isss", $appId, $version, $changelog, $targetPath); if ($stmt->execute() === TRUE) { header('Location: manage_versions.php?app_id=' . $appId . '&success=版本添加成功'); exit; } else { $error = '版本添加失败: ' . $conn->error; unlink($targetPath); // 删除已上传的文件 } } else { $error = '文件上传失败'; } } } // 处理版本删除请求 if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_version'])) { // 设置内容类型为纯文本 header('Content-Type: text/plain'); $versionId = $_POST['version_id']; $appId = $_POST['app_id']; // 初始化消息变量 $message = ''; // 开始事务 $conn->begin_transaction(); try { // 1. 获取文件路径 $filePath = ''; $getFilePathSql = "SELECT file_path FROM app_versions WHERE id = ? AND app_id = ?"; $getFileStmt = $conn->prepare($getFilePathSql); if (!$getFileStmt) { throw new Exception("获取文件路径查询准备失败: " . $conn->error); } $getFileStmt->bind_param("ii", $versionId, $appId); $getFileStmt->execute(); $fileResult = $getFileStmt->get_result(); if ($fileResult->num_rows > 0) { $fileRow = $fileResult->fetch_assoc(); $filePath = $fileRow['file_path']; } // 2. 从数据库删除版本记录 $deleteVersionSql = "DELETE FROM app_versions WHERE id = ? AND app_id = ?"; $deleteVersionStmt = $conn->prepare($deleteVersionSql); if (!$deleteVersionStmt) { throw new Exception("版本删除查询准备失败: " . $conn->error); } $deleteVersionStmt->bind_param("ii", $versionId, $appId); if (!$deleteVersionStmt->execute()) { throw new Exception("版本删除执行失败: " . $conn->error); } // 检查是否有记录被删除 if ($deleteVersionStmt->affected_rows === 0) { throw new Exception("未找到要删除的版本记录"); } // 3. 如果数据库删除成功,尝试删除文件(即使文件删除失败也不回滚数据库操作) if (!empty($filePath) && file_exists($filePath)) { if (!unlink($filePath)) { // 文件删除失败不影响数据库操作,继续处理 } } // 提交事务 $conn->commit(); $message = '版本删除成功'; } catch (Exception $e) { // 回滚事务 $conn->rollback(); $message = '版本删除失败: ' . $e->getMessage(); } // 只输出消息,不包含任何HTML echo $message; // 确保脚本终止,不执行后续代码 exit; } // 处理编辑版本 if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_version'])) { $versionId = $_POST['version_id']; $version = $_POST['version']; $changelog = $_POST['changelog']; if (empty($version)) { $error = '版本号不能为空'; } else { // 检查是否上传了新文件 $fileUpdate = ''; $params = ['ss', $version, $changelog, $versionId, $appId]; if (!empty($_FILES['new_app_file']['name'])) { $uploadDir = '../files/'; $fileName = basename($_FILES['new_app_file']['name']); $targetPath = $uploadDir . $fileName; if (move_uploaded_file($_FILES['new_app_file']['tmp_name'], $targetPath)) { // 获取旧文件路径 $getOldFileSql = "SELECT file_path FROM app_versions WHERE id = ? AND app_id = ?"; $stmt = $conn->prepare($getOldFileSql); $stmt->bind_param("ii", $versionId, $appId); $stmt->execute(); $result = $stmt->get_result(); $oldVersion = $result->fetch_assoc(); // 删除旧文件 if (file_exists($oldVersion['file_path'])) { unlink($oldVersion['file_path']); } $fileUpdate = ", file_path = ?"; $params[0] = 'sss'; $params[] = $targetPath; } else { $error = '文件上传失败'; } } if (empty($error)) { $updateVersionSql = "UPDATE app_versions SET version = ?, changelog = ?" . $fileUpdate . " WHERE id = ? AND app_id = ?"; $stmt = $conn->prepare($updateVersionSql); // 动态绑定参数 $stmt->bind_param(...$params); if ($stmt->execute() === TRUE) { header('Location: manage_versions.php?app_id=' . $appId . '&success=版本更新成功'); exit; } else { $error = '版本更新失败: ' . $conn->error; } } } } // 获取URL参数中的成功/错误消息 if (isset($_GET['success'])) { $success = $_GET['success']; } elseif (isset($_GET['error'])) { $error = $_GET['error']; } ?> 管理版本 - <?php echo htmlspecialchars($app['name']); ?>

管理版本:

管理该应用的所有版本

暂无版本记录
版本
发布日期: