refactor: 规范状态码、抛出异常的使用

This commit is contained in:
xboard
2023-12-07 04:01:32 +08:00
parent c25803aa74
commit 189b247ad8
71 changed files with 663 additions and 910 deletions
@@ -16,9 +16,7 @@ class GroupController extends Controller
public function fetch(Request $request)
{
if ($request->input('group_id')) {
return response([
'data' => [ServerGroup::find($request->input('group_id'))]
]);
return $this->success([ServerGroup::find($request->input('group_id'))]);
}
$serverGroups = ServerGroup::get();
$serverService = new ServerService();
@@ -32,15 +30,13 @@ class GroupController extends Controller
}
}
}
return response([
'data' => $serverGroups
]);
return $this->success($serverGroups);
}
public function save(Request $request)
{
if (empty($request->input('name'))) {
throw new ApiException(500, '组名不能为空');
return $this->fail([422,'组名不能为空']);
}
if ($request->input('id')) {
@@ -50,9 +46,7 @@ class GroupController extends Controller
}
$serverGroup->name = $request->input('name');
return response([
'data' => $serverGroup->save()
]);
return $this->success($serverGroup->save());
}
public function drop(Request $request)
@@ -60,25 +54,23 @@ class GroupController extends Controller
if ($request->input('id')) {
$serverGroup = ServerGroup::find($request->input('id'));
if (!$serverGroup) {
throw new ApiException(500, '组不存在');
return $this->fail([400202,'组不存在']);
}
}
$servers = ServerVmess::all();
foreach ($servers as $server) {
if (in_array($request->input('id'), $server->group_id)) {
throw new ApiException(500, '该组已被节点所使用,无法删除');
return $this->fail([400,'该组已被节点所使用,无法删除']);
}
}
if (Plan::where('group_id', $request->input('id'))->first()) {
throw new ApiException(500, '该组已被订阅所使用,无法删除');
return $this->fail([400, '该组已被订阅所使用,无法删除']);
}
if (User::where('group_id', $request->input('id'))->first()) {
throw new ApiException(500, '该组已被用户所使用,无法删除');
return $this->fail([400, '该组已被用户所使用,无法删除']);
}
return response([
'data' => $serverGroup->delete()
]);
return $this->success($serverGroup->delete());
}
}
@@ -45,25 +45,22 @@ class HysteriaController extends Controller
if ($request->input('id')) {
$server = ServerHysteria::find($request->input('id'));
if (!$server) {
throw new ApiException(500, '服务器不存在');
return $this->fail([400202, '服务器不存在']);
}
try {
$server->update($params);
} catch (\Exception $e) {
throw new ApiException(500, '保存失败');
\Log::error($e);
return $this->fail([500,'保存失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
if (!ServerHysteria::create($params)) {
throw new ApiException(500, '创建失败');
return $this->fail([500,'创建失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
public function drop(Request $request)
@@ -71,12 +68,10 @@ class HysteriaController extends Controller
if ($request->input('id')) {
$server = ServerHysteria::find($request->input('id'));
if (!$server) {
throw new ApiException(500, '节点ID不存在');
return $this->fail([400202,'节点ID不存在']);
}
}
return response([
'data' => $server->delete()
]);
return $this->success($server->delete());
}
public function update(Request $request)
@@ -93,17 +88,16 @@ class HysteriaController extends Controller
$server = ServerHysteria::find($request->input('id'));
if (!$server) {
throw new ApiException(500, '该服务器不存在');
return $this->fail([400202,'该服务器不存在']);
}
try {
$server->update($params);
} catch (\Exception $e) {
throw new ApiException(500, '保存失败');
\Log::error($e);
return $this->fail([500,'保存失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
public function copy(Request $request)
@@ -111,14 +105,9 @@ class HysteriaController extends Controller
$server = ServerHysteria::find($request->input('id'));
$server->show = 0;
if (!$server) {
throw new ApiException(500, '服务器不存在');
return $this->fail([400202,'服务器不存在']);
}
if (!ServerHysteria::create($server->toArray())) {
throw new ApiException(500, '复制失败');
}
return response([
'data' => true
]);
ServerHysteria::create($server->toArray());
return $this->success(true);
}
}
@@ -13,9 +13,7 @@ class ManageController extends Controller
public function getNodes(Request $request)
{
$serverService = new ServerService();
return response([
'data' => $serverService->getAllServers()
]);
return $this->success($serverService->getAllServers());
}
public function sort(Request $request)
@@ -33,15 +31,15 @@ class ManageController extends Controller
foreach ($params as $k => $v) {
$model = 'App\\Models\\Server' . ucfirst($k);
foreach($v as $id => $sort) {
if (!$model::find($id)->update(['sort' => $sort])) {
throw new ApiException(500, '保存失败');
}
$model::where('id', $id)->update(['sort' => $sort]);
}
}
DB::commit();
}catch (\Exception $e){
DB::rollBack();
throw $e;
\Log::error($e);
return $this->fail([500,'保存失败']);
}
return $this->success(true);
}
@@ -44,24 +44,26 @@ class RouteController extends Controller
try {
$route = ServerRoute::find($request->input('id'));
$route->update($params);
return [
'data' => true
];
return $this->success(true);
} catch (\Exception $e) {
throw new ApiException(500, '保存失败');
\Log::error($e);
return $this->fail([500,'保存失败']);
}
}
if (!ServerRoute::create($params)) throw new ApiException(500, '创建失败');
return [
'data' => true
];
try{
ServerRoute::create($params);
return $this->success(true);
}catch(\Exception $e){
\Log::error($e);
return $this->fail([500,'创建失败']);
}
}
public function drop(Request $request)
{
$route = ServerRoute::find($request->input('id'));
if (!$route) throw new ApiException(500, '路由不存在');
if (!$route->delete()) throw new ApiException(500, '删除失败');
if (!$route) throw new ApiException('路由不存在');
if (!$route->delete()) throw new ApiException('删除失败');
return [
'data' => true
];
@@ -17,25 +17,26 @@ class ShadowsocksController extends Controller
if ($request->input('id')) {
$server = ServerShadowsocks::find($request->input('id'));
if (!$server) {
throw new ApiException(500, '服务器不存在');
return $this->fail([400202, '服务器不存在']);
}
try {
$server->update($params);
return $this->success(true);
} catch (\Exception $e) {
throw new ApiException(500, '保存失败');
\Log::error($e);
return $this->fail([500,'保存失败']);
}
return response([
'data' => true
]);
}
if (!ServerShadowsocks::create($params)) {
throw new ApiException(500, '创建失败');
try{
ServerShadowsocks::create($params);
return $this->success(true);
}catch(\Exception $e){
\Log::error($e);
return $this->fail([500,'创建失败']);
}
return response([
'data' => true
]);
}
public function drop(Request $request)
@@ -43,12 +44,10 @@ class ShadowsocksController extends Controller
if ($request->input('id')) {
$server = ServerShadowsocks::find($request->input('id'));
if (!$server) {
throw new ApiException(500, '节点ID不存在');
return $this->fail([400202, '节点不存在']);
}
}
return response([
'data' => $server->delete()
]);
return $this->success($server->delete());
}
public function update(ServerShadowsocksUpdate $request)
@@ -60,17 +59,16 @@ class ShadowsocksController extends Controller
$server = ServerShadowsocks::find($request->input('id'));
if (!$server) {
throw new ApiException(500, '该服务器不存在');
return $this->fail([400202, '该服务器不存在']);
}
try {
$server->update($params);
} catch (\Exception $e) {
throw new ApiException(500, '保存失败');
\Log::error($e);
return $this->fail([500,'保存失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
public function copy(Request $request)
@@ -78,14 +76,9 @@ class ShadowsocksController extends Controller
$server = ServerShadowsocks::find($request->input('id'));
$server->show = 0;
if (!$server) {
throw new ApiException(500, '服务器不存在');
return $this->fail([400202,'服务器不存在']);
}
if (!ServerShadowsocks::create($server->toArray())) {
throw new ApiException(500, '复制失败');
}
return response([
'data' => true
]);
ServerShadowsocks::create($server->toArray());
return $this->success(true);
}
}
@@ -18,25 +18,19 @@ class TrojanController extends Controller
if ($request->input('id')) {
$server = ServerTrojan::find($request->input('id'));
if (!$server) {
throw new ApiException(500, '服务器不存在');
return $this->fail([400202,'服务器不存在']);
}
try {
$server->update($params);
} catch (\Exception $e) {
throw new ApiException(500, '保存失败');
\Log::error($e);
return $this->fail([500, '保存失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
if (!ServerTrojan::create($params)) {
throw new ApiException(500, '创建失败');
}
return response([
'data' => true
]);
ServerTrojan::create($params);
return $this->success(true);
}
public function drop(Request $request)
@@ -44,12 +38,10 @@ class TrojanController extends Controller
if ($request->input('id')) {
$server = ServerTrojan::find($request->input('id'));
if (!$server) {
throw new ApiException(500, '节点ID不存在');
return $this->fail([400202,'节点ID不存在']);
}
}
return response([
'data' => $server->delete()
]);
return $this->success($server->delete());
}
public function update(ServerTrojanUpdate $request)
@@ -61,17 +53,16 @@ class TrojanController extends Controller
$server = ServerTrojan::find($request->input('id'));
if (!$server) {
throw new ApiException(500, '该服务器不存在');
return $this->fail([400202,'该服务器不存在']);
}
try {
$server->update($params);
} catch (\Exception $e) {
throw new ApiException(500, '保存失败');
\Log::error($e);
return $this->fail([500,'保存失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
public function copy(Request $request)
@@ -79,22 +70,15 @@ class TrojanController extends Controller
$server = ServerTrojan::find($request->input('id'));
$server->show = 0;
if (!$server) {
throw new ApiException(500, '服务器不存在');
return $this->fail([400202,'服务器不存在']);
}
if (!ServerTrojan::create($server->toArray())) {
throw new ApiException(500, '复制失败');
}
return response([
'data' => true
]);
ServerTrojan::create($server->toArray());
return $this->success(true);
}
public function viewConfig(Request $request)
{
$serverService = new ServerService();
$config = $serverService->getTrojanConfig($request->input('node_id'), 23333);
return response([
'data' => $config
]);
return $this->success($config);
}
}
@@ -62,25 +62,19 @@ class VlessController extends Controller
if ($request->input('id')) {
$server = ServerVless::find($request->input('id'));
if (!$server) {
throw new ApiException(500, '服务器不存在');
return $this->fail([400202, '服务器不存在']);
}
try {
$server->update($params);
} catch (\Exception $e) {
throw new ApiException(500, '保存失败');
\Log::error($e);
return $this->fail([500, '保存失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
ServerVless::create($params);
if (!ServerVless::create($params)) {
throw new ApiException(500, '创建失败');
}
return response([
'data' => true
]);
return $this->success(true);
}
public function drop(Request $request)
@@ -88,12 +82,10 @@ class VlessController extends Controller
if ($request->input('id')) {
$server = ServerVless::find($request->input('id'));
if (!$server) {
throw new ApiException(500, '节点ID不存在');
return $this->fail([400202,'节点不存在']);
}
}
return response([
'data' => $server->delete()
]);
return $this->success($server->delete());
}
public function update(Request $request)
@@ -105,17 +97,16 @@ class VlessController extends Controller
$server = ServerVless::find($request->input('id'));
if (!$server) {
throw new ApiException(500, '该服务器不存在');
return $this->fail([400202, '该服务器不存在']);
}
try {
$server->update($params);
} catch (\Exception $e) {
throw new ApiException(500, '保存失败');
\Log::error($e);
return $this->fail([500, '保存失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
public function copy(Request $request)
@@ -123,14 +114,9 @@ class VlessController extends Controller
$server = ServerVless::find($request->input('id'));
$server->show = 0;
if (!$server) {
throw new ApiException(500, '服务器不存在');
return $this->fail([400202, '服务器不存在']);
}
if (!ServerVless::create($server->toArray())) {
throw new ApiException(500, '复制失败');
}
return response([
'data' => true
]);
ServerVless::create($server->toArray());
return $this->success(true);
}
}
@@ -18,25 +18,20 @@ class VmessController extends Controller
if ($request->input('id')) {
$server = ServerVmess::find($request->input('id'));
if (!$server) {
throw new ApiException(500, '服务器不存在');
return $this->fail([400202, '服务器不存在']);
}
try {
$server->update($params);
} catch (\Exception $e) {
throw new ApiException(500, '保存失败');
\Log::error($e);
return $this->fail([500, '保存失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
if (!ServerVmess::create($params)) {
throw new ApiException(500, '创建失败');
}
ServerVmess::create($params);
return response([
'data' => true
]);
return $this->success(true);
}
public function drop(Request $request)
@@ -44,12 +39,10 @@ class VmessController extends Controller
if ($request->input('id')) {
$server = ServerVmess::find($request->input('id'));
if (!$server) {
throw new ApiException(500, '节点ID不存在');
return $this->fail([400202, '节点不存在']);
}
}
return response([
'data' => $server->delete()
]);
return $this->success($server->delete());
}
public function update(ServerVmessUpdate $request)
@@ -61,17 +54,16 @@ class VmessController extends Controller
$server = ServerVmess::find($request->input('id'));
if (!$server) {
throw new ApiException(500, '该服务器不存在');
return $this->fail([400202, '该服务器不存在']);
}
try {
$server->update($params);
} catch (\Exception $e) {
throw new ApiException(500, '保存失败');
\Log::error($e);
return $this->fail([500, '保存失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
public function copy(Request $request)
@@ -79,14 +71,9 @@ class VmessController extends Controller
$server = ServerVmess::find($request->input('id'));
$server->show = 0;
if (!$server) {
throw new ApiException(500, '服务器不存在');
return $this->fail([400202, '服务器不存在']);
}
if (!ServerVmess::create($server->toArray())) {
throw new ApiException(500, '复制失败');
}
return response([
'data' => true
]);
ServerVmess::create($server->toArray());
return $this->success(true);
}
}