初始化本地仓库
This commit is contained in:
@@ -0,0 +1,306 @@
|
||||
<?php
|
||||
|
||||
namespace app\install\controller;
|
||||
|
||||
use app\service\core\install\InstallService;
|
||||
use core\tool\Json;
|
||||
use madong\swagger\annotation\response\SimpleResponse;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
use Throwable;
|
||||
use OpenApi\Attributes as OA;
|
||||
|
||||
class Index
|
||||
{
|
||||
protected InstallService $installService;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->installService = new InstallService();
|
||||
}
|
||||
|
||||
#[OA\Get(
|
||||
path: '/install/check',
|
||||
summary: '检查安装状态',
|
||||
tags: ['安装'])
|
||||
]
|
||||
#[OA\Response(
|
||||
response: 200,
|
||||
description: '安装状态检查结果',
|
||||
content: new OA\JsonContent(
|
||||
properties: [
|
||||
new OA\Property(
|
||||
property: 'installed',
|
||||
description: '系统是否已安装',
|
||||
type: 'boolean'
|
||||
),
|
||||
]
|
||||
)
|
||||
)]
|
||||
#[SimpleResponse(schema: [], example: [])]
|
||||
public function check(Request $request): Response
|
||||
{
|
||||
try {
|
||||
if ($this->installService->checkInstalled()) {
|
||||
// 获取安装时间
|
||||
$lockFile = base_path() . '/install.lock';
|
||||
$installTime = '';
|
||||
if (file_exists($lockFile)) {
|
||||
$installTime = trim(file_get_contents($lockFile));
|
||||
}
|
||||
return Json::fail('系统已安装', ['installed' => true, 'install_time' => $installTime]);
|
||||
}
|
||||
|
||||
return Json::success('系统未安装', ['installed' => false]);
|
||||
} catch (Throwable $e) {
|
||||
return Json::fail('检查失败: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取许可协议
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
#[OA\Get(
|
||||
path: '/install/agreement',
|
||||
summary: '获取许可协议',
|
||||
tags: ['安装'])
|
||||
]
|
||||
#[OA\Response(
|
||||
response: 200,
|
||||
description: '许可协议内容',
|
||||
content: new OA\JsonContent(
|
||||
properties: [
|
||||
new OA\Property(
|
||||
property: 'agreement',
|
||||
description: '许可协议内容',
|
||||
type: 'string'
|
||||
),
|
||||
]
|
||||
)
|
||||
)]
|
||||
#[SimpleResponse(schema: [], example: [])]
|
||||
public function agreement(Request $request): Response
|
||||
{
|
||||
try {
|
||||
$agreement = $this->installService->getAgreementData();
|
||||
|
||||
return Json::success('许可协议获取成功',
|
||||
$agreement
|
||||
);
|
||||
} catch (Throwable $e) {
|
||||
return Json::fail('获取许可协议失败: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查系统环境
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
#[OA\Get(
|
||||
path: '/install/environment',
|
||||
summary: '检查系统环境',
|
||||
tags: ['安装'])
|
||||
]
|
||||
#[OA\Response(
|
||||
response: 200,
|
||||
description: '环境检查结果',
|
||||
content: new OA\JsonContent(
|
||||
properties: [
|
||||
new OA\Property(
|
||||
property: 'environment',
|
||||
description: '环境检查结果',
|
||||
type: 'object'
|
||||
),
|
||||
]
|
||||
)
|
||||
)]
|
||||
#[SimpleResponse(schema: [], example: [])]
|
||||
public function environment(Request $request): Response
|
||||
{
|
||||
try {
|
||||
$environment = $this->installService->checkEnvironment();
|
||||
return Json::success('环境检查完成', $environment);
|
||||
} catch (Throwable $e) {
|
||||
return Json::fail('环境检查失败: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
#[OA\get(
|
||||
path: '/install',
|
||||
summary: '执行安装',
|
||||
tags: ['安装'])
|
||||
]
|
||||
#[SimpleResponse(schema: [], example: [])]
|
||||
public function install(Request $request): \Generator
|
||||
{
|
||||
|
||||
$connection = $request->connection;
|
||||
$data = $request->all();
|
||||
$connection->send(new Response(200, [
|
||||
'Content-Type' => 'text/event-stream',
|
||||
'Cache-Control' => 'no-cache',
|
||||
'Connection' => 'keep-alive',
|
||||
'Access-Control-Allow-Origin' => '*', // 或者允许所有来源(不推荐生产环境使用)
|
||||
'Access-Control-Allow-Credentials' => 'true', // 如果需要凭证
|
||||
'Access-Control-Expose-Headers' => 'Content-Type', // 暴露必要的头
|
||||
], "\r\n"));
|
||||
$generator = $this->installService->install($data);
|
||||
foreach ($generator as $chunk) {
|
||||
$connection->send($chunk);
|
||||
}
|
||||
}
|
||||
|
||||
#[OA\Post(
|
||||
path: '/install/test-database',
|
||||
summary: '测试数据库连接',
|
||||
tags: ['安装'])
|
||||
]
|
||||
#[OA\RequestBody(
|
||||
required: true,
|
||||
content: new OA\JsonContent(
|
||||
properties: [
|
||||
new OA\Property(
|
||||
property: 'database',
|
||||
description: '数据库配置',
|
||||
type: 'object'
|
||||
),
|
||||
]
|
||||
)
|
||||
)]
|
||||
#[OA\Response(
|
||||
response: 200,
|
||||
description: '数据库连接测试结果',
|
||||
content: new OA\JsonContent(
|
||||
properties: [
|
||||
new OA\Property(
|
||||
property: 'result',
|
||||
description: '数据库连接测试结果',
|
||||
type: 'object'
|
||||
),
|
||||
]
|
||||
)
|
||||
)]
|
||||
#[SimpleResponse(schema: [], example: [])]
|
||||
public function testDatabase(Request $request): Response
|
||||
{
|
||||
try {
|
||||
$allParams = $request->all();
|
||||
|
||||
// 提取数据库配置
|
||||
$databaseConfig = [
|
||||
'host' => $allParams['host'] ?? '',
|
||||
'port' => $allParams['port'] ?? '',
|
||||
'username' => $allParams['username'] ?? '',
|
||||
'password' => $allParams['password'] ?? '',
|
||||
'database' => $allParams['database'] ?? '',
|
||||
'prefix' => $allParams['prefix'] ?? 'ma_',
|
||||
];
|
||||
|
||||
// 提取 Redis 配置
|
||||
$redisConfig = [
|
||||
'host' => $allParams['redisHost'] ?? $allParams['redis_host'] ?? '127.0.0.1',
|
||||
'port' => $allParams['redisPort'] ?? $allParams['redis_port'] ?? '6379',
|
||||
'password' => $allParams['redisPassword'] ?? $allParams['redis_password'] ?? '',
|
||||
];
|
||||
|
||||
// 验证数据库配置
|
||||
$dbErrors = $this->installService->validateDatabaseConfig($databaseConfig);
|
||||
if (!empty($dbErrors)) {
|
||||
return Json::fail(implode('; ', $dbErrors));
|
||||
}
|
||||
|
||||
// 测试数据库连接
|
||||
$dbResult = $this->installService->testDatabaseConnection($databaseConfig);
|
||||
$dbMessage = $dbResult['message'] ?? '数据库连接成功';
|
||||
|
||||
// 测试 Redis 连接
|
||||
$redisMessage = '';
|
||||
try {
|
||||
$redisResult = $this->installService->testRedisConnection($redisConfig);
|
||||
$redisMessage = $redisResult['message'] ?? 'Redis 连接成功';
|
||||
} catch (Throwable $e) {
|
||||
return Json::fail('Redis 连接失败: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
// 全部测试成功后,保存配置到 .env
|
||||
$this->installService->saveDatabaseConfig($databaseConfig, $redisConfig);
|
||||
|
||||
return Json::success('数据库和 Redis 连接测试成功', [
|
||||
'database' => $dbMessage,
|
||||
'redis' => $redisMessage,
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
return Json::fail('数据库连接测试失败: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
#[OA\Post(
|
||||
path: '/install/validate-config',
|
||||
summary: '验证安装配置',
|
||||
tags: ['安装'])
|
||||
]
|
||||
#[OA\RequestBody(
|
||||
required: true,
|
||||
content: new OA\JsonContent(
|
||||
properties: [
|
||||
new OA\Property(
|
||||
property: 'database',
|
||||
description: '数据库配置',
|
||||
type: 'object'
|
||||
),
|
||||
new OA\Property(
|
||||
property: 'admin',
|
||||
description: '管理员配置',
|
||||
type: 'object'
|
||||
),
|
||||
]
|
||||
)
|
||||
)]
|
||||
#[OA\Response(
|
||||
response: 200,
|
||||
description: '配置验证结果',
|
||||
content: new OA\JsonContent(
|
||||
properties: [
|
||||
new OA\Property(
|
||||
property: 'result',
|
||||
description: '配置验证结果',
|
||||
type: 'object'
|
||||
),
|
||||
]
|
||||
)
|
||||
)]
|
||||
#[SimpleResponse(schema: [], example: [])]
|
||||
public function validateConfig(Request $request): Response
|
||||
{
|
||||
try {
|
||||
$databaseConfig = $request->post('database', []);
|
||||
$adminConfig = $request->post('admin', []);
|
||||
|
||||
// 验证数据库配置
|
||||
$dbErrors = $this->installService->validateDatabaseConfig($databaseConfig);
|
||||
if (!empty($dbErrors)) {
|
||||
return Json::fail('数据库配置验证失败', ['database' => $dbErrors]);
|
||||
}
|
||||
|
||||
// 验证管理员配置
|
||||
$adminErrors = $this->installService->validateAdminConfig($adminConfig);
|
||||
if (!empty($adminErrors)) {
|
||||
return Json::fail('管理员配置验证失败', ['admin' => $adminErrors]);
|
||||
}
|
||||
|
||||
return Json::success('配置验证通过');
|
||||
} catch (Throwable $e) {
|
||||
return Json::fail('配置验证失败: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user