提现金额限制及添加其它技术
This commit is contained in:
@@ -67,15 +67,19 @@ class Account extends Api
|
||||
$user_id = $this->auth->id ?? -1;
|
||||
$amount = $this->request->post('amount', 0);
|
||||
$name = $this->request->post('name', '');
|
||||
// 验证参数
|
||||
// 验证参数(单笔 0.1~200 元,与 AccountWithdrawService 中的限制保持一致)
|
||||
$error = $this->validate($this->request->post(), [
|
||||
'name' => 'require',
|
||||
'amount' => 'require|number|between:1,500',
|
||||
'amount' => 'require|number|float|between:'
|
||||
. AccountWithdrawService::WITHDRAW_MIN_AMOUNT . ','
|
||||
. AccountWithdrawService::WITHDRAW_MAX_AMOUNT,
|
||||
], [
|
||||
'name.require' => '微信真实姓名必填',
|
||||
'amount.require' => '提现金额不正确',
|
||||
'amount.number' => '提现金额必须为数字',
|
||||
'amount.between' => '提现金额必须在1到500之间',
|
||||
'amount.float' => '提现金额必须为数字',
|
||||
'amount.between' => '单笔提现金额需在' . AccountWithdrawService::WITHDRAW_MIN_AMOUNT . '~'
|
||||
. AccountWithdrawService::WITHDRAW_MAX_AMOUNT . '元之间',
|
||||
]);
|
||||
if (true !== $error) {
|
||||
throw new \Exception($error);
|
||||
|
||||
@@ -169,6 +169,7 @@ class Common extends Api
|
||||
$code_languages = config('site.general_config_code_languages') ?? '';
|
||||
$front_techs = config('site.general_config_front_techs') ?? '';
|
||||
$back_techs = config('site.general_config_back_techs') ?? '';
|
||||
$other_techs = config('site.general_config_other_techs') ?? '';
|
||||
$develop_types = config('site.general_config_develop_types') ?? '';
|
||||
$develop_types = explode(',', $develop_types);
|
||||
$developTypes = [];
|
||||
@@ -182,6 +183,7 @@ class Common extends Api
|
||||
'languageLabels' => explode(',', $code_languages),
|
||||
'frontLabels' => explode(',', $front_techs),
|
||||
'backendLabels' => explode(',', $back_techs),
|
||||
'otherLabels' => explode(',', $other_techs),
|
||||
'developTypes' => $developTypes,
|
||||
];
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user