Lae/app/Models/User.php

116 lines
2.6 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-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-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-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
protected static function boot()
{
parent::boot();
static::updating(function ($model) {
// balance 四舍五入
$model->balance = round($model->balance, 2);
});
}
2022-08-30 09:20:45 +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);
$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
}