Files
fanyin-php/application/api/model/UserWithdrawal.php
2025-08-11 16:56:05 +08:00

217 lines
9.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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];
}
//判断用户是否签约云账户
$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']];
$return_data[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
}
return ['code' => 1, 'msg' => '成功', 'data' => $return_data];
}
}