User.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Models;
  3. use Auth;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use Illuminate\Database\Eloquent\Relations\HasMany;
  6. use Illuminate\Database\Eloquent\Relations\HasOne;
  7. use Illuminate\Foundation\Auth\User as Authenticatable;
  8. use Illuminate\Notifications\Notifiable;
  9. /**
  10. * 用户信息
  11. *
  12. * @property int $id
  13. * @property string $username 昵称
  14. * @property string $email 邮箱
  15. * @property string $password 密码
  16. * @property int $port 代理端口
  17. * @property string $passwd 代理密码
  18. * @property string $vmess_id
  19. * @property int $transfer_enable 可用流量,单位字节,默认1TiB
  20. * @property int $u 已上传流量,单位字节
  21. * @property int $d 已下载流量,单位字节
  22. * @property int|null $t 最后使用时间
  23. * @property string|null $ip 最后连接IP
  24. * @property int $enable 代理状态
  25. * @property string $method 加密方式
  26. * @property string $protocol 协议
  27. * @property string|null $protocol_param 协议参数
  28. * @property string $obfs 混淆
  29. * @property int $speed_limit 用户限速,为0表示不限速,单位Byte
  30. * @property string|null $wechat 微信
  31. * @property string|null $qq QQ
  32. * @property int $credit 余额,单位分
  33. * @property mixed|null $enable_time 开通日期
  34. * @property mixed $expire_time 过期时间
  35. * @property int|null $ban_time 封禁到期时间
  36. * @property string|null $remark 备注
  37. * @property int $level 等级,默认0级
  38. * @property int $group_id 所属分组
  39. * @property int $is_admin 是否管理员:0-否、1-是
  40. * @property string $reg_ip 注册IP
  41. * @property int $last_login 最后登录时间
  42. * @property int $referral_uid 邀请人
  43. * @property mixed|null $reset_time 流量重置日期
  44. * @property int $invite_num 可生成邀请码数
  45. * @property int $status 状态:-1-禁用、0-未激活、1-正常
  46. * @property string|null $remember_token
  47. * @property \Illuminate\Support\Carbon $created_at 创建时间
  48. * @property \Illuminate\Support\Carbon $updated_at 最后更新时间
  49. * @property-read string $level_name
  50. * @property-read \App\Models\UserGroup|null $group
  51. * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
  52. * @property-read int|null $notifications_count
  53. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Payment[] $payment
  54. * @property-read int|null $payment_count
  55. * @property-read \App\Models\User|null $referral
  56. * @property-read \App\Models\UserSubscribe|null $subscribe
  57. * @method static Builder|User activeUser()
  58. * @method static Builder|User groupUserPermit($node_id = 0)
  59. * @method static Builder|User newModelQuery()
  60. * @method static Builder|User newQuery()
  61. * @method static Builder|User query()
  62. * @method static Builder|User uid()
  63. * @method static Builder|User whereBanTime($value)
  64. * @method static Builder|User whereCreatedAt($value)
  65. * @method static Builder|User whereCredit($value)
  66. * @method static Builder|User whereD($value)
  67. * @method static Builder|User whereEmail($value)
  68. * @method static Builder|User whereEnable($value)
  69. * @method static Builder|User whereEnableTime($value)
  70. * @method static Builder|User whereExpireTime($value)
  71. * @method static Builder|User whereGroupId($value)
  72. * @method static Builder|User whereId($value)
  73. * @method static Builder|User whereInviteNum($value)
  74. * @method static Builder|User whereIp($value)
  75. * @method static Builder|User whereIsAdmin($value)
  76. * @method static Builder|User whereLastLogin($value)
  77. * @method static Builder|User whereLevel($value)
  78. * @method static Builder|User whereMethod($value)
  79. * @method static Builder|User whereObfs($value)
  80. * @method static Builder|User wherePasswd($value)
  81. * @method static Builder|User wherePassword($value)
  82. * @method static Builder|User wherePort($value)
  83. * @method static Builder|User whereProtocol($value)
  84. * @method static Builder|User whereProtocolParam($value)
  85. * @method static Builder|User whereQq($value)
  86. * @method static Builder|User whereReferralUid($value)
  87. * @method static Builder|User whereRegIp($value)
  88. * @method static Builder|User whereRemark($value)
  89. * @method static Builder|User whereRememberToken($value)
  90. * @method static Builder|User whereResetTime($value)
  91. * @method static Builder|User whereSpeedLimit($value)
  92. * @method static Builder|User whereStatus($value)
  93. * @method static Builder|User whereT($value)
  94. * @method static Builder|User whereTransferEnable($value)
  95. * @method static Builder|User whereU($value)
  96. * @method static Builder|User whereUpdatedAt($value)
  97. * @method static Builder|User whereUsername($value)
  98. * @method static Builder|User whereVmessId($value)
  99. * @method static Builder|User whereWechat($value)
  100. * @mixin \Eloquent
  101. */
  102. class User extends Authenticatable {
  103. use Notifiable;
  104. protected $table = 'user';
  105. protected $dates = ['enable_time', 'expire_time', 'reset_time'];
  106. protected $casts = [
  107. 'enable_time' => 'date:Y-m-d',
  108. 'expire_time' => 'date:Y-m-d',
  109. 'reset_time' => 'date:Y-m-d',
  110. ];
  111. public function scopeUid($query) {
  112. return $query->whereId(Auth::id());
  113. }
  114. public function payment(): HasMany {
  115. return $this->hasMany(Payment::class, 'user_id', 'id');
  116. }
  117. public function getLevelNameAttribute(): string {
  118. return Level::whereLevel($this->attributes['level'])->first()->name;
  119. }
  120. public function group(): HasOne {
  121. return $this->hasOne(UserGroup::class, 'id', 'group_id');
  122. }
  123. public function subscribe(): HasOne {
  124. return $this->hasOne(UserSubscribe::class, 'user_id', 'id');
  125. }
  126. public function referral(): HasOne {
  127. return $this->hasOne(__CLASS__, 'id', 'referral_uid');
  128. }
  129. public function getCreditAttribute($value) {
  130. return $value / 100;
  131. }
  132. public function setCreditAttribute($value) {
  133. return $this->attributes['credit'] = $value * 100;
  134. }
  135. public function getEnableTimeAttribute() {
  136. return $this->attributes['enable_time'];
  137. }
  138. public function getExpireTimeAttribute() {
  139. return $this->attributes['expire_time'];
  140. }
  141. public function getResetTimeAttribute() {
  142. return $this->attributes['reset_time'];
  143. }
  144. // User查询,查那些用户有传入Node的权限
  145. public function scopeGroupUserPermit($query, $node_id = 0) {
  146. $groups = [0];
  147. if($node_id){
  148. foreach(UserGroup::all() as $userGroup){
  149. $nodes = $userGroup->nodes;
  150. if($nodes && in_array($node_id, $nodes, true)){
  151. $groups[] = $userGroup->id;
  152. }
  153. }
  154. }
  155. return $query->whereIn('group_id', $groups);
  156. }
  157. public function scopeActiveUser($query) {
  158. return $query->where('status', '<>', -1)->whereEnable(1);
  159. }
  160. }