refactor: 规范Expection处理

This commit is contained in:
xboard
2023-12-04 20:40:49 +08:00
parent aa0fe64afe
commit 0ab7dee52d
65 changed files with 625 additions and 362 deletions
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\CouponGenerate;
use App\Http\Requests\Admin\CouponSave;
@@ -31,15 +32,15 @@ class CouponController extends Controller
public function show(Request $request)
{
if (empty($request->input('id'))) {
abort(500, '参数有误');
throw new ApiException(422, '参数有误');
}
$coupon = Coupon::find($request->input('id'));
if (!$coupon) {
abort(500, '优惠券不存在');
throw new ApiException(500, '优惠券不存在');
}
$coupon->show = $coupon->show ? 0 : 1;
$coupon->show = !$coupon->show;
if (!$coupon->save()) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
@@ -60,13 +61,13 @@ class CouponController extends Controller
$params['code'] = Helper::randomChar(8);
}
if (!Coupon::create($params)) {
abort(500, '创建失败');
throw new ApiException(500, '创建失败');
}
} else {
try {
Coupon::find($request->input('id'))->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
}
@@ -98,7 +99,7 @@ class CouponController extends Controller
return $item;
}, $coupons))) {
DB::rollBack();
abort(500, '生成失败');
throw new ApiException(500, '生成失败');
}
DB::commit();
$data = "名称,类型,金额或比例,开始时间,结束时间,可用次数,可用于订阅,券码,生成时间\r\n";
@@ -118,14 +119,14 @@ class CouponController extends Controller
public function drop(Request $request)
{
if (empty($request->input('id'))) {
abort(500, '参数有误');
throw new ApiException(422, '参数有误');
}
$coupon = Coupon::find($request->input('id'));
if (!$coupon) {
abort(500, '优惠券不存在');
throw new ApiException(500, '优惠券不存在');
}
if (!$coupon->delete()) {
abort(500, '删除失败');
throw new ApiException(500, '删除失败');
}
return response([
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\KnowledgeSave;
use App\Http\Requests\Admin\KnowledgeSort;
@@ -15,7 +16,7 @@ class KnowledgeController extends Controller
{
if ($request->input('id')) {
$knowledge = Knowledge::find($request->input('id'))->toArray();
if (!$knowledge) abort(500, '知识不存在');
if (!$knowledge) throw new ApiException(500, '知识不存在');
return response([
'data' => $knowledge
]);
@@ -40,13 +41,13 @@ class KnowledgeController extends Controller
if (!$request->input('id')) {
if (!Knowledge::create($params)) {
abort(500, '创建失败');
throw new ApiException(500, '创建失败');
}
} else {
try {
Knowledge::find($request->input('id'))->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
}
@@ -58,15 +59,15 @@ class KnowledgeController extends Controller
public function show(Request $request)
{
if (empty($request->input('id'))) {
abort(500, '参数有误');
throw new ApiException(422, '参数有误');
}
$knowledge = Knowledge::find($request->input('id'));
if (!$knowledge) {
abort(500, '知识不存在');
throw new ApiException(500, '知识不存在');
}
$knowledge->show = $knowledge->show ? 0 : 1;
if (!$knowledge->save()) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
@@ -85,7 +86,7 @@ class KnowledgeController extends Controller
}
} catch (\Exception $e) {
DB::rollBack();
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
DB::commit();
return response([
@@ -96,14 +97,14 @@ class KnowledgeController extends Controller
public function drop(Request $request)
{
if (empty($request->input('id'))) {
abort(500, '参数有误');
throw new ApiException(422, '参数有误');
}
$knowledge = Knowledge::find($request->input('id'));
if (!$knowledge) {
abort(500, '知识不存在');
throw new ApiException(500, '知识不存在');
}
if (!$knowledge->delete()) {
abort(500, '删除失败');
throw new ApiException(500, '删除失败');
}
return response([
@@ -2,11 +2,11 @@
namespace App\Http\Controllers\V1\Admin;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\NoticeSave;
use App\Models\Notice;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
class NoticeController extends Controller
{
@@ -27,13 +27,13 @@ class NoticeController extends Controller
]);
if (!$request->input('id')) {
if (!Notice::create($data)) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
} else {
try {
Notice::find($request->input('id'))->update($data);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
}
return response([
@@ -46,15 +46,15 @@ class NoticeController extends Controller
public function show(Request $request)
{
if (empty($request->input('id'))) {
abort(500, '参数有误');
throw new ApiException(422, '参数有误');
}
$notice = Notice::find($request->input('id'));
if (!$notice) {
abort(500, '公告不存在');
throw new ApiException(500, '公告不存在');
}
$notice->show = $notice->show ? 0 : 1;
if (!$notice->save()) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
@@ -65,14 +65,14 @@ class NoticeController extends Controller
public function drop(Request $request)
{
if (empty($request->input('id'))) {
abort(500, '参数错误');
throw new ApiException(422, '参数错误');
}
$notice = Notice::find($request->input('id'));
if (!$notice) {
abort(500, '公告不存在');
throw new ApiException(500, '公告不存在');
}
if (!$notice->delete()) {
abort(500, '删除失败');
throw new ApiException(500, '删除失败');
}
return response([
'data' => true
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\OrderAssign;
use App\Http\Requests\Admin\OrderFetch;
@@ -40,7 +41,7 @@ class OrderController extends Controller
public function detail(Request $request)
{
$order = Order::find($request->input('id'));
if (!$order) abort(500, '订单不存在');
if (!$order) throw new ApiException(500, '订单不存在');
$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();
@@ -83,13 +84,13 @@ class OrderController extends Controller
$order = Order::where('trade_no', $request->input('trade_no'))
->first();
if (!$order) {
abort(500, '订单不存在');
throw new ApiException(500, '订单不存在');
}
if ($order->status !== 0) abort(500, '只能对待支付的订单进行操作');
if ($order->status !== 0) throw new ApiException(500, '只能对待支付的订单进行操作');
$orderService = new OrderService($order);
if (!$orderService->paid('manual_operation')) {
abort(500, '更新失败');
throw new ApiException(500, '更新失败');
}
return response([
'data' => true
@@ -101,13 +102,13 @@ class OrderController extends Controller
$order = Order::where('trade_no', $request->input('trade_no'))
->first();
if (!$order) {
abort(500, '订单不存在');
throw new ApiException(500, '订单不存在');
}
if ($order->status !== 0) abort(500, '只能对待支付的订单进行操作');
if ($order->status !== 0) throw new ApiException(500, '只能对待支付的订单进行操作');
$orderService = new OrderService($order);
if (!$orderService->cancel()) {
abort(500, '更新失败');
throw new ApiException(500, '更新失败');
}
return response([
'data' => true
@@ -123,13 +124,13 @@ class OrderController extends Controller
$order = Order::where('trade_no', $request->input('trade_no'))
->first();
if (!$order) {
abort(500, '订单不存在');
throw new ApiException(500, '订单不存在');
}
try {
$order->update($params);
} catch (\Exception $e) {
abort(500, '更新失败');
throw new ApiException(500, '更新失败');
}
return response([
@@ -143,16 +144,16 @@ class OrderController extends Controller
$user = User::where('email', $request->input('email'))->first();
if (!$user) {
abort(500, '该用户不存在');
throw new ApiException(500, '该用户不存在');
}
if (!$plan) {
abort(500, '该订阅不存在');
throw new ApiException(500, '该订阅不存在');
}
$userService = new UserService();
if ($userService->isNotCompleteOrderByUserId($user->id)) {
abort(500, '该用户还有待支付的订单,无法分配');
throw new ApiException(500, '该用户还有待支付的订单,无法分配');
}
DB::beginTransaction();
@@ -178,7 +179,7 @@ class OrderController extends Controller
if (!$order->save()) {
DB::rollback();
abort(500, '订单创建失败');
throw new ApiException(500, '订单创建失败');
}
DB::commit();
@@ -2,8 +2,8 @@
namespace App\Http\Controllers\V1\Admin;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\PaymentSave;
use App\Models\Payment;
use App\Services\PaymentService;
use App\Utils\Helper;
@@ -50,9 +50,9 @@ class PaymentController extends Controller
public function show(Request $request)
{
$payment = Payment::find($request->input('id'));
if (!$payment) abort(500, '支付方式不存在');
if (!$payment) throw new ApiException(500, '支付方式不存在');
$payment->enable = !$payment->enable;
if (!$payment->save()) abort(500, '保存失败');
if (!$payment->save()) throw new ApiException(500, '保存失败');
return response([
'data' => true
]);
@@ -61,7 +61,7 @@ class PaymentController extends Controller
public function save(Request $request)
{
if (!admin_setting('app_url')) {
abort(500, '请在站点配置中配置站点地址');
throw new ApiException(500, '请在站点配置中配置站点地址');
}
$params = $request->validate([
'name' => 'required',
@@ -81,11 +81,11 @@ class PaymentController extends Controller
]);
if ($request->input('id')) {
$payment = Payment::find($request->input('id'));
if (!$payment) abort(500, '支付方式不存在');
if (!$payment) throw new ApiException(500, '支付方式不存在');
try {
$payment->update($params);
} catch (\Exception $e) {
abort(500, $e->getMessage());
throw new ApiException(500, $e->getMessage());
}
return response([
'data' => true
@@ -93,7 +93,7 @@ class PaymentController extends Controller
}
$params['uuid'] = Helper::randomChar(8);
if (!Payment::create($params)) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
'data' => true
@@ -103,7 +103,7 @@ class PaymentController extends Controller
public function drop(Request $request)
{
$payment = Payment::find($request->input('id'));
if (!$payment) abort(500, '支付方式不存在');
if (!$payment) throw new ApiException(500, '支付方式不存在');
return response([
'data' => $payment->delete()
]);
@@ -122,7 +122,7 @@ class PaymentController extends Controller
foreach ($request->input('ids') as $k => $v) {
if (!Payment::find($v)->update(['sort' => $k + 1])) {
DB::rollBack();
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
}
DB::commit();
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\PlanSave;
use App\Http\Requests\Admin\PlanSort;
@@ -36,7 +37,7 @@ class PlanController extends Controller
if ($request->input('id')) {
$plan = Plan::find($request->input('id'));
if (!$plan) {
abort(500, '该订阅不存在');
throw new ApiException(500, '该订阅不存在');
}
DB::beginTransaction();
// update user group id and transfer
@@ -51,7 +52,7 @@ class PlanController extends Controller
$plan->update($params);
} catch (\Exception $e) {
DB::rollBack();
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
DB::commit();
return response([
@@ -59,7 +60,7 @@ class PlanController extends Controller
]);
}
if (!Plan::create($params)) {
abort(500, '创建失败');
throw new ApiException(500, '创建失败');
}
return response([
'data' => true
@@ -69,15 +70,15 @@ class PlanController extends Controller
public function drop(Request $request)
{
if (Order::where('plan_id', $request->input('id'))->first()) {
abort(500, '该订阅下存在订单无法删除');
throw new ApiException(500, '该订阅下存在订单无法删除');
}
if (User::where('plan_id', $request->input('id'))->first()) {
abort(500, '该订阅下存在用户无法删除');
throw new ApiException(500, '该订阅下存在用户无法删除');
}
if ($request->input('id')) {
$plan = Plan::find($request->input('id'));
if (!$plan) {
abort(500, '该订阅ID不存在');
throw new ApiException(500, '该订阅ID不存在');
}
}
return response([
@@ -94,13 +95,13 @@ class PlanController extends Controller
$plan = Plan::find($request->input('id'));
if (!$plan) {
abort(500, '该订阅不存在');
throw new ApiException(500, '该订阅不存在');
}
try {
$plan->update($updateData);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
@@ -114,7 +115,7 @@ class PlanController extends Controller
foreach ($request->input('plan_ids') as $k => $v) {
if (!Plan::find($v)->update(['sort' => $k + 1])) {
DB::rollBack();
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
}
DB::commit();
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin\Server;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Models\Plan;
use App\Models\ServerGroup;
@@ -39,7 +40,7 @@ class GroupController extends Controller
public function save(Request $request)
{
if (empty($request->input('name'))) {
abort(500, '组名不能为空');
throw new ApiException(500, '组名不能为空');
}
if ($request->input('id')) {
@@ -59,22 +60,22 @@ class GroupController extends Controller
if ($request->input('id')) {
$serverGroup = ServerGroup::find($request->input('id'));
if (!$serverGroup) {
abort(500, '组不存在');
throw new ApiException(500, '组不存在');
}
}
$servers = ServerVmess::all();
foreach ($servers as $server) {
if (in_array($request->input('id'), $server->group_id)) {
abort(500, '该组已被节点所使用,无法删除');
throw new ApiException(500, '该组已被节点所使用,无法删除');
}
}
if (Plan::where('group_id', $request->input('id'))->first()) {
abort(500, '该组已被订阅所使用,无法删除');
throw new ApiException(500, '该组已被订阅所使用,无法删除');
}
if (User::where('group_id', $request->input('id'))->first()) {
abort(500, '该组已被用户所使用,无法删除');
throw new ApiException(500, '该组已被用户所使用,无法删除');
}
return response([
'data' => $serverGroup->delete()
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin\Server;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Models\ServerHysteria;
use Illuminate\Http\Request;
@@ -44,12 +45,12 @@ class HysteriaController extends Controller
if ($request->input('id')) {
$server = ServerHysteria::find($request->input('id'));
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
'data' => true
@@ -57,7 +58,7 @@ class HysteriaController extends Controller
}
if (!ServerHysteria::create($params)) {
abort(500, '创建失败');
throw new ApiException(500, '创建失败');
}
return response([
@@ -70,7 +71,7 @@ class HysteriaController extends Controller
if ($request->input('id')) {
$server = ServerHysteria::find($request->input('id'));
if (!$server) {
abort(500, '节点ID不存在');
throw new ApiException(500, '节点ID不存在');
}
}
return response([
@@ -92,12 +93,12 @@ class HysteriaController extends Controller
$server = ServerHysteria::find($request->input('id'));
if (!$server) {
abort(500, '该服务器不存在');
throw new ApiException(500, '该服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
@@ -110,10 +111,10 @@ class HysteriaController extends Controller
$server = ServerHysteria::find($request->input('id'));
$server->show = 0;
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
if (!ServerHysteria::create($server->toArray())) {
abort(500, '复制失败');
throw new ApiException(500, '复制失败');
}
return response([
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin\Server;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Services\ServerService;
use Illuminate\Http\Request;
@@ -33,7 +34,7 @@ class ManageController extends Controller
foreach($v as $id => $sort) {
if (!$model::find($id)->update(['sort' => $sort])) {
DB::rollBack();
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
}
}
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin\Server;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Models\ServerRoute;
use Illuminate\Http\Request;
@@ -47,10 +48,10 @@ class RouteController extends Controller
'data' => true
];
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
}
if (!ServerRoute::create($params)) abort(500, '创建失败');
if (!ServerRoute::create($params)) throw new ApiException(500, '创建失败');
return [
'data' => true
];
@@ -59,8 +60,8 @@ class RouteController extends Controller
public function drop(Request $request)
{
$route = ServerRoute::find($request->input('id'));
if (!$route) abort(500, '路由不存在');
if (!$route->delete()) abort(500, '删除失败');
if (!$route) throw new ApiException(500, '路由不存在');
if (!$route->delete()) throw new ApiException(500, '删除失败');
return [
'data' => true
];
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin\Server;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\ServerShadowsocksSave;
use App\Http\Requests\Admin\ServerShadowsocksUpdate;
@@ -16,12 +17,12 @@ class ShadowsocksController extends Controller
if ($request->input('id')) {
$server = ServerShadowsocks::find($request->input('id'));
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
'data' => true
@@ -29,7 +30,7 @@ class ShadowsocksController extends Controller
}
if (!ServerShadowsocks::create($params)) {
abort(500, '创建失败');
throw new ApiException(500, '创建失败');
}
return response([
@@ -42,7 +43,7 @@ class ShadowsocksController extends Controller
if ($request->input('id')) {
$server = ServerShadowsocks::find($request->input('id'));
if (!$server) {
abort(500, '节点ID不存在');
throw new ApiException(500, '节点ID不存在');
}
}
return response([
@@ -59,12 +60,12 @@ class ShadowsocksController extends Controller
$server = ServerShadowsocks::find($request->input('id'));
if (!$server) {
abort(500, '该服务器不存在');
throw new ApiException(500, '该服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
@@ -77,10 +78,10 @@ class ShadowsocksController extends Controller
$server = ServerShadowsocks::find($request->input('id'));
$server->show = 0;
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
if (!ServerShadowsocks::create($server->toArray())) {
abort(500, '复制失败');
throw new ApiException(500, '复制失败');
}
return response([
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin\Server;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\ServerTrojanSave;
use App\Http\Requests\Admin\ServerTrojanUpdate;
@@ -17,12 +18,12 @@ class TrojanController extends Controller
if ($request->input('id')) {
$server = ServerTrojan::find($request->input('id'));
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
'data' => true
@@ -30,7 +31,7 @@ class TrojanController extends Controller
}
if (!ServerTrojan::create($params)) {
abort(500, '创建失败');
throw new ApiException(500, '创建失败');
}
return response([
@@ -43,7 +44,7 @@ class TrojanController extends Controller
if ($request->input('id')) {
$server = ServerTrojan::find($request->input('id'));
if (!$server) {
abort(500, '节点ID不存在');
throw new ApiException(500, '节点ID不存在');
}
}
return response([
@@ -60,12 +61,12 @@ class TrojanController extends Controller
$server = ServerTrojan::find($request->input('id'));
if (!$server) {
abort(500, '该服务器不存在');
throw new ApiException(500, '该服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
@@ -78,10 +79,10 @@ class TrojanController extends Controller
$server = ServerTrojan::find($request->input('id'));
$server->show = 0;
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
if (!ServerTrojan::create($server->toArray())) {
abort(500, '复制失败');
throw new ApiException(500, '复制失败');
}
return response([
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin\Server;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Models\ServerVless;
use Illuminate\Http\Request;
@@ -61,12 +62,12 @@ class VlessController extends Controller
if ($request->input('id')) {
$server = ServerVless::find($request->input('id'));
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
'data' => true
@@ -74,7 +75,7 @@ class VlessController extends Controller
}
if (!ServerVless::create($params)) {
abort(500, '创建失败');
throw new ApiException(500, '创建失败');
}
return response([
@@ -87,7 +88,7 @@ class VlessController extends Controller
if ($request->input('id')) {
$server = ServerVless::find($request->input('id'));
if (!$server) {
abort(500, '节点ID不存在');
throw new ApiException(500, '节点ID不存在');
}
}
return response([
@@ -104,12 +105,12 @@ class VlessController extends Controller
$server = ServerVless::find($request->input('id'));
if (!$server) {
abort(500, '该服务器不存在');
throw new ApiException(500, '该服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
@@ -122,10 +123,10 @@ class VlessController extends Controller
$server = ServerVless::find($request->input('id'));
$server->show = 0;
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
if (!ServerVless::create($server->toArray())) {
abort(500, '复制失败');
throw new ApiException(500, '复制失败');
}
return response([
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin\Server;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\ServerVmessSave;
use App\Http\Requests\Admin\ServerVmessUpdate;
@@ -17,12 +18,12 @@ class VmessController extends Controller
if ($request->input('id')) {
$server = ServerVmess::find($request->input('id'));
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
'data' => true
@@ -30,7 +31,7 @@ class VmessController extends Controller
}
if (!ServerVmess::create($params)) {
abort(500, '创建失败');
throw new ApiException(500, '创建失败');
}
return response([
@@ -43,7 +44,7 @@ class VmessController extends Controller
if ($request->input('id')) {
$server = ServerVmess::find($request->input('id'));
if (!$server) {
abort(500, '节点ID不存在');
throw new ApiException(500, '节点ID不存在');
}
}
return response([
@@ -60,12 +61,12 @@ class VmessController extends Controller
$server = ServerVmess::find($request->input('id'));
if (!$server) {
abort(500, '该服务器不存在');
throw new ApiException(500, '该服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
@@ -78,10 +79,10 @@ class VmessController extends Controller
$server = ServerVmess::find($request->input('id'));
$server->show = 0;
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
if (!ServerVmess::create($server->toArray())) {
abort(500, '复制失败');
throw new ApiException(500, '复制失败');
}
return response([
@@ -2,10 +2,10 @@
namespace App\Http\Controllers\V1\Admin;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Services\ThemeService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
class ThemeController extends Controller
@@ -59,11 +59,11 @@ class ThemeController extends Controller
'config' => 'required'
]);
$payload['config'] = json_decode(base64_decode($payload['config']), true);
if (!$payload['config'] || !is_array($payload['config'])) abort(500, '参数有误');
if (!$payload['config'] || !is_array($payload['config'])) throw new ApiException(422, '参数有误');
$themeConfigFile = public_path("theme/{$payload['name']}/config.json");
if (!File::exists($themeConfigFile)) abort(500, '主题不存在');
if (!File::exists($themeConfigFile)) throw new ApiException(500, '主题不存在');
$themeConfig = json_decode(File::get($themeConfigFile), true);
if (!isset($themeConfig['configs']) || !is_array($themeConfig)) abort(500, '主题配置文件有误');
if (!isset($themeConfig['configs']) || !is_array($themeConfig)) throw new ApiException(500, '主题配置文件有误');
$validateFields = array_column($themeConfig['configs'], 'field_name');
$config = [];
foreach ($validateFields as $validateField) {
@@ -77,7 +77,7 @@ class ThemeController extends Controller
admin_setting(["theme_{$payload['name']}" => $config]);
// sleep(2);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
@@ -2,14 +2,13 @@
namespace App\Http\Controllers\V1\Admin;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Models\Ticket;
use App\Models\TicketMessage;
use App\Models\User;
use App\Services\TicketService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
class TicketController extends Controller
{
@@ -19,7 +18,7 @@ class TicketController extends Controller
$ticket = Ticket::where('id', $request->input('id'))
->first();
if (!$ticket) {
abort(500, '工单不存在');
throw new ApiException(500, '工单不存在');
}
$ticket['message'] = TicketMessage::where('ticket_id', $ticket->id)->get();
for ($i = 0; $i < count($ticket['message']); $i++) {
@@ -58,10 +57,10 @@ class TicketController extends Controller
public function reply(Request $request)
{
if (empty($request->input('id'))) {
abort(500, '参数错误');
throw new ApiException(422, '参数错误');
}
if (empty($request->input('message'))) {
abort(500, '消息不能为空');
throw new ApiException(500, '消息不能为空');
}
$ticketService = new TicketService();
$ticketService->replyByAdmin(
@@ -77,16 +76,16 @@ class TicketController extends Controller
public function close(Request $request)
{
if (empty($request->input('id'))) {
abort(500, '参数错误');
throw new ApiException(422, '参数错误');
}
$ticket = Ticket::where('id', $request->input('id'))
->first();
if (!$ticket) {
abort(500, '工单不存在');
throw new ApiException(500, '工单不存在');
}
$ticket->status = 1;
if (!$ticket->save()) {
abort(500, '关闭失败');
throw new ApiException(500, '关闭失败');
}
return response([
'data' => true
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\UserFetch;
use App\Http\Requests\Admin\UserGenerate;
@@ -20,7 +21,7 @@ class UserController extends Controller
public function resetSecret(Request $request)
{
$user = User::find($request->input('id'));
if (!$user) abort(500, '用户不存在');
if (!$user) throw new ApiException(500, '用户不存在');
$user->token = Helper::guid();
$user->uuid = Helper::guid(true);
return response([
@@ -85,7 +86,7 @@ class UserController extends Controller
public function getUserInfoById(Request $request)
{
if (empty($request->input('id'))) {
abort(500, '参数错误');
throw new ApiException(422, '参数错误');
}
$user = User::find($request->input('id'));
if ($user->invite_user_id) {
@@ -101,10 +102,10 @@ class UserController extends Controller
$params = $request->validated();
$user = User::find($request->input('id'));
if (!$user) {
abort(500, '用户不存在');
throw new ApiException(500, '用户不存在');
}
if (User::where('email', $params['email'])->first() && $user->email !== $params['email']) {
abort(500, '邮箱已被使用');
throw new ApiException(500, '邮箱已被使用');
}
if (isset($params['password'])) {
$params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
@@ -115,7 +116,7 @@ class UserController extends Controller
if (isset($params['plan_id'])) {
$plan = Plan::find($params['plan_id']);
if (!$plan) {
abort(500, '订阅计划不存在');
throw new ApiException(500, '订阅计划不存在');
}
$params['group_id'] = $plan->group_id;
}
@@ -136,7 +137,7 @@ class UserController extends Controller
try {
$user->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
'data' => true
@@ -177,7 +178,7 @@ class UserController extends Controller
if ($request->input('plan_id')) {
$plan = Plan::find($request->input('plan_id'));
if (!$plan) {
abort(500, '订阅计划不存在');
throw new ApiException(500, '订阅计划不存在');
}
}
$user = [
@@ -190,11 +191,11 @@ class UserController extends Controller
'token' => Helper::guid()
];
if (User::where('email', $user['email'])->first()) {
abort(500, '邮箱已存在于系统中');
throw new ApiException(500, '邮箱已存在于系统中');
}
$user['password'] = password_hash($request->input('password') ?? $user['email'], PASSWORD_DEFAULT);
if (!User::create($user)) {
abort(500, '生成失败');
throw new ApiException(500, '生成失败');
}
return response([
'data' => true
@@ -210,7 +211,7 @@ class UserController extends Controller
if ($request->input('plan_id')) {
$plan = Plan::find($request->input('plan_id'));
if (!$plan) {
abort(500, '订阅计划不存在');
throw new ApiException(500, '订阅计划不存在');
}
}
$users = [];
@@ -232,7 +233,7 @@ class UserController extends Controller
DB::beginTransaction();
if (!User::insert($users)) {
DB::rollBack();
abort(500, '生成失败');
throw new ApiException(500, '生成失败');
}
DB::commit();
$data = "账号,密码,过期时间,UUID,创建时间,订阅地址\r\n";
@@ -283,7 +284,7 @@ class UserController extends Controller
'banned' => 1
]);
} catch (\Exception $e) {
abort(500, '处理失败');
throw new ApiException(500, '处理失败');
}
return response([