| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- <?php
- namespace App\Console\Commands;
- use App\Models\User;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Hash;
- class CreateAdminUserCommand extends Command
- {
- protected $signature = 'user:create-admin
- {username=17689974321 : 登录手机号(与后台「手机号」一致,对应 users.username)}
- {password=123qwe#0 : 明文密码;含 # 时请用单引号包裹整个参数}';
- protected $description = '创建或更新管理员账号(role=admin,is_active=1),用于登录 /admin';
- public function handle(): int
- {
- $username = (string) $this->argument('username');
- $password = (string) $this->argument('password');
- if ($username === '') {
- $this->error('username 不能为空');
- return self::FAILURE;
- }
- $user = User::where('username', $username)->first();
- if (! $user) {
- $user = new User;
- $user->username = $username;
- $user->full_name = '系统管理员';
- $user->email = null;
- }
- $user->password_hash = Hash::make($password);
- $user->role = 'admin';
- $user->is_active = true;
- if ($user->full_name === '' || $user->full_name === null) {
- $user->full_name = '系统管理员';
- }
- $user->save();
- $this->info("已就绪:username={$user->username} role=admin 登录 /admin 使用手机号+密码");
- return self::SUCCESS;
- }
- }
|