This commit is contained in:
2026-06-24 18:34:15 +08:00
parent 92f53a8bbb
commit 3ca5129087
+25 -33
View File
@@ -1,54 +1,46 @@
<?php <?php
require_once 'includes/db.php'; require_once 'includes/db.php';
header('Content-Type: application/json; charset=utf-8');
$id = $_GET['id'] ?? null; $id = $_GET['id'] ?? null;
try { try {
$app = null;
if ($id) { if ($id) {
// 按 ID 下载指定版本 // 按 ID 下载指定版本
$stmt = $db->prepare("SELECT * FROM apps WHERE id = ?"); $stmt = $db->prepare("SELECT * FROM apps WHERE id = ?");
$stmt->execute([$id]); $stmt->execute([$id]);
$app = $stmt->fetch(PDO::FETCH_ASSOC); $app = $stmt->fetch(PDO::FETCH_ASSOC);
if ($app && file_exists('data/apps/' . $app['filename'])) {
echo json_encode([
'success' => true,
'file' => 'data/apps/' . rawurlencode($app['filename']),
'filename' => $app['original_name'],
'version' => $app['version'],
'date' => date('Y-m-d H:i:s', strtotime($app['upload_time']))
]);
} else {
echo json_encode([
'success' => false,
'message' => 'APK 文件不存在'
]);
}
} else { } else {
// 兼容旧版:返回最新版本 // 兼容旧版:返回最新版本
$stmt = $db->query("SELECT * FROM apps ORDER BY upload_time DESC LIMIT 1"); $stmt = $db->query("SELECT * FROM apps ORDER BY upload_time DESC LIMIT 1");
$app = $stmt->fetch(PDO::FETCH_ASSOC); $app = $stmt->fetch(PDO::FETCH_ASSOC);
}
if ($app && file_exists('data/apps/' . $app['filename'])) { if ($app && file_exists('data/apps/' . $app['filename'])) {
echo json_encode([ $file_path = 'data/apps/' . $app['filename'];
'success' => true, $file_size = filesize($file_path);
'file' => 'data/apps/' . rawurlencode($app['filename']),
'filename' => $app['original_name'], // 设置下载响应头
'version' => $app['version'], header('Content-Type: application/vnd.android.package-archive');
'date' => date('Y-m-d H:i:s', strtotime($app['upload_time'])) header('Content-Disposition: attachment; filename="' . $app['original_name'] . '"');
]); header('Content-Length: ' . $file_size);
} else { header('Cache-Control: must-revalidate');
echo json_encode([ header('Pragma: public');
'success' => false,
'message' => '未找到 APK 文件' // 清空输出缓冲
]); if (ob_get_level()) {
ob_end_clean();
} }
// 输出文件
readfile($file_path);
exit;
} else {
header('Content-Type: text/html; charset=utf-8');
die('错误:APK 文件不存在');
} }
} catch (Exception $e) { } catch (Exception $e) {
echo json_encode([ header('Content-Type: text/html; charset=utf-8');
'success' => false, die('错误:' . htmlspecialchars($e->getMessage()));
'message' => '查询失败: ' . $e->getMessage()
]);
} }