From 1fcb6fa9118932e14846fff52f9e5c1676dd68b9 Mon Sep 17 00:00:00 2001 From: xboard Date: Wed, 6 Dec 2023 19:00:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=84=E8=8C=83=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E4=BA=8B=E7=89=A9=E7=9A=84=E4=BD=BF=E7=94=A8=EF=BC=8C?= =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=9C=A8swoole=E7=8E=AF=E5=A2=83=E4=B8=8B?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E4=BC=9A=E5=87=BA=E7=8E=B0=E4=BA=8B=E7=89=A9?= =?UTF-8?q?=E4=B8=80=E7=9B=B4=E4=B8=8D=E8=A2=AB=E6=8F=90=E4=BA=A4=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/CheckCommission.php | 23 ++-- app/Http/Controllers/Controller.php | 3 +- .../Controllers/V1/Admin/CouponController.php | 29 ++--- .../V1/Admin/KnowledgeController.php | 4 +- .../Controllers/V1/Admin/OrderController.php | 53 +++++----- .../V1/Admin/PaymentController.php | 17 +-- .../Controllers/V1/Admin/PlanController.php | 27 ++--- .../V1/Admin/Server/ManageController.php | 24 +++-- .../Controllers/V1/Admin/UserController.php | 12 ++- .../Controllers/V1/User/OrderController.php | 86 +++++++-------- .../Controllers/V1/User/TicketController.php | 100 ++++++++++-------- app/Services/OrderService.php | 100 +++++++++--------- app/Services/TicketService.php | 66 +++++++----- 13 files changed, 293 insertions(+), 251 deletions(-) diff --git a/app/Console/Commands/CheckCommission.php b/app/Console/Commands/CheckCommission.php index a552920..3c3589b 100644 --- a/app/Console/Commands/CheckCommission.php +++ b/app/Console/Commands/CheckCommission.php @@ -64,17 +64,22 @@ class CheckCommission extends Command ->where('invite_user_id', '!=', NULL) ->get(); foreach ($orders as $order) { - DB::beginTransaction(); - if (!$this->payHandle($order->invite_user_id, $order)) { + try{ + DB::beginTransaction(); + if (!$this->payHandle($order->invite_user_id, $order)) { + DB::rollBack(); + continue; + } + $order->commission_status = 2; + if (!$order->save()) { + DB::rollBack(); + continue; + } + DB::commit(); + } catch (\Exception $e){ DB::rollBack(); - continue; + throw $e; } - $order->commission_status = 2; - if (!$order->save()) { - DB::rollBack(); - continue; - } - DB::commit(); } } diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index af5cfb1..205d632 100755 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -2,11 +2,12 @@ namespace App\Http\Controllers; +use App\Helpers\ApiResponse; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Routing\Controller as BaseController; class Controller extends BaseController { - use DispatchesJobs, ValidatesRequests; + use DispatchesJobs, ValidatesRequests, ApiResponse; } diff --git a/app/Http/Controllers/V1/Admin/CouponController.php b/app/Http/Controllers/V1/Admin/CouponController.php index aaf0fb6..a5b29cf 100644 --- a/app/Http/Controllers/V1/Admin/CouponController.php +++ b/app/Http/Controllers/V1/Admin/CouponController.php @@ -87,21 +87,26 @@ class CouponController extends Controller $coupon['code'] = Helper::randomChar(8); array_push($coupons, $coupon); } - DB::beginTransaction(); - if (!Coupon::insert(array_map(function ($item) use ($coupon) { - // format data - if (isset($item['limit_plan_ids']) && is_array($item['limit_plan_ids'])) { - $item['limit_plan_ids'] = json_encode($coupon['limit_plan_ids']); + try{ + DB::beginTransaction(); + if (!Coupon::insert(array_map(function ($item) use ($coupon) { + // format data + if (isset($item['limit_plan_ids']) && is_array($item['limit_plan_ids'])) { + $item['limit_plan_ids'] = json_encode($coupon['limit_plan_ids']); + } + if (isset($item['limit_period']) && is_array($item['limit_period'])) { + $item['limit_period'] = json_encode($coupon['limit_period']); + } + return $item; + }, $coupons))) { + throw new ApiException(500, '生成失败'); } - if (isset($item['limit_period']) && is_array($item['limit_period'])) { - $item['limit_period'] = json_encode($coupon['limit_period']); - } - return $item; - }, $coupons))) { + DB::commit(); + }catch(\Exception $e){ DB::rollBack(); - throw new ApiException(500, '生成失败'); + throw $e; } - DB::commit(); + $data = "名称,类型,金额或比例,开始时间,结束时间,可用次数,可用于订阅,券码,生成时间\r\n"; foreach($coupons as $coupon) { $type = ['', '金额', '比例'][$coupon['type']]; diff --git a/app/Http/Controllers/V1/Admin/KnowledgeController.php b/app/Http/Controllers/V1/Admin/KnowledgeController.php index db4f38b..25c7a71 100644 --- a/app/Http/Controllers/V1/Admin/KnowledgeController.php +++ b/app/Http/Controllers/V1/Admin/KnowledgeController.php @@ -77,18 +77,18 @@ class KnowledgeController extends Controller public function sort(KnowledgeSort $request) { - DB::beginTransaction(); try { + DB::beginTransaction(); foreach ($request->input('knowledge_ids') as $k => $v) { $knowledge = Knowledge::find($v); $knowledge->timestamps = false; $knowledge->update(['sort' => $k + 1]); } + DB::commit(); } catch (\Exception $e) { DB::rollBack(); throw new ApiException(500, '保存失败'); } - DB::commit(); return response([ 'data' => true ]); diff --git a/app/Http/Controllers/V1/Admin/OrderController.php b/app/Http/Controllers/V1/Admin/OrderController.php index ecdf394..293ec33 100644 --- a/app/Http/Controllers/V1/Admin/OrderController.php +++ b/app/Http/Controllers/V1/Admin/OrderController.php @@ -156,34 +156,37 @@ class OrderController extends Controller throw new ApiException(500, '该用户还有待支付的订单,无法分配'); } - DB::beginTransaction(); - $order = new Order(); - $orderService = new OrderService($order); - $order->user_id = $user->id; - $order->plan_id = $plan->id; - $order->period = $request->input('period'); - $order->trade_no = Helper::guid(); - $order->total_amount = $request->input('total_amount'); + try { + DB::beginTransaction(); + $order = new Order(); + $orderService = new OrderService($order); + $order->user_id = $user->id; + $order->plan_id = $plan->id; + $order->period = $request->input('period'); + $order->trade_no = Helper::guid(); + $order->total_amount = $request->input('total_amount'); - if ($order->period === 'reset_price') { - $order->type = 4; - } else if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id) { - $order->type = 3; - } else if ($user->expired_at > time() && $order->plan_id == $user->plan_id) { - $order->type = 2; - } else { - $order->type = 1; + if ($order->period === 'reset_price') { + $order->type = 4; + } else if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id) { + $order->type = 3; + } else if ($user->expired_at > time() && $order->plan_id == $user->plan_id) { + $order->type = 2; + } else { + $order->type = 1; + } + + $orderService->setInvite($user); + + if (!$order->save()) { + throw new ApiException(500, '订单创建失败'); + } + DB::commit(); + }catch(\Exception $e){ + DB::rollBack(); + throw $e; } - $orderService->setInvite($user); - - if (!$order->save()) { - DB::rollback(); - throw new ApiException(500, '订单创建失败'); - } - - DB::commit(); - return response([ 'data' => $order->trade_no ]); diff --git a/app/Http/Controllers/V1/Admin/PaymentController.php b/app/Http/Controllers/V1/Admin/PaymentController.php index 187bd73..e7c5223 100644 --- a/app/Http/Controllers/V1/Admin/PaymentController.php +++ b/app/Http/Controllers/V1/Admin/PaymentController.php @@ -118,14 +118,19 @@ class PaymentController extends Controller 'ids.required' => '参数有误', 'ids.array' => '参数有误' ]); - DB::beginTransaction(); - foreach ($request->input('ids') as $k => $v) { - if (!Payment::find($v)->update(['sort' => $k + 1])) { - DB::rollBack(); - throw new ApiException(500, '保存失败'); + try{ + DB::beginTransaction(); + foreach ($request->input('ids') as $k => $v) { + if (!Payment::find($v)->update(['sort' => $k + 1])) { + throw new ApiException(500, '保存失败'); + } } + DB::commit(); + }catch(\Exception $e){ + DB::rollBack(); + throw $e; } - DB::commit(); + return response([ 'data' => true ]); diff --git a/app/Http/Controllers/V1/Admin/PlanController.php b/app/Http/Controllers/V1/Admin/PlanController.php index a3758e7..b2a3293 100755 --- a/app/Http/Controllers/V1/Admin/PlanController.php +++ b/app/Http/Controllers/V1/Admin/PlanController.php @@ -50,11 +50,11 @@ class PlanController extends Controller ]); } $plan->update($params); + DB::commit(); } catch (\Exception $e) { DB::rollBack(); throw new ApiException(500, '保存失败'); } - DB::commit(); return response([ 'data' => true ]); @@ -104,23 +104,24 @@ class PlanController extends Controller throw new ApiException(500, '保存失败'); } - return response([ - 'data' => true - ]); + return $this->success(); } public function sort(PlanSort $request) { - DB::beginTransaction(); - foreach ($request->input('plan_ids') as $k => $v) { - if (!Plan::find($v)->update(['sort' => $k + 1])) { - DB::rollBack(); - throw new ApiException(500, '保存失败'); + + try{ + DB::beginTransaction(); + foreach ($request->input('plan_ids') as $k => $v) { + if (!Plan::find($v)->update(['sort' => $k + 1])) { + throw new ApiException(500, '保存失败'); + } } + DB::commit(); + }catch (\Exception $e){ + DB::rollBack(); + throw $e; } - DB::commit(); - return response([ - 'data' => true - ]); + return $this->success(true); } } diff --git a/app/Http/Controllers/V1/Admin/Server/ManageController.php b/app/Http/Controllers/V1/Admin/Server/ManageController.php index 367848e..0bfa05e 100644 --- a/app/Http/Controllers/V1/Admin/Server/ManageController.php +++ b/app/Http/Controllers/V1/Admin/Server/ManageController.php @@ -28,19 +28,21 @@ class ManageController extends Controller 'hysteria', 'vless' ) ?? []; - DB::beginTransaction(); - foreach ($params as $k => $v) { - $model = 'App\\Models\\Server' . ucfirst($k); - foreach($v as $id => $sort) { - if (!$model::find($id)->update(['sort' => $sort])) { - DB::rollBack(); - throw new ApiException(500, '保存失败'); + try{ + DB::beginTransaction(); + 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, '保存失败'); + } } } + DB::commit(); + }catch (\Exception $e){ + DB::rollBack(); + throw $e; } - DB::commit(); - return response([ - 'data' => true - ]); + return $this->success(true); } } diff --git a/app/Http/Controllers/V1/Admin/UserController.php b/app/Http/Controllers/V1/Admin/UserController.php index d6e3890..90d492a 100644 --- a/app/Http/Controllers/V1/Admin/UserController.php +++ b/app/Http/Controllers/V1/Admin/UserController.php @@ -230,12 +230,16 @@ class UserController extends Controller $user['password'] = password_hash($request->input('password') ?? $user['email'], PASSWORD_DEFAULT); array_push($users, $user); } - DB::beginTransaction(); - if (!User::insert($users)) { + try{ + DB::beginTransaction(); + if (!User::insert($users)) { + throw new ApiException(500, '生成失败'); + } + DB::commit(); + }catch(\Exception $e){ DB::rollBack(); - throw new ApiException(500, '生成失败'); + throw $e; } - DB::commit(); $data = "账号,密码,过期时间,UUID,创建时间,订阅地址\r\n"; foreach($users as $user) { $expireDate = $user['expired_at'] === NULL ? '长期有效' : date('Y-m-d H:i:s', $user['expired_at']); diff --git a/app/Http/Controllers/V1/User/OrderController.php b/app/Http/Controllers/V1/User/OrderController.php index 2bf620a..978a801 100755 --- a/app/Http/Controllers/V1/User/OrderController.php +++ b/app/Http/Controllers/V1/User/OrderController.php @@ -114,54 +114,54 @@ class OrderController extends Controller throw new ApiException(500, __('This subscription has expired, please change to another subscription')); } - DB::beginTransaction(); - $order = new Order(); - $orderService = new OrderService($order); - $order->user_id = $request->user['id']; - $order->plan_id = $plan->id; - $order->period = $request->input('period'); - $order->trade_no = Helper::generateOrderNo(); - $order->total_amount = $plan[$request->input('period')]; + try{ + DB::beginTransaction(); + $order = new Order(); + $orderService = new OrderService($order); + $order->user_id = $request->user['id']; + $order->plan_id = $plan->id; + $order->period = $request->input('period'); + $order->trade_no = Helper::generateOrderNo(); + $order->total_amount = $plan[$request->input('period')]; - if ($request->input('coupon_code')) { - $couponService = new CouponService($request->input('coupon_code')); - if (!$couponService->use($order)) { - DB::rollBack(); - throw new ApiException(500, __('Coupon failed')); - } - $order->coupon_id = $couponService->getId(); - } - - $orderService->setVipDiscount($user); - $orderService->setOrderType($user); - $orderService->setInvite($user); - - if ($user->balance && $order->total_amount > 0) { - $remainingBalance = $user->balance - $order->total_amount; - $userService = new UserService(); - if ($remainingBalance > 0) { - if (!$userService->addBalance($order->user_id, - $order->total_amount)) { - DB::rollBack(); - throw new ApiException(500, __('Insufficient balance')); + if ($request->input('coupon_code')) { + $couponService = new CouponService($request->input('coupon_code')); + if (!$couponService->use($order)) { + throw new ApiException(500, __('Coupon failed')); } - $order->balance_amount = $order->total_amount; - $order->total_amount = 0; - } else { - if (!$userService->addBalance($order->user_id, - $user->balance)) { - DB::rollBack(); - throw new ApiException(500, __('Insufficient balance')); - } - $order->balance_amount = $user->balance; - $order->total_amount = $order->total_amount - $user->balance; + $order->coupon_id = $couponService->getId(); } - } - if (!$order->save()) { - DB::rollback(); - throw new ApiException(500, __('Failed to create order')); - } + $orderService->setVipDiscount($user); + $orderService->setOrderType($user); + $orderService->setInvite($user); - DB::commit(); + if ($user->balance && $order->total_amount > 0) { + $remainingBalance = $user->balance - $order->total_amount; + $userService = new UserService(); + if ($remainingBalance > 0) { + if (!$userService->addBalance($order->user_id, - $order->total_amount)) { + throw new ApiException(500, __('Insufficient balance')); + } + $order->balance_amount = $order->total_amount; + $order->total_amount = 0; + } else { + if (!$userService->addBalance($order->user_id, - $user->balance)) { + throw new ApiException(500, __('Insufficient balance')); + } + $order->balance_amount = $user->balance; + $order->total_amount = $order->total_amount - $user->balance; + } + } + + if (!$order->save()) { + throw new ApiException(500, __('Failed to create order')); + } + DB::commit(); + }catch (\Exception $e){ + DB::rollBack(); + throw $e; + } return response([ 'data' => $order->trade_no diff --git a/app/Http/Controllers/V1/User/TicketController.php b/app/Http/Controllers/V1/User/TicketController.php index 7137e9a..44df938 100644 --- a/app/Http/Controllers/V1/User/TicketController.php +++ b/app/Http/Controllers/V1/User/TicketController.php @@ -48,30 +48,33 @@ class TicketController extends Controller public function save(TicketSave $request) { - DB::beginTransaction(); - if ((int)Ticket::where('status', 0)->where('user_id', $request->user['id'])->lockForUpdate()->count()) { - throw new ApiException(500, __('There are other unresolved tickets')); + try{ + DB::beginTransaction(); + if ((int)Ticket::where('status', 0)->where('user_id', $request->user['id'])->lockForUpdate()->count()) { + throw new ApiException(500, __('There are other unresolved tickets')); + } + $ticket = Ticket::create(array_merge($request->only([ + 'subject', + 'level' + ]), [ + 'user_id' => $request->user['id'] + ])); + if (!$ticket) { + throw new ApiException(500, __('Failed to open ticket')); + } + $ticketMessage = TicketMessage::create([ + 'user_id' => $request->user['id'], + 'ticket_id' => $ticket->id, + 'message' => $request->input('message') + ]); + if (!$ticketMessage) { + throw new ApiException(500, __('Failed to open ticket')); + } + DB::commit(); + }catch(\Exception $e){ + DB::rollBack(); + throw $e; } - $ticket = Ticket::create(array_merge($request->only([ - 'subject', - 'level' - ]), [ - 'user_id' => $request->user['id'] - ])); - if (!$ticket) { - DB::rollback(); - throw new ApiException(500, __('Failed to open ticket')); - } - $ticketMessage = TicketMessage::create([ - 'user_id' => $request->user['id'], - 'ticket_id' => $ticket->id, - 'message' => $request->input('message') - ]); - if (!$ticketMessage) { - DB::rollback(); - throw new ApiException(500, __('Failed to open ticket')); - } - DB::commit(); $this->sendNotify($ticket, $request->input('message')); return response([ 'data' => true @@ -156,31 +159,34 @@ class TicketController extends Controller if ($limit > ($user->commission_balance / 100)) { throw new ApiException(500, __('The current required minimum withdrawal commission is :limit', ['limit' => $limit])); } - DB::beginTransaction(); - $subject = __('[Commission Withdrawal Request] This ticket is opened by the system'); - $ticket = Ticket::create([ - 'subject' => $subject, - 'level' => 2, - 'user_id' => $request->user['id'] - ]); - if (!$ticket) { - DB::rollback(); - throw new ApiException(500, __('Failed to open ticket')); + try{ + DB::beginTransaction(); + $subject = __('[Commission Withdrawal Request] This ticket is opened by the system'); + $ticket = Ticket::create([ + 'subject' => $subject, + 'level' => 2, + 'user_id' => $request->user['id'] + ]); + if (!$ticket) { + throw new ApiException(500, __('Failed to open ticket')); + } + $message = sprintf("%s\r\n%s", + __('Withdrawal method') . ":" . $request->input('withdraw_method'), + __('Withdrawal account') . ":" . $request->input('withdraw_account') + ); + $ticketMessage = TicketMessage::create([ + 'user_id' => $request->user['id'], + 'ticket_id' => $ticket->id, + 'message' => $message + ]); + if (!$ticketMessage) { + throw new ApiException(500, __('Failed to open ticket')); + } + DB::commit(); + }catch(\Exception $e){ + DB::rollBack(); + throw $e; } - $message = sprintf("%s\r\n%s", - __('Withdrawal method') . ":" . $request->input('withdraw_method'), - __('Withdrawal account') . ":" . $request->input('withdraw_account') - ); - $ticketMessage = TicketMessage::create([ - 'user_id' => $request->user['id'], - 'ticket_id' => $ticket->id, - 'message' => $message - ]); - if (!$ticketMessage) { - DB::rollback(); - throw new ApiException(500, __('Failed to open ticket')); - } - DB::commit(); $this->sendNotify($ticket, $message); return response([ 'data' => true diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index 2f0e5af..b541492 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -36,53 +36,51 @@ class OrderService if ($order->refund_amount) { $this->user->balance = $this->user->balance + $order->refund_amount; } - DB::beginTransaction(); - if ($order->surplus_order_ids) { - try { + try{ + DB::beginTransaction(); + if ($order->surplus_order_ids) { Order::whereIn('id', $order->surplus_order_ids)->update([ 'status' => 4 ]); - } catch (\Exception $e) { - DB::rollback(); - throw new ApiException(500, '开通失败'); } - } - switch ((string)$order->period) { - case 'onetime_price': - $this->buyByOneTime($plan); - break; - case 'reset_price': - $this->buyByResetTraffic(); - break; - default: - $this->buyByPeriod($order, $plan); - } + switch ((string)$order->period) { + case 'onetime_price': + $this->buyByOneTime($plan); + break; + case 'reset_price': + $this->buyByResetTraffic(); + break; + default: + $this->buyByPeriod($order, $plan); + } - switch ((int)$order->type) { - case 1: - $this->openEvent(admin_setting('new_order_event_id', 0)); - break; - case 2: - $this->openEvent(admin_setting('renew_order_event_id', 0)); - break; - case 3: - $this->openEvent(admin_setting('change_order_event_id', 0)); - break; - } + switch ((int)$order->type) { + case 1: + $this->openEvent(admin_setting('new_order_event_id', 0)); + break; + case 2: + $this->openEvent(admin_setting('renew_order_event_id', 0)); + break; + case 3: + $this->openEvent(admin_setting('change_order_event_id', 0)); + break; + } - $this->setSpeedLimit($plan->speed_limit); + $this->setSpeedLimit($plan->speed_limit); - if (!$this->user->save()) { + if (!$this->user->save()) { + throw new \Exception('用户信息保存失败'); + } + $order->status = 3; + if (!$order->save()) { + throw new \Exception('订单信息保存失败'); + } + DB::commit(); + }catch(\Exception $e){ DB::rollBack(); + \Log::error($e); throw new ApiException(500, '开通失败'); } - $order->status = 3; - if (!$order->save()) { - DB::rollBack(); - throw new ApiException(500, '开通失败'); - } - - DB::commit(); } @@ -233,21 +231,25 @@ class OrderService public function cancel():bool { $order = $this->order; - DB::beginTransaction(); - $order->status = 2; - if (!$order->save()) { + try { + DB::beginTransaction(); + $order->status = 2; + if (!$order->save()) { + throw new \Exception('Failed to save order status.'); + } + if ($order->balance_amount) { + $userService = new UserService(); + if (!$userService->addBalance($order->user_id, $order->balance_amount)) { + throw new \Exception('Failed to add balance.'); + } + } + DB::commit(); + return true; + }catch(\Exception $e){ DB::rollBack(); + \Log::error($e); return false; } - if ($order->balance_amount) { - $userService = new UserService(); - if (!$userService->addBalance($order->user_id, $order->balance_amount)) { - DB::rollBack(); - return false; - } - } - DB::commit(); - return true; } private function setSpeedLimit($speedLimit) diff --git a/app/Services/TicketService.php b/app/Services/TicketService.php index 985e145..a6d1659 100644 --- a/app/Services/TicketService.php +++ b/app/Services/TicketService.php @@ -13,23 +13,27 @@ use Illuminate\Support\Facades\DB; class TicketService { public function reply($ticket, $message, $userId) { - DB::beginTransaction(); - $ticketMessage = TicketMessage::create([ - 'user_id' => $userId, - 'ticket_id' => $ticket->id, - 'message' => $message - ]); - if ($userId !== $ticket->user_id) { - $ticket->reply_status = 0; - } else { - $ticket->reply_status = 1; - } - if (!$ticketMessage || !$ticket->save()) { + try{ + DB::beginTransaction(); + $ticketMessage = TicketMessage::create([ + 'user_id' => $userId, + 'ticket_id' => $ticket->id, + 'message' => $message + ]); + if ($userId !== $ticket->user_id) { + $ticket->reply_status = 0; + } else { + $ticket->reply_status = 1; + } + if (!$ticketMessage || !$ticket->save()) { + throw new \Exception(); + } + DB::commit(); + return $ticketMessage; + }catch(\Exception $e){ DB::rollback(); return false; } - DB::commit(); - return $ticketMessage; } public function replyByAdmin($ticketId, $message, $userId):void @@ -40,22 +44,26 @@ class TicketService { throw new ApiException(500, '工单不存在'); } $ticket->status = 0; - DB::beginTransaction(); - $ticketMessage = TicketMessage::create([ - 'user_id' => $userId, - 'ticket_id' => $ticket->id, - 'message' => $message - ]); - if ($userId !== $ticket->user_id) { - $ticket->reply_status = 0; - } else { - $ticket->reply_status = 1; + try{ + DB::beginTransaction(); + $ticketMessage = TicketMessage::create([ + 'user_id' => $userId, + 'ticket_id' => $ticket->id, + 'message' => $message + ]); + if ($userId !== $ticket->user_id) { + $ticket->reply_status = 0; + } else { + $ticket->reply_status = 1; + } + if (!$ticketMessage || !$ticket->save()) { + throw new ApiException(500, '工单回复失败'); + } + DB::commit(); + }catch(\Exception $e){ + DB::rollBack(); + throw $e; } - if (!$ticketMessage || !$ticket->save()) { - DB::rollback(); - throw new ApiException(500, '工单回复失败'); - } - DB::commit(); $this->sendEmailNotify($ticket, $ticketMessage); }