first commit
This commit is contained in:
35
Xboard/app/Observers/PlanObserver.php
Normal file
35
Xboard/app/Observers/PlanObserver.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\Plan;
|
||||
use App\Models\User;
|
||||
use App\Services\TrafficResetService;
|
||||
|
||||
class PlanObserver
|
||||
{
|
||||
/**
|
||||
* reset user next_reset_at
|
||||
*/
|
||||
public function updated(Plan $plan): void
|
||||
{
|
||||
if (!$plan->isDirty('reset_traffic_method')) {
|
||||
return;
|
||||
}
|
||||
$trafficResetService = app(TrafficResetService::class);
|
||||
User::where('plan_id', $plan->id)
|
||||
->where('banned', 0)
|
||||
->where(function ($query) {
|
||||
$query->where('expired_at', '>', time())
|
||||
->orWhereNull('expired_at');
|
||||
})
|
||||
->lazyById(500)
|
||||
->each(function (User $user) use ($trafficResetService) {
|
||||
$nextResetTime = $trafficResetService->calculateNextResetTime($user);
|
||||
$user->update([
|
||||
'next_reset_at' => $nextResetTime?->timestamp,
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
37
Xboard/app/Observers/ServerObserver.php
Normal file
37
Xboard/app/Observers/ServerObserver.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\Server;
|
||||
use App\Services\NodeSyncService;
|
||||
|
||||
class ServerObserver
|
||||
{
|
||||
public function updated(Server $server): void
|
||||
{
|
||||
if (
|
||||
$server->isDirty([
|
||||
'group_ids',
|
||||
])
|
||||
) {
|
||||
NodeSyncService::notifyUsersUpdatedByGroup($server->id);
|
||||
} else if (
|
||||
$server->isDirty([
|
||||
'server_port',
|
||||
'protocol_settings',
|
||||
'type',
|
||||
'route_ids',
|
||||
'custom_outbounds',
|
||||
'custom_routes',
|
||||
'cert_config',
|
||||
])
|
||||
) {
|
||||
NodeSyncService::notifyConfigUpdated($server->id);
|
||||
}
|
||||
}
|
||||
|
||||
public function deleted(Server $server): void
|
||||
{
|
||||
NodeSyncService::notifyConfigUpdated($server->id);
|
||||
}
|
||||
}
|
||||
31
Xboard/app/Observers/ServerRouteObserver.php
Normal file
31
Xboard/app/Observers/ServerRouteObserver.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\Server;
|
||||
use App\Models\ServerRoute;
|
||||
use App\Services\NodeSyncService;
|
||||
|
||||
class ServerRouteObserver
|
||||
{
|
||||
public function updated(ServerRoute $route): void
|
||||
{
|
||||
$this->notifyAffectedNodes($route->id);
|
||||
}
|
||||
|
||||
public function deleted(ServerRoute $route): void
|
||||
{
|
||||
$this->notifyAffectedNodes($route->id);
|
||||
}
|
||||
|
||||
private function notifyAffectedNodes(int $routeId): void
|
||||
{
|
||||
$servers = Server::where('show', 1)->get()->filter(
|
||||
fn ($s) => in_array($routeId, $s->route_ids ?? [])
|
||||
);
|
||||
|
||||
foreach ($servers as $server) {
|
||||
NodeSyncService::notifyConfigUpdated($server->id);
|
||||
}
|
||||
}
|
||||
}
|
||||
53
Xboard/app/Observers/UserObserver.php
Normal file
53
Xboard/app/Observers/UserObserver.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Jobs\NodeUserSyncJob;
|
||||
use App\Models\User;
|
||||
use App\Services\TrafficResetService;
|
||||
|
||||
class UserObserver
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TrafficResetService $trafficResetService
|
||||
) {
|
||||
}
|
||||
|
||||
public function updated(User $user): void
|
||||
{
|
||||
if ($user->isDirty(['plan_id', 'expired_at'])) {
|
||||
$this->recalculateNextResetAt($user);
|
||||
}
|
||||
|
||||
if ($user->isDirty(['group_id', 'uuid', 'speed_limit', 'device_limit', 'banned', 'expired_at', 'transfer_enable', 'u', 'd', 'plan_id'])) {
|
||||
$oldGroupId = $user->isDirty('group_id') ? $user->getOriginal('group_id') : null;
|
||||
NodeUserSyncJob::dispatch($user->id, 'updated', $oldGroupId);
|
||||
}
|
||||
}
|
||||
|
||||
public function created(User $user): void
|
||||
{
|
||||
$this->recalculateNextResetAt($user);
|
||||
NodeUserSyncJob::dispatch($user->id, 'created');
|
||||
}
|
||||
|
||||
public function deleted(User $user): void
|
||||
{
|
||||
if ($user->group_id) {
|
||||
NodeUserSyncJob::dispatch($user->id, 'deleted', $user->group_id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据当前用户状态重新计算 next_reset_at
|
||||
*/
|
||||
private function recalculateNextResetAt(User $user): void
|
||||
{
|
||||
$user->refresh();
|
||||
User::withoutEvents(function () use ($user) {
|
||||
$nextResetTime = $this->trafficResetService->calculateNextResetTime($user);
|
||||
$user->next_reset_at = $nextResetTime?->timestamp;
|
||||
$user->save();
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user