User.php 8.1 KB

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