Skip to content

Commit ed47ea5

Browse files
committed
refactor: update
1 parent ab2782c commit ed47ea5

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed

client/pages/install/index.tsx

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import React, { useState } from 'react';
2+
import { Form, Input, Button, Checkbox } from 'antd';
3+
import { UserOutlined, LockOutlined, MailOutlined } from '@ant-design/icons';
4+
import { useRouter } from 'next/router';
5+
6+
const InstallPage = () => {
7+
const [form] = Form.useForm();
8+
const router = useRouter();
9+
const [loading, setLoading] = useState(false);
10+
const [error, setError] = useState(null);
11+
12+
const onFinish = async (values) => {
13+
setLoading(true);
14+
setError(null);
15+
16+
try {
17+
// 发送POST请求到你的后端API进行数据库配置验证和初始化
18+
// const response = await axios.post('/api/install', values);
19+
20+
// if (response.data.success) {
21+
// // 安装成功,重定向到登录页面或其他页面
22+
// router.push('/login');
23+
// } else {
24+
// // 处理错误
25+
// setError(response.data.message);
26+
// }
27+
} catch (err) {
28+
setError('安装过程中发生错误,请重试。');
29+
} finally {
30+
setLoading(false);
31+
}
32+
};
33+
34+
return (
35+
<div className="install-container">
36+
<h1>安装向导</h1>
37+
{error && <div className="error-message">{error}</div>}
38+
<Form form={form} onFinish={onFinish} layout="vertical">
39+
<Form.Item
40+
name="site_title"
41+
label="站点标题"
42+
rules={[{ required: true, message: '请输入站点标题!' }]}
43+
>
44+
<Input prefix={<UserOutlined />} placeholder="请输入站点标题" />
45+
</Form.Item>
46+
<Form.Item
47+
name="username"
48+
label="用户名"
49+
rules={[{ required: true, message: '请输入用户名!' }]}
50+
>
51+
<Input prefix={<UserOutlined />} placeholder="请输入用户名" />
52+
</Form.Item>
53+
<Form.Item
54+
name="password"
55+
label="密码"
56+
rules={[{ required: true, message: '请输入密码!' }]}
57+
hasFeedback
58+
>
59+
<Input.Password prefix={<LockOutlined />} placeholder="请输入密码" />
60+
</Form.Item>
61+
<Form.Item
62+
name="email"
63+
label="电子邮箱"
64+
rules={[
65+
{ type: 'email', message: '请输入有效的电子邮箱!' },
66+
{ required: true, message: '请输入电子邮箱!' },
67+
]}
68+
>
69+
<Input prefix={<MailOutlined />} placeholder="请输入电子邮箱" />
70+
</Form.Item>
71+
<Form.Item>
72+
<Form.Item name="agree" valuePropName="checked" noStyle>
73+
<Checkbox>我同意以上条款和条件</Checkbox>
74+
</Form.Item>
75+
<div className="form-item-error" style={{ color: 'red' }}>
76+
{form.getFieldError('agree')?.message}
77+
</div>
78+
</Form.Item>
79+
<Form.Item>
80+
<Button type="primary" htmlType="submit" loading={loading}>
81+
安装
82+
</Button>
83+
</Form.Item>
84+
</Form>
85+
</div>
86+
);
87+
};
88+
89+
export default InstallPage;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Controller, Post, Body, Get, Res, UseGuards } from '@nestjs/common';
2+
import { InstallService } from './install.service';
3+
import { Response } from 'express';
4+
import { ApiBearerAuth, ApiOkResponse, ApiTags } from '@nestjs/swagger';
5+
6+
@ApiTags('install')
7+
@Controller('install')
8+
export class InstallController {
9+
constructor(private readonly installService: InstallService) {}
10+
11+
@Post('check')
12+
async checkEnvFileExists(): Promise<string> {
13+
return this.installService.checkEnvFileExists() ? 'Environment file exists.' : 'Please configure your database.';
14+
}
15+
16+
@Post('configure')
17+
async configureDatabase(@Body() body: { username: string, password: string, database: string }): Promise<string> {
18+
return this.installService.configureDatabase(body).then(() => 'Database configured successfully.');
19+
}
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Module } from '@nestjs/common';
2+
import { TypeOrmModule } from '@nestjs/typeorm';
3+
4+
import { AuthModule } from '../auth/auth.module';
5+
import { SettingModule } from '../setting/setting.module';
6+
import { InstallController } from './install.controller';
7+
import { InstallService } from './install.service';
8+
9+
@Module({
10+
imports: [TypeOrmModule.forFeature([File]), AuthModule, SettingModule],
11+
controllers: [InstallController],
12+
providers: [InstallService],
13+
})
14+
export class InstallModule {}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Injectable } from '@nestjs/common';
2+
import * as fs from 'fs';
3+
import * as path from 'path';
4+
import { createConnection } from 'typeorm';
5+
import { ConfigService } from '@nestjs/config';
6+
7+
@Injectable()
8+
export class InstallService {
9+
private envConfigPath = path.resolve(__dirname, '..', '..', '.env');
10+
private configService: ConfigService;
11+
12+
constructor(configService: ConfigService) {
13+
this.configService = configService;
14+
}
15+
16+
checkEnvFileExists(): boolean {
17+
return fs.existsSync(this.envConfigPath);
18+
}
19+
20+
async configureDatabase(config: { username: string, password: string, database: string }): Promise<void> {
21+
const connection = await createConnection({
22+
type: 'mysql',
23+
host: 'localhost', // 这里可以改为从body中获取,但为了简化我们固定为localhost
24+
port: 3306, // 同样,可以改为动态获取
25+
username: config.username,
26+
password: config.password,
27+
database: config.database,
28+
synchronize: true, // 注意:在生产中不要启用synchronize
29+
logging: false, // 控制台不输出orm日志
30+
entities: [__dirname + '/../**/*.entity{.ts,.js}'], // 扫描实体位置
31+
});
32+
33+
try {
34+
await connection.connect();
35+
console.log('Connected to the database');
36+
37+
// 写入.env文件
38+
const envConfigLines = [
39+
`DB_HOST=localhost`, // 也可以动态写入
40+
`DB_PORT=3306`, // 同上
41+
`DB_USERNAME=${config.username}`,
42+
`DB_PASSWORD=${config.password}`,
43+
`DB_NAME=${config.database}`,
44+
];
45+
46+
fs.writeFileSync(this.envConfigPath, envConfigLines.join('\n'), { encoding: 'utf8' });
47+
} catch (error) {
48+
throw new Error('Failed to connect to the database: ' + error.message);
49+
} finally {
50+
await connection.close();
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)