提现金额限制及添加其它技术

This commit is contained in:
Dev
2026-09-23 17:52:36 +08:00
parent eb5acfc466
commit a07c495fe0
5 changed files with 69 additions and 13 deletions
@@ -31,6 +31,13 @@ class AccountWithdrawService
CommonService::FAILED => '审核驳回',
];
//提现限制(与小程序端展示的「提现规则」保持一致)
const WITHDRAW_MIN_AMOUNT = 0.1; //单笔最低(元)
const WITHDRAW_MAX_AMOUNT = 200; //单笔最高(元)
const WITHDRAW_DAY_MAX_COUNT = 10; //每个自然日最多申请次数
const WITHDRAW_DAY_MAX_AMOUNT = 2000; //每个自然日累计提现上限(元)
const WITHDRAW_AMOUNT_SCALE = 2; //金额小数位数
/**
* 用户申请提现
@@ -42,12 +49,20 @@ class AccountWithdrawService
*/
public static function applyWithdraw($userId, $amount, string $name = '')
{
//每个自然日仅可发起一次提现申请
$todayCount = self::$Model::where('user_id', $userId)
->whereTime('createtime', 'today')
->count();
if ($todayCount >= 10) {
throw new \Exception('每个自然日仅可发起10次提现申请,请明日再试');
//单笔金额校验(0.1~200元,最多两位小数)
$amount = self::checkWithdrawAmount($amount);
//每个自然日次数限制(已驳回的不计入)
$todayCount = self::todayQuery($userId)->count();
if ($todayCount >= self::WITHDRAW_DAY_MAX_COUNT) {
throw new \Exception('每个自然日最多可发起' . self::WITHDRAW_DAY_MAX_COUNT . '次提现申请,请明日再试');
}
//每个自然日累计金额限制(已驳回的不计入)
$todayAmount = (float)self::todayQuery($userId)->sum('amount');
if ($todayAmount + $amount > self::WITHDRAW_DAY_MAX_AMOUNT) {
throw new \Exception('每个自然日累计提现金额不超过' . self::WITHDRAW_DAY_MAX_AMOUNT
. '元,今日已申请' . sprintf('%.2f', $todayAmount) . '元,请明日再试');
}
//开启事务 抛出异常时自动回滚
@@ -79,6 +94,41 @@ class AccountWithdrawService
});
}
/**
* 单笔提现金额校验
* @param int|float|string $amount
* @return float 校验通过返回规范化后的金额(保留2位小数)
* @throws \Exception
*/
public static function checkWithdrawAmount($amount): float
{
if (!is_numeric($amount)) {
throw new \Exception('提现金额必须为数字');
}
$amount = floatval($amount);
if ($amount < self::WITHDRAW_MIN_AMOUNT || $amount > self::WITHDRAW_MAX_AMOUNT) {
throw new \Exception('单笔提现金额需在' . self::WITHDRAW_MIN_AMOUNT . '~'
. self::WITHDRAW_MAX_AMOUNT . '元之间');
}
if (round($amount, self::WITHDRAW_AMOUNT_SCALE) != $amount) {
throw new \Exception('提现金额最多保留' . self::WITHDRAW_AMOUNT_SCALE . '位小数');
}
return round($amount, self::WITHDRAW_AMOUNT_SCALE);
}
/**
* 当日提现查询对象(已驳回的不计入次数与累计金额)
* @param int $userId
* @return \think\db\Query
*/
private static function todayQuery($userId)
{
return self::$Model::where('user_id', $userId)
->whereTime('createtime', 'today')
->where('status', '<>', CommonService::FAILED);
}
/**
* 审核提现
* @param $withdraw_record_id int 提现申请id