Files
yusheng-php/application/api/model/UserWallet.php
2026-01-07 14:13:19 +08:00

342 lines
16 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\Cache;
use think\Db;
use think\Model;
use Yzh\YunPay;
class UserWallet extends Model
{
public function create_data($uid)
{
$insert_data = ['user_id' => $uid, 'createtime' => time()];
$coin = Db::name('user_wallet_coin')->insert($insert_data);
$earnings = Db::name('user_wallet_earnings')->insert($insert_data);
if($coin && $earnings){
return true ;
}else{
return false;
}
}
//钱包
public function wallet($uid)
{
if(!isset($uid)){
return ['code' => 301, 'msg' => '登录失效,请重新登录!', 'data' => null];
}
// $user_info = db::name('user_wallet')->where('user_id', $uid)->find();
$user_info['coin'] = db::name('user_wallet_coin')->where('user_id', $uid)->sum('coin');
$user_info['earnings'] = db::name('user_wallet_earnings')->where('user_id', $uid)->sum('earnings');
//获取云账号签约协议
$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];
}
//金币/钻石 明细
//change_type
// 1.系统调节 2.充值 3.提现 4.金币转增(送出) 5.每日任务奖励 6.充值返利 7.购买装扮
// 8.礼盒奖励 9.房间补贴 10.购买礼物 11.收礼增加收益 12.工会补贴 13.转赠金币(接收) 14.收益兑换
// 15.首充 16.天降好礼充值 17.退出工会扣款 18.房主收益 19.主持人收益 20.抢头条 21.公会长收益22.提现驳回或提现失败返还23.财富等级奖励金币领取24.删除关系扣金币,
// 25.赠送好友金币26.好友转赠所得金币27.小时榜获得金币28.新人充值好礼29.发红包钻石30.抢红包金币31.抢红包钻石32.发红包(金币),
// 33.红包剩余退回金币34-红包剩余退回钻石35-爵位购买赠送金币金币40-后台充值金币41-后台充值钻石42-后台扣除金币43-后台扣除钻石,
//50-签约,51-签约失败退回,52-首次被签约收入53.签约师傅每日返还,54-叛徒补偿55-徒弟收礼返佣56-续约57-签到58-爆币,59-红包手续费
public function log_list($uid, $last_id, $page_limit,$in_out_type,$start_time,$end_time,$gift_type)
{
// 时间范围处理
if (!empty($start_time) && !empty($end_time)) {
$start_time = strtotime($start_time);
$end_time = strtotime($end_time);
$map['createtime'] = ['between', [$start_time, $end_time]];
}
$map['user_id'] =$uid;
if ($last_id > 0) {
$map['id'] = ['<', $last_id]; // 下一页只查id小于上一页最后一个id的记录
}
//1-金币
if($gift_type == 1){
//1-收入
if($in_out_type == 1){
$in_out_types = [2,5,6,8,13,14,15,16,22,23,26,27,30,28,33,40,51,57,58];
$map['change_type'] = ['in', $in_out_types];
}else{//2-支出
$in_out_types = [4,7,10,17,20,24,25,32,42,50,56,59];
$map['change_type'] = ['in', $in_out_types];
}
$list = Db::name('user_coin_log')
->where($map)
->field('id,change_type, coin as change_value, remarks, createtime')
->order('id desc')
->limit($page_limit) // 只取指定条数无offset
->select();
foreach ($list as &$value) {
$value['change_type_name'] = model('common/UserWallet')->ChangeTypeLable($value['change_type']);
}
}else{//-2收益钻石
//1-收入
if($in_out_type == 1){
$in_out_types = [9,11,12,18,19,21,22,31,28,34,41,52,53,54,55,57];
$map['change_type'] = ['in', $in_out_types];
}else{//2-支出
$in_out_types = [3,14,29,43];
$map['change_type'] = ['in', $in_out_types];
}
$list = Db::name('user_earnings_log')
->where($map)
->field('id,change_type, earnings as change_value, remarks, createtime')
->order('id desc')
->limit($page_limit) // 只取指定条数无offset
->select();
foreach ($list as &$value) {
$value['change_type_name'] = model('common/UserWallet')->ChangeTypeLable($value['change_type']);
}
}
$next_last_id = !empty($list) ? end($list)['id'] : 0; // 下一页的last_id
$lists = [
'list' => $list,
'next_last_id' => $next_last_id,
];
return ['code' => 1, 'msg' => '获取成功', 'data' => $lists];
}
//收益(钻石)兑换金币
public function exchange_coin($uid, $earnings_num)
{
//从缓存中获取
$exchange_user = Cache::get('exchange_user_' . $uid);
if($exchange_user){
//获取缓存的过期剩余 时间
$time = Cache::store('redis')->handler()->ttl('exchange_user_' . $uid);
return ['code' => 0, 'msg' => '请'.$time.'秒后再次兑换', 'data' => null];
}else{
Cache::set('exchange_user_' . $uid, 1, 5 * 60);
}
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_earnings')->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');
$coin_num = $earnings_num * $ear_exchange_coin;
//开启事务
db::startTrans();
try{
$earn = $this->change_user_earnings_log($uid,$earnings_num,0,14,'收益兑换');
$coin = $this->change_user_cion_log($uid,$coin_num,0,14,'收益兑换');
if($earn === true && $coin === true){
db::commit();
return ['code' => 1, 'msg' => '兑换成功', 'data' => null];
}else{
db::rollback();
return ['code' => 0, 'msg' => '兑换失败', 'data' => null];
}
}catch (\Exception $e){
db::rollback();
return ['code' => 0, 'msg' => '兑换失败', 'data' => null];
}
}
//转币
public function give_coin($uid, $give_uid, $coin){
//从缓存中获取
$exchange_user = Cache::get('give_coin_user_' . $uid);
if($exchange_user){
return ['code' => 0, 'msg' => '请5分钟后再试', 'data' => null];
}else{
Cache::set('give_coin_user_' . $uid, 1, 5 * 60);
}
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];
}
}
/*
*操作用户金币并记录日志
* @param int $user_id 用户id
* @param int $money 金币数量
* @param int $change_type 变动类型
* @param int $room_id 房间
* @param str $remarks 备注
*/
//change_type
// 1.系统调节 2.充值 3.提现 4.金币转增(送出) 5.每日任务奖励 6.充值返利 7.购买装扮
// 8.礼盒奖励 9.房间补贴 10.购买礼物 11.收礼增加收益 12.工会补贴 13.转赠金币(接收) 14.收益兑换
// 15.首充 16.天降好礼充值 17.退出工会扣款 18.房主收益 19.主持人收益 20.抢头条 21.公会长收益22.提现驳回或提现失败返还23.财富等级奖励金币领取24.删除关系扣金币,
// 25.赠送好友金币26.好友转赠所得金币27.小时榜获得金币28.新人充值好礼29.发红包钻石30.抢红包金币31.抢红包钻石32.发红包(金币),
// 33.红包剩余退回金币34-红包剩余退回钻石35-爵位购买赠送金币金币40-后台充值金币41-后台充值钻石42-后台扣除金币43-后台扣除钻石,
//50-签约,51-签约失败退回,52-首次被签约收入53.签约师傅每日返还,54-叛徒补偿55-徒弟收礼返佣56-续约57-签到58-爆币,59-红包手续费
//资金操作
public function change_user_cion_log($user_id,$money,$room_id,$change_type,$remarks)
{
//增加用户金币类型
$in_types = [2,5,6,8,13,14,15,16,23,26,27,28,30,33,35,40,51,57,58];
//减少用户金币类型
$out_types = [4,7,10,17,20,24,25,32,42,50,56,59];
$user_coin = Db::name('user_wallet_coin')->where(['user_id' => $user_id])->value('coin');
if(in_array($change_type,$in_types)){
$update_coin = $user_coin + $money;
}elseif(in_array($change_type,$out_types)){
if($user_coin < $money){
return false;
}
$update_coin = $user_coin - $money;
}else{
$update_coin = $user_coin;
}
$res1 = Db::name('user_wallet_coin')->where(['user_id' => $user_id])->update(['coin' => $update_coin,'updatetime' => time()]);
if(!$res1){
return false;
}
$data = [
'user_id' => $user_id,
'room_id' => $room_id,
'coin' => $money,
'before' => $user_coin,
'after' => $user_coin - $money,
'change_type' => $change_type,
'remarks' => $remarks,
'createtime' => time()
];
$res = Db::name('user_coin_log')->insert($data);
if(!$res){
return false;
}
return true;
}
/*
*操作用户收益(钻石)并记录日志
* @param int $user_id 用户id
* @param int $money 数量
* @param int $change_type 变动类型
* @param int $room_id 房间
* @param str $remarks 备注
*/
//change_type
// 1.系统调节 2.充值 3.提现 4.金币转增(送出) 5.每日任务奖励 6.充值返利 7.购买装扮
// 8.礼盒奖励 9.房间补贴 10.购买礼物 11.收礼增加收益 12.工会补贴 13.转赠金币(接收) 14.收益兑换
// 15.首充 16.天降好礼充值 17.退出工会扣款 18.房主收益 19.主持人收益 20.抢头条 21.公会长收益22.提现驳回或提现失败返还23.财富等级奖励金币领取24.删除关系扣金币,
// 25.赠送好友金币26.好友转赠所得金币27.小时榜获得金币28.新人充值好礼29.发红包钻石30.抢红包金币31.抢红包钻石32.发红包(金币),
// 33.红包剩余退回金币34-红包剩余退回钻石35-爵位购买赠送金币金币40-后台充值金币41-后台充值钻石42-后台扣除金币43-后台扣除钻石,
//50-签约,51-签约失败退回,52-首次被签约收入53.签约师傅每日返还,54-叛徒补偿55-徒弟收礼返佣56-续约57-签到58-爆币,59-红包手续费
//bcadd: 加法运算
//bcsub: 减法运算
//bcmul: 乘法运算
//bcdiv: 除法运算
public function change_user_earnings_log($user_id,$money,$room_id,$change_type,$remarks)
{
//增加用户钻石类型
$in_types = [9,11,12,18,19,21,22,31,28,34,41,52,53,54,55,57];
//减少用户钻石类型
$out_types = [3,14,29,43];
$user_earnings = Db::name('user_wallet_earnings')->where(['user_id' => $user_id])->value('earnings');
if(in_array($change_type,$in_types)){
$update_coin = bcadd($user_earnings , $money,4);
}elseif(in_array($change_type,$out_types)){
if($user_earnings < $money){
return false;
}
$update_coin = bcsub($user_earnings , $money,4);
}else{
$update_coin = $user_earnings;
}
$res1 = Db::name('user_wallet_earnings')->where(['user_id' => $user_id])->update(['earnings' => $update_coin,'updatetime' => time()]);
if(!$res1){
return false;
}
$data = [
'user_id' => $user_id,
'room_id' => $room_id,
'earnings' => $money,
'before' => $user_earnings,
'after' => $user_earnings - $money,
'change_type' => $change_type,
'remarks' => $remarks,
'createtime' => time()
];
$res = Db::name('user_earnings_log')->insert($data);
if(!$res){
return false;
}
return true;
}
}