fix: correct know file issues

This commit is contained in:
xboard 2025-01-09 18:36:14 +08:00
parent 720aa415ac
commit e1db186ee7
8 changed files with 362 additions and 226 deletions

View File

@ -17,6 +17,7 @@ class ManageController extends Controller
{ {
$servers = collect(ServerService::getAllServers())->map(function ($item) { $servers = collect(ServerService::getAllServers())->map(function ($item) {
$item['groups'] = ServerGroup::whereIn('id', $item['group_ids'])->get(['name', 'id']); $item['groups'] = ServerGroup::whereIn('id', $item['group_ids'])->get(['name', 'id']);
$item['parent'] = $item->parent;
return $item; return $item;
}); });
return $this->success($servers); return $this->success($servers);

View File

@ -13,6 +13,7 @@ use App\Models\Plan;
use App\Models\User; use App\Models\User;
use App\Services\AuthService; use App\Services\AuthService;
use App\Utils\Helper; use App\Utils\Helper;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
@ -28,59 +29,128 @@ class UserController extends Controller
return $this->success($user->save()); return $this->success($user->save());
} }
private function applyFiltersAndSorts(Request $request, $builder) /**
* Apply filters and sorts to the query builder
*
* @param Request $request
* @param Builder $builder
* @return void
*/
private function applyFiltersAndSorts(Request $request, Builder $builder): void
{ {
if ($request->has('filter')) { $this->applyFilters($request, $builder);
collect($request->input('filter'))->each(function ($filter) use ($builder) { $this->applySorting($request, $builder);
$key = $filter['id'];
$value = $filter['value'];
$builder->where(function ($query) use ($key, $value) {
if (is_array($value)) {
if ($key === 'group_ids') {
$query->where(function ($subQuery) use ($value) {
$subQuery->whereIn('group_id', $value);
});
} else {
$query->whereIn($key, $value);
}
} else {
$query->where($key, 'like', "%{$value}%");
}
});
});
}
if ($request->has('sort')) {
collect($request->input('sort'))->each(function ($sort) use ($builder) {
$key = $sort['id'];
$value = $sort['desc'] ? 'DESC' : 'ASC';
$builder->orderBy($key, $value);
});
}
} }
/**
* Apply filters to the query builder
*
* @param Request $request
* @param Builder $builder
* @return void
*/
private function applyFilters(Request $request, Builder $builder): void
{
if (!$request->has('filter')) {
return;
}
collect($request->input('filter'))->each(function ($filter) use ($builder) {
$field = $filter['id'];
$value = $filter['value'];
$builder->where(function ($query) use ($field, $value) {
$this->buildFilterQuery($query, $field, $value);
});
});
}
/**
* Build the filter query based on field and value
*
* @param Builder $query
* @param string $field
* @param mixed $value
* @return void
*/
private function buildFilterQuery(Builder $query, string $field, mixed $value): void
{
if (!is_array($value)) {
$query->where($field, 'like', "%{$value}%");
return;
}
if ($field === 'group_ids') {
$query->whereIn('group_id', $value);
return;
}
$query->whereIn($field, $value);
}
/**
* Apply sorting to the query builder
*
* @param Request $request
* @param Builder $builder
* @return void
*/
private function applySorting(Request $request, Builder $builder): void
{
if (!$request->has('sort')) {
return;
}
collect($request->input('sort'))->each(function ($sort) use ($builder) {
$field = $sort['id'];
$direction = $sort['desc'] ? 'DESC' : 'ASC';
$builder->orderBy($field, $direction);
});
}
/**
* Fetch paginated user list with filters and sorting
*
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function fetch(Request $request) public function fetch(Request $request)
{ {
$current = $request->input('current', 1); $current = $request->input('current', 1);
$pageSize = $request->input('pageSize', 10); $pageSize = $request->input('pageSize', 10);
$userModel = User::with(['plan:id,name', 'invite_user:id,email', 'group:id,name'])->select(
DB::raw('*'), $userModel = User::with(['plan:id,name', 'invite_user:id,email', 'group:id,name'])
DB::raw('(u+d) as total_used') ->select(DB::raw('*, (u+d) as total_used'));
);
$this->applyFiltersAndSorts($request, $userModel); $this->applyFiltersAndSorts($request, $userModel);
$users = $userModel->orderBy('id', 'desc')->paginate($pageSize, ['*'], 'page', $current);
$users = $userModel->orderBy('id', 'desc')
->paginate($pageSize, ['*'], 'page', $current);
$users->getCollection()->transform(function ($user) { $users->getCollection()->transform(function ($user) {
$user->subscribe_url = Helper::getSubscribeUrl($user->token); return $this->transformUserData($user);
$user->balance = $user->balance / 100;
$user->commission_balance = $user->commission_balance / 100;
return $user;
}); });
return response([ return response([
'data' => $users->items(), 'data' => $users->items(),
'total' => $users->total() 'total' => $users->total()
]); ]);
} }
/**
* Transform user data for response
*
* @param User $user
* @return User
*/
private function transformUserData(User $user): User
{
$user->subscribe_url = Helper::getSubscribeUrl($user->token);
$user->balance = $user->balance / 100;
$user->commission_balance = $user->commission_balance / 100;
return $user;
}
public function getUserInfoById(Request $request) public function getUserInfoById(Request $request)
{ {
$request->validate([ $request->validate([

View File

@ -27,9 +27,9 @@ class PlanResources extends JsonResource
'capacity_limit' => $this->formatCapacityLimit(), 'capacity_limit' => $this->formatCapacityLimit(),
'transfer_enable' => $this['transfer_enable'], 'transfer_enable' => $this['transfer_enable'],
'speed_limit' => $this['speed_limit'], 'speed_limit' => $this['speed_limit'],
'show' => (bool)$this['show'], 'show' => (bool) $this['show'],
'sell' => (bool)$this['sell'], 'sell' => (bool) $this['sell'],
'renew' => (bool)$this['renew'], 'renew' => (bool) $this['renew'],
'reset_traffic_method' => $this['reset_traffic_method'], 'reset_traffic_method' => $this['reset_traffic_method'],
'sort' => $this['sort'], 'sort' => $this['sort'],
'created_at' => $this['created_at'], 'created_at' => $this['created_at'],
@ -46,7 +46,7 @@ class PlanResources extends JsonResource
{ {
$prices = []; $prices = [];
foreach (Plan::LEGACY_PERIOD_MAPPING as $legacyPeriod => $newPeriod) { foreach (Plan::LEGACY_PERIOD_MAPPING as $legacyPeriod => $newPeriod) {
$prices[$legacyPeriod] = optional($this['prices'])[$newPeriod] ? (int)$this['prices'][$newPeriod] * 100 : null; $prices[$legacyPeriod] = optional($this['prices'])[$newPeriod] ? (float) $this['prices'][$newPeriod] * 100 : null;
} }
return $prices; return $prices;
} }
@ -66,6 +66,6 @@ class PlanResources extends JsonResource
return __('Sold out'); return __('Sold out');
} }
return (int)$this['capacity_limit']; return (int) $this['capacity_limit'];
} }
} }

View File

@ -47,4 +47,9 @@ class User extends Authenticatable
{ {
return $this->hasMany(Ticket::class, 'user_id', 'id'); return $this->hasMany(Ticket::class, 'user_id', 'id');
} }
public function parent()
{
return $this->belongsTo(self::class, 'parent_id', 'id');
}
} }

View File

@ -191,6 +191,9 @@ return new class extends Migration {
]); ]);
} }
// Update parent_id for all servers
$this->updateParentIds();
// Drop old tables // Drop old tables
Schema::dropIfExists('v2_server_trojan'); Schema::dropIfExists('v2_server_trojan');
Schema::dropIfExists('v2_server_vmess'); Schema::dropIfExists('v2_server_vmess');
@ -199,6 +202,56 @@ return new class extends Migration {
Schema::dropIfExists('v2_server_hysteria'); Schema::dropIfExists('v2_server_hysteria');
} }
/**
* Update parent_id references for all servers
*/
private function updateParentIds(): void
{
// Get all servers that have a parent_id
$servers = DB::table('v2_server')
->whereNotNull('parent_id')
->get();
// Update each server's parent_id to reference the new table's id
foreach ($servers as $server) {
$parentId = DB::table('v2_server')
->where('type', $server->type)
->where('code', $server->parent_id)
->value('id');
if ($parentId) {
DB::table('v2_server')
->where('id', $server->id)
->update(['parent_id' => $parentId]);
}
}
}
/**
* Restore parent_id references when rolling back
*/
private function restoreParentIds(string $type, string $table): void
{
// Get all servers of the specified type that have a parent_id
$servers = DB::table($table)
->whereNotNull('parent_id')
->get();
// Update each server's parent_id to reference back to the original id
foreach ($servers as $server) {
$originalParentId = DB::table('v2_server')
->where('type', $type)
->where('id', $server->parent_id)
->value('code');
if ($originalParentId) {
DB::table($table)
->where('id', $server->id)
->update(['parent_id' => $originalParentId]);
}
}
}
/** /**
* Reverse the migrations. * Reverse the migrations.
*/ */
@ -457,6 +510,13 @@ return new class extends Migration {
} }
} }
// Restore parent_id references for each server type
$this->restoreParentIds('trojan', 'v2_server_trojan');
$this->restoreParentIds('vmess', 'v2_server_vmess');
$this->restoreParentIds('vless', 'v2_server_vless');
$this->restoreParentIds('shadowsocks', 'v2_server_shadowsocks');
$this->restoreParentIds('hysteria', 'v2_server_hysteria');
// Drop new table // Drop new table
Schema::dropIfExists('v2_server'); Schema::dropIfExists('v2_server');
} }

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long