Lae/app/Models/User.php

319 lines
9.2 KiB
PHP
Raw Normal View History

2022-08-12 07:56:56 +00:00
<?php
namespace App\Models;
2023-02-12 18:22:26 +00:00
use App\Events\Users;
2023-01-16 20:36:43 +00:00
use App\Exceptions\User\BalanceNotEnoughException;
2023-02-22 18:00:53 +00:00
use App\Models\Affiliate\Affiliates;
use App\Models\Affiliate\AffiliateUser;
2023-03-07 16:45:29 +00:00
use App\Notifications\User\BalanceNotEnough;
use App\Notifications\User\LowBalance;
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-22 18:00:53 +00:00
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
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-02-07 09:20:54 +00:00
use Illuminate\Support\Arr;
2023-02-10 05:06:42 +00:00
use Illuminate\Support\Carbon;
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;
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',
2023-02-07 09:45:31 +00:00
'uuid',
2023-01-30 16:14:07 +00:00
'name',
'email',
'real_name',
'balance',
2023-02-08 05:16:16 +00:00
'user_group_id',
2023-01-30 16:14:07 +00:00
];
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',
2023-02-22 13:32:33 +00:00
'receive_marketing_email',
2023-02-22 18:00:53 +00:00
'affiliate_id',
2022-08-12 07:56:56 +00:00
];
/**
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',
];
2023-01-05 14:12:47 +00:00
public function hosts(): HasMany
{
return $this->hasMany(Host::class);
}
2023-02-22 18:00:53 +00:00
public function affiliate(): HasOne
{
return $this->hasOne(Affiliates::class);
}
public function affiliateUsers(): HasManyThrough
{
return $this->hasManyThrough(AffiliateUser::class, Affiliates::class, 'user_id', 'affiliate_id');
}
public function affiliateUser(): BelongsTo
{
return $this->belongsTo(AffiliateUser::class, 'affiliate_id');
}
// 通过 affiliate_id 获取到 affiliates 中的 user_id
public function promoter(): HasOneThrough
{
return $this->hasOneThrough(User::class, Affiliates::class, 'id', 'id', 'affiliate_id', 'user_id');
}
2023-02-10 05:06:42 +00:00
public function getBirthdayFromIdCard(string|null $id_card = null): Carbon
2023-01-14 23:25:05 +00:00
{
2023-02-10 05:06:42 +00:00
if (empty($id_card)) {
$id_card = $this->id_card;
}
2023-01-14 23:25:05 +00:00
2023-02-10 05:06:42 +00:00
$bir = substr($id_card, 6, 8);
2023-02-12 18:22:26 +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-12 18:22:26 +00:00
return Carbon::parse($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-03-11 06:21:21 +00:00
2023-02-07 09:45:31 +00:00
public function getOnlyPublic($appened_excepts = [], $display = []): array
2023-02-07 09:20:54 +00:00
{
2023-02-07 09:45:31 +00:00
if ($display) {
$this->publics = array_merge($this->publics, $display);
}
if ($appened_excepts) {
$this->publics = array_diff($this->publics, $appened_excepts);
2023-02-07 09:20:54 +00:00
}
return Arr::only($this->toArray(), $this->publics);
}
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-20 14:43:46 +00:00
public function reduce(string|null $amount = '0', string $description = '消费', bool $fail = false, array $options = []): Transaction
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-28 10:04:58 +00:00
/**
* @throws BalanceNotEnoughException
*/
2023-02-20 14:43:46 +00:00
return 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) {
2023-03-07 16:45:29 +00:00
// 发送邮件通知
$this->notify(new BalanceNotEnough());
2023-01-16 20:36:43 +00:00
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);
}
2023-02-12 18:22:26 +00:00
broadcast(new Users($this, 'balances.amount.reduced', $this));
2023-01-16 20:36:43 +00:00
2023-03-07 16:45:29 +00:00
// 如果用户的余额小于 5 元,则发送邮件提醒(一天只发送一次,使用缓存)
if (! $this->hasBalance(5) && ! Cache::has('user_balance_less_than_5_'.$this->id)) {
$this->notify(new LowBalance());
Cache::put('user_balance_less_than_5_'.$this->id, true, now()->addDay());
}
2023-02-20 14:43:46 +00:00
return (new Transaction)->create($data);
});
2023-01-16 20:36:43 +00:00
}
/**
* 增加余额
*/
2023-02-20 14:43:46 +00:00
public function charge(string|null $amount = '0', string $payment = 'console', string $description = '充值', array $options = []): Transaction
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-20 14:43:46 +00:00
return 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);
}
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
2023-02-20 14:43:46 +00:00
return (new Transaction)->create($data);
});
2023-01-16 20:36:43 +00:00
}
2023-02-12 19:12:26 +00:00
public function getCostPrice(string $price): string
{
$this->load('user_group');
if (! $this->user_group) {
return $price;
}
return $this->user_group->getCostPrice($price);
}
2023-02-28 10:04:58 +00:00
public function subscriptions(): HasMany
{
return $this->hasMany(Subscription::class);
}
2023-01-16 20:42:59 +00:00
/**
* 获取用户的身份证号
*/
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
}