123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- <?php
- namespace App\Models;
- use Hash;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use Illuminate\Database\Eloquent\Relations\HasManyThrough;
- use Illuminate\Database\Eloquent\Relations\HasOne;
- use Illuminate\Foundation\Auth\User as Authenticatable;
- use Illuminate\Notifications\Notifiable;
- use Spatie\Permission\Traits\HasRoles;
- use Tymon\JWTAuth\Contracts\JWTSubject;
- /**
- * 用户信息.
- *
- * @property int $id
- * @property string $username 昵称
- * @property string $email 邮箱
- * @property string $password 密码
- * @property int $port 代理端口
- * @property string $passwd 代理密码
- * @property string $vmess_id
- * @property int $transfer_enable 可用流量,单位字节,默认1TiB
- * @property int $u 已上传流量,单位字节
- * @property int $d 已下载流量,单位字节
- * @property int|null $t 最后使用时间
- * @property string|null $ip 最后连接IP
- * @property int $enable 代理状态
- * @property string $method 加密方式
- * @property string $protocol 协议
- * @property string|null $protocol_param 协议参数
- * @property string $obfs 混淆
- * @property int $speed_limit 用户限速,为0表示不限速,单位Byte
- * @property string|null $wechat 微信
- * @property string|null $qq QQ
- * @property int $credit 余额,单位分
- * @property \Illuminate\Support\Carbon $expired_at 过期时间
- * @property int|null $ban_time 封禁到期时间
- * @property string|null $remark 备注
- * @property int $level 等级,默认0级
- * @property int|null $user_group_id 所属分组
- * @property string $reg_ip 注册IP
- * @property int $last_login 最后登录时间
- * @property int|null $inviter_id 邀请人
- * @property \Illuminate\Support\Carbon|null $reset_time 流量重置日期
- * @property int $invite_num 可生成邀请码数
- * @property int $status 状态:-1-禁用、0-未激活、1-正常
- * @property string|null $remember_token
- * @property \Illuminate\Support\Carbon $created_at 创建时间
- * @property \Illuminate\Support\Carbon $updated_at 最后更新时间
- * @property int|null $client_limit 用户链接的设备
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserBanedLog[] $banedLogs
- * @property-read int|null $baned_logs_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ReferralLog[] $commissionLogs
- * @property-read int|null $commission_logs_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ReferralApply[] $commissionSettlements
- * @property-read int|null $commission_settlements_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserCreditLog[] $creditLogs
- * @property-read int|null $credit_logs_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserDailyDataFlow[] $dailyDataFlows
- * @property-read int|null $daily_data_flows_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserDataFlowLog[] $dataFlowLogs
- * @property-read int|null $data_flow_logs_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserDataModifyLog[] $dataModifyLogs
- * @property-read int|null $data_modify_logs_count
- * @property-read bool $is_available
- * @property-read string $level_name
- * @property-read mixed $transfer_enable_formatted
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserHourlyDataFlow[] $hourlyDataFlows
- * @property-read int|null $hourly_data_flows_count
- * @property-read \Illuminate\Database\Eloquent\Collection|User[] $invitees
- * @property-read int|null $invitees_count
- * @property-read User|null $inviter
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Invite[] $invites
- * @property-read int|null $invites_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserLoginLog[] $loginLogs
- * @property-read int|null $login_logs_count
- * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
- * @property-read int|null $notifications_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\NodeOnlineIp[] $onlineIpLogs
- * @property-read int|null $online_ip_logs_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Order[] $orders
- * @property-read int|null $orders_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Payment[] $payments
- * @property-read int|null $payments_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Permission\Models\Permission[] $permissions
- * @property-read int|null $permissions_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Permission\Models\Role[] $roles
- * @property-read int|null $roles_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\RuleLog[] $ruleLogs
- * @property-read int|null $rule_logs_count
- * @property-read \App\Models\UserSubscribe|null $subscribe
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserSubscribeLog[] $subscribeLogs
- * @property-read int|null $subscribe_logs_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\TicketReply[] $ticketReplies
- * @property-read int|null $ticket_replies_count
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Ticket[] $tickets
- * @property-read int|null $tickets_count
- * @property-read \App\Models\UserGroup|null $userGroup
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Verify[] $verify
- * @property-read int|null $verify_count
- * @method static \Illuminate\Database\Eloquent\Builder|User activeUser()
- * @method static \Illuminate\Database\Eloquent\Builder|User newModelQuery()
- * @method static \Illuminate\Database\Eloquent\Builder|User newQuery()
- * @method static \Illuminate\Database\Eloquent\Builder|User permission($permissions)
- * @method static \Illuminate\Database\Eloquent\Builder|User query()
- * @method static \Illuminate\Database\Eloquent\Builder|User role($roles, $guard = null)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereBanTime($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereClientLimit($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereCreatedAt($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereCredit($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereD($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereEmail($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereEnable($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereExpiredAt($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereId($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereInviteNum($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereInviterId($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereIp($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereLastLogin($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereLevel($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereMethod($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereObfs($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User wherePasswd($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User wherePassword($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User wherePort($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereProtocol($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereProtocolParam($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereQq($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereRegIp($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereRemark($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereRememberToken($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereResetTime($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereSpeedLimit($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereStatus($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereT($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereTransferEnable($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereU($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereUpdatedAt($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereUserGroupId($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereUsername($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereVmessId($value)
- * @method static \Illuminate\Database\Eloquent\Builder|User whereWechat($value)
- * @mixin \Eloquent
- */
- class User extends Authenticatable implements JWTSubject
- {
- use Notifiable, HasRoles;
- protected $table = 'user';
- //protected $casts = ['expired_at' => 'date:Y-m-d', 'reset_time' => 'date:Y-m-d', 'ban_time' => 'date:Y-m-d'];
- protected $dates = ['expired_at', 'reset_time'];
- protected $guarded = [];
- public function usedTrafficPercentage()
- {
- return round(($this->usedTraffic()) / $this->transfer_enable, 2);
- }
- public function usedTraffic(): int
- {
- return $this->d + $this->u;
- }
- public function profile()
- {
- $totalTransfer = $this->transfer_enable;
- $usedTransfer = $this->usedTraffic();
- $unusedTraffic = $totalTransfer - $usedTransfer > 0 ? $totalTransfer - $usedTransfer : 0;
- $expireTime = $this->expired_at;
- return [
- 'id' => $this->id,
- 'nickname' => $this->username,
- 'account' => $this->email,
- 'port' => $this->port,
- 'passwd' => $this->passwd,
- 'uuid' => $this->vmess_id,
- 'transfer_enable' => $this->transfer_enable,
- 'unusedTraffic' => flowAutoShow($unusedTraffic),
- 'u' => $this->u,
- 'd' => $this->d,
- 't' => $this->t,
- 'enable' => $this->enable,
- 'speed_limit' => $this->speed_limit,
- 'credit' => $this->credit,
- 'expired_at' => $this->expired_at,
- 'ban_time' => $this->ban_time,
- 'level' => $this->level_name,
- 'group' => $this->userGroup->name ?? null,
- 'last_login' => $this->last_login,
- 'reset_time' => $this->reset_time,
- 'invite_num' => $this->invite_num,
- 'user_group_id'=> $this->user_group_id,
- 'status' => $this->status,
- 'code' => $this->subscribe->code,
- ];
- }
- public function onlineIpLogs(): HasMany
- {
- return $this->hasMany(NodeOnlineIp::class);
- }
- public function payments(): HasMany
- {
- return $this->hasMany(Payment::class);
- }
- public function commissionSettlements(): HasMany
- {
- return $this->hasMany(ReferralApply::class);
- }
- public function commissionLogs(): HasMany
- {
- return $this->hasMany(ReferralLog::class, 'inviter_id');
- }
- public function ruleLogs(): HasMany
- {
- return $this->hasMany(RuleLog::class);
- }
- public function tickets(): HasMany
- {
- return $this->hasMany(Ticket::class);
- }
- public function ticketReplies(): HasMany
- {
- return $this->hasMany(TicketReply::class);
- }
- public function banedLogs(): HasMany
- {
- return $this->hasMany(UserBanedLog::class);
- }
- public function creditLogs(): HasMany
- {
- return $this->hasMany(UserCreditLog::class);
- }
- public function dailyDataFlows(): HasMany
- {
- return $this->hasMany(UserDailyDataFlow::class);
- }
- public function dataFlowLogs(): HasMany
- {
- return $this->hasMany(UserDataFlowLog::class);
- }
- public function dataModifyLogs(): HasMany
- {
- return $this->hasMany(UserDataModifyLog::class);
- }
- public function hourlyDataFlows(): HasMany
- {
- return $this->HasMany(UserHourlyDataFlow::class);
- }
- public function loginLogs(): HasMany
- {
- return $this->HasMany(UserLoginLog::class);
- }
- public function subscribe(): HasOne
- {
- return $this->hasOne(UserSubscribe::class);
- }
- public function subUrl()
- {
- return route('sub', $this->subscribe->code);
- }
- public function subscribeLogs(): HasManyThrough
- {
- return $this->hasManyThrough(UserSubscribeLog::class, UserSubscribe::class);
- }
- public function verify(): HasMany
- {
- return $this->hasMany(Verify::class);
- }
- public function inviter(): BelongsTo
- {
- return $this->belongsTo(__CLASS__);
- }
- public function invites(): HasMany
- {
- return $this->hasMany(Invite::class, 'inviter_id');
- }
- public function invitees(): HasMany
- {
- return $this->hasMany(__CLASS__, 'inviter_id');
- }
- public function getLevelNameAttribute(): string
- {
- return Level::whereLevel($this->attributes['level'])->first()->name;
- }
- public function getCreditAttribute()
- {
- return $this->attributes['credit'] / 100;
- }
- public function getTransferEnableFormattedAttribute()
- {
- return flowAutoShow($this->attributes['transfer_enable']);
- }
- public function getSpeedLimitAttribute()
- {
- return $this->attributes['speed_limit'] / Mbps;
- }
- public function getExpiredAtAttribute()
- {
- return $this->attributes['expired_at'];
- }
- public function getResetTimeAttribute()
- {
- return $this->attributes['reset_time'];
- }
- public function setPasswordAttribute($password)
- {
- return $this->attributes['password'] = Hash::make($password);
- }
- public function setCreditAttribute($value)
- {
- return $this->attributes['credit'] = $value * 100;
- }
- public function setSpeedLimitAttribute($value)
- {
- return $this->attributes['speed_limit'] = $value * Mbps;
- }
- public function scopeActiveUser($query)
- {
- return $query->where('status', '<>', -1)->whereEnable(1);
- }
- public function nodes()
- {
- if (!empty($this->attributes['user_group_id']) || $this->attributes['user_group_id']) {
- $query = $this->userGroup->nodes();
- } else {
- $query = Node::query();
- }
- return $query->whereStatus(1)->where('level', '<=', $this->attributes['level'] ?? 0);
- }
- public function userGroup(): BelongsTo
- {
- return $this->belongsTo(UserGroup::class);
- }
- public function getIsAvailableAttribute(): bool
- {
- return ! $this->ban_time && $this->transfer_enable && $this->expired_at > time();
- }
- public function updateCredit(float $credit): bool
- {
- $this->credit += $credit;
- return $this->credit >= 0 && $this->save();
- }
- public function incrementData(int $data): bool
- {// 添加用户流量
- $this->transfer_enable += $data;
- return $this->save();
- }
- public function isNotCompleteOrderByUserId(int $userId): bool
- { // 添加用户余额
- return Order::uid($userId)->whereIn('status', [0, 1])->exists();
- }
- public function trafficFetch(int $u, int $d): bool
- {
- $this->u += $u;
- $this->d += $d;
- return $this->save();
- }
- public function expired_status(): int
- {
- $expired_status = 2; // 大于一个月过期
- if ($this->expired_at < date('Y-m-d H:i:s')) {
- $expired_status = -1; // 已过期
- } elseif ($this->expired_at === date('Y-m-d H:i:s')) {
- $expired_status = 0; // 今天过期
- } elseif ($this->expired_at > date('Y-m-d H:i:s') && $this->expired_at <= date('Y-m-d H:i:s', strtotime('+30 days'))) {
- $expired_status = 1; // 最近一个月过期
- }
- return $expired_status;
- }
- public function isTrafficWarning(): bool
- {// 流量异常警告
- return $this->recentTrafficUsed() >= (sysConfig('traffic_ban_value') * GB);
- }
- public function recentTrafficUsed()
- {
- return UserHourlyDataFlow::userRecentUsed($this->id)->sum('total');
- }
- public function activePayingUser()
- { //付费用户判断
- return $this->orders()->active()->where('origin_amount', '>', 0)->exists();
- }
- public function orders(): HasMany
- {
- return $this->hasMany(Order::class);
- }
- public function getJWTIdentifier()
- {
- return $this->getKey();
- }
- public function getJWTCustomClaims()
- {
- return [];
- }
- }
|