From 1134e64415600902a8b27fcf0bb689e462c01588 Mon Sep 17 00:00:00 2001 From: Nick Zeng Date: Mon, 23 Mar 2026 09:46:22 +0800 Subject: [PATCH] fix request --- backend/app/Command/AppInstall.php | 34 +++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/backend/app/Command/AppInstall.php b/backend/app/Command/AppInstall.php index 785e0fa..a9f2230 100644 --- a/backend/app/Command/AppInstall.php +++ b/backend/app/Command/AppInstall.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Command; +use App\Model\Role; // phpcs:ignore -- used in handle() use App\Model\User; use Hyperf\Command\Command as HyperfCommand; use Hyperf\Command\Annotation\Command; @@ -29,29 +30,52 @@ class AppInstall extends HyperfCommand // 检查 admin 用户是否已存在 $existingUser = User::query() - ->where('username', 'adminstrator') + ->where('username', 'administrator') ->orWhere('email', 'admin@admin.com') ->first(); if ($existingUser) { - $this->error('Admin user already exists!'); + $this->line('Admin user already exists.', 'comment'); $this->line('Username: ' . $existingUser->username); $this->line('Email: ' . $existingUser->email); - return 1; + + // 自动修复:缺失的 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'); + } + + return 0; } // 创建默认 admin 用户 try { + $adminRole = Role::query()->where('name', 'administrator')->first(); + if (!$adminRole) { + $this->error('Role "administrator" not found. Please run migrations first.'); + return 1; + } + $user = User::create([ - 'username' => 'adminstrator', + 'username' => 'administrator', 'email' => 'admin@admin.com', 'password' => 'admin', 'status' => 1, + 'role_id' => $adminRole->id, 'ext' => [], ]); $this->line('Default admin user created successfully!', 'info'); - $this->line('Username: adminstrator', 'comment'); + $this->line('Username: administrator', 'comment'); $this->line('Email: admin@admin.com', 'comment'); $this->line('Password: admin', 'comment'); $this->line('');