User.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. namespace App\Models;
  3. use App\Casts\data_rate;
  4. use App\Casts\money;
  5. use App\Observers\UserObserver;
  6. use App\Utils\Avatar;
  7. use App\Utils\Helpers;
  8. use DB;
  9. use Hash;
  10. use Illuminate\Database\Eloquent\Attributes\ObservedBy;
  11. use Illuminate\Database\Eloquent\Builder;
  12. use Illuminate\Database\Eloquent\Factories\HasFactory;
  13. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  14. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  15. use Illuminate\Database\Eloquent\Relations\HasMany;
  16. use Illuminate\Database\Eloquent\Relations\HasManyThrough;
  17. use Illuminate\Database\Eloquent\Relations\HasOne;
  18. use Illuminate\Foundation\Auth\User as Authenticatable;
  19. use Illuminate\Notifications\Notifiable;
  20. use Kyslik\ColumnSortable\Sortable;
  21. use Laravel\Sanctum\HasApiTokens;
  22. use Spatie\Permission\Traits\HasRoles;
  23. /**
  24. * 用户信息.
  25. */
  26. #[ObservedBy([UserObserver::class])]
  27. class User extends Authenticatable
  28. {
  29. use HasApiTokens, HasFactory, HasRoles, Notifiable, Sortable;
  30. public array $sortable = ['id', 'credit', 'port', 't', 'expired_at'];
  31. protected $table = 'user';
  32. protected $casts = ['credit' => money::class, 'speed_limit' => data_rate::class, 'expired_at' => 'date:Y-m-d', 'reset_time' => 'date:Y-m-d', 'ban_time' => 'date:Y-m-d'];
  33. protected $guarded = [];
  34. public function routeNotificationForMail($notification): string
  35. {
  36. return $this->username;
  37. }
  38. public function usedTrafficPercentage(): float
  39. {
  40. return round($this->used_traffic / $this->transfer_enable, 2);
  41. }
  42. public function getUsedTrafficAttribute(): int
  43. {
  44. return $this->d + $this->u;
  45. }
  46. public function getExpirationDateAttribute(): ?string
  47. {
  48. return $this->attributes['expired_at'];
  49. }
  50. public function getResetDateAttribute(): ?string
  51. {
  52. return $this->attributes['reset_time'];
  53. }
  54. public function getTelegramUserIdAttribute(): ?string
  55. {
  56. $telegram = $this->userAuths()->whereType('telegram')->first();
  57. return $telegram->identifier ?? null;
  58. }
  59. public function userAuths(): HasMany
  60. {
  61. return $this->hasMany(UserOauth::class);
  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(): string
  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->level)->first()->name;
  150. }
  151. public function getCreditTagAttribute(): string
  152. {
  153. return Helpers::getPriceTag($this->credit);
  154. }
  155. public function getTransferEnableFormattedAttribute(): string
  156. {
  157. return formatBytes($this->attributes['transfer_enable']);
  158. }
  159. public function setPasswordAttribute(string $password): string
  160. {
  161. return $this->attributes['password'] = Hash::make($password);
  162. }
  163. public function getAvatarAttribute(): string
  164. {
  165. $img = session('avatar_url_'.$this->id);
  166. if ($img) {
  167. return $img;
  168. }
  169. if ($this->qq) {
  170. $img = Avatar::getQQAvatar($this->qq);
  171. } elseif (stripos(strtolower($this->username), '@qq.com') !== false) {
  172. $img = Avatar::getQQAvatar($this->username);
  173. } else {
  174. $img = Avatar::getRandomAvatar($this->username);
  175. }
  176. session(['avatar_url_'.$this->id => $img]);
  177. return $img;
  178. }
  179. public function scopeActiveUser(Builder $query): Builder
  180. {
  181. return $query->where('status', '<>', -1)->whereEnable(1);
  182. }
  183. public function scopeBannedUser(Builder $query): Builder
  184. {
  185. return $query->where('status', '<>', -1)->whereEnable(0);
  186. }
  187. public function nodes(?int $userLevel = null, ?int $userGroupId = null): Node|Builder|BelongsToMany
  188. {
  189. if ($userGroupId === null && $this->user_group_id) { // 使用默认的用户分组
  190. $query = $this->userGroup->nodes();
  191. } elseif ($userGroupId) { // 使用给的用户分组
  192. $query = UserGroup::findOrFail($userGroupId)->nodes();
  193. } else { // 无用户分组
  194. $query = Node::query();
  195. }
  196. return $query->whereStatus(1)->where('level', '<=', $userLevel ?? $this->level ?? 0);
  197. }
  198. public function userGroup(): BelongsTo
  199. {
  200. return $this->belongsTo(UserGroup::class);
  201. }
  202. public function getIsAvailableAttribute(): bool
  203. {
  204. return ! $this->ban_time && $this->transfer_enable && $this->expired_at > time();
  205. }
  206. public function updateCredit(float $credit): bool
  207. {
  208. $this->credit += $credit;
  209. return $this->credit >= 0 && $this->save();
  210. }
  211. public function incrementData(int $data): bool
  212. { // 添加用户流量
  213. $this->transfer_enable += $data;
  214. return $this->save();
  215. }
  216. public function isNotCompleteOrderByUserId(int $userId): bool
  217. {
  218. return Order::uid($userId)->whereIn('status', [0, 1])->exists();
  219. }
  220. public function trafficFetch(int $u, int $d): bool
  221. {
  222. $this->u += $u;
  223. $this->d += $d;
  224. return $this->save();
  225. }
  226. public function expiration_status(): int
  227. {
  228. $today = date('Y-m-d');
  229. $nextMonth = date('Y-m-d', strtotime('next month'));
  230. if ($this->expiration_date < $today) {
  231. $status = 0; // 已过期
  232. } elseif ($this->expiration_date === $today) {
  233. $status = 1; // 今日过期
  234. } elseif ($this->expiration_date <= $nextMonth) {
  235. $status = 2; // 一个月内过期
  236. }
  237. return $status ?? 3;
  238. }
  239. public function isTrafficWarning(): bool
  240. { // 流量异常警告
  241. return ((int) sysConfig('traffic_ban_value') * GiB) <= $this->recentTrafficUsed();
  242. }
  243. public function recentTrafficUsed()
  244. {
  245. return UserHourlyDataFlow::userRecentUsed($this->id)->sum(DB::raw('u + d'));
  246. }
  247. public function orders(): HasMany
  248. {
  249. return $this->hasMany(Order::class);
  250. }
  251. public function paidOrders()
  252. {
  253. return $this->hasMany(Order::class)->where('status', 2)->whereNotNull('goods_id')->where('is_expire', 0)->where('amount', '>', 0);
  254. }
  255. public function routeNotificationForTelegram()
  256. {
  257. return $this->telegram_user_id;
  258. }
  259. }