webman_duanju/app/api/model/WithDraw.php

252 lines
8.9 KiB
PHP

<?php
namespace app\api\model;
use app\common\library\DatabaseRoute;
use app\exception\SysException;
use app\utils\WuYouPayUtils;
use think\facade\Db;
class WithDraw
{
public static function goWithDraw($userId, $amount, $msg='', bool $isSys=false, bool $isAlipay=false)
{
if (bccomp($amount, "0", 2) <= 0) {
throw new SysException('请不要输入小于0的数字,请输入正确的提现金额!');
}
$cashInfo = [
'is_out' => 0,
'money' => $amount,
'user_id' => $userId,
'user_type' => $isSys ? 2 : 1,
'state' => 0,
'rate' => 0,
'create_at' => getNormalDate(),
'withdraw_type' => 1,
'order_number' => uuid(),
];
$moneyDetails = [
'user_id' => $userId,
'sys_user_id' => $userId,
'title' => '[提现]',
'content' => "提现:{$amount}",
'type' => 2,
'state' => 2,
'classify' => 4,
'money' => $amount,
'create_time' => getNormalDate(),
'money_type' => 1,
];
$userMoney = [];
$flag = true;
// 系统用户提现
if ($isSys) {
$user = DatabaseRoute::getDb('sys_user', $userId)->find();
$msgCount = Db::name('msg')->where([
'phone' => $user['mobile'],
'code' => $msg
])->count();
if (!$msgCount) {
throw new SysException("验证码不正确");
}
if (empty($user['zhi_fu_bao']) || empty($user['zhi_fu_bao_name'])) {
throw new SysException([
'code' => 9999,
'msg' => '请先绑定支付宝账号'
]);
}
$cashInfo['zhifubao'] = $user['zhi_fu_bao'];
$cashInfo['zhifubao_name'] = $user['zhi_fu_bao_name'];
$cashInfo['bank_name'] = '';
// 校验余额
$userMoney = Db::name('sys_user_money')->where([
'user_id' => $userId
])->find();
}else{
$userMoney = DatabaseRoute::getDb('user_money', $userId)->find();
$user = TbUser::selectUserById($userId);
if ($user['status'] != 1) {
throw new SysException([
'code' => 9999,
'msg' => $user['status'] == 0 ? '账号不存在': '账号已被禁用,请联系客服处理'
]);
}
$userInfo = DatabaseRoute::getDb('user_info', $userId)->find();
if (!$userInfo || empty($userInfo['cert_name'])) {
throw new SysException([
'code' => 9991,
'msg' => '请先实名认证'
]);
}
if (empty($userInfo['account_no']) || empty($userInfo['mobile'])) {
throw new SysException([
'code' => 9991,
'msg' => '需重新完成实名认证后才可提现'
]);
}
if (empty($userInfo['bank_name'] && !empty($userInfo['resp_json']))) {
DatabaseRoute::getDb('user_info', $userId, true, true)->update([
'bank_name' => self::getBankName($userInfo['resp_json'])
]);
}
if (empty($userInfo['bank_name'])) {
DatabaseRoute::getDb('user_info', $userId, true, true)->delete();
throw new SysException([
'code' => 9991,
'msg' => '需重新完成实名认证后才可提现'
]);
}
if ($isAlipay) {
$cashInfo['zhifubao'] = $user['zhi_fu_bao'];
$cashInfo['zhifubao_name'] = $user['zhi_fu_bao_name'];
if ($userInfo['cert_name'] != $user['zhi_fu_bao_name']) {
throw new SysException([
'code' => 9991,
'msg' => '认证名称不一致,请重新确认!'
]);
}
}else{
$cashInfo['zhifubao'] = $userInfo['account_no'];
$cashInfo['zhifubao_name'] = $userInfo['cert_name'];
}
$cashInfo['bank_name'] = $userInfo['bank_name'];
$cashInfo['id_card_no'] = $userInfo['cert_no'];
$cashInfo['province'] = $userInfo['province'];
$cashInfo['city'] = $userInfo['city'];
$cashInfo['bank_branch'] = $userInfo['bank_branch'];
// 校验黑名单用户
$count = Db::name('tb_withdraw_blacklist')->where([
'real_name' => $cashInfo['zhifubao_name']
])->count();
$blackCount = Db::name('tb_user_blacklist')->where([
'id_card_no' => trim($userInfo['cert_no'])
])->count();
if ($blackCount) {
$cashInfo['state'] = 2;
$cashInfo['out_at'] = getNormalDate();
$moneyDetails['content'] = "刷单用户禁止提现:$amount";
$flag = false;
}else if($count) {
$cashInfo['state'] = 3;
$cashInfo['content'] = "提现=$amount";
$moneyDetails['relation_id'] = '提现黑名单用户,请谨慎审核!';
$flag = false;
}
}
$info = (new CommonInfo())->getByCode(112);
if (!$info || bccomp($amount, $info['value'], 2) < 0) {
throw new SysException("不满足最低提现金额,请重新输入!");
}
// 校验余额
if (bccomp($userMoney['amount'], $amount, 2) < 0) {
throw new SysException("可提现余额不足");
}
if (!self::checkCanCash($userId, 1, $amount)) {
$cashInfo['state'] = 3;
$cashInfo['content'] = "成功提现:$amount";
$cashInfo['relation_id'] = '单次提现超额';
}else {
if($flag) {
$cashInfo['state'] = 4;
$resp = WuYouPayUtils::extractOrder($isAlipay, $cashInfo['order_number'], $userId, $amount, $isSys, $cashInfo['zhifubao'], $cashInfo['zhifubao_name'],
$cashInfo['bank_name'], !empty($cashInfo['province'])?$cashInfo['province']:'', !empty($cashInfo['city'])?$cashInfo['city']:'', !empty($cashInfo['bank_branch'])?$cashInfo['bank_branch']:'');
if (isset($resp['status']) && ($resp['status'] == 2 || $resp['status'] == 10000)) {
$cashInfo['content'] = "成功提现:$amount";
$cashInfo['state'] = 1;
$cashInfo['out_at'] = getNormalDate();
}
if (!empty($resp['error_msg'])){
throw new SysException($resp['error_msg']);
}
}
}
// 插入提现记录
unset($cashInfo['id_card_no']);
unset($cashInfo['content']);
DatabaseRoute::getDb('cash_out', $userId, true)->insert($cashInfo);
DatabaseRoute::getDb($isSys ? 'sys_user_money_details' : 'user_money_details', $userId, true)->insert($moneyDetails);
if ($isSys) {
Db::name('sys_user_money')->where([
'user_id' => $userId
])->dec('amount', floatval($amount))->update();
}else{
DatabaseRoute::getDb('user_money', $userId, true)->where([
'user_id' => $userId
])->dec('amount', floatval($amount))->update();
}
}
private static function checkCanCash($userId, $type, $money)
{
if ($type == 1) {
// 查询当日体现次数
$count = DatabaseRoute::getDb('cash_out', $userId)->where([
['state', 'in', [1, 3]],
'withdraw_type' => 1,
['create_at', '>=', date('Y-m-d 00:00:00')],
])->count();
$commonInfo = (new CommonInfo())->getByCode(922);
if (!$commonInfo) {
throw new SysException("【922】每日提现次数上限未配置");
}
if ($count >= intval($commonInfo['value'])) {
throw new SysException("超过当日提现限制次数{$commonInfo['value']}次,请明天再试!");
}
$commonInfo = (new CommonInfo())->getByCode(923);
if (!$commonInfo) {
throw new SysException("【923】单次提现超额未配置");
}
if (bccomp($money, $commonInfo['value']) > 0) {
return false;
}
}
return true;
}
private static function getBankName($json)
{
$resp = json_decode($json);
$result = $resp['result'];
$errorCode = $resp['error_code'];
$respCode = $resp['respCode'];
if ($errorCode == 0 && $respCode == '0') {
return $result['bancardInfor'] ? $result['bancardInfor']['bankName'] : '';
}
return '';
}
}