Files
midi-php/application/api/model/UserWallet.php

200 lines
8.6 KiB
PHP
Raw Normal View History

2025-08-13 10:43:56 +08:00
<?php
namespace app\api\model;
use think\Db;
use think\Model;
use Yzh\YunPay;
class UserWallet extends Model
{
public function create_data($uid)
{
$insert_data = ['user_id' => $uid];
return Db::name('user_wallet')->insert($insert_data);
}
//钱包
public function wallet($uid)
{
if(!isset($uid)){
return ['code' => 301, 'msg' => '登录失效,请重新登录!', 'data' => null];
}
$user_info = db::name('user_wallet')->where('user_id', $uid)->find();
//获取云账号签约协议
$yun_pay = new YunPay();
$yun_pay_agreement = $yun_pay->getAgreementPreviewUrl();
if($yun_pay_agreement['code'] == 1){
$user_info['url'] = $yun_pay_agreement['data']['url'];
$user_info['title'] = $yun_pay_agreement['data']['title'];
}else{
$user_info['url'] = "";
$user_info['title'] = "";
}
if($user_info){
return ['code' => 1, 'msg' => '获取成功', 'data' => $user_info];
}
return ['code' => 0, 'msg' => '获取失败', 'data' => null];
}
//金币/钻石 明细
public function log_list($uid, $page, $page_limit,$in_out_type,$start_time,$end_time,$gift_type)
{
$page = intval($page);
$page_limit = $page_limit < 30 ? $page_limit : 30;
$map = [];
$map['money_type'] = $gift_type;//1金币2收益钻石
$in_out_types = [];
// 1.系统调节 2.充值 3.提现 4.金币转增(送出) 5.每日任务奖励 6.充值返利 7.购买装扮
// 8.礼盒奖励 9.房间补贴 10.购买礼物 11.收礼增加收益 12.工会补贴 13.转赠金币(接收) 14.收益兑换
// 15.首充 16.天降好礼充值 17.退出工会扣款 18.房主收益 19.主持人收益20.发布头条扣除余额,21.公会长收益,22.提现驳回或提现失败返还,23.财富等级奖励金币领取,24.删除关系扣金币
2025-10-11 16:30:11 +08:00
//27.小时榜获得28-新人充值好礼,32-发红包金币29-发红包钻石30-抢红包金币31-抢红包(钻石)
2025-08-13 10:43:56 +08:00
if($gift_type == 1){ //1金币2收益钻石
if($in_out_type == 1){//1收入
2025-10-11 16:30:11 +08:00
$in_out_types = [2,5,6,8,13,14,15,16,22,23,26,27,30,28];
2025-08-13 10:43:56 +08:00
}elseif($in_out_type == 2){//2支出
2025-10-11 16:30:11 +08:00
$in_out_types = [4,7,10,17,20,24,25,32];
2025-08-13 10:43:56 +08:00
}
}elseif($gift_type == 2){ //1金币2收益钻石
if($in_out_type == 1){//1收入
2025-10-11 16:30:11 +08:00
$in_out_types = [6,9,11,12,18,19,21,22,31,28];
2025-08-13 10:43:56 +08:00
}elseif($in_out_type == 2){//2支出
2025-10-11 14:49:25 +08:00
$in_out_types = [3,14,29];
2025-08-13 10:43:56 +08:00
}
}
$map['change_type'] = ['in', $in_out_types];
$map['user_id'] =$uid;
// 时间范围处理
if (!empty($start_time) && !empty($end_time)) {
$start_time = strtotime($start_time);
$end_time = strtotime($end_time.' 23:59:59');
$map['createtime'] = ['between', [$start_time, $end_time]];
}
$list = Db::name('vs_user_money_log')
->where($map)
->field('log_id, user_id, change_type, change_value, remarks, createtime')
->order('log_id desc')
->page($page, $page_limit)
->select();
foreach ($list as $key => &$value) {
$value['change_type_name'] = model('common/UserWallet')->ChangeTypeLable($value['change_type']);
if($in_out_type==2){
$value['change_value'] = $value['change_value'] * -1;
}
}
return ['code' => 1, 'msg' => '获取成功', 'data' => $list];
}
//收益(钻石)兑换金币
public function exchange_coin($uid, $earnings_num)
{
if(empty($earnings_num)){
return ['code' => 0, 'msg' => '请输入需要兑换的钻石数量', 'data' => null];
}
//判断钻石数量 小数点后有效数字是一位
if(strlen($earnings_num) - strlen(str_replace('.', '', $earnings_num)) > 1){
return ['code' => 0, 'msg' => '请输入正确的钻石数量', 'data' => null];
}
//判断用户的钻石数量是否大于等于兑换数量
$user_info = db::name('user_wallet')->where('user_id',$uid)->find();
if($user_info['earnings'] < $earnings_num){
return ['code' => 0, 'msg' => '钻石数量不足', 'data' => null];
}
$ear_exchange_coin = get_system_config_value('coin_exchange_rate');
2025-08-13 10:43:56 +08:00
$data = [
'user_id' => $uid,
'earnings_num' => $earnings_num,
'coin_num' => $earnings_num * $ear_exchange_coin,
2025-08-13 10:43:56 +08:00
'createtime' => time(),
];
//开启事务
db::startTrans();
$give_gift_id = db::name('user_exchange')->insertGetId($data);
// 1.系统调节 2.充值 3.提现 4.金币转增(送出) 5.每日任务奖励 6.充值返利 7.购买装扮
// 8.礼盒奖励 9.房间补贴 10.购买礼物 11.收礼增加收益 12.工会补贴 13.转赠金币(接收) 14.收益兑换
$account_log[] = [
'user_id' => $uid,
'money_type' => 2,//1金币2钻石
'change_value' => $earnings_num,
'remarks' => '收益兑换',
'change_type' => 14,
'createtime' => time(),
'from_id' => $give_gift_id,
];
$account_log[] = [
'user_id' => $uid,
'money_type' => 1,
'change_value' => $earnings_num * $ear_exchange_coin,
2025-08-13 10:43:56 +08:00
'remarks' => '收益兑换',
'change_type' => 14,
'createtime' => time(),
'from_id' => $give_gift_id,
];
$re = db::name('vs_user_money_log')->insertAll($account_log);
$re1 = db::name('user_wallet')->where('user_id',$uid)->setDec('earnings',$earnings_num);
$re2 = db::name('user_wallet')->where('user_id',$uid)->setInc('coin',$earnings_num * $ear_exchange_coin);
2025-08-13 10:43:56 +08:00
if($re && $re1 && $re2){
db::commit();
return ['code' => 1, 'msg' => '兑换成功', 'data' => null];
}else{
db::rollback();
return ['code' => 0, 'msg' => '兑换失败', 'data' => null];
}
}
//转币
public function give_coin($uid, $give_uid, $coin){
if(empty($coin)){
return ['code' => 0, 'msg' => '请输入需要转的金币数量', 'data' => null];
}
if(strlen($coin) - strlen(str_replace('.', '', $coin)) > 1){
return ['code' => 0, 'msg' => '请输入正确的金币数量', 'data' => null];
}
$user_info = db::name('user_wallet')->where('user_id',$uid)->find();
if($user_info['coin'] < $coin){
return ['code' => 0, 'msg' => '金币数量不足', 'data' => null];
}
$user_info = db::name('user')->where('id',$uid)->find();
$give_useer = db::name('user')->where('id',$give_uid)->find();
if(empty($user_info)){
return ['code' => 0, 'msg' => '用户不存在', 'data' => null];
}
if(empty($give_useer)){
return ['code' => 0, 'msg' => '接受用户不存在', 'data' => null];
}
if($user_info['red_status'] == 0){
return ['code' => 0, 'msg' => '未开启转赠功能', 'data' => null];
}
if($user_info['id'] == $give_uid){
return ['code' => 0, 'msg' => '不能转给自己', 'data' => null];
}
$data = [
'user_id' => $uid,
'accept_user_id' => $give_uid,
'coin_num' => $coin,
'createtime' => time(),
];
//开启事务
db::startTrans();
$give_gift_id = db::name('user_coin_transfer')->insertGetId($data);
//金币处理
$res1 = model('common/UserWallet')->change_user_money($uid, $coin, model('common/UserWallet')::MONEYTYPECOIN, model('common/UserWallet')::TRANSFER_COIN,model('common/UserWallet')::ChangeTypeLable(model('common/UserWallet')::TRANSFER_COIN).':转赠给'.$give_useer['nickname'].$coin.'金币');
$res2 = model('common/UserWallet')->change_user_money($give_uid, $coin, model('common/UserWallet')::MONEYTYPECOIN, model('common/UserWallet')::RECEIVE_COIN,model('common/UserWallet')::ChangeTypeLable(model('common/UserWallet')::RECEIVE_COIN).':收到好友'.$user_info['nickname'].$coin.'金币');
if($res1 && $res2){
db::commit();
return ['code' => 1, 'msg' => '转赠成功', 'data' => null];
}else{
db::rollback();
return ['code' => 0, 'msg' => '转赠失败', 'data' => null];
}
}
}