2022-08-12 07:56:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2022-11-06 11:28:22 +00:00
|
|
|
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
2023-01-14 21:37:25 +00:00
|
|
|
use Carbon\Exceptions\InvalidFormatException;
|
2022-11-06 11:28:22 +00:00
|
|
|
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
|
2022-08-12 07:56:56 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2022-11-26 13:52:30 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2022-11-18 09:16:30 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2022-11-06 11:28:22 +00:00
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
|
|
|
use Laravel\Sanctum\HasApiTokens;
|
2022-08-12 07:56:56 +00:00
|
|
|
|
2022-11-06 11:28:22 +00:00
|
|
|
class User extends Authenticatable
|
2022-08-12 07:56:56 +00:00
|
|
|
{
|
2022-11-06 11:28:22 +00:00
|
|
|
use HasApiTokens, HasFactory, Notifiable, Cachable;
|
2022-08-12 07:56:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
2022-11-06 11:28:22 +00:00
|
|
|
* @var array<int, string>
|
2022-08-12 07:56:56 +00:00
|
|
|
*/
|
|
|
|
protected $fillable = [
|
2023-01-03 06:37:15 +00:00
|
|
|
'uuid',
|
2022-08-12 07:56:56 +00:00
|
|
|
'name',
|
|
|
|
'email',
|
|
|
|
'password',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2022-11-06 11:28:22 +00:00
|
|
|
* The attributes that should be hidden for serialization.
|
2022-08-12 07:56:56 +00:00
|
|
|
*
|
2022-11-06 11:28:22 +00:00
|
|
|
* @var array<int, string>
|
2022-08-12 07:56:56 +00:00
|
|
|
*/
|
|
|
|
protected $hidden = [
|
2022-11-06 11:28:22 +00:00
|
|
|
'password',
|
|
|
|
'remember_token',
|
2023-01-14 21:37:25 +00:00
|
|
|
'id_card',
|
2022-08-12 07:56:56 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
'email_verified_at' => 'datetime',
|
2023-01-14 21:37:25 +00:00
|
|
|
'real_name_verified_at' => 'datetime',
|
2023-01-14 10:35:13 +00:00
|
|
|
'balance' => 'decimal:2',
|
2022-09-13 11:24:01 +00:00
|
|
|
'banned_at' => 'datetime',
|
2022-12-30 13:32:04 +00:00
|
|
|
'birthday_at' => 'date',
|
2022-08-12 07:56:56 +00:00
|
|
|
];
|
2022-08-30 09:20:45 +00:00
|
|
|
|
2023-01-03 06:37:15 +00:00
|
|
|
protected static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
static::creating(function (self $user) {
|
|
|
|
$user->email_md5 = md5($user->email);
|
|
|
|
});
|
|
|
|
|
2023-01-14 21:37:25 +00:00
|
|
|
static::updating(function (self $user) {
|
|
|
|
if ($user->isDirty('banned_at')) {
|
|
|
|
if ($user->banned_at) {
|
|
|
|
$user->tokens()->delete();
|
|
|
|
$user->hosts()->update(['status' => 'suspended', 'suspended_at' => now()]);
|
2023-01-03 06:37:15 +00:00
|
|
|
} else {
|
2023-01-14 21:37:25 +00:00
|
|
|
$user->hosts()->update(['status' => 'stopped']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($user->isDirty('email')) {
|
|
|
|
$user->email_md5 = md5($user->email);
|
|
|
|
}
|
|
|
|
|
2023-01-14 23:28:44 +00:00
|
|
|
if ($user->isDirty('id_card') || $user->isDirty('real_name')) {
|
2023-01-14 21:37:25 +00:00
|
|
|
|
2023-01-14 23:28:44 +00:00
|
|
|
if (empty($user->id_card) && empty($user->real_name)) {
|
2023-01-14 23:22:34 +00:00
|
|
|
$user->real_name_verified_at = null;
|
2023-01-14 23:28:44 +00:00
|
|
|
$user->real_name = null;
|
|
|
|
$user->id_card = null;
|
2023-01-14 23:22:34 +00:00
|
|
|
} else {
|
2023-01-14 23:28:44 +00:00
|
|
|
$user->real_name_verified_at = now();
|
2023-01-14 23:22:34 +00:00
|
|
|
|
|
|
|
// 更新生日
|
|
|
|
try {
|
|
|
|
$user->birthday_at = $user->getBirthdayFromIdCard();
|
|
|
|
} catch (InvalidFormatException) {
|
|
|
|
$user->birthday_at = null;
|
|
|
|
}
|
2023-01-03 06:37:15 +00:00
|
|
|
}
|
2023-01-14 23:22:34 +00:00
|
|
|
|
|
|
|
|
2023-01-03 06:37:15 +00:00
|
|
|
}
|
2023-01-14 23:22:34 +00:00
|
|
|
|
2023-01-03 06:37:15 +00:00
|
|
|
});
|
|
|
|
}
|
2023-01-05 14:12:47 +00:00
|
|
|
|
|
|
|
public function hosts(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(Host::class);
|
|
|
|
}
|
|
|
|
|
2023-01-14 23:25:05 +00:00
|
|
|
private function getBirthdayFromIdCard(): string
|
|
|
|
{
|
|
|
|
$idCard = $this->id_card;
|
|
|
|
|
|
|
|
$year = substr($idCard, 6, 4);
|
|
|
|
$month = substr($idCard, 10, 2);
|
|
|
|
$day = substr($idCard, 12, 2);
|
|
|
|
return $year . '-' . $month . '-' . $day;
|
|
|
|
}
|
|
|
|
|
2023-01-05 14:12:47 +00:00
|
|
|
public function user_group(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(UserGroup::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeBirthday()
|
|
|
|
{
|
2023-01-10 13:42:27 +00:00
|
|
|
/** @noinspection PhpUndefinedMethodInspection */
|
2023-01-05 14:12:47 +00:00
|
|
|
return $this->select(['id', 'name', 'birthday_at', 'email_md5', 'created_at'])->whereMonth('birthday_at', now()->month)
|
2023-01-10 14:36:49 +00:00
|
|
|
->whereDay('birthday_at', now()->day)->whereNull('banned_at');
|
2023-01-05 14:12:47 +00:00
|
|
|
}
|
2023-01-11 12:19:05 +00:00
|
|
|
|
|
|
|
public function selectPublic(): User
|
|
|
|
{
|
|
|
|
// 过滤掉私有字段
|
|
|
|
return $this->select(['id', 'name', 'email_md5', 'created_at']);
|
|
|
|
}
|
2022-08-12 07:56:56 +00:00
|
|
|
}
|