Node.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Relations\HasMany;
  5. use Illuminate\Database\Eloquent\Relations\HasOne;
  6. /**
  7. * 节点配置信息
  8. */
  9. class Node extends Model
  10. {
  11. protected $table = 'ss_node';
  12. protected $guarded = ['id', 'created_at'];
  13. public function labels(): HasMany
  14. {
  15. return $this->hasMany(NodeLabel::class);
  16. }
  17. public function heartBeats(): HasMany
  18. {
  19. return $this->hasMany(NodeHeartBeat::class);
  20. }
  21. public function onlineLogs(): HasMany
  22. {
  23. return $this->hasMany(NodeOnlineLog::class);
  24. }
  25. public function pingLogs(): HasMany
  26. {
  27. return $this->hasMany(NodePing::class);
  28. }
  29. public function dailyDataFlows(): HasMany
  30. {
  31. return $this->hasMany(NodeDailyDataFlow::class);
  32. }
  33. public function hourlyDataFlows(): HasMany
  34. {
  35. return $this->hasMany(NodeHourlyDataFlow::class);
  36. }
  37. public function rules(): hasMany
  38. {
  39. return $this->hasMany(NodeRule::class);
  40. }
  41. public function ruleGroup(): hasOne
  42. {
  43. return $this->hasOne(RuleGroupNode::class);
  44. }
  45. public function auth(): HasOne
  46. {
  47. return $this->hasOne(NodeAuth::class);
  48. }
  49. public function level_table(): HasOne
  50. {
  51. return $this->hasOne(Level::class, 'level', 'level');
  52. }
  53. public function scopeUserAllowNodes($query, $user_group_id, $user_level)
  54. {
  55. $userGroup = UserGroup::find($user_group_id);
  56. if ($userGroup) {
  57. $query->whereIn('id', $userGroup->nodes);
  58. }
  59. return $query->whereStatus(1)->where('level', '<=', $user_level ?: 0);
  60. }
  61. public function getNodeAccessUsersAttribute()
  62. {
  63. return User::nodeAllowUsers(
  64. $this->attributes['id'],
  65. $this->attributes['level']
  66. )->get();
  67. }
  68. public function getTypeLabelAttribute(): string
  69. {
  70. switch ($this->attributes['type']) {
  71. case 1:
  72. $type_label = 'ShadowsocksR';
  73. break;
  74. case 2:
  75. $type_label = 'V2Ray';
  76. break;
  77. case 3:
  78. $type_label = 'Trojan';
  79. break;
  80. case 4:
  81. $type_label = 'VNet';
  82. break;
  83. default:
  84. $type_label = 'UnKnown';
  85. }
  86. return $type_label;
  87. }
  88. }