2022-08-12 07:56:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2022-08-30 09:20:45 +00:00
|
|
|
use App\Exceptions\CommonException;
|
2022-09-08 16:12:02 +00:00
|
|
|
use Illuminate\Auth\Authenticatable;
|
|
|
|
use Laravel\Lumen\Auth\Authorizable;
|
2022-08-30 09:20:45 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2022-09-08 16:12:02 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use App\Exceptions\User\BalanceNotEnoughException;
|
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-09-08 16:12:02 +00:00
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
|
2022-08-12 07:56:56 +00:00
|
|
|
|
2022-09-08 16:12:02 +00:00
|
|
|
class User extends Model implements AuthenticatableContract, AuthorizableContract
|
2022-08-12 07:56:56 +00:00
|
|
|
{
|
2022-09-08 16:12:02 +00:00
|
|
|
use Authenticatable, Authorizable, HasFactory;
|
2022-08-12 07:56:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
2022-09-08 16:12:02 +00:00
|
|
|
* @var string[]
|
2022-08-12 07:56:56 +00:00
|
|
|
*/
|
|
|
|
protected $fillable = [
|
|
|
|
'name',
|
|
|
|
'email',
|
|
|
|
'password',
|
2022-09-06 14:08:41 +00:00
|
|
|
'balance'
|
2022-08-12 07:56:56 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2022-09-08 16:12:02 +00:00
|
|
|
* The attributes excluded from the model's JSON form.
|
2022-08-12 07:56:56 +00:00
|
|
|
*
|
2022-09-08 16:12:02 +00:00
|
|
|
* @var string[]
|
2022-08-12 07:56:56 +00:00
|
|
|
*/
|
|
|
|
protected $hidden = [
|
|
|
|
'password',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
'email_verified_at' => 'datetime',
|
2022-08-30 09:20:45 +00:00
|
|
|
'balance' => 'float',
|
2022-08-12 07:56:56 +00:00
|
|
|
];
|
2022-08-30 09:20:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
public function toDrops($amount = 1)
|
|
|
|
{
|
|
|
|
$rate = Cache::get('drops_rate', 100);
|
|
|
|
$cache_key = 'user_drops_' . $this->id;
|
|
|
|
|
2022-09-04 14:18:49 +00:00
|
|
|
$drops = Cache::get($cache_key, 0);
|
|
|
|
|
|
|
|
$total = 0;
|
|
|
|
|
|
|
|
if ($drops < 0) {
|
|
|
|
$amount += abs($drops) / $rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
$total += $amount * $rate;
|
|
|
|
|
|
|
|
|
2022-08-30 09:20:45 +00:00
|
|
|
$lock = Cache::lock("lock_" . $cache_key, 5);
|
|
|
|
try {
|
|
|
|
$lock->block(5);
|
|
|
|
|
|
|
|
$this->balance -= $amount;
|
|
|
|
$this->save();
|
|
|
|
|
|
|
|
// increment user drops
|
|
|
|
Cache::increment($cache_key, $total);
|
|
|
|
|
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-09-08 16:12:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-08-12 07:56:56 +00:00
|
|
|
}
|