doge před 1 hodinou
rodič
revize
c0bd928e61

+ 5 - 3
app/Http/Controllers/admin/ManualAudit.php

@@ -64,10 +64,11 @@ class ManualAudit extends Controller
                 'account' => ['nullable', 'required_without:member_id', 'string', 'max:64'],
                 'change_type' => ['required', 'string', 'in:' . implode(',', BalanceLogService::$manualRecharge)],
                 'amount' => ['required', 'numeric', 'min:0.01', 'regex:' . self::AMOUNT_PATTERN],
-                // 人工充值、“其他”按平台倍数自动计算;其余类型自行填写,留空为0(可直接提现)
+                // wager_amount 仅用于识别升级前已处理请求的重试。
                 'wager_amount' => ['nullable', 'numeric', 'min:0', 'regex:' . self::AMOUNT_PATTERN],
+                'wager_multiple' => ['nullable', 'regex:' . ManualAuditService::WAGER_MULTIPLE_PATTERN],
                 'remark' => ['nullable', 'string', 'max:255'],
-            ]);
+            ], ['wager_multiple.regex' => '打码量倍数必须为非负整数']);
             $remark = trim((string)($params['remark'] ?? ''));
             $result = ManualAuditService::credit(
                 (string)$params['request_id'],
@@ -76,7 +77,8 @@ class ManualAudit extends Controller
                 (string)$params['amount'],
                 (string)$params['change_type'],
                 $remark,
-                isset($params['wager_amount']) ? (string)$params['wager_amount'] : null
+                isset($params['wager_amount']) ? (string)$params['wager_amount'] : null,
+                isset($params['wager_multiple']) ? (string)$params['wager_multiple'] : null
             );
 
             return $this->adjustmentResponse($result);

+ 5 - 4
app/Http/Controllers/admin/Wallet.php

@@ -182,20 +182,21 @@ class Wallet extends Controller
     {
         DB::beginTransaction();
         try {
-            request()->validate([
+            $params = request()->validate([
                 'amount' => ['required', 'numeric', 'min:0.01'],
                 'member_id' => ['required', 'string', 'min:1'],
                 'remark' => ['required', 'string', 'min:1'],
                 'change_type' => ['required', 'string', 'in:' . implode(',', BalanceLogService::$manualRecharge)],
-                // 人工充值、“其他”按平台倍数自动计算;其余类型自行填写,留空为0
                 'wager_amount' => ['nullable', 'numeric', 'min:0'],
-            ]);
+                'wager_multiple' => ['nullable', 'regex:' . ManualAuditService::WAGER_MULTIPLE_PATTERN],
+            ], ['wager_multiple.regex' => '打码量倍数必须为非负整数']);
             $memberId = request()->input('member_id');
             $amount = request()->input('amount');
             $remark = request()->input('remark');
             $changeType = request()->input('change_type');
             [$wagerDelta, $writeWager] = ManualAuditService::wagerDelta(false, (string)$amount, (string)$changeType, null,
-                request()->filled('wager_amount') ? (string)request()->input('wager_amount') : null);
+                isset($params['wager_amount']) ? (string)$params['wager_amount'] : null,
+                isset($params['wager_multiple']) ? (string)$params['wager_multiple'] : null);
             $key = 'api_request_' . md5($memberId . json_encode([
                         'amount' => $amount,
                         'remark' => $remark,

+ 1 - 1
app/Services/BalanceLogService.php

@@ -17,7 +17,7 @@ use Illuminate\Support\Facades\Log;
 class BalanceLogService extends BaseService
 {
     public static string $MODEL = BalanceLog::class;
-    //后台充值可选择的类型。神秘彩金、其他不参与任何活动(不计等级/首充/返现/赠送),只加打码量:神秘彩金按填写值,其他按平台倍数
+    // 后台充值可选类型。神秘彩金按填写的倍数加打码量,其他按平台倍数,两者均不参与充值活动。
     public static array $manualRecharge = ['人工充值', '注册赠送', '优惠活动', '神秘彩金', '其他'];
     //计入累计充值(等级经验)的类型:后台直接加款的“人工充值”与前台充值一致计入
     public static array $levelRechargeTypes = ['充值', '人工充值', '三方充值'];

+ 45 - 10
app/Services/ManualAuditService.php

@@ -22,6 +22,8 @@ class ManualAuditService
     public const PLATFORM_WAGER_TYPE = '人工充值';
     /** 按平台倍数计打码量的全部类型:“其他”与人工充值同样按倍数,但不触发充值钩子(不参与任何活动) */
     public const PLATFORM_WAGER_TYPES = ['人工充值', '其他'];
+    public const CREDIT_WAGER_MULTIPLE_TYPES = ['注册赠送', '优惠活动', '神秘彩金'];
+    public const WAGER_MULTIPLE_PATTERN = '/\A[0-9]+\z/';
     public const STATUS_FAILED = ManualAuditRecord::STATUS_FAILED;
     public const STATUS_SUCCESS = ManualAuditRecord::STATUS_SUCCESS;
 
@@ -60,9 +62,9 @@ class ManualAuditService
                     ['label' => '失败', 'value' => self::STATUS_FAILED],
                     ['label' => '成功', 'value' => self::STATUS_SUCCESS],
                 ],
-                // 人工充值、其他按平台提现打码倍数自动增减打码量,其余类型需填写 wager_amount
                 'platform_wager_type' => self::PLATFORM_WAGER_TYPE,
                 'platform_wager_types' => self::PLATFORM_WAGER_TYPES,
+                'credit_wager_multiple_types' => self::CREDIT_WAGER_MULTIPLE_TYPES,
                 'withdraw_wager_multiple' => WageringService::multiple(),
             ],
         ];
@@ -105,7 +107,8 @@ class ManualAuditService
     }
 
     /**
-     * @param string|null $wagerAmount 非“人工充值”类型时填写的打码量(为空视为0,可直接提现)
+     * @param string|null $wagerAmount 旧版固定打码量,仅用于识别历史请求重试
+     * @param string|null $wagerMultiple 指定赠送类型上分使用的非负整数倍数
      */
     public static function credit(
         string $requestId,
@@ -114,9 +117,10 @@ class ManualAuditService
         string $amount,
         string $changeType,
         string $remark,
-        ?string $wagerAmount = null
+        ?string $wagerAmount = null,
+        ?string $wagerMultiple = null
     ): array {
-        return self::adjust($requestId, $adminId, $memberId, $amount, $changeType, $remark, false, null, $wagerAmount);
+        return self::adjust($requestId, $adminId, $memberId, $amount, $changeType, $remark, false, null, $wagerAmount, $wagerMultiple);
     }
 
     /**
@@ -148,8 +152,33 @@ class ManualAuditService
      * 本次上下分对打码量的有符号调整。上分“人工充值”由充值钩子自动按平台倍数增加,这里只返回展示值。
      * @return array{0:string,1:bool} [有符号打码量, 是否需要在此处手动写入]
      */
-    public static function wagerDelta(bool $isDebit, string $amount, string $changeType, ?string $rechargeType, ?string $wagerAmount): array
-    {
+    public static function wagerDelta(
+        bool $isDebit,
+        string $amount,
+        string $changeType,
+        ?string $rechargeType,
+        ?string $wagerAmount,
+        ?string $wagerMultiple = null
+    ): array {
+        if (!$isDebit && in_array($changeType, self::CREDIT_WAGER_MULTIPLE_TYPES, true)) {
+            if ($wagerAmount !== null) {
+                throw new Exception('请使用 wager_multiple 提交打码量倍数', HttpStatus::CUSTOM_ERROR);
+            }
+            $multiple = $wagerMultiple ?? '0';
+            if (!preg_match(self::WAGER_MULTIPLE_PATTERN, $multiple)) {
+                throw new Exception('打码量倍数必须为非负整数', HttpStatus::CUSTOM_ERROR);
+            }
+            try {
+                $delta = bcmul($amount, $multiple, 10);
+            } catch (\ValueError) {
+                throw new Exception('充值金额格式不正确', HttpStatus::CUSTOM_ERROR);
+            }
+            if (bccomp($delta, '100000000000000000000', 10) >= 0) {
+                throw new Exception('计算后的打码量超出范围', HttpStatus::CUSTOM_ERROR);
+            }
+            return [$delta, true];
+        }
+
         // 仅自填分支解析 wager_amount:按平台倍数的类型忽略该值;校验器的 numeric 会放行 "1e2" 等 bcmath 不接受的写法
         $custom = static function () use ($wagerAmount): string {
             try {
@@ -178,14 +207,19 @@ class ManualAuditService
         string $remark,
         bool $isDebit,
         ?string $rechargeType = null,
-        ?string $wagerAmount = null
+        ?string $wagerAmount = null,
+        ?string $wagerMultiple = null
     ): array {
         $signedAmount = bcadd($amount, '0', 10);
         if ($isDebit) {
             $signedAmount = bcmul($signedAmount, '-1', 10);
         }
         $transactionType = $isDebit ? 'debit' : 'credit';
-        [$wagerDelta, $writeWager] = self::wagerDelta($isDebit, $amount, $changeType, $rechargeType, $wagerAmount);
+        $wagerParameters = $rechargeType === null && $wagerAmount === null ? null
+            : ['recharge_type' => $rechargeType, 'wager_amount' => $wagerAmount === null ? null : bcadd($wagerAmount, '0', 10)];
+        if ($wagerMultiple !== null && in_array($changeType, self::CREDIT_WAGER_MULTIPLE_TYPES, true)) {
+            $wagerParameters['wager_multiple'] = $wagerMultiple;
+        }
         $requestHash = self::requestHash(
             $adminId,
             $memberId,
@@ -194,8 +228,7 @@ class ManualAuditService
             $changeType,
             $remark,
             // 只摘要请求原始参数,平台倍数调整后同一 request_id 重试仍能识别为同一请求
-            $rechargeType === null && $wagerAmount === null ? null
-                : ['recharge_type' => $rechargeType, 'wager_amount' => $wagerAmount === null ? null : bcadd($wagerAmount, '0', 10)]
+            $wagerParameters
         );
 
         $existing = ManualAuditRecord::where('request_id', $requestId)->first();
@@ -204,6 +237,8 @@ class ManualAuditService
             return self::format($existing, self::accounts([$memberId]));
         }
 
+        [$wagerDelta, $writeWager] = self::wagerDelta($isDebit, $amount, $changeType, $rechargeType, $wagerAmount, $wagerMultiple);
+
         $created = false;
         try {
             $audit = DB::transaction(function () use (

+ 23 - 4
tests/Integration/WageringTest.php

@@ -274,14 +274,33 @@ class WageringTest extends TestCase
     {
         $this->assertSame([WageringService::platformRequirement('10'), false], ManualAuditService::wagerDelta(false, '10', '人工充值', null, '999'));
         $this->assertSame('20.0000000000', WageringService::platformRequirement('10'));
-        $this->assertSame(['30.0000000000', true], ManualAuditService::wagerDelta(false, '10', '注册赠送', null, '30'));
-        $this->assertSame(['0.0000000000', true], ManualAuditService::wagerDelta(false, '10', '优惠活动', null, null));
+        foreach (['注册赠送', '优惠活动', '神秘彩金'] as $type) {
+            foreach ([null, '0', '1', '2', '18'] as $multiple) {
+                $expected = match ($multiple) {
+                    null, '0' => '0.0000000000',
+                    '1' => '10.2500000000',
+                    '2' => '20.5000000000',
+                    '18' => '184.5000000000',
+                };
+                $this->assertSame([$expected, true], ManualAuditService::wagerDelta(false, '10.25', $type, null, null, $multiple));
+            }
+            foreach (['-1', '1.5', '1e2'] as $invalid) {
+                $this->assertBlocked(
+                    fn () => ManualAuditService::wagerDelta(false, '10', $type, null, null, $invalid),
+                    '打码量倍数必须为非负整数'
+                );
+            }
+        }
+        $this->assertBlocked(fn () => ManualAuditService::wagerDelta(false, '10', '注册赠送', null, '18'), 'wager_multiple');
+        $this->assertBlocked(
+            fn () => ManualAuditService::wagerDelta(false, '10', '注册赠送', null, null, '99999999999999999999'),
+            '计算后的打码量超出范围'
+        );
         foreach (['神秘彩金', '其他'] as $type) {
             $this->assertContains($type, BalanceLogService::$manualRecharge);
             $this->assertContains($type, BalanceLogService::$RW);
             $this->assertNotContains($type, BalanceLogService::$levelRechargeTypes, '不参与任何活动');
         }
-        $this->assertSame(['30.0000000000', true], ManualAuditService::wagerDelta(false, '10', '神秘彩金', null, '30'));
         $this->assertSame(['-5.0000000000', true], ManualAuditService::wagerDelta(true, '10', '人工扣款', '神秘彩金', '5'));
         // 其他:按平台倍数,忽略填写值;不走充值钩子所以需在此处写入
         $this->assertSame(['20.0000000000', true], ManualAuditService::wagerDelta(false, '10', '其他', null, '999'));
@@ -291,7 +310,7 @@ class WageringTest extends TestCase
         $this->assertSame(['-20.0000000000', true], ManualAuditService::wagerDelta(true, '10', '人工扣款', '其他', '1e2'));
         $this->assertSame(['0', false], ManualAuditService::wagerDelta(true, '10', '人工扣款', null, '1e2'));
         try {
-            ManualAuditService::wagerDelta(false, '10', '神秘彩金', null, '1e2');
+            ManualAuditService::wagerDelta(true, '10', '人工扣款', '神秘彩金', '1e2');
             $this->fail('自填打码量格式错误应被拒绝');
         } catch (\Exception $e) {
             $this->assertSame('打码量格式不正确', $e->getMessage());