User.php 6.9 KB

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