User.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. * @property int $id
  16. * @property string $username 昵称
  17. * @property string $email 邮箱
  18. * @property string $password 密码
  19. * @property int $port 代理端口
  20. * @property string $passwd 代理密码
  21. * @property string $vmess_id
  22. * @property int $transfer_enable 可用流量,单位字节,默认1TiB
  23. * @property int $u 已上传流量,单位字节
  24. * @property int $d 已下载流量,单位字节
  25. * @property int|null $t 最后使用时间
  26. * @property string|null $ip 最后连接IP
  27. * @property int $enable 代理状态
  28. * @property string $method 加密方式
  29. * @property string $protocol 协议
  30. * @property string|null $protocol_param 协议参数
  31. * @property string $obfs 混淆
  32. * @property int $speed_limit 用户限速,为0表示不限速,单位Byte
  33. * @property string|null $wechat 微信
  34. * @property string|null $qq QQ
  35. * @property int $credit 余额,单位分
  36. * @property \Illuminate\Support\Carbon $expired_at 过期时间
  37. * @property int|null $ban_time 封禁到期时间
  38. * @property string|null $remark 备注
  39. * @property int $level 等级,默认0级
  40. * @property int|null $user_group_id 所属分组
  41. * @property string $reg_ip 注册IP
  42. * @property int $last_login 最后登录时间
  43. * @property int|null $inviter_id 邀请人
  44. * @property \Illuminate\Support\Carbon|null $reset_time 流量重置日期
  45. * @property int $invite_num 可生成邀请码数
  46. * @property int $status 状态:-1-禁用、0-未激活、1-正常
  47. * @property string|null $remember_token
  48. * @property \Illuminate\Support\Carbon $created_at 创建时间
  49. * @property \Illuminate\Support\Carbon $updated_at 最后更新时间
  50. * @property int|null $client_limit 用户链接的设备
  51. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserBanedLog[] $banedLogs
  52. * @property-read int|null $baned_logs_count
  53. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ReferralLog[] $commissionLogs
  54. * @property-read int|null $commission_logs_count
  55. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ReferralApply[] $commissionSettlements
  56. * @property-read int|null $commission_settlements_count
  57. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserCreditLog[] $creditLogs
  58. * @property-read int|null $credit_logs_count
  59. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserDailyDataFlow[] $dailyDataFlows
  60. * @property-read int|null $daily_data_flows_count
  61. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserDataFlowLog[] $dataFlowLogs
  62. * @property-read int|null $data_flow_logs_count
  63. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserDataModifyLog[] $dataModifyLogs
  64. * @property-read int|null $data_modify_logs_count
  65. * @property-read bool $is_available
  66. * @property-read string $level_name
  67. * @property-read mixed $transfer_enable_formatted
  68. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserHourlyDataFlow[] $hourlyDataFlows
  69. * @property-read int|null $hourly_data_flows_count
  70. * @property-read \Illuminate\Database\Eloquent\Collection|User[] $invitees
  71. * @property-read int|null $invitees_count
  72. * @property-read User|null $inviter
  73. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Invite[] $invites
  74. * @property-read int|null $invites_count
  75. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserLoginLog[] $loginLogs
  76. * @property-read int|null $login_logs_count
  77. * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
  78. * @property-read int|null $notifications_count
  79. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\NodeOnlineIp[] $onlineIpLogs
  80. * @property-read int|null $online_ip_logs_count
  81. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Order[] $orders
  82. * @property-read int|null $orders_count
  83. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Payment[] $payments
  84. * @property-read int|null $payments_count
  85. * @property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Permission\Models\Permission[] $permissions
  86. * @property-read int|null $permissions_count
  87. * @property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Permission\Models\Role[] $roles
  88. * @property-read int|null $roles_count
  89. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\RuleLog[] $ruleLogs
  90. * @property-read int|null $rule_logs_count
  91. * @property-read \App\Models\UserSubscribe|null $subscribe
  92. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserSubscribeLog[] $subscribeLogs
  93. * @property-read int|null $subscribe_logs_count
  94. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\TicketReply[] $ticketReplies
  95. * @property-read int|null $ticket_replies_count
  96. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Ticket[] $tickets
  97. * @property-read int|null $tickets_count
  98. * @property-read \App\Models\UserGroup|null $userGroup
  99. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Verify[] $verify
  100. * @property-read int|null $verify_count
  101. * @method static \Illuminate\Database\Eloquent\Builder|User activeUser()
  102. * @method static \Illuminate\Database\Eloquent\Builder|User newModelQuery()
  103. * @method static \Illuminate\Database\Eloquent\Builder|User newQuery()
  104. * @method static \Illuminate\Database\Eloquent\Builder|User permission($permissions)
  105. * @method static \Illuminate\Database\Eloquent\Builder|User query()
  106. * @method static \Illuminate\Database\Eloquent\Builder|User role($roles, $guard = null)
  107. * @method static \Illuminate\Database\Eloquent\Builder|User whereBanTime($value)
  108. * @method static \Illuminate\Database\Eloquent\Builder|User whereClientLimit($value)
  109. * @method static \Illuminate\Database\Eloquent\Builder|User whereCreatedAt($value)
  110. * @method static \Illuminate\Database\Eloquent\Builder|User whereCredit($value)
  111. * @method static \Illuminate\Database\Eloquent\Builder|User whereD($value)
  112. * @method static \Illuminate\Database\Eloquent\Builder|User whereEmail($value)
  113. * @method static \Illuminate\Database\Eloquent\Builder|User whereEnable($value)
  114. * @method static \Illuminate\Database\Eloquent\Builder|User whereExpiredAt($value)
  115. * @method static \Illuminate\Database\Eloquent\Builder|User whereId($value)
  116. * @method static \Illuminate\Database\Eloquent\Builder|User whereInviteNum($value)
  117. * @method static \Illuminate\Database\Eloquent\Builder|User whereInviterId($value)
  118. * @method static \Illuminate\Database\Eloquent\Builder|User whereIp($value)
  119. * @method static \Illuminate\Database\Eloquent\Builder|User whereLastLogin($value)
  120. * @method static \Illuminate\Database\Eloquent\Builder|User whereLevel($value)
  121. * @method static \Illuminate\Database\Eloquent\Builder|User whereMethod($value)
  122. * @method static \Illuminate\Database\Eloquent\Builder|User whereObfs($value)
  123. * @method static \Illuminate\Database\Eloquent\Builder|User wherePasswd($value)
  124. * @method static \Illuminate\Database\Eloquent\Builder|User wherePassword($value)
  125. * @method static \Illuminate\Database\Eloquent\Builder|User wherePort($value)
  126. * @method static \Illuminate\Database\Eloquent\Builder|User whereProtocol($value)
  127. * @method static \Illuminate\Database\Eloquent\Builder|User whereProtocolParam($value)
  128. * @method static \Illuminate\Database\Eloquent\Builder|User whereQq($value)
  129. * @method static \Illuminate\Database\Eloquent\Builder|User whereRegIp($value)
  130. * @method static \Illuminate\Database\Eloquent\Builder|User whereRemark($value)
  131. * @method static \Illuminate\Database\Eloquent\Builder|User whereRememberToken($value)
  132. * @method static \Illuminate\Database\Eloquent\Builder|User whereResetTime($value)
  133. * @method static \Illuminate\Database\Eloquent\Builder|User whereSpeedLimit($value)
  134. * @method static \Illuminate\Database\Eloquent\Builder|User whereStatus($value)
  135. * @method static \Illuminate\Database\Eloquent\Builder|User whereT($value)
  136. * @method static \Illuminate\Database\Eloquent\Builder|User whereTransferEnable($value)
  137. * @method static \Illuminate\Database\Eloquent\Builder|User whereU($value)
  138. * @method static \Illuminate\Database\Eloquent\Builder|User whereUpdatedAt($value)
  139. * @method static \Illuminate\Database\Eloquent\Builder|User whereUserGroupId($value)
  140. * @method static \Illuminate\Database\Eloquent\Builder|User whereUsername($value)
  141. * @method static \Illuminate\Database\Eloquent\Builder|User whereVmessId($value)
  142. * @method static \Illuminate\Database\Eloquent\Builder|User whereWechat($value)
  143. * @mixin \Eloquent
  144. */
  145. class User extends Authenticatable implements JWTSubject
  146. {
  147. use Notifiable, HasRoles;
  148. protected $table = 'user';
  149. //protected $casts = ['expired_at' => 'date:Y-m-d', 'reset_time' => 'date:Y-m-d', 'ban_time' => 'date:Y-m-d'];
  150. protected $dates = ['expired_at', 'reset_time'];
  151. protected $guarded = [];
  152. public function usedTrafficPercentage()
  153. {
  154. return round(($this->usedTraffic()) / $this->transfer_enable, 2);
  155. }
  156. public function usedTraffic(): int
  157. {
  158. return $this->d + $this->u;
  159. }
  160. public function profile()
  161. {
  162. $totalTransfer = $this->transfer_enable;
  163. $usedTransfer = $this->usedTraffic();
  164. $unusedTraffic = $totalTransfer - $usedTransfer > 0 ? $totalTransfer - $usedTransfer : 0;
  165. $expireTime = $this->expired_at;
  166. return [
  167. 'id' => $this->id,
  168. 'nickname' => $this->username,
  169. 'account' => $this->email,
  170. 'port' => $this->port,
  171. 'passwd' => $this->passwd,
  172. 'uuid' => $this->vmess_id,
  173. 'transfer_enable' => $this->transfer_enable,
  174. 'unusedTraffic' => flowAutoShow($unusedTraffic),
  175. 'u' => $this->u,
  176. 'd' => $this->d,
  177. 't' => $this->t,
  178. 'enable' => $this->enable,
  179. 'speed_limit' => $this->speed_limit,
  180. 'credit' => $this->credit,
  181. 'expired_at' => $this->expired_at,
  182. 'ban_time' => $this->ban_time,
  183. 'level' => $this->level_name,
  184. 'group' => $this->userGroup->name ?? null,
  185. 'last_login' => $this->last_login,
  186. 'reset_time' => $this->reset_time,
  187. 'invite_num' => $this->invite_num,
  188. 'user_group_id'=> $this->user_group_id,
  189. 'status' => $this->status,
  190. 'code' => $this->subscribe->code,
  191. ];
  192. }
  193. public function onlineIpLogs(): HasMany
  194. {
  195. return $this->hasMany(NodeOnlineIp::class);
  196. }
  197. public function payments(): HasMany
  198. {
  199. return $this->hasMany(Payment::class);
  200. }
  201. public function commissionSettlements(): HasMany
  202. {
  203. return $this->hasMany(ReferralApply::class);
  204. }
  205. public function commissionLogs(): HasMany
  206. {
  207. return $this->hasMany(ReferralLog::class, 'inviter_id');
  208. }
  209. public function ruleLogs(): HasMany
  210. {
  211. return $this->hasMany(RuleLog::class);
  212. }
  213. public function tickets(): HasMany
  214. {
  215. return $this->hasMany(Ticket::class);
  216. }
  217. public function ticketReplies(): HasMany
  218. {
  219. return $this->hasMany(TicketReply::class);
  220. }
  221. public function banedLogs(): HasMany
  222. {
  223. return $this->hasMany(UserBanedLog::class);
  224. }
  225. public function creditLogs(): HasMany
  226. {
  227. return $this->hasMany(UserCreditLog::class);
  228. }
  229. public function dailyDataFlows(): HasMany
  230. {
  231. return $this->hasMany(UserDailyDataFlow::class);
  232. }
  233. public function dataFlowLogs(): HasMany
  234. {
  235. return $this->hasMany(UserDataFlowLog::class);
  236. }
  237. public function dataModifyLogs(): HasMany
  238. {
  239. return $this->hasMany(UserDataModifyLog::class);
  240. }
  241. public function hourlyDataFlows(): HasMany
  242. {
  243. return $this->HasMany(UserHourlyDataFlow::class);
  244. }
  245. public function loginLogs(): HasMany
  246. {
  247. return $this->HasMany(UserLoginLog::class);
  248. }
  249. public function subscribe(): HasOne
  250. {
  251. return $this->hasOne(UserSubscribe::class);
  252. }
  253. public function subUrl()
  254. {
  255. return route('sub', $this->subscribe->code);
  256. }
  257. public function subscribeLogs(): HasManyThrough
  258. {
  259. return $this->hasManyThrough(UserSubscribeLog::class, UserSubscribe::class);
  260. }
  261. public function verify(): HasMany
  262. {
  263. return $this->hasMany(Verify::class);
  264. }
  265. public function inviter(): BelongsTo
  266. {
  267. return $this->belongsTo(__CLASS__);
  268. }
  269. public function invites(): HasMany
  270. {
  271. return $this->hasMany(Invite::class, 'inviter_id');
  272. }
  273. public function invitees(): HasMany
  274. {
  275. return $this->hasMany(__CLASS__, 'inviter_id');
  276. }
  277. public function getLevelNameAttribute(): string
  278. {
  279. return Level::whereLevel($this->attributes['level'])->first()->name;
  280. }
  281. public function getCreditAttribute()
  282. {
  283. return $this->attributes['credit'] / 100;
  284. }
  285. public function getTransferEnableFormattedAttribute()
  286. {
  287. return flowAutoShow($this->attributes['transfer_enable']);
  288. }
  289. public function getSpeedLimitAttribute()
  290. {
  291. return $this->attributes['speed_limit'] / Mbps;
  292. }
  293. public function getExpiredAtAttribute()
  294. {
  295. return $this->attributes['expired_at'];
  296. }
  297. public function getResetTimeAttribute()
  298. {
  299. return $this->attributes['reset_time'];
  300. }
  301. public function setPasswordAttribute($password)
  302. {
  303. return $this->attributes['password'] = Hash::make($password);
  304. }
  305. public function setCreditAttribute($value)
  306. {
  307. return $this->attributes['credit'] = $value * 100;
  308. }
  309. public function setSpeedLimitAttribute($value)
  310. {
  311. return $this->attributes['speed_limit'] = $value * Mbps;
  312. }
  313. public function scopeActiveUser($query)
  314. {
  315. return $query->where('status', '<>', -1)->whereEnable(1);
  316. }
  317. public function nodes()
  318. {
  319. if (!empty($this->attributes['user_group_id']) || $this->attributes['user_group_id']) {
  320. $query = $this->userGroup->nodes();
  321. } else {
  322. $query = Node::query();
  323. }
  324. return $query->whereStatus(1)->where('level', '<=', $this->attributes['level'] ?? 0);
  325. }
  326. public function userGroup(): BelongsTo
  327. {
  328. return $this->belongsTo(UserGroup::class);
  329. }
  330. public function getIsAvailableAttribute(): bool
  331. {
  332. return ! $this->ban_time && $this->transfer_enable && $this->expired_at > time();
  333. }
  334. public function updateCredit(float $credit): bool
  335. {
  336. $this->credit += $credit;
  337. return $this->credit >= 0 && $this->save();
  338. }
  339. public function incrementData(int $data): bool
  340. {// 添加用户流量
  341. $this->transfer_enable += $data;
  342. return $this->save();
  343. }
  344. public function isNotCompleteOrderByUserId(int $userId): bool
  345. { // 添加用户余额
  346. return Order::uid($userId)->whereIn('status', [0, 1])->exists();
  347. }
  348. public function trafficFetch(int $u, int $d): bool
  349. {
  350. $this->u += $u;
  351. $this->d += $d;
  352. return $this->save();
  353. }
  354. public function expired_status(): int
  355. {
  356. $expired_status = 2; // 大于一个月过期
  357. if ($this->expired_at < date('Y-m-d H:i:s')) {
  358. $expired_status = -1; // 已过期
  359. } elseif ($this->expired_at === date('Y-m-d H:i:s')) {
  360. $expired_status = 0; // 今天过期
  361. } elseif ($this->expired_at > date('Y-m-d H:i:s') && $this->expired_at <= date('Y-m-d H:i:s', strtotime('+30 days'))) {
  362. $expired_status = 1; // 最近一个月过期
  363. }
  364. return $expired_status;
  365. }
  366. public function isTrafficWarning(): bool
  367. {// 流量异常警告
  368. return $this->recentTrafficUsed() >= (sysConfig('traffic_ban_value') * GB);
  369. }
  370. public function recentTrafficUsed()
  371. {
  372. return UserHourlyDataFlow::userRecentUsed($this->id)->sum('total');
  373. }
  374. public function activePayingUser()
  375. { //付费用户判断
  376. return $this->orders()->active()->where('origin_amount', '>', 0)->exists();
  377. }
  378. public function orders(): HasMany
  379. {
  380. return $this->hasMany(Order::class);
  381. }
  382. public function getJWTIdentifier()
  383. {
  384. return $this->getKey();
  385. }
  386. public function getJWTCustomClaims()
  387. {
  388. return [];
  389. }
  390. }