修复提现并发超提与金额校验

This commit is contained in:
JIA-MAC
2026-09-23 14:03:37 +08:00
parent f127ddea07
commit df5e6a997c
2 changed files with 22 additions and 8 deletions

View File

@@ -22,7 +22,7 @@ namespace app\api\logic;
use app\common\logic\{AccountLogLogic, LogicBase};
use app\common\model\{AccountLog, Client_, User, Withdraw};
use app\common\model\{AccountLog, Client_, Withdraw};
use app\admin\logic\WechatMerchantTransferLogic;
use app\admin\model\WithdrawApply;
use app\common\server\ConfigServer;
@@ -96,10 +96,18 @@ class WithdrawLogic extends LogicBase
];
$withdraw_id = Db::name('withdraw_apply')->insertGetId($data);
//提交申请后,扣减用户的佣金
$user = User::get($user_id);
$user->earnings = ['dec', $post['money']];
$user->save();
//提交申请后,锁行重读可提现余额并条件原子扣减,防止并发超提
$user = Db::name('user')->where('id', $user_id)->lock(true)->find();
if (empty($user) || !is_numeric($user['earnings']) || $user['earnings'] < $post['money']) {
throw new Exception('可提现金额不足');
}
$affected = Db::name('user')
->where('id', $user_id)
->where('earnings', '>=', $post['money'])
->setDec('earnings', $post['money']);
if ($affected !== 1) {
throw new Exception('可提现金额不足');
}
//增加佣金变动记录
AccountLogLogic::AccountRecord(
$user_id,
@@ -113,7 +121,7 @@ class WithdrawLogic extends LogicBase
Db::commit();
return self::dataSuccess('', ['id' => $withdraw_id]);
}catch (Exception $e){
}catch (\Exception $e){
Db::rollback();
return self::dataError($e->getMessage());
}
@@ -194,4 +202,4 @@ class WithdrawLogic extends LogicBase
return true;
}
}
}

View File

@@ -66,6 +66,9 @@ class Withdraw extends Validate
//提现佣金验证
protected function checkMoney($value, $rule, $data = [])
{
if (!is_numeric($value) || $value <= 0) {
return '提现金额不正确';
}
$able_withdraw = Db::name('user')->where('id', $data['user_id'])->value('earnings');
if ($value > $able_withdraw){
return '可提现金额不足';
@@ -73,6 +76,9 @@ class Withdraw extends Validate
//1.最低提现金额
$min_withdraw = ConfigServer::get('withdraw', 'min_withdraw', 0);
if (!is_numeric($min_withdraw) || $min_withdraw <= 0) {
return '最低提现金额未设置';
}
if($value < $min_withdraw){
return '最低提现'.$min_withdraw.'元';
}
@@ -84,4 +90,4 @@ class Withdraw extends Validate
}
return true;
}
}
}