Files
datahub/backend/app/Command/AppInstall.php
T

97 lines
3.1 KiB
PHP
Raw Normal View History

2025-11-10 15:27:00 +08:00
<?php
declare(strict_types=1);
namespace App\Command;
2026-03-23 09:46:22 +08:00
use App\Model\Role; // phpcs:ignore -- used in handle()
2025-11-10 15:27:00 +08:00
use App\Model\User;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;
#[Command]
class AppInstall extends HyperfCommand
{
public function __construct(protected ContainerInterface $container)
{
parent::__construct('app:install');
}
public function configure()
{
parent::configure();
$this->setDescription('Install application and create default admin user');
}
public function handle()
{
$this->line('Starting application installation...', 'info');
// 检查 admin 用户是否已存在
$existingUser = User::query()
2026-03-23 09:46:22 +08:00
->where('username', 'administrator')
2025-11-10 15:27:00 +08:00
->orWhere('email', 'admin@admin.com')
->first();
if ($existingUser) {
2026-03-23 09:46:22 +08:00
$this->line('Admin user already exists.', 'comment');
2025-11-10 15:27:00 +08:00
$this->line('Username: ' . $existingUser->username);
$this->line('Email: ' . $existingUser->email);
2026-03-23 09:46:22 +08:00
// 自动修复:缺失的 role_id
$adminRole = Role::query()->where('name', 'administrator')->first();
if ($adminRole && $existingUser->role_id !== $adminRole->id) {
$existingUser->role_id = $adminRole->id;
$existingUser->save();
$this->line('Fixed: role_id set to administrator.', 'info');
}
// 自动修复:旧拼写错误的用户名
if ($existingUser->username === 'adminstrator') {
$existingUser->username = 'administrator';
$existingUser->save();
$this->line('Fixed: username corrected to "administrator".', 'info');
}
2026-03-23 16:24:17 +08:00
$this->call('route:sync');
$this->call('route-group:seed');
2026-03-23 09:46:22 +08:00
return 0;
2025-11-10 15:27:00 +08:00
}
// 创建默认 admin 用户
try {
2026-03-23 09:46:22 +08:00
$adminRole = Role::query()->where('name', 'administrator')->first();
if (!$adminRole) {
$this->error('Role "administrator" not found. Please run migrations first.');
return 1;
}
2025-11-10 15:27:00 +08:00
$user = User::create([
2026-03-23 09:46:22 +08:00
'username' => 'administrator',
2025-11-10 15:27:00 +08:00
'email' => 'admin@admin.com',
'password' => 'admin',
'status' => 1,
2026-03-23 09:46:22 +08:00
'role_id' => $adminRole->id,
2025-11-10 15:27:00 +08:00
'ext' => [],
]);
$this->line('Default admin user created successfully!', 'info');
2026-03-23 09:46:22 +08:00
$this->line('Username: administrator', 'comment');
2025-11-10 15:27:00 +08:00
$this->line('Email: admin@admin.com', 'comment');
$this->line('Password: admin', 'comment');
$this->line('');
$this->warn('Please change the default password after first login!');
2026-03-23 16:24:17 +08:00
$this->call('route:sync');
$this->call('route-group:seed');
2025-11-10 15:27:00 +08:00
return 0;
2026-03-23 16:24:17 +08:00
} catch (\Throwable $e) {
2025-11-10 15:27:00 +08:00
$this->error('Failed to create admin user: ' . $e->getMessage());
return 1;
}
}
}