diff --git a/app/Http/Controllers/Admin/OrderController.php b/app/Http/Controllers/Admin/OrderController.php index c245f5de..be3ab3ad 100644 --- a/app/Http/Controllers/Admin/OrderController.php +++ b/app/Http/Controllers/Admin/OrderController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Admin; use App\Http\Requests\Admin\OrderUpdate; +use App\Services\OrderService; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Models\Order; @@ -48,7 +49,7 @@ class OrderController extends Controller public function update(OrderUpdate $request) { - $updateData = $request->only([ + $params = $request->only([ 'status', 'commission_status' ]); @@ -59,8 +60,18 @@ class OrderController extends Controller abort(500, '订单不存在'); } + if ((int)$params['status'] === 2) { + $orderService = new OrderService($order); + if (!$orderService->cancel()) { + abort(500, '更新失败'); + } + return response([ + 'data' => true + ]); + } + try { - $order->update($updateData); + $order->update($params); } catch (\Exception $e) { abort(500, '更新失败'); } diff --git a/app/Http/Controllers/User/OrderController.php b/app/Http/Controllers/User/OrderController.php index 3fb954a2..36daf4cf 100755 --- a/app/Http/Controllers/User/OrderController.php +++ b/app/Http/Controllers/User/OrderController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers\User; use App\Http\Controllers\Controller; use App\Http\Requests\User\OrderSave; +use App\Services\OrderService; use App\Services\UserService; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; @@ -395,8 +396,8 @@ class OrderController extends Controller if ($order->status !== 0) { abort(500, '只可以取消待支付订单'); } - $order->status = 2; - if (!$order->save()) { + $orderService = new OrderService($order); + if (!$orderService->cancel()) { abort(500, '取消失败'); } return response([ diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index 803bde4d..6bacdca8 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -14,20 +14,23 @@ class OrderService $this->order = $order; } - public function cancel():void + public function cancel():bool { $order = $this->order; DB::beginTransaction(); $order->status = 2; if (!$order->save()) { DB::rollBack(); + 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; } }