feat: 重构安装向导页(管理员并入基础、存储测试、必填标记、主题语言切换)
管理员账号/密码/姓名并入基础卡片并移除独立管理员卡片; 移除 MySQL 建库管理员字段,库不存在自动建库、无权限给出清晰错误提示; 新增存储测试(本地目录可写 / OSS-S3 真实读写校验)后端 /api/install/test-storage; CDN 增加留空即不启用说明;必填项加红色 * 标记; 头部加入与后台一致的主题/语言切换按钮;清理页面冗余注释。
This commit is contained in:
@@ -27,17 +27,6 @@ export class InstallMysqlDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
password!: string;
|
||||
|
||||
// 仅当业务账号无建库权限时使用:用于自动 CREATE DATABASE。
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
adminUsername?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
adminPassword?: string;
|
||||
}
|
||||
|
||||
export class InstallRedisDto {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Body, Controller, Get, Post } from '@nestjs/common';
|
||||
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { Public } from '../common/decorators/public.decorator';
|
||||
import { InstallRequestDto, TestConnectionDto } from './dto/install.dto';
|
||||
import { InstallRequestDto, InstallStorageDto, TestConnectionDto } from './dto/install.dto';
|
||||
import { InstallService } from './install.service';
|
||||
|
||||
@ApiTags('Install')
|
||||
@@ -23,6 +23,13 @@ export class InstallController {
|
||||
return this.installService.testConnection(dto);
|
||||
}
|
||||
|
||||
@Public()
|
||||
@Post('test-storage')
|
||||
@ApiOperation({ summary: 'Test storage (local/OSS) read-write' })
|
||||
testStorage(@Body() dto: InstallStorageDto) {
|
||||
return this.installService.testStorage(dto);
|
||||
}
|
||||
|
||||
@Public()
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'Initialize installation' })
|
||||
|
||||
@@ -7,7 +7,10 @@ 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, TestConnectionDto } from './dto/install.dto';
|
||||
import { InstallRequestDto, InstallStorageDto, TestConnectionDto } from './dto/install.dto';
|
||||
import { LocalDriver } from '../storage/local.driver';
|
||||
import { OssDriver } from '../storage/oss.driver';
|
||||
import type { StorageDriver } from '../storage/storage.types';
|
||||
|
||||
const ENCRYPTED_PREFIX = 'enc:v1:';
|
||||
|
||||
@@ -99,6 +102,42 @@ export class InstallService {
|
||||
}
|
||||
}
|
||||
|
||||
// 安装页「测试存储」:本地校验目录可写、OSS/S3 校验凭据可读写。只做健康检查、随后清理。
|
||||
async testStorage(dto: InstallStorageDto): Promise<{ success: true; message: string }> {
|
||||
if (!isInstallMode()) {
|
||||
throw new Error('系统已安装,存储测试不可用');
|
||||
}
|
||||
let driver: StorageDriver;
|
||||
if (dto.driver === 'oss') {
|
||||
if (!dto.ossBucket || !dto.ossAccessKeyId || !dto.ossAccessKeySecret) {
|
||||
throw new Error('OSS 配置不完整:Endpoint / Bucket / AccessKey ID / AccessKey Secret 均为必填');
|
||||
}
|
||||
driver = new OssDriver({
|
||||
endpoint: dto.ossEndpoint || '',
|
||||
region: dto.ossRegion || '',
|
||||
bucket: dto.ossBucket,
|
||||
accessKeyId: dto.ossAccessKeyId,
|
||||
accessKeySecret: dto.ossAccessKeySecret,
|
||||
cdnBaseUrl: dto.cdnBaseUrl || '',
|
||||
});
|
||||
} else {
|
||||
driver = new LocalDriver({
|
||||
localDir: dto.localDir || resolve(process.cwd(), 'storage'),
|
||||
publicPrefix: '/uploads',
|
||||
cdnBaseUrl: dto.cdnBaseUrl || '',
|
||||
});
|
||||
}
|
||||
const key = `.health/install-${Date.now()}.txt`;
|
||||
try {
|
||||
await driver.put(key, Buffer.from('ok'), { contentType: 'text/plain' });
|
||||
await driver.get(key);
|
||||
await driver.remove(key);
|
||||
} catch (e) {
|
||||
throw new Error(`存储测试失败:${(e as Error).message}`);
|
||||
}
|
||||
return { success: true, message: dto.driver === 'oss' ? 'OSS/S3 读写测试通过' : '本地目录读写测试通过' };
|
||||
}
|
||||
|
||||
private buildMysqlUrl(dto: InstallRequestDto): string {
|
||||
const user = encodeURIComponent(dto.mysql.username);
|
||||
const password = encodeURIComponent(dto.mysql.password);
|
||||
@@ -116,39 +155,29 @@ export class InstallService {
|
||||
}
|
||||
}
|
||||
|
||||
// 首次安装自动建库:优先用业务账号,失败再尝试建库管理员账号;库已存在则幂等跳过。
|
||||
// 库不存在时用业务账号自动尝试建库;无建库权限则给出清晰错误提示。
|
||||
private async ensureDatabase(dto: InstallRequestDto): Promise<void> {
|
||||
const db = dto.mysql.database;
|
||||
if (!/^[A-Za-z0-9_]+$/.test(db)) {
|
||||
throw new Error('数据库名仅允许字母、数字和下划线');
|
||||
}
|
||||
const credentials = [{ username: dto.mysql.username, password: dto.mysql.password }];
|
||||
if (dto.mysql.adminUsername) {
|
||||
credentials.push({ username: dto.mysql.adminUsername, password: dto.mysql.adminPassword ?? '' });
|
||||
const user = encodeURIComponent(dto.mysql.username);
|
||||
const password = encodeURIComponent(dto.mysql.password);
|
||||
const serverUrl = `mysql://${user}:${password}@${dto.mysql.host}:${dto.mysql.port}/information_schema`;
|
||||
const prisma = new PrismaClient({ datasources: { db: { url: serverUrl } } });
|
||||
try {
|
||||
await prisma.$connect();
|
||||
await prisma.$executeRawUnsafe(
|
||||
`CREATE DATABASE IF NOT EXISTS \`${db}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`,
|
||||
);
|
||||
} catch {
|
||||
throw new Error(
|
||||
`所需库「${db}」不存在,当前账号无建库权限,请手动建库或提供足够权限的账号。` +
|
||||
`可手动执行:CREATE DATABASE \`${db}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;`,
|
||||
);
|
||||
} finally {
|
||||
await prisma.$disconnect().catch(() => undefined);
|
||||
}
|
||||
let lastError: unknown;
|
||||
for (const cred of credentials) {
|
||||
const user = encodeURIComponent(cred.username);
|
||||
const password = encodeURIComponent(cred.password);
|
||||
const serverUrl = `mysql://${user}:${password}@${dto.mysql.host}:${dto.mysql.port}/information_schema`;
|
||||
const prisma = new PrismaClient({ datasources: { db: { url: serverUrl } } });
|
||||
try {
|
||||
await prisma.$connect();
|
||||
await prisma.$executeRawUnsafe(
|
||||
`CREATE DATABASE IF NOT EXISTS \`${db}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`,
|
||||
);
|
||||
return;
|
||||
} catch (e) {
|
||||
lastError = e;
|
||||
} finally {
|
||||
await prisma.$disconnect().catch(() => undefined);
|
||||
}
|
||||
}
|
||||
throw new Error(
|
||||
`自动创建数据库「${db}」失败:${(lastError as Error)?.message ?? '未知错误'}。` +
|
||||
`请确认账号具有建库权限,或在安装页填写“建库管理员账号”;` +
|
||||
`也可手动执行:CREATE DATABASE \`${db}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;`,
|
||||
);
|
||||
}
|
||||
|
||||
private async assertRedis(dto: InstallRequestDto): Promise<void> {
|
||||
|
||||
Reference in New Issue
Block a user