v2board/app/Http/Controllers/Admin/Server/GroupController.php

86 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2020-06-19 01:25:42 +08:00
<?php
namespace App\Http\Controllers\Admin\Server;
use App\Models\Plan;
2021-11-01 02:31:27 +08:00
use App\Models\ServerShadowsocks;
use App\Models\ServerTrojan;
2023-02-15 14:53:13 +08:00
use App\Models\ServerVmess;
2020-06-19 01:25:42 +08:00
use App\Models\ServerGroup;
use App\Models\User;
2021-11-01 02:31:27 +08:00
use App\Services\ServerService;
2020-06-19 01:25:42 +08:00
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class GroupController extends Controller
{
public function fetch(Request $request)
{
if ($request->input('group_id')) {
return response([
'data' => [ServerGroup::find($request->input('group_id'))]
]);
}
2021-11-01 02:31:27 +08:00
$serverGroups = ServerGroup::get();
$serverService = new ServerService();
$servers = $serverService->getAllServers();
foreach ($serverGroups as $k => $v) {
$serverGroups[$k]['user_count'] = User::where('group_id', $v['id'])->count();
$serverGroups[$k]['server_count'] = 0;
foreach ($servers as $server) {
if (in_array($v['id'], $server['group_id'])) {
$serverGroups[$k]['server_count'] = $serverGroups[$k]['server_count']+1;
}
}
}
2020-06-19 01:25:42 +08:00
return response([
2021-11-01 02:31:27 +08:00
'data' => $serverGroups
2020-06-19 01:25:42 +08:00
]);
}
public function save(Request $request)
{
if (empty($request->input('name'))) {
abort(500, '组名不能为空');
}
if ($request->input('id')) {
$serverGroup = ServerGroup::find($request->input('id'));
} else {
$serverGroup = new ServerGroup();
}
$serverGroup->name = $request->input('name');
return response([
'data' => $serverGroup->save()
]);
}
public function drop(Request $request)
{
if ($request->input('id')) {
$serverGroup = ServerGroup::find($request->input('id'));
if (!$serverGroup) {
abort(500, '组不存在');
}
}
2023-02-15 14:53:13 +08:00
$servers = ServerVmess::all();
2020-06-19 01:25:42 +08:00
foreach ($servers as $server) {
2021-08-06 00:43:01 +08:00
if (in_array($request->input('id'), $server->group_id)) {
2020-06-19 01:25:42 +08:00
abort(500, '该组已被节点所使用,无法删除');
}
}
if (Plan::where('group_id', $request->input('id'))->first()) {
abort(500, '该组已被订阅所使用,无法删除');
}
if (User::where('group_id', $request->input('id'))->first()) {
abort(500, '该组已被用户所使用,无法删除');
}
return response([
'data' => $serverGroup->delete()
]);
}
}