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
@@ -19,9 +19,7 @@ class ConfigController extends Controller
$files = array_map(function ($item) use ($path) {
return str_replace($path, '', $item);
}, glob($path . '*'));
return response([
'data' => $files
]);
return $this->success($files);
}
public function getThemeTemplate()
@@ -30,9 +28,7 @@ class ConfigController extends Controller
$files = array_map(function ($item) use ($path) {
return str_replace($path, '', $item);
}, glob($path . '*'));
return response([
'data' => $files
]);
return $this->success($files);
}
public function testSendMail(Request $request)
@@ -59,9 +55,7 @@ class ConfigController extends Controller
$telegramService = new TelegramService($request->input('telegram_bot_token'));
$telegramService->getMe();
$telegramService->setWebhook($hookUrl);
return response([
'data' => true
]);
return $this->success(true);
}
public function fetch(Request $request)
@@ -163,16 +157,12 @@ class ConfigController extends Controller
]
];
if ($key && isset($data[$key])) {
return response([
'data' => [
$key => $data[$key]
]
return $this->success([
$key => $data[$key]
]);
};
// TODO: default should be in Dict
return response([
'data' => $data
]);
return $this->success($data);
}
public function save(ConfigSave $request)
@@ -196,8 +186,6 @@ class ConfigController extends Controller
Cache::forget('admin_settings');
// \Artisan::call('horizon:terminate'); //重启队列使配置生效
return response([
'data' => true
]);
return $this->success(true);
}
}
@@ -31,21 +31,21 @@ class CouponController extends Controller
public function show(Request $request)
{
if (empty($request->input('id'))) {
throw new ApiException(422, '参数有误');
}
$request->validate([
'id' => 'required|numeric'
],[
'id.required' => '优惠券ID不能为空',
'id.numeric' => '优惠券ID必须为数字'
]);
$coupon = Coupon::find($request->input('id'));
if (!$coupon) {
throw new ApiException(500, '优惠券不存在');
return $this->fail([400202,'优惠券不存在']);
}
$coupon->show = !$coupon->show;
if (!$coupon->save()) {
throw new ApiException(500, '保存失败');
return $this->fail([500,'保存失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
public function generate(CouponGenerate $request)
@@ -61,19 +61,18 @@ class CouponController extends Controller
$params['code'] = Helper::randomChar(8);
}
if (!Coupon::create($params)) {
throw new ApiException(500, '创建失败');
return $this->fail([500,'创建失败']);
}
} else {
try {
Coupon::find($request->input('id'))->update($params);
} catch (\Exception $e) {
throw new ApiException(500, '保存失败');
\Log::error($e);
return $this->fail([500,'保存失败']);
}
}
return response([
'data' => true
]);
return $this->success(true);
}
private function multiGenerate(CouponGenerate $request)
@@ -99,12 +98,12 @@ class CouponController extends Controller
}
return $item;
}, $coupons))) {
throw new ApiException(500, '生成失败');
throw new \Exception();
}
DB::commit();
}catch(\Exception $e){
DB::rollBack();
throw $e;
return $this->fail([500, '生成失败']);
}
$data = "名称,类型,金额或比例,开始时间,结束时间,可用次数,可用于订阅,券码,生成时间\r\n";
@@ -123,19 +122,20 @@ class CouponController extends Controller
public function drop(Request $request)
{
if (empty($request->input('id'))) {
throw new ApiException(422, '参数有误');
}
$request->validate([
'id' => 'required|numeric'
],[
'id.required' => '优惠券ID不能为空',
'id.numeric' => '优惠券ID必须为数字'
]);
$coupon = Coupon::find($request->input('id'));
if (!$coupon) {
throw new ApiException(500, '优惠券不存在');
return $this->fail([400202,'优惠券不存在']);
}
if (!$coupon->delete()) {
throw new ApiException(500, '删除失败');
return $this->fail([500,'删除失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
}
@@ -16,23 +16,18 @@ class KnowledgeController extends Controller
{
if ($request->input('id')) {
$knowledge = Knowledge::find($request->input('id'))->toArray();
if (!$knowledge) throw new ApiException(500, '知识不存在');
return response([
'data' => $knowledge
]);
if (!$knowledge) return $this->fail([400202,'知识不存在']);
return $this->success($knowledge);
}
return response([
'data' => Knowledge::select(['title', 'id', 'updated_at', 'category', 'show'])
->orderBy('sort', 'ASC')
->get()
]);
$data = Knowledge::select(['title', 'id', 'updated_at', 'category', 'show'])
->orderBy('sort', 'ASC')
->get();
return $this->success($data);
}
public function getCategory(Request $request)
{
return response([
'data' => array_keys(Knowledge::get()->groupBy('category')->toArray())
]);
return $this->success(array_keys(Knowledge::get()->groupBy('category')->toArray()));
}
public function save(KnowledgeSave $request)
@@ -41,38 +36,37 @@ class KnowledgeController extends Controller
if (!$request->input('id')) {
if (!Knowledge::create($params)) {
throw new ApiException(500, '创建失败');
return $this->fail([500,'创建失败']);
}
} else {
try {
Knowledge::find($request->input('id'))->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 show(Request $request)
{
if (empty($request->input('id'))) {
throw new ApiException(422, '参数有误');
}
$request->validate([
'id' => 'required|numeric'
],[
'id.required' => '知识库ID不能为空'
]);
$knowledge = Knowledge::find($request->input('id'));
if (!$knowledge) {
throw new ApiException(500, '知识不存在');
throw new ApiException('知识不存在');
}
$knowledge->show = $knowledge->show ? 0 : 1;
$knowledge->show = !$knowledge->show;
if (!$knowledge->save()) {
throw new ApiException(500, '保存失败');
throw new ApiException('保存失败');
}
return response([
'data' => true
]);
return $this->success(true);
}
public function sort(KnowledgeSort $request)
@@ -87,28 +81,26 @@ class KnowledgeController extends Controller
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
throw new ApiException(500, '保存失败');
throw new ApiException('保存失败');
}
return response([
'data' => true
]);
return $this->success(true);
}
public function drop(Request $request)
{
if (empty($request->input('id'))) {
throw new ApiException(422, '参数有误');
}
$request->validate([
'id' => 'required|numeric'
],[
'id.required' => '知识库ID不能为空'
]);
$knowledge = Knowledge::find($request->input('id'));
if (!$knowledge) {
throw new ApiException(500, '知识不存在');
return $this->fail([400202,'知识不存在']);
}
if (!$knowledge->delete()) {
throw new ApiException(500, '删除失败');
return $this->fail([500,'删除失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
}
@@ -12,9 +12,7 @@ class NoticeController extends Controller
{
public function fetch(Request $request)
{
return response([
'data' => Notice::orderBy('id', 'DESC')->get()
]);
return $this->success(Notice::orderBy('id', 'DESC')->get());
}
public function save(NoticeSave $request)
@@ -27,18 +25,16 @@ class NoticeController extends Controller
]);
if (!$request->input('id')) {
if (!Notice::create($data)) {
throw new ApiException(500, '保存失败');
return $this->fail([500 ,'保存失败']);
}
} else {
try {
Notice::find($request->input('id'))->update($data);
} catch (\Exception $e) {
throw new ApiException(500, '保存失败');
return $this->fail([500 ,'保存失败']);
}
}
return response([
'data' => true
]);
return $this->success(true);
}
@@ -46,36 +42,32 @@ class NoticeController extends Controller
public function show(Request $request)
{
if (empty($request->input('id'))) {
throw new ApiException(422, '参数有误');
return $this->fail([500 ,'公告ID不能为空']);
}
$notice = Notice::find($request->input('id'));
if (!$notice) {
throw new ApiException(500, '公告不存在');
return $this->fail([400202 ,'公告不存在']);
}
$notice->show = $notice->show ? 0 : 1;
if (!$notice->save()) {
throw new ApiException(500, '保存失败');
return $this->fail([500 ,'保存失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
public function drop(Request $request)
{
if (empty($request->input('id'))) {
throw new ApiException(422, '参数错误');
return $this->fail([422 ,'公告ID不能为空']);
}
$notice = Notice::find($request->input('id'));
if (!$notice) {
throw new ApiException(500, '公告不存在');
return $this->fail([400202 ,'公告不存在']);
}
if (!$notice->delete()) {
throw new ApiException(500, '删除失败');
return $this->fail([500 ,'删除失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
}
@@ -41,14 +41,12 @@ class OrderController extends Controller
public function detail(Request $request)
{
$order = Order::find($request->input('id'));
if (!$order) throw new ApiException(500, '订单不存在');
if (!$order) return $this->fail([400202 ,'订单不存在']);
$order['commission_log'] = CommissionLog::where('trade_no', $order->trade_no)->get();
if ($order->surplus_order_ids) {
$order['surplus_orders'] = Order::whereIn('id', $order->surplus_order_ids)->get();
}
return response([
'data' => $order
]);
return $this->success($order);
}
public function fetch(OrderFetch $request)
@@ -84,17 +82,15 @@ class OrderController extends Controller
$order = Order::where('trade_no', $request->input('trade_no'))
->first();
if (!$order) {
throw new ApiException(500, '订单不存在');
return $this->fail([400202 ,'订单不存在']);
}
if ($order->status !== 0) throw new ApiException(500, '只能对待支付的订单进行操作');
if ($order->status !== 0) return $this->fail([400 ,'只能对待支付的订单进行操作']);
$orderService = new OrderService($order);
if (!$orderService->paid('manual_operation')) {
throw new ApiException(500, '更新失败');
return $this->fail([500 ,'更新失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
public function cancel(Request $request)
@@ -102,17 +98,15 @@ class OrderController extends Controller
$order = Order::where('trade_no', $request->input('trade_no'))
->first();
if (!$order) {
throw new ApiException(500, '订单不存在');
return $this->fail([400202 ,'订单不存在']);
}
if ($order->status !== 0) throw new ApiException(500, '只能对待支付的订单进行操作');
if ($order->status !== 0) return $this->fail([400 ,'只能对待支付的订单进行操作']);
$orderService = new OrderService($order);
if (!$orderService->cancel()) {
throw new ApiException(500, '更新失败');
return $this->fail([400 ,'更新失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
public function update(OrderUpdate $request)
@@ -124,18 +118,17 @@ class OrderController extends Controller
$order = Order::where('trade_no', $request->input('trade_no'))
->first();
if (!$order) {
throw new ApiException(500, '订单不存在');
return $this->fail([400202 ,'订单不存在']);
}
try {
$order->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 assign(OrderAssign $request)
@@ -144,16 +137,16 @@ class OrderController extends Controller
$user = User::where('email', $request->input('email'))->first();
if (!$user) {
throw new ApiException(500, '该用户不存在');
return $this->fail([400202 ,'该用户不存在']);
}
if (!$plan) {
throw new ApiException(500, '该订阅不存在');
return $this->fail([400202 ,'该订阅不存在']);
}
$userService = new UserService();
if ($userService->isNotCompleteOrderByUserId($user->id)) {
throw new ApiException(500, '该用户还有待支付的订单,无法分配');
return $this->fail([400 ,'该用户还有待支付的订单,无法分配']);
}
try {
@@ -179,7 +172,7 @@ class OrderController extends Controller
$orderService->setInvite($user);
if (!$order->save()) {
throw new ApiException(500, '订单创建失败');
return $this->fail([500 ,'订单创建失败']);
}
DB::commit();
}catch(\Exception $e){
@@ -187,8 +180,6 @@ class OrderController extends Controller
throw $e;
}
return response([
'data' => $order->trade_no
]);
return $this->success($order->trade_no);
}
}
@@ -18,9 +18,7 @@ class PaymentController extends Controller
foreach (glob(base_path('app//Payments') . '/*.php') as $file) {
array_push($methods, pathinfo($file)['filename']);
}
return response([
'data' => $methods
]);
return $this->success($methods);
}
public function fetch()
@@ -34,34 +32,28 @@ class PaymentController extends Controller
}
$payments[$k]['notify_url'] = $notifyUrl;
}
return response([
'data' => $payments
]);
return $this->success($payments);
}
public function getPaymentForm(Request $request)
{
$paymentService = new PaymentService($request->input('payment'), $request->input('id'));
return response([
'data' => $paymentService->form()
]);
return $this->success($paymentService->form());
}
public function show(Request $request)
{
$payment = Payment::find($request->input('id'));
if (!$payment) throw new ApiException(500, '支付方式不存在');
if (!$payment) return $this->fail([400202 ,'支付方式不存在']);
$payment->enable = !$payment->enable;
if (!$payment->save()) throw new ApiException(500, '保存失败');
return response([
'data' => true
]);
if (!$payment->save()) return $this->fail([500 ,'保存失败']);
return $this->success(true);
}
public function save(Request $request)
{
if (!admin_setting('app_url')) {
throw new ApiException(500, '请在站点配置中配置站点地址');
return $this->fail([400 ,'请在站点配置中配置站点地址']);
}
$params = $request->validate([
'name' => 'required',
@@ -81,32 +73,27 @@ class PaymentController extends Controller
]);
if ($request->input('id')) {
$payment = Payment::find($request->input('id'));
if (!$payment) throw new ApiException(500, '支付方式不存在');
if (!$payment) return $this->fail([400202 ,'支付方式不存在']);
try {
$payment->update($params);
} catch (\Exception $e) {
throw new ApiException(500, $e->getMessage());
\Log::error($e);
return $this->fail([500 ,'保存失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
$params['uuid'] = Helper::randomChar(8);
if (!Payment::create($params)) {
throw new ApiException(500, '保存失败');
return $this->fail([500 ,'保存失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
public function drop(Request $request)
{
$payment = Payment::find($request->input('id'));
if (!$payment) throw new ApiException(500, '支付方式不存在');
return response([
'data' => $payment->delete()
]);
if (!$payment) return $this->fail([400202 ,'支付方式不存在']);
return $this->success($payment->delete());
}
@@ -122,17 +109,15 @@ class PaymentController extends Controller
DB::beginTransaction();
foreach ($request->input('ids') as $k => $v) {
if (!Payment::find($v)->update(['sort' => $k + 1])) {
throw new ApiException(500, '保存失败');
throw new \Exception();
}
}
DB::commit();
}catch(\Exception $e){
DB::rollBack();
throw $e;
return $this->fail([500 ,'保存失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
}
@@ -26,9 +26,7 @@ class PlanController extends Controller
if ($plans[$k]->id === $counts[$kk]->plan_id) $plans[$k]->count = $counts[$kk]->count;
}
}
return response([
'data' => $plans
]);
return $this->success($plans);
}
public function save(PlanSave $request)
@@ -37,7 +35,7 @@ class PlanController extends Controller
if ($request->input('id')) {
$plan = Plan::find($request->input('id'));
if (!$plan) {
throw new ApiException(500, '该订阅不存在');
return $this->fail([400202 ,'该订阅不存在']);
}
DB::beginTransaction();
// update user group id and transfer
@@ -53,37 +51,32 @@ class PlanController extends Controller
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
throw new ApiException(500, '保存失败');
\Log::error($e);
return $this->fail([500 ,'保存失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
if (!Plan::create($params)) {
throw new ApiException(500, '创建失败');
return $this->fail([500 ,'创建失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
public function drop(Request $request)
{
if (Order::where('plan_id', $request->input('id'))->first()) {
throw new ApiException(500, '该订阅下存在订单无法删除');
return $this->fail([400201 ,'该订阅下存在订单无法删除']);
}
if (User::where('plan_id', $request->input('id'))->first()) {
throw new ApiException(500, '该订阅下存在用户无法删除');
return $this->fail([400201 ,'该订阅下存在用户无法删除']);
}
if ($request->input('id')) {
$plan = Plan::find($request->input('id'));
if (!$plan) {
throw new ApiException(500, '该订阅ID不存在');
return $this->fail([400202 ,'该订阅不存在']);
}
}
return response([
'data' => $plan->delete()
]);
return $this->success($plan->delete());
}
public function update(PlanUpdate $request)
@@ -95,13 +88,14 @@ class PlanController extends Controller
$plan = Plan::find($request->input('id'));
if (!$plan) {
throw new ApiException(500, '该订阅不存在');
return $this->fail([400202 ,'该订阅不存在']);
}
try {
$plan->update($updateData);
} catch (\Exception $e) {
throw new ApiException(500, '保存失败');
\Log::error($e);
return $this->fail([500 ,'保存失败']);
}
return $this->success();
@@ -114,13 +108,14 @@ class PlanController extends Controller
DB::beginTransaction();
foreach ($request->input('plan_ids') as $k => $v) {
if (!Plan::find($v)->update(['sort' => $k + 1])) {
throw new ApiException(500, '保存失败');
throw new \Exception();
}
}
DB::commit();
}catch (\Exception $e){
DB::rollBack();
throw $e;
\Log::error($e);
return $this->fail([500 ,'保存失败']);
}
return $this->success(true);
}
@@ -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);
}
}
@@ -20,20 +20,17 @@ class SystemController extends Controller
{
public function getSystemStatus()
{
return response([
'data' => [
'schedule' => $this->getScheduleStatus(),
'horizon' => $this->getHorizonStatus(),
'schedule_last_runtime' => Cache::get(CacheKey::get('SCHEDULE_LAST_CHECK_AT', null))
]
]);
$data = [
'schedule' => $this->getScheduleStatus(),
'horizon' => $this->getHorizonStatus(),
'schedule_last_runtime' => Cache::get(CacheKey::get('SCHEDULE_LAST_CHECK_AT', null))
];
return $this->success($data);
}
public function getQueueWorkload(WorkloadRepository $workload)
{
return response([
'data' => collect($workload->get())->sortBy('name')->values()->toArray()
]);
return $this->success(collect($workload->get())->sortBy('name')->values()->toArray());
}
protected function getScheduleStatus():bool
@@ -54,23 +51,22 @@ class SystemController extends Controller
public function getQueueStats()
{
return response([
'data' => [
'failedJobs' => app(JobRepository::class)->countRecentlyFailed(),
'jobsPerMinute' => app(MetricsRepository::class)->jobsProcessedPerMinute(),
'pausedMasters' => $this->totalPausedMasters(),
'periods' => [
'failedJobs' => config('horizon.trim.recent_failed', config('horizon.trim.failed')),
'recentJobs' => config('horizon.trim.recent'),
],
'processes' => $this->totalProcessCount(),
'queueWithMaxRuntime' => app(MetricsRepository::class)->queueWithMaximumRuntime(),
'queueWithMaxThroughput' => app(MetricsRepository::class)->queueWithMaximumThroughput(),
'recentJobs' => app(JobRepository::class)->countRecent(),
'status' => $this->getHorizonStatus(),
'wait' => collect(app(WaitTimeCalculator::class)->calculate())->take(1),
]
]);
$data = [
'failedJobs' => app(JobRepository::class)->countRecentlyFailed(),
'jobsPerMinute' => app(MetricsRepository::class)->jobsProcessedPerMinute(),
'pausedMasters' => $this->totalPausedMasters(),
'periods' => [
'failedJobs' => config('horizon.trim.recent_failed', config('horizon.trim.failed')),
'recentJobs' => config('horizon.trim.recent'),
],
'processes' => $this->totalProcessCount(),
'queueWithMaxRuntime' => app(MetricsRepository::class)->queueWithMaximumRuntime(),
'queueWithMaxThroughput' => app(MetricsRepository::class)->queueWithMaximumThroughput(),
'recentJobs' => app(JobRepository::class)->countRecent(),
'status' => $this->getHorizonStatus(),
'wait' => collect(app(WaitTimeCalculator::class)->calculate())->take(1),
];
return $this->success($data);
}
/**
@@ -34,12 +34,11 @@ class ThemeController extends Controller
$themeService = new ThemeService($theme);
$themeService->init();
}
return response([
'data' => [
'themes' => $themeConfigs,
'active' => admin_setting('frontend_theme', 'Xboard')
]
]);
$data = [
'themes' => $themeConfigs,
'active' => admin_setting('frontend_theme', 'Xboard')
];
return $this->success($data);
}
public function getThemeConfig(Request $request)
@@ -47,9 +46,7 @@ class ThemeController extends Controller
$payload = $request->validate([
'name' => 'required|in:' . join(',', $this->themes)
]);
return response([
'data' => admin_setting("theme_{$payload['name']}") ?? config("theme.{$payload['name']}")
]);
return $this->success(admin_setting("theme_{$payload['name']}") ?? config("theme.{$payload['name']}"));
}
public function saveThemeConfig(Request $request)
@@ -59,11 +56,11 @@ class ThemeController extends Controller
'config' => 'required'
]);
$payload['config'] = json_decode(base64_decode($payload['config']), true);
if (!$payload['config'] || !is_array($payload['config'])) throw new ApiException(422, '参数有误');
if (!$payload['config'] || !is_array($payload['config'])) return $this->fail([422,'参数不正确']);
$themeConfigFile = public_path("theme/{$payload['name']}/config.json");
if (!File::exists($themeConfigFile)) throw new ApiException(500, '主题不存在');
if (!File::exists($themeConfigFile)) return $this->fail([400202,'主题不存在']);
$themeConfig = json_decode(File::get($themeConfigFile), true);
if (!isset($themeConfig['configs']) || !is_array($themeConfig)) throw new ApiException(500, '主题配置文件有误');
if (!isset($themeConfig['configs']) || !is_array($themeConfig)) return $this->fail([422,'主题配置文件有误']);
$validateFields = array_column($themeConfig['configs'], 'field_name');
$config = [];
foreach ($validateFields as $validateField) {
@@ -77,11 +74,8 @@ class ThemeController extends Controller
admin_setting(["theme_{$payload['name']}" => $config]);
// sleep(2);
} catch (\Exception $e) {
throw new ApiException(500, '保存失败');
return $this->fail([200002, '保存失败']);
}
return response([
'data' => $config
]);
return $this->success($config);
}
}
@@ -8,6 +8,7 @@ use App\Models\Ticket;
use App\Models\TicketMessage;
use App\Models\User;
use App\Services\TicketService;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
class TicketController extends Controller
@@ -18,7 +19,7 @@ class TicketController extends Controller
$ticket = Ticket::where('id', $request->input('id'))
->first();
if (!$ticket) {
throw new ApiException(500, '工单不存在');
return $this->fail([400202,'工单不存在']);
}
$ticket['message'] = TicketMessage::where('ticket_id', $ticket->id)->get();
for ($i = 0; $i < count($ticket['message']); $i++) {
@@ -28,9 +29,7 @@ class TicketController extends Controller
$ticket['message'][$i]['is_me'] = false;
}
}
return response([
'data' => $ticket
]);
return $this->success($ticket);
}
$current = $request->input('current') ? $request->input('current') : 1;
$pageSize = $request->input('pageSize') >= 10 ? $request->input('pageSize') : 10;
@@ -56,39 +55,38 @@ class TicketController extends Controller
public function reply(Request $request)
{
if (empty($request->input('id'))) {
throw new ApiException(422, '参数错误');
}
if (empty($request->input('message'))) {
throw new ApiException(500, '消息不能为空');
}
$request->validate([
'id' => 'required|numeric',
'message' => 'required|string'
],[
'id.required' => '工单ID不能为空',
'message.required' => '消息不能为空'
]);
$ticketService = new TicketService();
$ticketService->replyByAdmin(
$request->input('id'),
$request->input('message'),
$request->user['id']
);
return response([
'data' => true
]);
return $this->success(true);
}
public function close(Request $request)
{
if (empty($request->input('id'))) {
throw new ApiException(422, '参数错误');
}
$ticket = Ticket::where('id', $request->input('id'))
->first();
if (!$ticket) {
throw new ApiException(500, '工单不存在');
}
$ticket->status = 1;
if (!$ticket->save()) {
throw new ApiException(500, '关闭失败');
}
return response([
'data' => true
$request->validate([
'id' => 'required|numeric'
],[
'id.required' => '工单ID不能为空'
]);
try {
$ticket = Ticket::findOrFail($request->input('id'));
$ticket->status = 1;
$ticket->save();
return $this->success(true);
} catch (ModelNotFoundException $e) {
return $this->fail([400202, '工单不存在']);
} catch (\Exception $e) {
return $this->fail([500101, '关闭失败']);
}
}
}
@@ -21,12 +21,10 @@ class UserController extends Controller
public function resetSecret(Request $request)
{
$user = User::find($request->input('id'));
if (!$user) throw new ApiException(500, '用户不存在');
if (!$user) return $this->fail([400202,'用户不存在']);
$user->token = Helper::guid();
$user->uuid = Helper::guid(true);
return response([
'data' => $user->save()
]);
return $this->success($user->save());
}
private function filter(Request $request, $builder)
@@ -85,46 +83,45 @@ class UserController extends Controller
public function getUserInfoById(Request $request)
{
if (empty($request->input('id'))) {
throw new ApiException(422, '参数错误');
}
$user = User::find($request->input('id'));
if ($user->invite_user_id) {
$user['invite_user'] = User::find($user->invite_user_id);
}
return response([
'data' => $user
$request->validate([
'id'=> 'required|numeric'
],[
'id.required' => '用户ID不能为空'
]);
$user = User::find($request->input('id'))->load('invite_user');
return $this->success($user);
}
public function update(UserUpdate $request)
{
$params = $request->validated();
$user = User::find($request->input('id'));
if (!$user) {
throw new ApiException(500, '用户不存在');
return $this->fail([400202, '用户不存在']);
}
// 检查邮箱是否被使用
if (User::where('email', $params['email'])->first() && $user->email !== $params['email']) {
throw new ApiException(500, '邮箱已被使用');
return $this->fail([400201, '邮箱已被使用']);
}
// 处理密码
if (isset($params['password'])) {
$params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
$params['password_algo'] = NULL;
} else {
unset($params['password']);
}
// 处理订阅计划
if (isset($params['plan_id'])) {
$plan = Plan::find($params['plan_id']);
if (!$plan) {
throw new ApiException(500, '订阅计划不存在');
return $this->fail([400202, '订阅计划不存在']);
}
$params['group_id'] = $plan->group_id;
}
if ($request->input('invite_user_email')) {
$inviteUser = User::where('email', $request->input('invite_user_email'))->first();
if ($inviteUser) {
$params['invite_user_id'] = $inviteUser->id;
}
// 处理邀请用户
if ($request->input('invite_user_email') && $inviteUser = User::where('email', $request->input('invite_user_email'))->first()) {
$params['invite_user_id'] = $inviteUser->id;
} else {
$params['invite_user_id'] = null;
}
@@ -137,11 +134,10 @@ class UserController extends Controller
try {
$user->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 dumpCSV(Request $request)
@@ -178,7 +174,7 @@ class UserController extends Controller
if ($request->input('plan_id')) {
$plan = Plan::find($request->input('plan_id'));
if (!$plan) {
throw new ApiException(500, '订阅计划不存在');
return $this->fail([400202,'订阅计划不存在']);
}
}
$user = [
@@ -191,15 +187,13 @@ class UserController extends Controller
'token' => Helper::guid()
];
if (User::where('email', $user['email'])->first()) {
throw new ApiException(500, '邮箱已存在于系统中');
return $this->fail([400201,'邮箱已存在于系统中']);
}
$user['password'] = password_hash($request->input('password') ?? $user['email'], PASSWORD_DEFAULT);
if (!User::create($user)) {
throw new ApiException(500, '生成失败');
return $this->fail([500,'生成失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
if ($request->input('generate_count')) {
$this->multiGenerate($request);
@@ -211,7 +205,7 @@ class UserController extends Controller
if ($request->input('plan_id')) {
$plan = Plan::find($request->input('plan_id'));
if (!$plan) {
throw new ApiException(500, '订阅计划不存在');
return $this->fail([400202,'订阅计划不存在']);
}
}
$users = [];
@@ -233,12 +227,13 @@ class UserController extends Controller
try{
DB::beginTransaction();
if (!User::insert($users)) {
throw new ApiException(500, '生成失败');
throw new \Exception();
}
DB::commit();
}catch(\Exception $e){
DB::rollBack();
throw $e;
\Log::error($e);
return $this->fail([500,'生成失败']);
}
$data = "账号,密码,过期时间,UUID,创建时间,订阅地址\r\n";
foreach($users as $user) {
@@ -272,9 +267,7 @@ class UserController extends Controller
'send_email_mass');
}
return response([
'data' => true
]);
return $this->success(true);
}
public function ban(Request $request)
@@ -288,11 +281,10 @@ class UserController extends Controller
'banned' => 1
]);
} catch (\Exception $e) {
throw new ApiException(500, '处理失败');
\Log::error($e);
return $this->fail([500,'处理失败']);
}
return response([
'data' => true
]);
return $this->success(true);
}
}