384 lines
8.3 KiB
PHP
384 lines
8.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
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 Illuminate\Support\Str;
|
|
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; // 正常
|
|
public const STATUS_DISABLED = 2; // 禁用
|
|
|
|
/**
|
|
* @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',
|
|
];
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
parent::created(function ($user) {
|
|
//初始化钱包
|
|
$user->wallet()->create();
|
|
//初始化余额
|
|
$user->balance()->create();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 属于此用户的个人信息
|
|
*
|
|
* @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');
|
|
}
|
|
|
|
public function socialites()
|
|
{
|
|
return $this->hasMany(SocialiteUser::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 salesValueLogs()
|
|
{
|
|
return $this->hasMany(SalesValueLog::class)->where('type', SalesValueLog::TYPE_INDIVIDUAL);
|
|
}
|
|
|
|
/**
|
|
* 用户的粉丝
|
|
*/
|
|
public function fans()
|
|
{
|
|
return $this->belongsToMany(User::class, 'user_infos', 'inviter_id', 'user_id');
|
|
}
|
|
|
|
/**
|
|
* 用户的砍价单
|
|
*/
|
|
public function bargainOrders()
|
|
{
|
|
return $this->hasMany(BargainOrder::class);
|
|
}
|
|
|
|
/**
|
|
* 获取我的粉丝总数
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getFansCount(): int
|
|
{
|
|
return UserInfo::where('inviter_id', $this->id)->count();
|
|
}
|
|
|
|
/**
|
|
* 禁用用户
|
|
*
|
|
* @param string|null $statusRemark
|
|
* @return void
|
|
*/
|
|
public function disable(?string $statusRemark = null)
|
|
{
|
|
if ($this->status === static::STATUS_ACTIVE) {
|
|
$this->update([
|
|
'status' => static::STATUS_DISABLED,
|
|
'status_remark' => $statusRemark,
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 启用用户
|
|
*
|
|
* @return void
|
|
*/
|
|
public function enable()
|
|
{
|
|
if ($this->status !== static::STATUS_ACTIVE) {
|
|
return $this->update([
|
|
'status' => static::STATUS_ACTIVE,
|
|
'status_remark' => null,
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 设置此用户的密码
|
|
*
|
|
* @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['old_password'] = null;
|
|
}
|
|
|
|
$this->attributes['password'] = $value;
|
|
}
|
|
|
|
/**
|
|
* 确认给定的密码是否正确
|
|
*
|
|
* @param string $password
|
|
* @return bool
|
|
*/
|
|
public function verifyPassword(string $password): bool
|
|
{
|
|
// 如果旧密码存在,则校验旧密码
|
|
if ($this->old_password) {
|
|
return $this->old_password === md5($password);
|
|
}
|
|
|
|
return $this->password && Hash::check($password, $this->password);
|
|
}
|
|
|
|
/**
|
|
* 创建用户
|
|
*
|
|
* @param array $attributes
|
|
* @param self|null $inviter
|
|
* @return self
|
|
*/
|
|
public static function create(array $attributes = [], ?self $inviter = null): self
|
|
{
|
|
if ($inviter === null) {
|
|
$inviter = static::find(1);
|
|
}
|
|
|
|
$user = static::query()->create($attributes);
|
|
|
|
// 邀请人的深度
|
|
$depth = (int) $inviter?->userInfo?->depth;
|
|
|
|
$user->userInfo()->create([
|
|
'inviter_id' => $inviter?->id,
|
|
'depth' => $depth + 1,
|
|
'path' => Str::finish($inviter?->userInfo?->full_path, '-'),
|
|
'real_inviter_id' => $inviter?->id,
|
|
]);
|
|
|
|
return $user;
|
|
}
|
|
|
|
public function checkStatus()
|
|
{
|
|
if (in_array($this->status, [static::STATUS_ACTIVE, static::STATUS_INACTIVATED])) {
|
|
return;
|
|
} elseif ($this->status === static::STATUS_FROZEN) {
|
|
abort(403, '账号已冻结');
|
|
} elseif ($this->status === static::STATUS_DISABLED) {
|
|
abort(403, '账号已禁用');
|
|
}
|
|
|
|
abort(403, '账号状态异常');
|
|
}
|
|
}
|