User.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. namespace App\Models;
  3. use Hash;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. use Illuminate\Database\Eloquent\Relations\HasMany;
  6. use Illuminate\Database\Eloquent\Relations\HasManyThrough;
  7. use Illuminate\Database\Eloquent\Relations\HasOne;
  8. use Illuminate\Foundation\Auth\User as Authenticatable;
  9. use Illuminate\Notifications\Notifiable;
  10. use Kyslik\ColumnSortable\Sortable;
  11. use Spatie\Permission\Traits\HasRoles;
  12. use Tymon\JWTAuth\Contracts\JWTSubject;
  13. /**
  14. * 用户信息.
  15. */
  16. class User extends Authenticatable implements JWTSubject
  17. {
  18. use Notifiable, HasRoles, Sortable;
  19. public $sortable = ['id', 'credit', 'port', 't', 'expired_at'];
  20. protected $table = 'user';
  21. protected $casts = ['expired_at' => 'date:Y-m-d', 'reset_time' => 'date:Y-m-d', 'ban_time' => 'date:Y-m-d'];
  22. protected $dates = ['expired_at', 'reset_time'];
  23. protected $guarded = [];
  24. public function usedTrafficPercentage()
  25. {
  26. return round(($this->used_traffic) / $this->transfer_enable, 2);
  27. }
  28. public function getUsedTrafficAttribute(): int
  29. {
  30. return $this->d + $this->u;
  31. }
  32. public function getTelegramUserIdAttribute()
  33. {
  34. $telegram = $this->userAuths()->whereType('telegram')->first();
  35. return $telegram->identifier ?? null;
  36. }
  37. public function userAuths(): HasMany
  38. {
  39. return $this->hasMany(UserOauth::class);
  40. }
  41. public function profile()
  42. {
  43. return [
  44. 'id' => $this->id,
  45. 'nickname' => $this->nickname,
  46. 'account' => $this->username,
  47. 'port' => $this->port,
  48. 'passwd' => $this->passwd,
  49. 'uuid' => $this->vmess_id,
  50. 'transfer_enable' => $this->transfer_enable,
  51. 'u' => $this->u,
  52. 'd' => $this->d,
  53. 't' => $this->t,
  54. 'enable' => $this->enable,
  55. 'speed_limit' => $this->speed_limit,
  56. 'credit' => $this->credit,
  57. 'expired_at' => $this->expired_at,
  58. 'ban_time' => $this->ban_time,
  59. 'level' => $this->level_name,
  60. 'group' => $this->userGroup->name ?? null,
  61. 'last_login' => $this->last_login,
  62. 'reset_time' => $this->reset_time,
  63. 'invite_num' => $this->invite_num,
  64. 'status' => $this->status,
  65. ];
  66. }
  67. public function onlineIpLogs(): HasMany
  68. {
  69. return $this->hasMany(NodeOnlineIp::class);
  70. }
  71. public function payments(): HasMany
  72. {
  73. return $this->hasMany(Payment::class);
  74. }
  75. public function commissionSettlements(): HasMany
  76. {
  77. return $this->hasMany(ReferralApply::class);
  78. }
  79. public function commissionLogs(): HasMany
  80. {
  81. return $this->hasMany(ReferralLog::class, 'inviter_id');
  82. }
  83. public function ruleLogs(): HasMany
  84. {
  85. return $this->hasMany(RuleLog::class);
  86. }
  87. public function tickets(): HasMany
  88. {
  89. return $this->hasMany(Ticket::class);
  90. }
  91. public function ticketReplies(): HasMany
  92. {
  93. return $this->hasMany(TicketReply::class);
  94. }
  95. public function banedLogs(): HasMany
  96. {
  97. return $this->hasMany(UserBanedLog::class);
  98. }
  99. public function creditLogs(): HasMany
  100. {
  101. return $this->hasMany(UserCreditLog::class);
  102. }
  103. public function dailyDataFlows(): HasMany
  104. {
  105. return $this->hasMany(UserDailyDataFlow::class);
  106. }
  107. public function dataFlowLogs(): HasMany
  108. {
  109. return $this->hasMany(UserDataFlowLog::class);
  110. }
  111. public function dataModifyLogs(): HasMany
  112. {
  113. return $this->hasMany(UserDataModifyLog::class);
  114. }
  115. public function hourlyDataFlows(): HasMany
  116. {
  117. return $this->HasMany(UserHourlyDataFlow::class);
  118. }
  119. public function loginLogs(): HasMany
  120. {
  121. return $this->HasMany(UserLoginLog::class);
  122. }
  123. public function subscribe(): HasOne
  124. {
  125. return $this->hasOne(UserSubscribe::class);
  126. }
  127. public function subUrl()
  128. {
  129. return route('sub', $this->subscribe->code);
  130. }
  131. public function subscribeLogs(): HasManyThrough
  132. {
  133. return $this->hasManyThrough(UserSubscribeLog::class, UserSubscribe::class);
  134. }
  135. public function verify(): HasMany
  136. {
  137. return $this->hasMany(Verify::class);
  138. }
  139. public function inviter(): BelongsTo
  140. {
  141. return $this->belongsTo(__CLASS__);
  142. }
  143. public function invites(): HasMany
  144. {
  145. return $this->hasMany(Invite::class, 'inviter_id');
  146. }
  147. public function invitees(): HasMany
  148. {
  149. return $this->hasMany(__CLASS__, 'inviter_id');
  150. }
  151. public function getLevelNameAttribute(): string
  152. {
  153. return Level::whereLevel($this->attributes['level'])->first()->name;
  154. }
  155. public function getCreditAttribute()
  156. {
  157. return $this->attributes['credit'] / 100;
  158. }
  159. public function getTransferEnableFormattedAttribute()
  160. {
  161. return flowAutoShow($this->attributes['transfer_enable']);
  162. }
  163. public function getSpeedLimitAttribute()
  164. {
  165. return $this->attributes['speed_limit'] / Mbps;
  166. }
  167. public function getExpiredAtAttribute()
  168. {
  169. return $this->attributes['expired_at'];
  170. }
  171. public function getResetTimeAttribute()
  172. {
  173. return $this->attributes['reset_time'];
  174. }
  175. public function setPasswordAttribute($password)
  176. {
  177. return $this->attributes['password'] = Hash::make($password);
  178. }
  179. public function setCreditAttribute($value)
  180. {
  181. return $this->attributes['credit'] = $value * 100;
  182. }
  183. public function setSpeedLimitAttribute($value)
  184. {
  185. return $this->attributes['speed_limit'] = $value * Mbps;
  186. }
  187. public function scopeActiveUser($query)
  188. {
  189. return $query->where('status', '<>', -1)->whereEnable(1);
  190. }
  191. public function scopeBannedUser($query)
  192. {
  193. return $query->where('status', '>=', 0)->whereEnable(0);
  194. }
  195. public function nodes()
  196. {
  197. if ($this->attributes['user_group_id']) {
  198. $query = $this->userGroup->nodes();
  199. } else {
  200. $query = Node::query();
  201. }
  202. return $query->whereStatus(1)->where('level', '<=', $this->attributes['level'] ?? 0);
  203. }
  204. public function userGroup(): BelongsTo
  205. {
  206. return $this->belongsTo(UserGroup::class);
  207. }
  208. public function getIsAvailableAttribute(): bool
  209. {
  210. return ! $this->ban_time && $this->transfer_enable && $this->expired_at > time();
  211. }
  212. public function updateCredit(float $credit): bool
  213. {
  214. $this->credit += $credit;
  215. return $this->credit >= 0 && $this->save();
  216. }
  217. public function incrementData(int $data): bool
  218. { // 添加用户流量
  219. $this->transfer_enable += $data;
  220. return $this->save();
  221. }
  222. public function isNotCompleteOrderByUserId(int $userId): bool
  223. { // 添加用户余额
  224. return Order::uid($userId)->whereIn('status', [0, 1])->exists();
  225. }
  226. public function trafficFetch(int $u, int $d): bool
  227. {
  228. $this->u += $u;
  229. $this->d += $d;
  230. return $this->save();
  231. }
  232. public function expired_status(): int
  233. {
  234. $expired_status = 2; // 大于一个月过期
  235. if ($this->expired_at < date('Y-m-d')) {
  236. $expired_status = -1; // 已过期
  237. } elseif ($this->expired_at === date('Y-m-d')) {
  238. $expired_status = 0; // 今天过期
  239. } elseif ($this->expired_at > date('Y-m-d') && $this->expired_at <= date('Y-m-d', strtotime('30 days'))) {
  240. $expired_status = 1; // 最近一个月过期
  241. }
  242. return $expired_status;
  243. }
  244. public function isTrafficWarning(): bool
  245. { // 流量异常警告
  246. return $this->recentTrafficUsed() >= (sysConfig('traffic_ban_value') * GB);
  247. }
  248. public function recentTrafficUsed()
  249. {
  250. return UserHourlyDataFlow::userRecentUsed($this->id)->sum('total');
  251. }
  252. public function activePayingUser()
  253. { //付费用户判断
  254. return $this->orders()->active()->where('origin_amount', '>', 0)->exists();
  255. }
  256. public function orders(): HasMany
  257. {
  258. return $this->hasMany(Order::class);
  259. }
  260. public function getJWTIdentifier()
  261. {
  262. return $this->getKey();
  263. }
  264. public function getJWTCustomClaims()
  265. {
  266. return [];
  267. }
  268. public function routeNotificationForTelegram()
  269. {
  270. return $this->telegram_user_id;
  271. }
  272. }