true, 'data' => ['installed' => $installed]]); } public static function testDb(): void { $input = json_decode(file_get_contents('php://input'), true); $host = $input['host'] ?? ''; $port = $input['port'] ?? 3306; $user = $input['user'] ?? ''; $password = $input['password'] ?? ''; $database = $input['database'] ?? ''; if (!$host || !$user || !$database) { http_response_code(400); echo json_encode(['success' => false, 'message' => '缺少必要参数']); return; } try { $dsn = "mysql:host={$host};port={$port};dbname={$database};charset=utf8mb4"; $pdo = new \PDO($dsn, $user, $password, [ \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION ]); echo json_encode(['success' => true, 'message' => '数据库连接成功']); } catch (\PDOException $e) { http_response_code(500); echo json_encode(['success' => false, 'message' => '数据库连接失败: ' . $e->getMessage()]); } } public static function setup(): void { if (Installer::isInstalled()) { http_response_code(400); echo json_encode(['success' => false, 'message' => '系统已安装,不能重复安装']); return; } $input = json_decode(file_get_contents('php://input'), true); $username = $input['username'] ?? ''; $password = $input['password'] ?? ''; $dbConfig = $input['dbConfig'] ?? []; $providers = $input['providers'] ?? []; if (!$username || !$password) { http_response_code(400); echo json_encode(['success' => false, 'message' => '用户名和密码不能为空']); return; } if (strlen($password) < 6) { http_response_code(400); echo json_encode(['success' => false, 'message' => '密码长度不能少于6位']); return; } if (!$dbConfig || !$dbConfig['host'] || !$dbConfig['user'] || !$dbConfig['database']) { http_response_code(400); echo json_encode(['success' => false, 'message' => '请填写数据库配置']); return; } if (!is_array($providers) || count($providers) === 0) { http_response_code(400); echo json_encode(['success' => false, 'message' => '请至少配置一个AI服务提供商']); return; } try { $dsn = "mysql:host={$dbConfig['host']};port=" . ($dbConfig['port'] ?? 3306) . ";dbname={$dbConfig['database']};charset=utf8mb4"; $pdo = new \PDO($dsn, $dbConfig['user'], $dbConfig['password'], [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]); } catch (\PDOException $e) { http_response_code(400); echo json_encode(['success' => false, 'message' => '数据库连接失败: ' . $e->getMessage()]); return; } $configDir = __DIR__ . '/../../config'; file_put_contents($configDir . '/db-config.json', json_encode($dbConfig, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); try { Installer::runMigration(); } catch (\Exception $e) { http_response_code(500); echo json_encode(['success' => false, 'message' => '数据库迁移失败: ' . $e->getMessage()]); return; } Installer::seedDefaults(); $admin = User::create(['username' => $username, 'password' => $password, 'role' => 'admin']); $jwtSecret = bin2hex(random_bytes(32)); AppConfig::set('jwtSecret', $jwtSecret); AppConfig::set('jwtExpiry', 86400); AppConfig::set('corsOrigin', ''); Config::setByKey('providers', json_encode($providers)); echo json_encode([ 'success' => true, 'message' => '安装成功', 'data' => ['user' => $admin] ]); } }