feat: 安装向导页支持滚动并新增 MySQL/Redis 连接测试

Install 页作为独立满屏路由自身承载纵向滚动,修复内容超出视口无法滚动;
MySQL/Redis 卡片新增测试连接按钮,后端新增 /api/install/test-connection(仅安装模式可用,
不建库不落盘;MySQL 库不存在也视为可连接因安装时会自动建库)。
This commit is contained in:
Nixevol
2026-07-09 15:04:45 +08:00
parent 0780fc2d0f
commit 8db618a807
6 changed files with 182 additions and 5 deletions
+53 -1
View File
@@ -7,7 +7,7 @@ import * as bcrypt from 'bcryptjs';
import { PrismaClient, RoleType, UserStatus } from '@prisma/client';
import Redis from 'ioredis';
import { envFilePath, isInstallMode } from '../config/runtime-config';
import { InstallRequestDto } from './dto/install.dto';
import { InstallRequestDto, TestConnectionDto } from './dto/install.dto';
const ENCRYPTED_PREFIX = 'enc:v1:';
@@ -48,6 +48,58 @@ export class InstallService {
return { installed: true, restartRequired: true };
}
// 安装页「测试连接」:只连通、不建库、不落盘。target=mysql|redis
async testConnection(dto: TestConnectionDto): Promise<{ success: true; message: string }> {
// 仅安装模式可用,避免安装完成后被当作公开的内网端口探测工具
if (!isInstallMode()) {
throw new Error('系统已安装,连接测试不可用');
}
if (dto.target === 'redis') {
const redis = new Redis({
host: dto.host,
port: dto.port,
password: dto.password || undefined,
maxRetriesPerRequest: 1,
connectTimeout: 5000,
lazyConnect: true,
});
try {
await redis.connect();
await redis.ping();
} finally {
redis.disconnect();
}
return { success: true, message: 'Redis 连接成功' };
}
// MySQL:优先连业务库,库不存在(P1049)也视为「可连接」,因为安装时会自动建库
const user = encodeURIComponent(dto.username ?? '');
const password = encodeURIComponent(dto.password ?? '');
const base = `mysql://${user}:${password}@${dto.host}:${dto.port}`;
const dbName = dto.database || '';
const tryConnect = async (url: string) => {
const prisma = new PrismaClient({ datasources: { db: { url } } });
try {
await prisma.$connect();
await prisma.$queryRaw`SELECT 1`;
} finally {
await prisma.$disconnect().catch(() => undefined);
}
};
try {
await tryConnect(`${base}/${encodeURIComponent(dbName)}`);
return { success: true, message: 'MySQL 连接成功,业务库已存在' };
} catch (e) {
const code = (e as { code?: string }).code;
const msg = (e as Error).message || '';
if (code === 'P1003' || /Unknown database|P1049|does not exist/i.test(msg)) {
// 库不存在,但服务器可连接:再连 information_schema 确认凭据可用
await tryConnect(`${base}/information_schema`);
return { success: true, message: 'MySQL 连接成功,业务库不存在(安装时将自动创建)' };
}
throw new Error(`MySQL 连接失败:${msg}`);
}
}
private buildMysqlUrl(dto: InstallRequestDto): string {
const user = encodeURIComponent(dto.mysql.username);
const password = encodeURIComponent(dto.mysql.password);