User.php 7.7 KB

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