Revert "fix: resolve PHPStan static analysis warnings"

This reverts commit 2d3e4b4a95.
This commit is contained in:
xboard
2025-04-14 21:23:08 +08:00
parent 2d3e4b4a95
commit db235c10e8
84 changed files with 1190 additions and 2330 deletions

View File

@@ -3,38 +3,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* App\Models\Order
*
* @property int $id
* @property int $user_id
* @property int $plan_id
* @property int|null $payment_id
* @property int $period
* @property string $trade_no
* @property int $total_amount
* @property int|null $handling_amount
* @property int|null $balance_amount
* @property int $type
* @property int $status
* @property array|null $surplus_order_ids
* @property int|null $coupon_id
* @property int $created_at
* @property int $updated_at
* @property int|null $commission_status
* @property int|null $invite_user_id
* @property int|null $actual_commission_balance
* @property int|null $commission_rate
* @property int|null $commission_auto_check
*
* @property-read Plan $plan
* @property-read Payment|null $payment
* @property-read User $user
* @property-read \Illuminate\Database\Eloquent\Collection<int, CommissionLog> $commission_log
*/
class Order extends Model
{
protected $table = 'v2_order';
@@ -43,8 +12,7 @@ class Order extends Model
protected $casts = [
'created_at' => 'timestamp',
'updated_at' => 'timestamp',
'surplus_order_ids' => 'array',
'handling_amount' => 'integer'
'surplus_order_ids' => 'array'
];
const STATUS_PENDING = 0; // 待支付
@@ -72,34 +40,21 @@ class Order extends Model
self::TYPE_RESET_TRAFFIC => '流量重置',
];
/**
* 获取与订单关联的支付方式
*/
public function payment(): BelongsTo
public function payment()
{
return $this->belongsTo(Payment::class, 'payment_id', 'id');
}
/**
* 获取与订单关联的用户
*/
public function user(): BelongsTo
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
/**
* 获取与订单关联的套餐
*/
public function plan(): BelongsTo
public function plan()
{
return $this->belongsTo(Plan::class, 'plan_id', 'id');
return $this->belongsTo(Plan::class);
}
/**
* 获取与订单关联的佣金记录
*/
public function commission_log(): HasMany
public function commission_log()
{
return $this->hasMany(CommissionLog::class, 'trade_no', 'trade_no');
}

View File

@@ -12,7 +12,6 @@ class Payment extends Model
protected $casts = [
'created_at' => 'timestamp',
'updated_at' => 'timestamp',
'config' => 'array',
'enable' => 'boolean'
'config' => 'array'
];
}

View File

@@ -7,31 +7,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use InvalidArgumentException;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Relations\HasOne;
/**
* App\Models\Plan
*
* @property int $id
* @property string $name 套餐名称
* @property int|null $group_id 权限组ID
* @property int $transfer_enable 流量(KB)
* @property int|null $speed_limit 速度限制Mbps
* @property bool $show 是否显示
* @property bool $renew 是否允许续费
* @property bool $sell 是否允许购买
* @property array|null $prices 价格配置
* @property int $sort 排序
* @property string|null $content 套餐描述
* @property int $reset_traffic_method 流量重置方式
* @property int|null $capacity_limit 订阅人数限制
* @property int|null $device_limit 设备数量限制
* @property int $created_at
* @property int $updated_at
*
* @property-read ServerGroup|null $group 关联的权限组
* @property-read \Illuminate\Database\Eloquent\Collection<int, Order> $order 关联的订单
*/
class Plan extends Model
{
use HasFactory;
@@ -40,12 +16,12 @@ class Plan extends Model
protected $dateFormat = 'U';
// 定义流量重置方式
public const RESET_TRAFFIC_FOLLOW_SYSTEM = null; // 跟随系统设置
public const RESET_TRAFFIC_FIRST_DAY_MONTH = 0; // 每月1号
public const RESET_TRAFFIC_MONTHLY = 1; // 按月重置
public const RESET_TRAFFIC_NEVER = 2; // 不重置
public const RESET_TRAFFIC_FIRST_DAY_YEAR = 3; // 每年1月1日
public const RESET_TRAFFIC_YEARLY = 4; // 按年重置
public const RESET_TRAFFIC_FOLLOW_SYSTEM = 0; // 跟随系统设置
public const RESET_TRAFFIC_FIRST_DAY_MONTH = 1; // 每月1号
public const RESET_TRAFFIC_MONTHLY = 2; // 按月重置
public const RESET_TRAFFIC_NEVER = 3; // 不重置
public const RESET_TRAFFIC_FIRST_DAY_YEAR = 4; // 每年1月1日
public const RESET_TRAFFIC_YEARLY = 5; // 按年重置
// 定义价格类型
public const PRICE_TYPE_RESET_TRAFFIC = 'reset_traffic'; // 重置流量价格
@@ -370,7 +346,7 @@ class Plan extends Model
return $this->hasMany(User::class);
}
public function group(): HasOne
public function group()
{
return $this->hasOne(ServerGroup::class, 'id', 'group_id');
}
@@ -408,9 +384,4 @@ class Plan extends Model
$prices[self::PRICE_TYPE_RESET_TRAFFIC] = max(0, $price);
$this->prices = $prices;
}
public function order(): HasMany
{
return $this->hasMany(Order::class);
}
}

View File

@@ -9,45 +9,7 @@ use Illuminate\Support\Facades\Cache;
use App\Utils\CacheKey;
use App\Utils\Helper;
use App\Models\User;
use Illuminate\Database\Eloquent\Casts\Attribute;
/**
* App\Models\Server
*
* @property int $id
* @property string $name 节点名称
* @property string $type 服务类型
* @property string $host 主机地址
* @property string $port 端口
* @property string|null $server_port 服务器端口
* @property array|null $group_ids 分组IDs
* @property array|null $route_ids 路由IDs
* @property array|null $tags 标签
* @property string|null $show 是否显示
* @property string|null $allow_insecure 是否允许不安全
* @property string|null $network 网络类型
* @property int|null $parent_id 父节点ID
* @property float|null $rate 倍率
* @property int|null $sort 排序
* @property array|null $protocol_settings 协议设置
* @property int $created_at
* @property int $updated_at
*
* @property-read Server|null $parent 父节点
* @property-read \Illuminate\Database\Eloquent\Collection<int, StatServer> $stats 节点统计
*
* @property-read int|null $last_check_at 最后检查时间Unix时间戳
* @property-read int|null $last_push_at 最后推送时间Unix时间戳
* @property-read int $online 在线用户数
* @property-read int $is_online 是否在线1在线 0离线
* @property-read string $available_status 可用状态描述
* @property-read string $cache_key 缓存键
* @property string|null $ports 端口范围
* @property string|null $password 密码
* @property int|null $u 上行流量
* @property int|null $d 下行流量
* @property int|null $total 总流量
*/
class Server extends Model
{
public const TYPE_HYSTERIA = 'hysteria';
@@ -57,9 +19,6 @@ class Server extends Model
public const TYPE_TUIC = 'tuic';
public const TYPE_SHADOWSOCKS = 'shadowsocks';
public const TYPE_SOCKS = 'socks';
public const TYPE_NAIVE = 'naive';
public const TYPE_HTTP = 'http';
public const TYPE_MIERU = 'mieru';
public const STATUS_OFFLINE = 0;
public const STATUS_ONLINE_NO_PUSH = 1;
public const STATUS_ONLINE = 2;
@@ -94,9 +53,6 @@ class Server extends Model
self::TYPE_TUIC,
self::TYPE_SHADOWSOCKS,
self::TYPE_SOCKS,
self::TYPE_NAIVE,
self::TYPE_HTTP,
self::TYPE_MIERU,
];
protected $table = 'v2_server';
@@ -187,32 +143,6 @@ class Server extends Model
'allow_insecure' => ['type' => 'boolean', 'default' => false]
]
]
],
self::TYPE_SOCKS => [
'tls' => ['type' => 'integer', 'default' => 0],
'tls_settings' => [
'type' => 'object',
'fields' => [
'allow_insecure' => ['type' => 'boolean', 'default' => false]
]
]
],
self::TYPE_NAIVE => [
'tls' => ['type' => 'integer', 'default' => 0],
'tls_settings' => ['type' => 'array', 'default' => null]
],
self::TYPE_HTTP => [
'tls' => ['type' => 'integer', 'default' => 0],
'tls_settings' => [
'type' => 'object',
'fields' => [
'allow_insecure' => ['type' => 'boolean', 'default' => false]
]
]
],
self::TYPE_MIERU => [
'transport' => ['type' => 'string', 'default' => 'tcp'],
'multiplexing' => ['type' => 'string', 'default' => 'MULTIPLEXING_LOW']
]
];
@@ -244,6 +174,19 @@ class Server extends Model
return $result;
}
private function getDefaultSettings(array $configs): array
{
$defaults = [];
foreach ($configs as $key => $config) {
if ($config['type'] === 'object') {
$defaults[$key] = $this->getDefaultSettings($config['fields']);
} else {
$defaults[$key] = $config['default'];
}
}
return $defaults;
}
public function getProtocolSettingsAttribute($value)
{
$settings = json_decode($value, true) ?? [];
@@ -263,22 +206,53 @@ class Server extends Model
$this->attributes['protocol_settings'] = json_encode($castedSettings);
}
public function generateShadowsocksPassword(User $user): string
public function loadParentCreatedAt(): void
{
if ($this->parent_id) {
$this->created_at = $this->parent()->value('created_at');
}
}
public function loadServerStatus(): void
{
$type = strtoupper($this->type);
$serverId = $this->parent_id ?: $this->id;
$this->last_check_at = Cache::get(CacheKey::get("SERVER_{$type}_LAST_CHECK_AT", $serverId));
$this->last_push_at = Cache::get(CacheKey::get("SERVER_{$type}_LAST_PUSH_AT", $serverId));
$this->online = Cache::get(CacheKey::get("SERVER_{$type}_ONLINE_USER", $serverId)) ?? 0;
$this->is_online = (time() - 300 > $this->last_check_at) ? 0 : 1;
$this->available_status = $this->getAvailableStatus();
$this->cache_key = "{$this->type}-{$this->id}-{$this->updated_at}-{$this->is_online}";
}
public function handlePortAllocation(): void
{
if (strpos($this->port, '-') !== false) {
$this->ports = $this->port;
$this->port = Helper::randomPort($this->port);
} else {
$this->port = (int) $this->port;
}
}
public function generateShadowsocksPassword(User $user): void
{
if ($this->type !== self::TYPE_SHADOWSOCKS) {
return $user->uuid;
return;
}
$this->password = $user->uuid;
$cipher = data_get($this, 'protocol_settings.cipher');
if (!$cipher || !isset(self::CIPHER_CONFIGURATIONS[$cipher])) {
return $user->uuid;
return;
}
$config = self::CIPHER_CONFIGURATIONS[$cipher];
$serverKey = Helper::getServerKey($this->created_at, $config['serverKeySize']);
$userKey = Helper::uuidToBase64($user->uuid, $config['userKeySize']);
return "{$serverKey}:{$userKey}";
$this->password = "{$serverKey}:{$userKey}";
}
public static function normalizeType(string $type): string
@@ -323,99 +297,4 @@ class Server extends Model
return ServerRoute::whereIn('id', $this->route_ids)->get();
}
/**
* 最后检查时间访问器
*/
protected function lastCheckAt(): Attribute
{
return Attribute::make(
get: function () {
$type = strtoupper($this->type);
$serverId = $this->parent_id ?: $this->id;
return Cache::get(CacheKey::get("SERVER_{$type}_LAST_CHECK_AT", $serverId));
}
);
}
/**
* 最后推送时间访问器
*/
protected function lastPushAt(): Attribute
{
return Attribute::make(
get: function () {
$type = strtoupper($this->type);
$serverId = $this->parent_id ?: $this->id;
return Cache::get(CacheKey::get("SERVER_{$type}_LAST_PUSH_AT", $serverId));
}
);
}
/**
* 在线用户数访问器
*/
protected function online(): Attribute
{
return Attribute::make(
get: function () {
$type = strtoupper($this->type);
$serverId = $this->parent_id ?: $this->id;
return Cache::get(CacheKey::get("SERVER_{$type}_ONLINE_USER", $serverId)) ?? 0;
}
);
}
/**
* 是否在线访问器
*/
protected function isOnline(): Attribute
{
return Attribute::make(
get: function () {
return (time() - 300 > $this->last_check_at) ? 0 : 1;
}
);
}
/**
* 可用状态访问器
*/
protected function availableStatus(): Attribute
{
return Attribute::make(
get: function () {
if ($this->is_online) {
return true;
}
return false;
}
);
}
/**
* 缓存键访问器
*/
protected function cacheKey(): Attribute
{
return Attribute::make(
get: function () {
return "{$this->type}-{$this->id}-{$this->updated_at}-{$this->is_online}";
}
);
}
/**
* 服务器密钥访问器
*/
protected function serverKey(): Attribute
{
return Attribute::make(
get: function () {
if ($this->type === self::TYPE_SHADOWSOCKS) {
return Helper::getServerKey($this->created_at, 16);
}
return null;
}
);
}
}

View File

@@ -4,17 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Casts\Attribute;
/**
* App\Models\ServerGroup
*
* @property int $id
* @property string $name 分组名
* @property int $created_at
* @property int $updated_at
* @property-read int $server_count 服务器数量
*/
class ServerGroup extends Model
{
protected $table = 'v2_server_group';
@@ -33,14 +23,4 @@ class ServerGroup extends Model
{
return Server::whereJsonContains('group_ids', (string) $this->id)->get();
}
/**
* 获取服务器数量
*/
protected function serverCount(): Attribute
{
return Attribute::make(
get: fn () => Server::whereJsonContains('group_ids', (string) $this->id)->count(),
);
}
}

View File

@@ -4,18 +4,6 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* App\Models\StatServer
*
* @property int $id
* @property int $server_id 服务器ID
* @property int $u 上行流量
* @property int $d 下行流量
* @property int $record_at 记录时间
* @property int $created_at
* @property int $updated_at
* @property-read int $value 通过SUM(u + d)计算的总流量值,仅在查询指定时可用
*/
class StatServer extends Model
{
protected $table = 'v2_stat_server';

View File

@@ -4,18 +4,6 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* App\Models\StatUser
*
* @property int $id
* @property int $user_id 用户ID
* @property int $u 上行流量
* @property int $d 下行流量
* @property int $record_at 记录时间
* @property int $created_at
* @property int $updated_at
* @property-read int $value 通过SUM(u + d)计算的总流量值,仅在查询指定时可用
*/
class StatUser extends Model
{
protected $table = 'v2_stat_user';

View File

@@ -3,25 +3,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* App\Models\Ticket
*
* @property int $id
* @property int $user_id 用户ID
* @property string $subject 工单主题
* @property string|null $level 工单等级
* @property int $status 工单状态
* @property int|null $reply_status 回复状态
* @property int|null $last_reply_user_id 最后回复人
* @property int $created_at
* @property int $updated_at
*
* @property-read User $user 关联的用户
* @property-read \Illuminate\Database\Eloquent\Collection<int, TicketMessage> $messages 关联的工单消息
*/
class Ticket extends Model
{
protected $table = 'v2_ticket';
@@ -39,21 +21,16 @@ class Ticket extends Model
self::STATUS_CLOSED => '关闭'
];
public function user(): BelongsTo
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
/**
* 关联的工单消息
*/
public function messages(): HasMany
public function messages()
{
return $this->hasMany(TicketMessage::class, 'ticket_id', 'id');
}
// 即将删除
public function message(): HasMany
public function message()
{
return $this->hasMany(TicketMessage::class, 'ticket_id', 'id');
}

View File

@@ -3,20 +3,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* App\Models\TicketMessage
*
* @property int $id
* @property int $ticket_id
* @property int $user_id
* @property string $message
* @property \Illuminate\Support\Carbon $created_at
* @property \Illuminate\Support\Carbon $updated_at
* @property-read \App\Models\Ticket $ticket 关联的工单
* @property-read bool $is_me 当前消息是否由工单发起人发送
*/
class TicketMessage extends Model
{
protected $table = 'v2_ticket_message';
@@ -26,22 +13,4 @@ class TicketMessage extends Model
'created_at' => 'timestamp',
'updated_at' => 'timestamp'
];
protected $appends = ['is_me'];
/**
* 关联的工单
*/
public function ticket(): BelongsTo
{
return $this->belongsTo(Ticket::class, 'ticket_id', 'id');
}
/**
* 判断消息是否由工单发起人发送
*/
public function getIsMeAttribute(): bool
{
return $this->ticket->user_id === $this->user_id;
}
}

View File

@@ -2,55 +2,10 @@
namespace App\Models;
use App\Utils\Helper;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* App\Models\User
*
* @property int $id 用户ID
* @property string $email 邮箱
* @property string $password 密码
* @property string|null $password_algo 加密方式
* @property string|null $password_salt 加密盐
* @property string $token 邀请码
* @property string $uuid
* @property int|null $invite_user_id 邀请人
* @property int|null $plan_id 订阅ID
* @property int|null $group_id 权限组ID
* @property int|null $transfer_enable 流量(KB)
* @property int|null $speed_limit 限速Mbps
* @property int|null $u 上行流量
* @property int|null $d 下行流量
* @property int|null $banned 是否封禁
* @property int|null $remind_expire 到期提醒
* @property int|null $remind_traffic 流量提醒
* @property int|null $expired_at 过期时间
* @property int|null $balance 余额
* @property int|null $commission_balance 佣金余额
* @property float $commission_rate 返佣比例
* @property int|null $device_limit 设备限制数量
* @property int|null $discount 折扣
* @property int|null $last_login_at 最后登录时间
* @property int|null $parent_id 父账户ID
* @property int|null $is_admin 是否管理员
* @property int $created_at
* @property int $updated_at
* @property bool $commission_auto_check 是否自动计算佣金
*
* @property-read User|null $invite_user 邀请人信息
* @property-read \App\Models\Plan|null $plan 用户订阅计划
* @property-read ServerGroup|null $group 权限组
* @property-read \Illuminate\Database\Eloquent\Collection<int, InviteCode> $codes 邀请码列表
* @property-read \Illuminate\Database\Eloquent\Collection<int, Order> $orders 订单列表
* @property-read \Illuminate\Database\Eloquent\Collection<int, StatUser> $stat 统计信息
* @property-read \Illuminate\Database\Eloquent\Collection<int, Ticket> $tickets 工单列表
* @property-read User|null $parent 父账户
* @property-read string $subscribe_url 订阅链接(动态生成)
*/
class User extends Authenticatable
{
use HasApiTokens;
@@ -59,72 +14,52 @@ class User extends Authenticatable
protected $guarded = ['id'];
protected $casts = [
'created_at' => 'timestamp',
'updated_at' => 'timestamp',
'banned' => 'boolean',
'remind_expire' => 'boolean',
'remind_traffic' => 'boolean',
'commission_auto_check' => 'boolean',
'commission_rate' => 'float'
'updated_at' => 'timestamp'
];
protected $hidden = ['password'];
public const COMMISSION_TYPE_SYSTEM = 0;
public const COMMISSION_TYPE_PERIOD = 1;
public const COMMISSION_TYPE_ONETIME = 2;
// 获取邀请人信息
public function invite_user(): BelongsTo
public function invite_user()
{
return $this->belongsTo(self::class, 'invite_user_id', 'id');
}
/**
* 获取用户订阅计划
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function plan(): BelongsTo
// 获取用户套餐
public function plan()
{
return $this->belongsTo(Plan::class, 'plan_id', 'id');
}
public function group(): BelongsTo
public function group()
{
return $this->belongsTo(ServerGroup::class, 'group_id', 'id');
}
// 获取用户邀请码列表
public function codes(): HasMany
public function codes()
{
return $this->hasMany(InviteCode::class, 'user_id', 'id');
}
public function orders(): HasMany
public function orders()
{
return $this->hasMany(Order::class, 'user_id', 'id');
}
public function stat(): HasMany
public function stat()
{
return $this->hasMany(StatUser::class, 'user_id', 'id');
}
// 关联工单列表
public function tickets(): HasMany
public function tickets()
{
return $this->hasMany(Ticket::class, 'user_id', 'id');
}
public function parent(): BelongsTo
public function parent()
{
return $this->belongsTo(self::class, 'parent_id', 'id');
}
/**
* 获取订阅链接属性
*/
public function getSubscribeUrlAttribute(): string
{
return Helper::getSubscribeUrl($this->token);
}
}