Ken hai 1 semana
pai
achega
c4e0a01d8d
Modificáronse 2 ficheiros con 49 adicións e 16 borrados
  1. 47 16
      app/Http/Controllers/admin/Wallet.php
  2. 2 0
      routes/admin.php

+ 47 - 16
app/Http/Controllers/admin/Wallet.php

@@ -62,30 +62,62 @@ class Wallet extends Controller
         return $this->success($data);
     }
 
-    /**
-     * @api {post} /admin/wallet/topUp 人工充值
-     * @apiGroup 充值管理
-     * @apiUse result
-     * @apiUse header
-     * @apiVersion 1.0.0
-     * @apiParam {float} amount 充值金额
-     * - 可以是负数  负数则为扣款
-     * @apiParam {string} member_id 会员ID
-     * @apiParam {string} remark 充值/扣款 说明
-     */
-    public
-    function topUp()
+    public function debiting()
+    {
+        DB::beginTransaction();
+        try {
+            request()->validate([
+                'amount' => ['required', 'numeric', 'min:0.01'],
+                'member_id' => ['required', 'string', 'min:1'],
+                'remark' => ['required', 'string', 'min:1'],
+            ]);
+            $memberId = request()->input('member_id');
+            $amount = request()->input('amount');
+            $amount = $amount * -1;
+            $remark = request()->input('remark');
+            $key = 'api_request_' . md5($memberId . json_encode([
+                        'amount' => $amount,
+                        'remark' => $remark,
+                        'action' => "wallet/debiting"
+                    ]));
+            if (Cache::has($key)) throw new Exception("请求太频繁,请稍后再试。", HttpStatus::CUSTOM_ERROR);
+            Cache::put($key, true, 3);
+            $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());
+        }
+        $availableBalance = floatval($availableBalance);
+        // 去除多余0后,再用 sprintf 补足两位
+        $availableBalance = sprintf('%.2f', $availableBalance);
+        TopUpService::notifyTransferSuccess($memberId, "您的账户余额更新:" . ($amount > 0 ? '+' : '') . "{$amount} \n总余额为:{$availableBalance}");
+        return $this->success();
+    }
+
+    public function topUp()
     {
         DB::beginTransaction();
         try {
             request()->validate([
-                'amount' => ['required', 'numeric'],
+                'amount' => ['required', 'numeric', 'min:0.01'],
                 'member_id' => ['required', 'string', 'min:1'],
-                'remark' => ['required', 'string', 'min:1']
+                'remark' => ['required', 'string', 'min:1'],
+                'change_type' => ['required', 'string', 'in:' . implode(',', BalanceLogService::$manualRecharge)],
             ]);
             $memberId = request()->input('member_id');
             $amount = request()->input('amount');
             $remark = request()->input('remark');
+            $changeType = request()->input('change_type');
             $key = 'api_request_' . md5($memberId . json_encode([
                         'amount' => $amount,
                         'remark' => $remark,
@@ -97,7 +129,6 @@ class Wallet extends Controller
             if (!$wallet) throw new Exception('用户不存在', HttpStatus::CUSTOM_ERROR);
             $availableBalance = bcadd($wallet->available_balance, $amount, 10);
             if ($availableBalance < 0) throw new Exception('可用余额不足', HttpStatus::CUSTOM_ERROR);
-            $changeType = ($amount > 0 ? "人工充值" : "人工扣款");
             BalanceLogService::addLog($memberId, $amount, $wallet->available_balance, $availableBalance, $changeType, null, $remark);
             $wallet->available_balance = $availableBalance;
             $wallet->save();

+ 2 - 0
routes/admin.php

@@ -159,6 +159,8 @@ Route::middleware(['admin.jwt'])->group(function () {
             Route::post('/verifyRecharge', [Wallet::class, 'verifyRecharge']);
             Route::get('/getPendingTasks', [Wallet::class, 'getPendingTasks']);
             Route::get('/getManualRecharge', [Wallet::class, 'getManualRecharge']);
+            Route::post('/debiting', [Wallet::class, 'debiting']);
+