User.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace App\Models;
  3. use App\Utils\Helpers;
  4. use App\Utils\QQInfo;
  5. use Hash;
  6. use Illuminate\Database\Eloquent\Factories\HasFactory;
  7. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  8. use Illuminate\Database\Eloquent\Relations\HasMany;
  9. use Illuminate\Database\Eloquent\Relations\HasManyThrough;
  10. use Illuminate\Database\Eloquent\Relations\HasOne;
  11. use Illuminate\Foundation\Auth\User as Authenticatable;
  12. use Illuminate\Notifications\Notifiable;
  13. use Kyslik\ColumnSortable\Sortable;
  14. use Laravel\Sanctum\HasApiTokens;
  15. use Spatie\Permission\Traits\HasRoles;
  16. /**
  17. * 用户信息.
  18. */
  19. class User extends Authenticatable
  20. {
  21. use HasApiTokens, HasFactory, Notifiable, HasRoles, Sortable;
  22. public $sortable = ['id', 'credit', 'port', 't', 'expired_at'];
  23. protected $table = 'user';
  24. protected $casts = ['expired_at' => 'date:Y-m-d', 'reset_time' => 'date:Y-m-d', 'ban_time' => 'date:Y-m-d'];
  25. protected $guarded = [];
  26. public function routeNotificationForMail($notification)
  27. {
  28. return $this->username;
  29. }
  30. public function usedTrafficPercentage(): float
  31. {
  32. return round($this->used_traffic / $this->transfer_enable, 2);
  33. }
  34. public function getUsedTrafficAttribute(): int
  35. {
  36. return $this->d + $this->u;
  37. }
  38. public function getExpirationDateAttribute()
  39. {
  40. return $this->attributes['expired_at'];
  41. }
  42. public function getResetDateAttribute()
  43. {
  44. return $this->attributes['reset_time'];
  45. }
  46. public function getTelegramUserIdAttribute()
  47. {
  48. $telegram = $this->userAuths()->whereType('telegram')->first();
  49. return $telegram->identifier ?? null;
  50. }
  51. public function userAuths(): HasMany
  52. {
  53. return $this->hasMany(UserOauth::class);
  54. }
  55. public function onlineIpLogs(): HasMany
  56. {
  57. return $this->hasMany(NodeOnlineIp::class);
  58. }
  59. public function payments(): HasMany
  60. {
  61. return $this->hasMany(Payment::class);
  62. }
  63. public function commissionSettlements(): HasMany
  64. {
  65. return $this->hasMany(ReferralApply::class);
  66. }
  67. public function commissionLogs(): HasMany
  68. {
  69. return $this->hasMany(ReferralLog::class, 'inviter_id');
  70. }
  71. public function ruleLogs(): HasMany
  72. {
  73. return $this->hasMany(RuleLog::class);
  74. }
  75. public function tickets(): HasMany
  76. {
  77. return $this->hasMany(Ticket::class);
  78. }
  79. public function ticketReplies(): HasMany
  80. {
  81. return $this->hasMany(TicketReply::class);
  82. }
  83. public function banedLogs(): HasMany
  84. {
  85. return $this->hasMany(UserBanedLog::class);
  86. }
  87. public function creditLogs(): HasMany
  88. {
  89. return $this->hasMany(UserCreditLog::class);
  90. }
  91. public function dailyDataFlows(): HasMany
  92. {
  93. return $this->hasMany(UserDailyDataFlow::class);
  94. }
  95. public function dataFlowLogs(): HasMany
  96. {
  97. return $this->hasMany(UserDataFlowLog::class);
  98. }
  99. public function dataModifyLogs(): HasMany
  100. {
  101. return $this->hasMany(UserDataModifyLog::class);
  102. }
  103. public function hourlyDataFlows(): HasMany
  104. {
  105. return $this->HasMany(UserHourlyDataFlow::class);
  106. }
  107. public function loginLogs(): HasMany
  108. {
  109. return $this->HasMany(UserLoginLog::class);
  110. }
  111. public function subscribe(): HasOne
  112. {
  113. return $this->hasOne(UserSubscribe::class);
  114. }
  115. public function subUrl(): string
  116. {
  117. return route('sub', $this->subscribe->code);
  118. }
  119. public function subscribeLogs(): HasManyThrough
  120. {
  121. return $this->hasManyThrough(UserSubscribeLog::class, UserSubscribe::class);
  122. }
  123. public function verify(): HasMany
  124. {
  125. return $this->hasMany(Verify::class);
  126. }
  127. public function inviter(): BelongsTo
  128. {
  129. return $this->belongsTo(__CLASS__);
  130. }
  131. public function invites(): HasMany
  132. {
  133. return $this->hasMany(Invite::class, 'inviter_id');
  134. }
  135. public function invitees(): HasMany
  136. {
  137. return $this->hasMany(__CLASS__, 'inviter_id');
  138. }
  139. public function getLevelNameAttribute(): string
  140. {
  141. return Level::whereLevel($this->attributes['level'])->first()->name;
  142. }
  143. public function getCreditAttribute()
  144. {
  145. return $this->attributes['credit'] / 100;
  146. }
  147. public function setCreditAttribute($value)
  148. {
  149. return $this->attributes['credit'] = $value * 100;
  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 getSpeedLimitAttribute()
  160. {
  161. return $this->attributes['speed_limit'] / Mbps;
  162. }
  163. public function setPasswordAttribute($password): string
  164. {
  165. return $this->attributes['password'] = Hash::make($password);
  166. }
  167. public function setSpeedLimitAttribute($value)
  168. {
  169. return $this->attributes['speed_limit'] = $value * Mbps;
  170. }
  171. public function getAvatarAttribute(): string
  172. {
  173. if ($this->qq) {
  174. $url = QQInfo::getQQAvatar($this->qq);
  175. } elseif (stripos(strtolower($this->username), '@qq.com') !== false) {
  176. $url = QQInfo::getQQAvatar($this->username);
  177. } else {
  178. // $url = 'https://gravatar.loli.net/avatar/'.md5(strtolower(trim($this->username)))."?&d=identicon";
  179. // $url = 'https://robohash.org/'.md5(strtolower(trim($this->username))).'?set=set4&bgset=bg2&size=400x400';
  180. $url = 'https://api.btstu.cn/sjtx/api.php?lx=c1&format=images&method=zsy';
  181. }
  182. return $url;
  183. }
  184. public function scopeActiveUser($query)
  185. {
  186. return $query->where('status', '<>', -1)->whereEnable(1);
  187. }
  188. public function scopeBannedUser($query)
  189. {
  190. return $query->where('status', '<>', -1)->whereEnable(0);
  191. }
  192. public function nodes($userLevel = -1, $userGroupId = -1)
  193. {
  194. if ($userGroupId === -1 && $this->attributes['user_group_id']) {
  195. $query = $this->userGroup->nodes();
  196. } elseif ($userGroupId !== -1 && $userGroupId) {
  197. $query = UserGroup::findOrFail($userGroupId)->nodes();
  198. } else {
  199. $query = Node::query();
  200. }
  201. return $query->whereStatus(1)->where('level', '<=', $userLevel !== -1 && $userLevel !== null ? $userLevel : $this->attributes['level'] ?? 0);
  202. }
  203. public function userGroup(): BelongsTo
  204. {
  205. return $this->belongsTo(UserGroup::class);
  206. }
  207. public function getIsAvailableAttribute(): bool
  208. {
  209. return ! $this->ban_time && $this->transfer_enable && $this->expired_at > time();
  210. }
  211. public function updateCredit(float $credit): bool
  212. {
  213. $this->credit += $credit;
  214. return $this->credit >= 0 && $this->save();
  215. }
  216. public function incrementData(int $data): bool
  217. { // 添加用户流量
  218. $this->transfer_enable += $data;
  219. return $this->save();
  220. }
  221. public function isNotCompleteOrderByUserId(int $userId): bool
  222. { // 添加用户余额
  223. return Order::uid($userId)->whereIn('status', [0, 1])->exists();
  224. }
  225. public function trafficFetch(int $u, int $d): bool
  226. {
  227. $this->u += $u;
  228. $this->d += $d;
  229. return $this->save();
  230. }
  231. public function expiration_status(): int
  232. {
  233. $today = date('Y-m-d');
  234. $nextMonth = date('Y-m-d', strtotime('next month'));
  235. if ($this->expiration_date < $today) {
  236. $status = 0; // 已过期
  237. } elseif ($this->expiration_date === $today) {
  238. $status = 1; // 今日过期
  239. } elseif ($this->expiration_date <= $nextMonth) {
  240. $status = 2; // 一个月内过期
  241. }
  242. return $status ?? 3;
  243. }
  244. public function isTrafficWarning(): bool
  245. { // 流量异常警告
  246. return (sysConfig('traffic_ban_value') * GB) <= $this->recentTrafficUsed();
  247. }
  248. public function recentTrafficUsed()
  249. {
  250. return UserHourlyDataFlow::userRecentUsed($this->id)->sum('total');
  251. }
  252. public function orders(): HasMany
  253. {
  254. return $this->hasMany(Order::class);
  255. }
  256. public function routeNotificationForTelegram()
  257. {
  258. return $this->telegram_user_id;
  259. }
  260. }