v2.2更新

This commit is contained in:
2026-05-28 15:38:32 +08:00
parent f84c9d3efb
commit ca53fdc349
38 changed files with 688 additions and 686 deletions

View File

@@ -1,7 +1,6 @@
<?php
/**
* 检查数据库版本是否需要升级
* 返回 JSON: {needs_upgrade: bool, current: string, target: string}
* 检查数据库版本是否需要升级(代理至后端 API
*/
require_once __DIR__ . '/../config.php';
@@ -20,84 +19,43 @@ if ($role !== '班主任') {
exit();
}
// 读取后端 .env 获取数据库配置
$envPath = __DIR__ . '/../../backend/.env';
if (!file_exists($envPath)) {
echo json_encode(['error' => '数据库配置文件不存在']);
// 从 session 获取 JWT token
$token = $_SESSION['jwt_token'] ?? '';
if (empty($token)) {
echo json_encode(['error' => '会话已过期,请重新登录']);
exit();
}
$lines = file($envPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$dbConfig = [];
foreach ($lines as $line) {
$line = trim($line);
if ($line === '' || strpos($line, '#') === 0) {
continue;
}
if (strpos($line, '=') !== false) {
list($key, $value) = explode('=', $line, 2);
$dbConfig[trim($key)] = trim($value);
}
// 调用后端 API
$apiUrl = API_BASE_URL . '/api/upgrade/check';
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $apiUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => API_TIMEOUT,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $token,
'Content-Type: application/json'
],
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 0
]);
$apiResponse = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200 || empty($apiResponse)) {
echo json_encode(['error' => '无法连接升级服务']);
exit();
}
$required = ['DB_HOST', 'DB_PORT', 'DB_USER', 'DB_PASSWORD', 'DB_NAME'];
foreach ($required as $key) {
if (!isset($dbConfig[$key]) || $dbConfig[$key] === '') {
echo json_encode(['error' => "缺少数据库配置: {$key}"]);
exit();
}
$result = json_decode($apiResponse, true);
if (!$result || !isset($result['success']) || !$result['success']) {
echo json_encode(['error' => $result['message'] ?? '升级检查失败']);
exit();
}
try {
$dsn = "mysql:host={$dbConfig['DB_HOST']};port={$dbConfig['DB_PORT']};dbname={$dbConfig['DB_NAME']};charset=utf8mb4";
$pdo = new PDO($dsn, $dbConfig['DB_USER'], $dbConfig['DB_PASSWORD'], [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
// 检测当前版本
$currentVersion = '0.0.0';
try {
$stmt = $pdo->query("SELECT setting_value FROM system_settings WHERE setting_key = 'db_version'");
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row) {
$currentVersion = $row['setting_value'];
}
} catch (PDOException $e) {
// 表不存在,使用默认值
}
// 读取目标版本
$versionFile = __DIR__ . '/../../VERSION';
if (!file_exists($versionFile)) {
echo json_encode(['error' => 'VERSION 文件不存在']);
exit();
}
$targetVersion = trim(file_get_contents($versionFile));
$needsUpgrade = version_compare($targetVersion, $currentVersion, '>');
$allVersions = [
'1.7' => 'v1.7.sql',
'1.8' => 'v1.8.sql',
'2.0' => 'v2.0.sql',
'2.0.1' => 'v2.0.1.sql',
'2.1' => 'v2.1.sql',
];
$steps = [];
foreach ($allVersions as $version => $file) {
if (version_compare($version, $currentVersion, '>') &&
version_compare($version, $targetVersion, '<=')) {
$steps[] = ['version' => $version, 'file' => $file];
}
}
usort($steps, function($a, $b) { return version_compare($a['version'], $b['version']); });
echo json_encode([
'needs_upgrade' => $needsUpgrade,
'current' => $currentVersion,
'target' => $targetVersion,
'steps' => $steps
]);
} catch (PDOException $e) {
echo json_encode(['error' => '数据库连接失败: ' . $e->getMessage()]);
}
// 转发后端返回的升级数据
echo json_encode($result['data']);