Lae/app/Models/User.php

219 lines
6.8 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;
2022-08-30 09:20:45 +00:00
use App\Exceptions\CommonException;
2022-09-08 16:12:02 +00:00
use App\Exceptions\User\BalanceNotEnoughException;
2022-12-27 16:25:22 +00:00
use Database\Factories\UserFactory;
use Eloquent;
use GeneaLabs\LaravelModelCaching\CachedBuilder;
2022-11-06 11:28:22 +00:00
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
2022-12-27 16:25:22 +00:00
use Illuminate\Database\Eloquent\Collection;
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;
2022-12-27 16:25:22 +00:00
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Notifications\DatabaseNotificationCollection;
2022-11-06 11:28:22 +00:00
use Illuminate\Notifications\Notifiable;
2022-12-27 16:25:22 +00:00
use Illuminate\Support\Carbon;
2022-11-06 11:28:22 +00:00
use Laravel\Sanctum\HasApiTokens;
2022-08-12 07:56:56 +00:00
2022-11-20 03:40:20 +00:00
/**
* App\Models\User
*
2022-11-20 12:32:49 +00:00
* @property int
* $id
* @property string
* $name
* @property string
* $email
2022-12-27 16:25:22 +00:00
* @property Carbon|null
2022-11-20 12:32:49 +00:00
* $email_verified_at
* @property string|null
* $password
* @property float
* $balance
2022-12-27 16:25:22 +00:00
* @property Carbon|null
2022-11-20 12:32:49 +00:00
* $banned_at 封禁时间
* @property string|null
* $banned_reason
* @property string|null
* $remember_token
2022-12-27 16:25:22 +00:00
* @property Carbon|null
2022-11-20 12:32:49 +00:00
* $created_at
2022-12-27 16:25:22 +00:00
* @property Carbon|null
2022-11-20 12:32:49 +00:00
* $updated_at
2022-12-27 16:25:22 +00:00
* @property-read Collection|Host[]
2022-11-20 12:32:49 +00:00
* $hosts
* @property-read int|null
* $hosts_count
2022-12-27 16:25:22 +00:00
* @property-read DatabaseNotificationCollection|DatabaseNotification[]
2022-11-20 12:32:49 +00:00
* $notifications
* @property-read int|null
* $notifications_count
2022-12-27 16:25:22 +00:00
* @property-read Collection|PersonalAccessToken[]
2022-11-20 12:32:49 +00:00
* $tokens
* @property-read int|null
* $tokens_count
2022-12-27 16:25:22 +00:00
* @method static CachedBuilder|User all($columns = [])
* @method static CachedBuilder|User avg($column)
* @method static CachedBuilder|User cache(array $tags = [])
* @method static CachedBuilder|User cachedValue(array $arguments, string $cacheKey)
* @method static CachedBuilder|User count($columns = '*')
* @method static CachedBuilder|User disableCache()
* @method static CachedBuilder|User disableModelCaching()
* @method static CachedBuilder|User exists()
* @method static UserFactory factory(...$parameters)
* @method static CachedBuilder|User flushCache(array $tags = [])
2022-11-20 12:32:49 +00:00
* @method static \GeneaLabs\LaravelModelCaching\CachedBuilder|User
* getModelCacheCooldown(\Illuminate\Database\Eloquent\Model $instance)
2022-12-27 16:25:22 +00:00
* @method static CachedBuilder|User inRandomOrder($seed = '')
* @method static CachedBuilder|User insert(array $values)
* @method static CachedBuilder|User isCachable()
* @method static CachedBuilder|User max($column)
* @method static CachedBuilder|User min($column)
* @method static CachedBuilder|User newModelQuery()
* @method static CachedBuilder|User newQuery()
* @method static CachedBuilder|User query()
* @method static CachedBuilder|User sum($column)
* @method static CachedBuilder|User truncate()
* @method static CachedBuilder|User whereBalance($value)
* @method static CachedBuilder|User whereBannedAt($value)
* @method static CachedBuilder|User whereBannedReason($value)
* @method static CachedBuilder|User whereCreatedAt($value)
* @method static CachedBuilder|User whereEmail($value)
* @method static CachedBuilder|User whereEmailVerifiedAt($value)
* @method static CachedBuilder|User whereId($value)
* @method static CachedBuilder|User whereName($value)
* @method static CachedBuilder|User wherePassword($value)
* @method static CachedBuilder|User whereRememberToken($value)
* @method static CachedBuilder|User whereUpdatedAt($value)
* @method static CachedBuilder|User withCacheCooldownSeconds(?int $seconds = null)
* @mixin Eloquent
2022-11-20 03:40:20 +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 = [
'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',
2022-08-12 07:56:56 +00:00
];
protected $casts = [
'email_verified_at' => 'datetime',
2022-11-30 12:26:35 +00:00
'balance' => 'decimal:2',
2022-09-13 11:24:01 +00:00
'banned_at' => 'datetime',
2022-12-28 13:28:12 +00:00
'birthday_at' => 'datetime',
2022-08-12 07:56:56 +00:00
];
2022-08-30 09:20:45 +00:00
protected static function boot()
{
parent::boot();
static::updating(function ($model) {
// balance 四舍五入
2022-11-18 08:29:57 +00:00
2022-11-30 12:26:35 +00:00
// if ($model->isDirty('balance')) {
// $model->balance = round($model->balance, 2, PHP_ROUND_HALF_DOWN);
// }
2022-11-18 08:29:57 +00:00
2022-11-18 09:16:30 +00:00
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']);
}
}
});
}
2022-08-30 09:20:45 +00:00
2022-11-19 04:38:26 +00:00
public function hosts(): HasMany
{
return $this->hasMany(Host::class);
}
2022-11-26 13:52:30 +00:00
public function user_group(): BelongsTo
{
return $this->belongsTo(UserGroup::class);
}
/**
* @throws CommonException
* @throws BalanceNotEnoughException
*/
2022-11-20 12:32:49 +00:00
// public function toDrops($amount = 1)
// {
//
// $cache_key = 'user_drops_' . $this->id;
//
// if ($amount === 0 || $amount === null) {
// return $this;
// }
//
// $rate = config('drops.rate');
//
//
// $transactions = new Transaction();
//
// $drops = $transactions->getDrops($this->id);
//
// $total = 0;
//
// if ($drops < 0) {
// $amount += abs($drops) / $rate;
// }
//
// $total += $amount * $rate;
//
//
// // amount 保留两位小数
// $amount = round($amount, 2);
//
// $lock = Cache::lock("lock_" . $cache_key, 5);
// try {
// $lock->block(5);
//
// $this->balance -= $amount;
// $this->save();
//
// $transactions->increaseDrops($this->id, $total);
//
// // $transactions
//
// $transactions->addPayoutBalance($this->id, $amount, '自动转换为 Drops');
//
// // if user balance <= 0
// if ($this->balance < $amount) {
// throw new BalanceNotEnoughException('余额不足');
// }
// } catch (LockTimeoutException) {
// throw new CommonException('暂时无法处理此请求,请稍后再试。');
// } finally {
// optional($lock)->release();
// }
//
// return $this;
// }
2022-08-12 07:56:56 +00:00
}