341 lines
7.1 KiB
PHP
341 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Constants\Device;
|
|
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
|
use Illuminate\Auth\Authenticatable;
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Illuminate\Foundation\Auth\Access\Authorizable;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
|
|
class User extends Model implements AuthorizableContract, AuthenticatableContract
|
|
{
|
|
use Authenticatable;
|
|
use Authorizable;
|
|
use HasFactory;
|
|
use HasApiTokens;
|
|
use HasDateTimeFormatter;
|
|
|
|
public const STATUS_FROZEN = -1; // 冻结
|
|
public const STATUS_INACTIVATED = 0; // 未激活
|
|
public const STATUS_ACTIVE = 1; // 正常
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $attributes = [
|
|
'status' => self::STATUS_ACTIVE,
|
|
];
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'username',
|
|
'password',
|
|
'phone',
|
|
'phone_verified_at',
|
|
'email',
|
|
'email_verified_at',
|
|
'last_login_ip',
|
|
'last_login_at',
|
|
'register_ip',
|
|
'status',
|
|
'status_remark',
|
|
];
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'phone_verified_at' => 'datetime',
|
|
'email_verified_at' => 'datetime',
|
|
'last_login_at' => 'datetime',
|
|
'status' => 'int',
|
|
];
|
|
|
|
/**
|
|
* 属于此用户的个人信息
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
*/
|
|
public function userInfo(): HasOne
|
|
{
|
|
return $this->hasOne(UserInfo::class);
|
|
}
|
|
|
|
/**
|
|
* 用户的VIP信息
|
|
*/
|
|
public function userVip()
|
|
{
|
|
return $this->hasOne(UserVip::class, 'user_id');
|
|
}
|
|
|
|
/**
|
|
* 属于此用户的购物车中的商品
|
|
*/
|
|
public function shoppingCartItems()
|
|
{
|
|
return $this->hasMany(ShoppingCartItem::class);
|
|
}
|
|
|
|
/**
|
|
* 属于此用户的收货地址
|
|
*/
|
|
public function shippingAddresses()
|
|
{
|
|
return $this->hasMany(ShippingAddress::class);
|
|
}
|
|
|
|
/**
|
|
* 属于此用户的订单
|
|
*/
|
|
public function orders()
|
|
{
|
|
return $this->hasMany(Order::class);
|
|
}
|
|
|
|
/**
|
|
* 属于此用户的订单包裹
|
|
*/
|
|
public function orderPackages()
|
|
{
|
|
return $this->hasMany(OrderPackage::class);
|
|
}
|
|
|
|
/**
|
|
* 属于此用户的商品收藏记录
|
|
*/
|
|
public function skuFavorites()
|
|
{
|
|
return $this->hasMany(ProductSkuFavorite::class);
|
|
}
|
|
|
|
/**
|
|
* 属于此用户的推送cid
|
|
*/
|
|
public function cid()
|
|
{
|
|
return $this->hasOne(UserCid::class, 'user_id');
|
|
}
|
|
|
|
/**
|
|
* 确认此用户是否是 VIP
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isVip(): bool
|
|
{
|
|
return $this->userInfo?->growth_value >= 650;
|
|
}
|
|
|
|
/**
|
|
* 属于此用户的售后订单
|
|
*/
|
|
public function afterSales()
|
|
{
|
|
return $this->hasMany(AfterSale::class);
|
|
}
|
|
|
|
/**
|
|
* 属于此用户的优惠券
|
|
*/
|
|
public function coupons()
|
|
{
|
|
return $this->hasMany(UserCoupon::class);
|
|
}
|
|
|
|
/**
|
|
* 签到情况
|
|
*
|
|
* @return void
|
|
*/
|
|
public function click()
|
|
{
|
|
return $this->hasOne(Click::class, 'user_id');
|
|
}
|
|
|
|
/**
|
|
* 用户签到记录
|
|
*
|
|
* @return void
|
|
*/
|
|
public function clickLogs()
|
|
{
|
|
return $this->hasMany(ClickLog::class);
|
|
}
|
|
|
|
/**
|
|
* 用户的银行卡
|
|
*/
|
|
public function bank()
|
|
{
|
|
return $this->hasOne(UserBank::class, 'user_id');
|
|
}
|
|
|
|
/**
|
|
* 用户的资产
|
|
*/
|
|
public function wallet()
|
|
{
|
|
return $this->hasOne(Wallet::class, 'user_id');
|
|
}
|
|
|
|
/**
|
|
* 用户的钱包日志
|
|
*/
|
|
public function walletLogs()
|
|
{
|
|
return $this->hasMany(WalletLog::class, 'user_id');
|
|
}
|
|
|
|
/**
|
|
* 用户的提现到银行卡记录
|
|
*/
|
|
public function walletToBankLogs()
|
|
{
|
|
return $this->hasMany(WalletToBankLog::class, 'user_id');
|
|
}
|
|
|
|
/**
|
|
* 用户的余额
|
|
*/
|
|
public function balance()
|
|
{
|
|
return $this->hasOne(Balance::class, 'user_id');
|
|
}
|
|
|
|
/**
|
|
* 用户的余额日志
|
|
*/
|
|
public function balanceLogs()
|
|
{
|
|
return $this->hasOne(BalanceLog::class, 'user_id');
|
|
}
|
|
|
|
/**
|
|
* 用户的预收益
|
|
*/
|
|
public function distributionPreIncomes()
|
|
{
|
|
return $this->hasMany(DistributionPreIncome::class, 'user_id');
|
|
}
|
|
|
|
/**
|
|
* 用户的粉丝
|
|
*
|
|
* @return void
|
|
*/
|
|
public function fans()
|
|
{
|
|
return $this->belongsToMany(User::class, 'user_infos', 'inviter_id', 'user_id');
|
|
}
|
|
|
|
/**
|
|
* 禁用用户
|
|
*
|
|
* @param string|null $status_remarks
|
|
* @return void
|
|
*/
|
|
public function disable(?string $status_remark = null)
|
|
{
|
|
$res = null;
|
|
if ($this->status == 1) {//只操作正常用户
|
|
$this->status = 2;
|
|
$this->status_remark = $status_remark;
|
|
$res = $this->save();
|
|
}
|
|
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 启用用户
|
|
*
|
|
* @return void
|
|
*/
|
|
public function enable()
|
|
{
|
|
$res = null;
|
|
if ($this->status != 1) {//
|
|
$this->status = 1;
|
|
$res = $this->save();
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 设置此用户的密码
|
|
*
|
|
* @param string $value
|
|
* @return void
|
|
*/
|
|
public function setPasswordAttribute($value): void
|
|
{
|
|
if ((string) $value === '') {
|
|
$value = null;
|
|
} elseif (Hash::needsRehash($value)) {
|
|
$value = Hash::make($value);
|
|
}
|
|
|
|
$this->attributes['password'] = $value;
|
|
}
|
|
|
|
/**
|
|
* 确认给定的密码是否正确
|
|
*
|
|
* @param string $password
|
|
* @return bool
|
|
*/
|
|
public function verifyPassword(string $password): bool
|
|
{
|
|
return $this->password && Hash::check($password, $this->password);
|
|
}
|
|
|
|
/**
|
|
* 创建设备授权令牌
|
|
*
|
|
* @param string $device
|
|
* @return array
|
|
*/
|
|
public function createDeviceToken(string $device = null): array
|
|
{
|
|
return [
|
|
'token' => $this->createToken($device ?: Device::PC)->plainTextToken,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 创建用户
|
|
*
|
|
* @param array $attributes
|
|
* @param self|null $inviter
|
|
* @return self
|
|
*/
|
|
public static function create(array $attributes = [], ?self $inviter = null): self
|
|
{
|
|
$user = static::query()->create($attributes);
|
|
|
|
$user->userInfo()->create(
|
|
$inviter ? [
|
|
'inviter_id' => $inviter->id,
|
|
'depth' => $inviter->userInfo->depth + 1,
|
|
'path' => $inviter->userInfo->path.$inviter->id.'/',
|
|
] : [
|
|
'inviter_id' => null,
|
|
'depth' => 1,
|
|
'path' => '/',
|
|
]
|
|
);
|
|
|
|
return $user;
|
|
}
|
|
}
|