2025-08-13 10:43:56 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace app\api\model;
|
|
|
|
|
|
|
|
|
|
|
|
use think\Db;
|
|
|
|
|
|
use think\Model;
|
|
|
|
|
|
use Yzh\YunPay;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UserWithdrawal extends Model
|
|
|
|
|
|
{
|
|
|
|
|
|
public $withdraw_status = [
|
|
|
|
|
|
1=>'待处理',
|
|
|
|
|
|
2=>'已通过',
|
|
|
|
|
|
3=>'已拒绝',
|
|
|
|
|
|
4=>'打款中[云账户]',
|
|
|
|
|
|
5=>'打款失败[云账户]',
|
|
|
|
|
|
6=>'已打款[云账户]',
|
|
|
|
|
|
];
|
|
|
|
|
|
/*
|
|
|
|
|
|
* 用户提现
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function withdrawal($user_id,$number, $type,$sms_code){
|
|
|
|
|
|
if(empty($sms_code)){
|
|
|
|
|
|
return V(0, '请输入短信验证码');
|
|
|
|
|
|
}
|
|
|
|
|
|
$user_info = model('User')->get_user_info($user_id);
|
|
|
|
|
|
// default-默认登录,1-更换手机号,2绑定手机号,3-忘记密码,4-设置密码,5-账号注销,6-提现
|
|
|
|
|
|
$sms_reslut = model('sms')->verification_code(6,$user_info['mobile'], $sms_code);
|
|
|
|
|
|
if ($sms_reslut['code'] == 0) {
|
|
|
|
|
|
return v($sms_reslut['code'], $sms_reslut['msg'], $sms_reslut['data']);
|
|
|
|
|
|
}
|
|
|
|
|
|
if($user_info['status'] != 1){
|
|
|
|
|
|
return ['code' => 0, 'msg' => '用户被禁用', 'data' => null];
|
|
|
|
|
|
}
|
|
|
|
|
|
if($user_info['is_real']!=1 || empty($user_info['card_id'])){
|
|
|
|
|
|
return ['code' => 0, 'msg' => '请先实名认证', 'data' => null];
|
|
|
|
|
|
}
|
|
|
|
|
|
if($user_info['is_teenager'] == 1){
|
|
|
|
|
|
return ['code' => 0, 'msg' => '已开启青少年模式', 'data' => null];
|
|
|
|
|
|
}
|
|
|
|
|
|
$age = getAgeId($user_info['card_id']);
|
|
|
|
|
|
if(!$age){
|
|
|
|
|
|
return ['code' => 0, 'msg' => '该身份证号未满18岁', 'data' => null];
|
|
|
|
|
|
}
|
2025-09-30 14:50:58 +08:00
|
|
|
|
if(empty($type)){
|
|
|
|
|
|
return ['code' => 0, 'msg' => '请选择提现方式', 'data' => null];
|
|
|
|
|
|
}
|
2025-08-13 10:43:56 +08:00
|
|
|
|
//判断用户是否签约云账户
|
|
|
|
|
|
$yun_pay = new YunPay();
|
|
|
|
|
|
$sign_status = $yun_pay->getApiUserSignStatus($user_info['real_name'],$user_info['card_id']);
|
|
|
|
|
|
if($sign_status['code'] == 1){
|
|
|
|
|
|
if($sign_status['data']['sign_status'] !=1){
|
|
|
|
|
|
//用户签约
|
|
|
|
|
|
$reslut = $yun_pay->apiUserSign($user_info['real_name'],$user_info['card_id']);
|
|
|
|
|
|
if($reslut['code'] != 1){
|
|
|
|
|
|
return ['code' => $reslut['code'], 'msg' => $reslut['msg'], 'data' => $reslut['data']];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}else{
|
|
|
|
|
|
return ['code' => $sign_status['code'], 'msg' => $sign_status['msg'], 'data' => $sign_status['data']];
|
|
|
|
|
|
}
|
|
|
|
|
|
//每日提现次数
|
|
|
|
|
|
$daily_withdrawals_num = get_system_config_value('daily_withdrawals_num');
|
|
|
|
|
|
//今日已提现次数
|
|
|
|
|
|
$today_withdrawals_num = db::name('vs_user_withdrawal')->where(['user_id'=>$user_id,'status'=>1])->whereTime('createtime', 'today')->count();
|
|
|
|
|
|
if($today_withdrawals_num >= $daily_withdrawals_num){
|
|
|
|
|
|
return ['code' => 0, 'msg' => '今日提现次数已用完,每日最多提现'.$daily_withdrawals_num.'次', 'data' => null];
|
|
|
|
|
|
}
|
|
|
|
|
|
if($type == 1){//微信
|
|
|
|
|
|
|
|
|
|
|
|
}else if($type == 2){//支付宝
|
|
|
|
|
|
if (empty($user_info['alipay_account'])) {
|
|
|
|
|
|
return ['code' => 0, 'msg' => '请先绑定支付宝', 'data' => null];
|
|
|
|
|
|
}
|
|
|
|
|
|
}else if($type == 3){//银行卡
|
|
|
|
|
|
if (empty($user_info['bank_card_number'])) {
|
|
|
|
|
|
return ['code' => 0, 'msg' => '请先绑定银行卡', 'data' => null];
|
|
|
|
|
|
}
|
|
|
|
|
|
//银行卡姓名 和 真实姓名
|
|
|
|
|
|
if(trim($user_info['bank_user_name']) != trim($user_info['real_name'])){
|
|
|
|
|
|
return ['code' => 0, 'msg' => '实名认证姓名和银行卡姓名不一致!', 'data' => null];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if ($number > $user_info['earnings']) {
|
|
|
|
|
|
return ['code' => 0, 'msg' => '余额不足', 'data' => null];
|
|
|
|
|
|
}
|
|
|
|
|
|
//钻石兑换人民币比例
|
|
|
|
|
|
$diamond_to_rmb_ratio = get_system_config_value('diamond_to_rmb_ratio');
|
|
|
|
|
|
$money = $number / $diamond_to_rmb_ratio; //提现金额转换 钻石转为人民币
|
|
|
|
|
|
|
|
|
|
|
|
$min_withdraw_amount = get_system_config_value('min_withdraw_amount');
|
|
|
|
|
|
if($money < $min_withdraw_amount){
|
|
|
|
|
|
return ['code' => 0, 'msg' => '提现金额需大于等于'.$min_withdraw_amount."钻石", 'data' => null];
|
|
|
|
|
|
}
|
|
|
|
|
|
//提现服务费
|
|
|
|
|
|
$withdrawal_service_fee = get_system_config_value('withdrawal_service_fee');
|
|
|
|
|
|
$withdrawal_service_fee = $withdrawal_service_fee ? $withdrawal_service_fee / 100 : 0;
|
|
|
|
|
|
Db::startTrans();
|
|
|
|
|
|
try {
|
|
|
|
|
|
$order_sn = $this->create_user_withdrawal_order_sn(); //订单号
|
|
|
|
|
|
$data = [];
|
|
|
|
|
|
$data['order_sn'] = $order_sn;
|
|
|
|
|
|
$data['user_id'] = $user_id;
|
|
|
|
|
|
$data['money'] = $money;
|
|
|
|
|
|
//向上取小数
|
|
|
|
|
|
$data['server_money'] = round((($money * $withdrawal_service_fee) * 100))/100; //提现服务费
|
|
|
|
|
|
$data['general_money'] = $money -$data['server_money']; //到账金额
|
|
|
|
|
|
$data['type'] = $type;
|
|
|
|
|
|
if($type == 2){
|
|
|
|
|
|
$data['real_name'] = $user_info['real_name'];
|
|
|
|
|
|
$data['alipay_account'] = $user_info['alipay_account'];
|
|
|
|
|
|
}else if($type == 3){
|
|
|
|
|
|
$data['real_name'] = $user_info['bank_user_name']??$user_info['real_name'];
|
|
|
|
|
|
$data['bank_card'] = $user_info['bank_card'];
|
|
|
|
|
|
$data['open_bank'] = $user_info['open_bank'];
|
|
|
|
|
|
$data['bank_card_number'] = $user_info['bank_card_number'];
|
|
|
|
|
|
}
|
|
|
|
|
|
$data['remarke'] = '';
|
|
|
|
|
|
$data['status'] = 1;
|
|
|
|
|
|
$data['deal_time'] = 0;
|
|
|
|
|
|
$data['createtime'] = time();
|
|
|
|
|
|
$data['updatetime'] = time();
|
|
|
|
|
|
$user_withdrawal_wid = db::name('vs_user_withdrawal')->insertGetId($data);
|
|
|
|
|
|
if (!$user_withdrawal_wid) {
|
|
|
|
|
|
Db::rollback();
|
|
|
|
|
|
return ['code' => 0, 'msg' => "请重试1", 'data' => null];
|
|
|
|
|
|
}
|
|
|
|
|
|
//扣除账户余额
|
|
|
|
|
|
$reslut = model('common/UserWallet')->change_user_money($user_id, $number, model('common/UserWallet')::MONEYTYPEARNINGS, model('common/UserWallet')::OPERATION_WITHDRAW,model('common/UserWallet')::ChangeTypeLable(model('common/UserWallet')::OPERATION_WITHDRAW));
|
|
|
|
|
|
if ($reslut['code'] != 1) {
|
|
|
|
|
|
Db::rollback();
|
|
|
|
|
|
return V($reslut['code'],$reslut['msg']);
|
|
|
|
|
|
}
|
|
|
|
|
|
//增加冻结余额
|
|
|
|
|
|
$reslut = Db::name('user_wallet')->where('user_id', $user_id)->setInc('frozen_earnings', $number);
|
|
|
|
|
|
if (!$reslut) {
|
|
|
|
|
|
Db::rollback();
|
|
|
|
|
|
return ['code' => 0, 'msg' => "请重试2", 'data' => null];
|
|
|
|
|
|
}
|
|
|
|
|
|
//修改提现余额
|
|
|
|
|
|
$surplus_earnings = Db::name('user_wallet')->where('user_id', $user_id)->value('earnings');
|
|
|
|
|
|
if ($surplus_earnings > 0) {
|
|
|
|
|
|
$surplus_earnings = $surplus_earnings / $diamond_to_rmb_ratio; //剩余金额(人民币)
|
|
|
|
|
|
}
|
|
|
|
|
|
DB::name('vs_user_withdrawal')->where(['wid'=>$user_withdrawal_wid])->update(['surplus_money'=>$surplus_earnings]);
|
|
|
|
|
|
// 提交事务
|
|
|
|
|
|
Db::commit();
|
|
|
|
|
|
return ['code' => 1, 'msg' => "提现成功", 'data' => null];
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
|
// 回滚事务
|
|
|
|
|
|
Db::rollback();
|
|
|
|
|
|
return ['code' => 0, 'msg' => "提现失败", 'data' => null];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//生成订单号
|
|
|
|
|
|
private function create_user_withdrawal_order_sn()
|
|
|
|
|
|
{
|
|
|
|
|
|
$order_sn = 'TX' . date('YmdHis') . mt_rand(10000, 99999);
|
|
|
|
|
|
$reslut = db::name('vs_user_withdrawal')->where('order_sn', $order_sn)->find();
|
|
|
|
|
|
if (empty($reslut)) {
|
|
|
|
|
|
return $order_sn;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$this->create_user_withdrawal_order_sn();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* 提现失败处理
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function withdrawal_fail($order_id){
|
|
|
|
|
|
$withdraw_info = db::name('vs_user_withdrawal')->where('order_sn', $order_id)->find();
|
|
|
|
|
|
//退回提现金额(释放冻结金额)
|
|
|
|
|
|
//钻石兑换人民币比例
|
|
|
|
|
|
$diamond_to_rmb_ratio = get_system_config_value('diamond_to_rmb_ratio');
|
|
|
|
|
|
$money = $withdraw_info['money'] * $diamond_to_rmb_ratio; //提现金额转换 人民币转为钻石
|
|
|
|
|
|
$result = db::name('user_wallet')->where('user_id', $withdraw_info['user_id'])->setDec('frozen_earnings', $money);
|
|
|
|
|
|
if (!$result) {
|
|
|
|
|
|
return ['code' => 0, 'msg' => '提现失败'];
|
|
|
|
|
|
}
|
|
|
|
|
|
//归还账户余额
|
|
|
|
|
|
$reslut = model('common/UserWallet')->change_user_money($withdraw_info['user_id'], $money, model('common/UserWallet')::MONEYTYPEARNINGS, model('common/UserWallet')::WITHDRAW_FAILURE,model('common/UserWallet')::ChangeTypeLable(model('common/UserWallet')::WITHDRAW_FAILURE));
|
|
|
|
|
|
if ($reslut['code'] != 1) {
|
|
|
|
|
|
return ['code' => 0, 'msg' => $reslut['msg']];
|
|
|
|
|
|
}
|
|
|
|
|
|
return ['code' => 1, 'msg' => '成功'];
|
|
|
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
|
|
|
* 提现列表
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function withdrawal_list($user_id,$page=1,$page_limit=10,$search_stime="",$search_etime=""){
|
|
|
|
|
|
$where = [];
|
|
|
|
|
|
$where['user_id'] = $user_id;
|
|
|
|
|
|
if($search_stime){
|
|
|
|
|
|
$where['createtime'] = ['>=',strtotime($search_stime)];
|
|
|
|
|
|
}
|
|
|
|
|
|
if($search_etime){
|
|
|
|
|
|
$where['createtime'] = ['<=',strtotime($search_etime.' 23:59:59')];
|
|
|
|
|
|
}
|
|
|
|
|
|
if(!empty($search_stime) && !empty($search_etime)){
|
|
|
|
|
|
$where['createtime'] = ['between',[strtotime($search_stime),strtotime($search_etime.' 23:59:59')]];
|
|
|
|
|
|
}
|
|
|
|
|
|
$withdraw_list = db::name('vs_user_withdrawal')
|
|
|
|
|
|
->where($where)
|
|
|
|
|
|
->page($page,$page_limit)
|
|
|
|
|
|
->select();
|
|
|
|
|
|
$return_data = [];
|
|
|
|
|
|
foreach ($withdraw_list as $key=>$value){
|
|
|
|
|
|
$return_data[$key]['name'] = "收益提现";
|
|
|
|
|
|
$return_data[$key]['money'] = $value['money'];
|
|
|
|
|
|
$return_data[$key]['status'] = $value['status'];
|
|
|
|
|
|
$return_data[$key]['status_str'] = $this->withdraw_status[$value['status']];
|
2025-09-30 12:04:35 +08:00
|
|
|
|
//手续费
|
|
|
|
|
|
$return_data[$key]['withdraw_fee'] = $value['server_money'] ?? 0;
|
|
|
|
|
|
$return_data[$key]['personal_tax_rate'] = $value['personal_tax_rate'] ?? 0; //税率
|
|
|
|
|
|
$return_data[$key]['received_tax_amount'] = $value['received_tax_amount'] ?? 0; //税费
|
|
|
|
|
|
//预计到账金额
|
|
|
|
|
|
//1待处理2已通过3已拒绝 4打款中[云账户]5打款失败[云账户]6已打款[云账户]
|
|
|
|
|
|
if($value['status'] == 1 || $value['status'] == 2 || $value['status'] ==4){
|
|
|
|
|
|
$surplus_money = $value['money'] - $value['server_money'];
|
2025-09-30 15:09:55 +08:00
|
|
|
|
$return_data[$key]['received_tax_amount'] = 0;
|
2025-09-30 12:04:35 +08:00
|
|
|
|
}elseif($value['status'] == 3 || $value['status'] ==5){
|
|
|
|
|
|
$surplus_money = $value['money'];
|
2025-09-30 15:09:55 +08:00
|
|
|
|
$return_data[$key]['withdraw_fee'] = 0;
|
|
|
|
|
|
$return_data[$key]['received_tax_amount'] = 0;
|
2025-09-30 12:04:35 +08:00
|
|
|
|
}elseif($value['status'] == 6){
|
|
|
|
|
|
$surplus_money = $value['money'] - $value['server_money'] - $value['received_tax_amount'];
|
|
|
|
|
|
}else{
|
|
|
|
|
|
$surplus_money = $value['money'];
|
|
|
|
|
|
}
|
|
|
|
|
|
$return_data[$key]['surplus_money'] = $surplus_money;
|
|
|
|
|
|
|
2025-08-13 10:43:56 +08:00
|
|
|
|
$return_data[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
|
|
|
|
|
|
}
|
|
|
|
|
|
return ['code' => 1, 'msg' => '成功', 'data' => $return_data];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|