Lae/app/Models/User.php

309 lines
8.7 KiB
PHP
Raw Normal View History

2022-08-12 07:56:56 +00:00
<?php
namespace App\Models;
2023-01-16 20:36:43 +00:00
use App\Exceptions\User\BalanceNotEnoughException;
2023-01-14 21:37:25 +00:00
use Carbon\Exceptions\InvalidFormatException;
2023-01-29 16:31:11 +00:00
use GeneaLabs\LaravelModelCaching\CachedBuilder;
2022-11-06 11:28:22 +00:00
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
2023-02-01 18:58:44 +00:00
use Illuminate\Contracts\Auth\MustVerifyEmail;
2023-01-15 12:22:55 +00:00
use Illuminate\Contracts\Encryption\DecryptException;
2023-01-30 14:51:26 +00:00
use Illuminate\Database\Eloquent\Builder;
2023-01-15 12:22:55 +00:00
use Illuminate\Database\Eloquent\Casts\Attribute;
2022-08-12 07:56:56 +00:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2023-02-01 18:58:44 +00:00
use Illuminate\Database\Eloquent\Prunable;
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;
2023-02-01 18:58:44 +00:00
use Illuminate\Database\Eloquent\SoftDeletes;
2022-11-06 11:28:22 +00:00
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
2023-01-16 20:36:43 +00:00
use Illuminate\Support\Facades\Cache;
2023-01-15 12:22:55 +00:00
use Illuminate\Support\Facades\Crypt;
2023-02-01 17:56:51 +00:00
use Illuminate\Support\Str;
2022-11-06 11:28:22 +00:00
use Laravel\Sanctum\HasApiTokens;
2022-08-12 07:56:56 +00:00
2023-02-01 18:58:44 +00:00
class User extends Authenticatable implements MustVerifyEmail
2022-08-12 07:56:56 +00:00
{
2023-02-01 18:58:44 +00:00
use HasApiTokens, HasFactory, Notifiable, SoftDeletes, Prunable, Cachable;
2022-08-12 07:56:56 +00:00
2023-01-30 16:14:07 +00:00
public array $publics = [
'id',
'name',
'email',
'real_name',
'balance',
];
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-18 15:38:31 +00:00
'real_name',
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-17 16:09:15 +00:00
'balance' => 'decimal:4',
2022-09-13 11:24:01 +00:00
'banned_at' => 'datetime',
2023-01-30 16:14:07 +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',
];
protected static function boot()
{
parent::boot();
static::creating(function (self $user) {
$user->email_md5 = md5($user->email);
2023-02-01 17:56:51 +00:00
$user->uuid = Str::uuid();
});
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-30 16:14:07 +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-02-01 18:58:44 +00:00
static::deleting(function (self $user) {
$user->tokens()->delete();
$user->hosts()->update(['status' => 'suspended', 'suspended_at' => now()]);
});
}
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;
2023-01-15 13:05:48 +00:00
$bir = substr($idCard, 6, 8);
2023-02-07 09:04:11 +00:00
$year = (int) substr($bir, 0, 4);
$month = (int) substr($bir, 4, 2);
$day = (int) substr($bir, 6, 2);
2023-01-15 13:05:48 +00:00
2023-02-07 09:04:11 +00:00
return $year.'-'.$month.'-'.$day;
2023-01-14 23:25:05 +00:00
}
2023-01-30 16:14:07 +00:00
public function hasBalance(string $amount = '0.01'): bool
2023-01-30 05:16:49 +00:00
{
return bccomp($this->balance, $amount, 4) >= 0;
}
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);
}
2023-01-30 14:51:26 +00:00
public function scopeBirthday(): Builder|CachedBuilder
2023-01-05 14:12:47 +00:00
{
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
2023-01-30 14:51:26 +00:00
public function selectPublic(): self|Builder|CachedBuilder
2023-01-11 12:19:05 +00:00
{
2023-01-18 15:38:31 +00:00
// 仅需选择公开的
return $this->select($this->publics);
2023-01-11 12:19:05 +00:00
}
2023-01-16 20:36:43 +00:00
2023-02-02 05:59:34 +00:00
public function prunable(): self|Builder|CachedBuilder
2023-02-01 18:58:44 +00:00
{
return static::where('deleted_at', '<=', now()->subWeek());
}
2023-01-30 14:51:26 +00:00
public function startTransfer(self $to, string $amount, string|null $description)
2023-01-16 20:42:59 +00:00
{
$description_from = "转账给 $to->name($to->email)";
$description_to = "收到 $this->name($this->email) 的转账";
if ($description) {
$description_from .= ",备注:$description";
$description_to .= ",备注:$description";
}
$this->reduce($amount, $description_from, true);
$to->charge($amount, 'transfer', $description_to);
return $this->balance;
}
2023-01-16 20:36:43 +00:00
/**
* 扣除费用
*
2023-02-07 09:04:11 +00:00
* @param string|null $amount
* @param string $description
* @param bool $fail
* @param array $options
2023-01-16 20:36:43 +00:00
* @return string
*/
2023-01-30 16:14:07 +00:00
public function reduce(string|null $amount = '0', string $description = '消费', bool $fail = false, array $options = []): string
2023-01-16 20:36:43 +00:00
{
2023-01-17 05:09:06 +00:00
if ($amount === null || $amount === '') {
return $this->balance;
}
2023-02-07 09:04:11 +00:00
Cache::lock('user_balance_'.$this->id, 10)->block(10, function () use ($amount, $fail, $description, $options) {
2023-01-16 20:36:43 +00:00
$this->refresh();
if ($this->balance < $amount) {
if ($fail) {
throw new BalanceNotEnoughException();
}
}
2023-01-17 16:09:15 +00:00
$this->balance = bcsub($this->balance, $amount, 4);
2023-01-16 20:36:43 +00:00
$this->save();
$data = [
'user_id' => $this->id,
'amount' => $amount,
'description' => $description,
'payment' => 'balance',
'type' => 'payout',
];
if ($options) {
$data = array_merge($data, $options);
}
(new Transaction)->create($data);
});
return $this->balance;
}
/**
* 增加余额
*
2023-02-07 09:04:11 +00:00
* @param string|null $amount
* @param string $payment
* @param string $description
* @param array $options
2023-01-16 20:36:43 +00:00
* @return string
*/
2023-01-30 16:14:07 +00:00
public function charge(string|null $amount = '0', string $payment = 'console', string $description = '充值', array $options = []): string
2023-01-16 20:36:43 +00:00
{
2023-01-17 05:05:43 +00:00
if ($amount === null || $amount === '') {
return $this->balance;
}
2023-02-07 09:04:11 +00:00
Cache::lock('user_balance_'.$this->id, 10)->block(10, function () use ($amount, $description, $payment, $options) {
2023-01-16 20:36:43 +00:00
$this->refresh();
2023-01-17 16:09:15 +00:00
$this->balance = bcadd($this->balance, $amount, 4);
2023-01-16 20:36:43 +00:00
$this->save();
$data = [
'user_id' => $this->id,
'amount' => $amount,
'payment' => $payment,
'description' => $description,
'type' => 'income',
];
if ($options) {
$data = array_merge($data, $options);
}
(new Transaction)->create($data);
2023-02-03 18:30:07 +00:00
if (isset($options['add_balances_log']) && $options['add_balances_log'] === true) {
(new Balance)->create([
'user_id' => $this->id,
'amount' => $amount,
'payment' => $payment,
'description' => $description,
'paid_at' => now(),
]);
}
2023-01-16 20:36:43 +00:00
});
return $this->balance;
}
2023-01-16 20:42:59 +00:00
/**
* 获取用户的身份证号
*
* @return Attribute
*/
protected function idCard(): Attribute
2023-01-16 20:36:43 +00:00
{
2023-01-16 20:42:59 +00:00
return Attribute::make(
function ($value) {
try {
return Crypt::decryptString($value);
} catch (DecryptException) {
return $value;
}
}
);
2023-01-16 20:36:43 +00:00
}
2022-08-12 07:56:56 +00:00
}