lip 1 周之前
父節點
當前提交
d5af7cf257
共有 2 個文件被更改,包括 115 次插入30 次删除
  1. 113 30
      app/Http/Controllers/admin/Wallet.php
  2. 2 0
      routes/admin.php

+ 113 - 30
app/Http/Controllers/admin/Wallet.php

@@ -61,6 +61,7 @@ class Wallet extends Controller
         return $this->success($data);
     }
 
+    //扣款
     public function debiting()
     {
         DB::beginTransaction();
@@ -69,8 +70,6 @@ class Wallet extends Controller
                 'amount' => ['required', 'numeric', 'min:0.01'],
                 'member_id' => ['required', 'string', 'min:1'],
                 'remark' => ['required', 'string', 'min:1'],
-                'order_type' => ['nullable'],
-                'order_id' => ['nullable'],
             ]);
             $memberId = request()->input('member_id');
             $amount = request()->input('amount');
@@ -91,19 +90,6 @@ class Wallet extends Controller
             $wallet->available_balance = $availableBalance;
             $wallet->save();
 
-            if (!empty($params['order_type']) && !empty($params['order_id'])) {
-                if ($params['order_type'] == 'sport') {
-                    $info = Order::where('id', $params['order_id'])->first();
-                } elseif ($params['order_type'] == 'lhc') {
-                    $info = LhcOrder::where('id', $params['order_id'])->first();
-                }
-                if (!$info) {
-                    throw new \Exception('订单不存在');
-                } else {
-                    $info->remark = $info->remark ? $info->remark. '|' . $params['remark'] : $params['remark'];
-                    $info->save();
-                }
-            }
             DB::commit();
         } catch (ValidationException $e) {
             DB::rollBack();
@@ -129,8 +115,6 @@ class Wallet extends Controller
                 'member_id' => ['required', 'string', 'min:1'],
                 'remark' => ['required', 'string', 'min:1'],
                 'change_type' => ['required', 'string', 'in:' . implode(',', BalanceLogService::$manualRecharge)],
-                'order_type' => ['nullable'],
-                'order_id' => ['nullable'],
             ]);
             $memberId = request()->input('member_id');
             $amount = request()->input('amount');
@@ -150,19 +134,6 @@ class Wallet extends Controller
             $wallet->available_balance = $availableBalance;
             $wallet->save();
 
-            if (!empty($params['order_type']) && !empty($params['order_id'])) {
-                if ($params['order_type'] == 'sport') {
-                    $info = Order::where('id', $params['order_id'])->first();
-                } elseif ($params['order_type'] == 'lhc') {
-                    $info = LhcOrder::where('id', $params['order_id'])->first();
-                }
-                if (!$info) {
-                    throw new \Exception('订单不存在');
-                } else {
-                    $info->remark = $info->remark ? $info->remark. '|' . $params['remark'] : $params['remark'];
-                    $info->save();
-                }
-            }
             DB::commit();
         } catch (ValidationException $e) {
             DB::rollBack();
@@ -179,6 +150,118 @@ class Wallet extends Controller
         return $this->success();
     }
 
+    //订单充值
+    public function orderTopUp()
+    {
+        DB::beginTransaction();
+        try {
+            request()->validate([
+                'amount' => ['required', 'numeric', 'min:0.01'],
+                'remark' => ['required', 'string', 'min:1'],
+                'order_type' => ['nullable'],
+                'order_id' => ['nullable'],
+            ]);
+            $order_type = request()->input('order_type');
+            $order_id = request()->input('order_id');
+            $amount = request()->input('amount');
+            $remark = request()->input('remark');
+            $changeType = "人工充值";
+            $key = 'api_request_' . md5($order_id . json_encode([
+                        'amount' => $amount,
+                        'remark' => $remark,
+                        'action' => "wallet/orderTopUp"
+                    ]));
+            if (Cache::has($key)) throw new Exception("请求太频繁,请稍后再试。", HttpStatus::CUSTOM_ERROR);
+            Cache::put($key, true, 3);
+
+            if ($order_type == 'sport') {
+                $info = Order::where('id', $order_id)->first();
+            } elseif ($order_type == 'lhc') {
+                $info = LhcOrder::where('id', $order_id)->first();
+            }
+            if (!$info) {
+                throw new \Exception('订单不存在');
+            } else {
+                $info->remark = $info->remark ? $info->remark. '|' . $remark : $remark;
+                $info->save();
+            }
+            $memberId = $order_type == 'sport' ? $info->user_id : $info->member_id;
+
+            $wallet = WalletModel::where('member_id', $memberId)->first();
+            if (!$wallet) throw new Exception('用户不存在', HttpStatus::CUSTOM_ERROR);
+            $availableBalance = bcadd($wallet->available_balance, $amount, 10);
+            BalanceLogService::addLog($memberId, $amount, $wallet->available_balance, $availableBalance, $changeType, null, $remark);
+            $wallet->available_balance = $availableBalance;
+            $wallet->save();
+
+            DB::commit();
+        } catch (ValidationException $e) {
+            DB::rollBack();
+            return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
+        } catch (Exception $e) {
+            DB::rollBack();
+            return $this->error(intval($e->getCode()), $e->getMessage());
+        }
+
+        return $this->success();
+    }
+
+    //订单扣款
+    public function orderDebiting()
+    {
+        DB::beginTransaction();
+        try {
+            request()->validate([
+                'amount' => ['required', 'numeric', 'min:0.01'],
+                'remark' => ['required', 'string', 'min:1'],
+                'order_type' => ['required'],
+                'order_id' => ['required'],
+            ]);
+            $order_type = request()->input('order_type');
+            $order_id = request()->input('order_id');
+            $amount = request()->input('amount');
+            $amount = $amount * -1;
+            $remark = request()->input('remark');
+            $key = 'api_request_' . md5($order_id . json_encode([
+                        'amount' => $amount,
+                        'remark' => $remark,
+                        'action' => "wallet/orderDebiting"
+                    ]));
+            if (Cache::has($key)) throw new Exception("请求太频繁,请稍后再试。", HttpStatus::CUSTOM_ERROR);
+            Cache::put($key, true, 3);
+
+            if ($order_type == 'sport') {
+                $info = Order::where('id', $order_id)->first();
+            } elseif ($order_type == 'lhc') {
+                $info = LhcOrder::where('id', $order_id)->first();
+            }
+            if (!$info) {
+                throw new \Exception('订单不存在');
+            } else {
+                $info->remark = $info->remark ? $info->remark. '|' . $remark : $remark;
+                $info->save();
+            }
+            $memberId = $order_type == 'sport' ? $info->user_id : $info->member_id;
+
+            $wallet = WalletModel::where('member_id', $memberId)->first();
+            if (!$wallet) throw new Exception('用户不存在', HttpStatus::CUSTOM_ERROR);
+            $availableBalance = bcadd($wallet->available_balance, $amount, 10);
+            if ($availableBalance < 0) throw new Exception('可用余额不足', HttpStatus::CUSTOM_ERROR);
+            BalanceLogService::addLog($memberId, $amount, $wallet->available_balance, $availableBalance, "人工扣款", null, $remark);
+            $wallet->available_balance = $availableBalance;
+            $wallet->save();
+
+            DB::commit();
+        } catch (ValidationException $e) {
+            DB::rollBack();
+            return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
+        } catch (Exception $e) {
+            DB::rollBack();
+            return $this->error(intval($e->getCode()), $e->getMessage());
+        }
+        return $this->success();
+    }
+
     /**
      * @api {post} /admin/wallet/verifyRecharge 审核
      * @apiGroup 充值管理

+ 2 - 0
routes/admin.php

@@ -188,6 +188,8 @@ Route::middleware(['admin.jwt'])->group(function () {
             Route::get('/getPendingTasks', [Wallet::class, 'getPendingTasks']);
             Route::get('/getChangeTypes', [Wallet::class, 'getChangeTypes']);
             Route::post('/debiting', [Wallet::class, 'debiting']);
+            Route::post('/orderDebiting', [Wallet::class, 'orderDebiting']);
+            Route::post('/orderTopUp', [Wallet::class, 'orderTopUp']);
 
 
         });