Lae/app/Models/User.php

166 lines
4.4 KiB
PHP
Raw Normal View History

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;
2023-01-15 12:22:55 +00:00
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Database\Eloquent\Casts\Attribute;
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;
2023-01-15 12:22:55 +00:00
use Illuminate\Support\Facades\Crypt;
2022-11-06 11:28:22 +00:00
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 = [
'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',
2023-01-15 12:22:55 +00:00
'birthday_at' => 'date:Y-m-d'
2022-08-12 07:56:56 +00:00
];
2022-08-30 09:20:45 +00:00
2023-01-14 23:34:09 +00:00
protected $dates = [
'email_verified_at',
'real_name_verified_at',
'banned_at',
'birthday_at',
];
2023-01-15 12:22:55 +00:00
// id card 必须加密
2023-01-14 23:34:09 +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()]);
} 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-15 12:22:55 +00:00
if ($user->isDirty('id_card')) {
$user->id_card = Crypt::encryptString($user->id_card);
}
2023-01-14 21:37:25 +00:00
2023-01-15 12:22:55 +00:00
if ($user->isDirty('id_card') || $user->isDirty('real_name')) if (empty($user->id_card) || empty($user->real_name)) {
$user->real_name_verified_at = null;
} else {
$user->real_name_verified_at = now();
// 更新生日
try {
$user->birthday_at = $user->getBirthdayFromIdCard();
} catch (InvalidFormatException) {
$user->birthday_at = null;
}
}
2023-01-14 23:22:34 +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-15 12:22:55 +00:00
/**
* 获取用户的身份证号
*
* @return Attribute
*/
protected function idCard(): Attribute
{
return Attribute::make(
function ($value) {
try {
return Crypt::decryptString($value);
} catch (DecryptException) {
return $value;
}
}
);
}
2023-01-14 23:45:44 +00:00
public function isAdult(): bool
{
2023-01-15 01:14:53 +00:00
// 如果 birthday_at 为空,那么就返回 false
return $this->birthday_at?->diffInYears(now()) >= 18;
2023-01-14 23:45:44 +00:00
}
2023-01-15 00:34:20 +00:00
public function isRealNamed(): bool
{
return $this->real_name_verified_at !== null;
}
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
}