From f11fc0a0996360551b27c658273d7d9285d9e040 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=8D=8E=E6=B8=85?= <18691022700@163.com> Date: Wed, 7 Jan 2026 13:57:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E6=96=B0=E9=92=B1=E5=8C=85?= =?UTF-8?q?=E5=92=8C=E9=92=BB=E7=9F=B3=E5=85=91=E6=8D=A25=E5=88=86?= =?UTF-8?q?=E9=92=9F=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/api/model/UserWallet.php | 64 +++++++++++----------------- 1 file changed, 24 insertions(+), 40 deletions(-) diff --git a/application/api/model/UserWallet.php b/application/api/model/UserWallet.php index ab63dc7a..8d523f6a 100644 --- a/application/api/model/UserWallet.php +++ b/application/api/model/UserWallet.php @@ -2,6 +2,7 @@ namespace app\api\model; +use think\Cache; use think\Db; use think\Model; use Yzh\YunPay; @@ -11,8 +12,14 @@ class UserWallet extends Model { public function create_data($uid) { - $insert_data = ['user_id' => $uid]; - return Db::name('user_wallet')->insert($insert_data); + $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; + } } //钱包 @@ -21,8 +28,9 @@ class UserWallet extends Model if(!isset($uid)){ return ['code' => 301, 'msg' => '登录失效,请重新登录!', 'data' => null]; } - - $user_info = db::name('user_wallet')->where('user_id', $uid)->find(); +// $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(); @@ -67,11 +75,9 @@ class UserWallet extends Model 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') @@ -114,6 +120,13 @@ class UserWallet extends Model //收益(钻石)兑换金币 public function exchange_coin($uid, $earnings_num) { + //从缓存中获取 + $exchange_user = Cache::get('exchange_user_' . $uid); + if($exchange_user){ + return ['code' => 0, 'msg' => '请5分钟后再次兑换', 'data' => null]; + }else{ + Cache::set('exchange_user_' . $uid, 1, 5 * 60); + } if(empty($earnings_num)){ return ['code' => 0, 'msg' => '请输入需要兑换的钻石数量', 'data' => null]; } @@ -122,46 +135,19 @@ class UserWallet extends Model return ['code' => 0, 'msg' => '请输入正确的钻石数量', 'data' => null]; } //判断用户的钻石数量是否大于等于兑换数量 - $user_info = db::name('user_wallet')->where('user_id',$uid)->find(); + $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'); - $data = [ - 'user_id' => $uid, - 'earnings_num' => $earnings_num, - 'coin_num' => $earnings_num * $ear_exchange_coin, - 'createtime' => time(), - ]; + $coin_num = $earnings_num * $ear_exchange_coin; //开启事务 db::startTrans(); try{ - $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, - '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,'earnings' => ['>=', $earnings_num]])->setDec('earnings',$earnings_num); - $re2 = db::name('user_wallet')->where('user_id',$uid)->setInc('coin',$earnings_num * $ear_exchange_coin); - if($re && $re1 && $re2){ + $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{ @@ -172,8 +158,6 @@ class UserWallet extends Model db::rollback(); return ['code' => 0, 'msg' => '兑换失败', 'data' => null]; } - - } //转币