2023-11-17 01:44:01 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\V1\Admin\Server;
|
|
|
|
|
2023-12-04 07:40:49 -05:00
|
|
|
use App\Exceptions\ApiException;
|
2023-11-17 01:44:01 -05:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\Plan;
|
|
|
|
use App\Models\ServerGroup;
|
|
|
|
use App\Models\ServerVmess;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Services\ServerService;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class GroupController extends Controller
|
|
|
|
{
|
|
|
|
public function fetch(Request $request)
|
|
|
|
{
|
|
|
|
if ($request->input('group_id')) {
|
2023-12-06 15:01:32 -05:00
|
|
|
return $this->success([ServerGroup::find($request->input('group_id'))]);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
$serverGroups = ServerGroup::get();
|
2024-04-09 12:51:03 -04:00
|
|
|
$servers = ServerService::getAllServers();
|
2023-11-17 01:44:01 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-06 15:01:32 -05:00
|
|
|
return $this->success($serverGroups);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function save(Request $request)
|
|
|
|
{
|
|
|
|
if (empty($request->input('name'))) {
|
2023-12-06 15:01:32 -05:00
|
|
|
return $this->fail([422,'组名不能为空']);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->input('id')) {
|
|
|
|
$serverGroup = ServerGroup::find($request->input('id'));
|
|
|
|
} else {
|
|
|
|
$serverGroup = new ServerGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
$serverGroup->name = $request->input('name');
|
2023-12-06 15:01:32 -05:00
|
|
|
return $this->success($serverGroup->save());
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function drop(Request $request)
|
|
|
|
{
|
|
|
|
if ($request->input('id')) {
|
|
|
|
$serverGroup = ServerGroup::find($request->input('id'));
|
|
|
|
if (!$serverGroup) {
|
2023-12-06 15:01:32 -05:00
|
|
|
return $this->fail([400202,'组不存在']);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$servers = ServerVmess::all();
|
|
|
|
foreach ($servers as $server) {
|
|
|
|
if (in_array($request->input('id'), $server->group_id)) {
|
2023-12-06 15:01:32 -05:00
|
|
|
return $this->fail([400,'该组已被节点所使用,无法删除']);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Plan::where('group_id', $request->input('id'))->first()) {
|
2023-12-06 15:01:32 -05:00
|
|
|
return $this->fail([400, '该组已被订阅所使用,无法删除']);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
if (User::where('group_id', $request->input('id'))->first()) {
|
2023-12-06 15:01:32 -05:00
|
|
|
return $this->fail([400, '该组已被用户所使用,无法删除']);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
2023-12-06 15:01:32 -05:00
|
|
|
return $this->success($serverGroup->delete());
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
}
|