User.php 8.1 KB

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