增加 用户 UUID

修复 邮箱 md5 为 null 的问题
This commit is contained in:
iVampireSP.com 2023-01-03 14:37:15 +08:00
parent 4fed8eb395
commit 796fd6fce2
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
2 changed files with 66 additions and 23 deletions

View File

@ -101,6 +101,7 @@ class User extends Authenticatable
* @var array<int, string>
*/
protected $fillable = [
'uuid',
'name',
'email',
'password',
@ -123,29 +124,6 @@ class User extends Authenticatable
'birthday_at' => 'date',
];
protected static function boot()
{
parent::boot();
static::updating(function ($model) {
// balance 四舍五入
// if ($model->isDirty('balance')) {
// $model->balance = round($model->balance, 2, PHP_ROUND_HALF_DOWN);
// }
if ($model->isDirty('banned_at')) {
if ($model->banned_at) {
$model->tokens()->delete();
$model->hosts()->update(['status' => 'suspended', 'suspended_at' => now()]);
} else {
$model->hosts()->update(['status' => 'stopped']);
}
}
});
}
public function hosts(): HasMany
{
return $this->hasMany(Host::class);
@ -161,4 +139,24 @@ public function scopeBirthday()
return $this->select(['id', 'name', 'birthday_at', 'email_md5', 'created_at'])->whereMonth('birthday_at', now()->month)
->whereDay('birthday_at', now()->day);
}
protected static function boot()
{
parent::boot();
static::creating(function (self $user) {
$user->email_md5 = md5($user->email);
});
static::updating(function ($model) {
if ($model->isDirty('banned_at')) {
if ($model->banned_at) {
$model->tokens()->delete();
$model->hosts()->update(['status' => 'suspended', 'suspended_at' => now()]);
} else {
$model->hosts()->update(['status' => 'stopped']);
}
}
});
}
}

View File

@ -0,0 +1,45 @@
<?php
use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->uuid()->unique()->after('id')->nullable();
});
$count = User::count();
$i = 0;
User::chunk(100, function ($users) use (&$i, $count) {
foreach ($users as $user) {
echo sprintf('Updating %d/%d', ++$i, $count) . PHP_EOL;
$user->email_md5 = md5($user->email);
$user->uuid = Str::uuid();
$user->saveQuietly();
}
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('uuid');
});
}
};