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