where(['activities_id'=>1,'status'=>1,'delete_time'=>0,'money'=>['>=',$money]])->order('money desc')->find(); $gift_bag_id = $gift_bag['id'] ?? 0; if (!$gift_bag_id) { return ['code' => 0, 'msg' => '礼包不存在','data' => null]; } $gift_bag_detail = DB::name('vs_gift_bag_detail')->where(['gift_bag_id'=>$gift_bag_id])->select(); if (!$gift_bag_detail) { return ['code' => 0, 'msg' => '礼包未配置','data' => null]; } Db::startTrans(); try { foreach ($gift_bag_detail as $k=>$v){ $gift_id =0; $gift_price = 0; $gift_num = $v['quantity']; switch ($v['type']) { case 1: //金币 方法1:直接添加到用户钱包 $gift_price = $v['quantity']; $res = model('common/UserWallet')->change_user_money($user_id, $v['quantity'], model('common/UserWallet')::MONEYTYPECOIN, model('common/UserWallet')::FIRST_CHARGE,model('common/UserWallet')::ChangeTypeLable(model('common/UserWallet')::FIRST_CHARGE)); break; case 2: //礼物 方法2:添加到用户礼物背包 $gift_id = $v['foreign_id']; $res = model('UserGiftPack')->change_user_gift_pack($user_id,$v['foreign_id'],$v['quantity'],model('UserGiftPack')::FIRST_CHARGE,"首充好礼获得礼物"); break; case 3: //坐骑 方法3:添加到用户装扮 $gift_id = $v['foreign_id']; $decorate_price_info = db::name('vs_decorate_price')->where(['id'=>$v['foreign_id']])->find(); if(empty($decorate_price_info)){ break; } $gift_price = $decorate_price_info['price']; $res = model('Decorate')->pay_decorate($user_id,$decorate_price_info['did'],$decorate_price_info['day'],5); break; case 4: //道具 方法5:钻石 $gift_price = $v['quantity']; $res = model('common/UserWallet')->change_user_money($user_id, $v['quantity'], model('common/UserWallet')::MONEYTYPEARNINGS, model('common/UserWallet')::FIRST_CHARGE,model('common/UserWallet')::ChangeTypeLable(model('common/UserWallet')::FIRST_CHARGE)); default: break; } if ($res['code'] != 1) { Db::rollback(); return ['code' => 0, 'msg' => $res['msg'], 'data' => null]; } // 记录日志 //添加礼盒记录 $reslut = Db::name('vs_gift_bag_receive_log')->insert([ 'user_id' => $user_id, 'gift_bag_id' => $gift_bag_id, 'gift_id'=> $gift_id, 'gift_price' => $gift_price, 'num' => $gift_num, 'bag_price' => $money, 'createtime' => time(), 'updatetime' => time() ]); } // 添加活动记录 $reslut = Db::name('vs_activities_receive')->insert([ 'user_id' => $user_id, 'activities_id' => 1, 'createtime' => time(), 'updatetime' => time() ]); Db::commit(); return ['code' => 1, 'msg' => '操作成功','data' => null]; }catch (\Exception $e) { // 回滚事务 Db::rollback(); return ['code' => 0, 'msg' => "请重试", 'data' => null]; } } /* * 天降好礼发放 */ public static function drop_gift_send($user_id,$money=0) { $activities_id = 3; //礼包 $gift_bag = DB::name('vs_gift_bag')->where(['activities_id'=>$activities_id,'status'=>1])->find(); $gift_bag_id = $gift_bag['id'] ?? 0; if (!$gift_bag_id) { return ['code' => 0, 'msg' => '礼包不存在','data' => null]; } if($money != $gift_bag['money']){ return ['code' => 0, 'msg' => '充值金额未命中天降好礼礼包','data' => null]; } $gift_bag_detail = DB::name('vs_gift_bag_detail')->where(['gift_bag_id'=>$gift_bag_id])->select(); if (!$gift_bag_detail) { return ['code' => 0, 'msg' => '礼包不存在','data' => null]; } Db::startTrans(); try { foreach ($gift_bag_detail as $k=>$v){ switch ($v['type']) { case 1: //金币 方法1:直接添加到用户钱包 $res = model('common/UserWallet')->change_user_money($user_id, $v['quantity'], model('common/UserWallet')::MONEYTYPECOIN, model('common/UserWallet')::DROP_GIFT_REWARD,model('common/UserWallet')::ChangeTypeLable(model('common/UserWallet')::DROP_GIFT_REWARD)); break; case 2: //礼物 方法2:添加到用户礼物背包 $res = model('UserGiftPack')->change_user_gift_pack($user_id,$v['foreign_id'],$v['quantity'],model('UserGiftPack')::DRAW_GIFT,"天降好礼获得礼物"); break; case 3: //坐骑 方法3:添加到用户装扮 $decorate_price_info = db::name('vs_decorate_price')->where(['id'=>$v['foreign_id']])->find(); if(empty($decorate_price_info)){ break; } $res = model('Decorate')->pay_decorate($user_id,$decorate_price_info['did'],$decorate_price_info['day'],6); break; case 4: //道具 方法5:钻石 $res = model('common/UserWallet')->change_user_money($user_id, $v['quantity'], model('common/UserWallet')::MONEYTYPEARNINGS, model('common/UserWallet')::DROP_GIFT_REWARD,model('common/UserWallet')::ChangeTypeLable(model('common/UserWallet')::DROP_GIFT_REWARD)); default: break; } if ($res['code'] != 1) { Db::rollback(); return ['code' => 0, 'msg' => $res['msg'], 'data' => null]; } // 记录日志 //添加礼盒记录 $reslut = Db::name('vs_gift_bag_receive_log')->insert([ 'user_id' => $user_id, 'gift_bag_id' => $gift_bag_id, 'parent_id' => $v['id'], 'gift_id'=> $v['foreign_id'], 'num' => $v['quantity'], 'bag_price' => $gift_bag['money'], 'gift_price' => $v['gold'], 'createtime' => time(), 'updatetime' => time() ]); } // 添加活动记录 $reslut = Db::name('vs_activities_receive')->insert([ 'user_id' => $user_id, 'activities_id' => $activities_id, 'createtime' => time(), 'updatetime' => time() ]); Db::commit(); return ['code' => 1, 'msg' => '操作成功','data' => null]; }catch (\Exception $e) { // 回滚事务 Db::rollback(); return ['code' => 0, 'msg' => "请重试", 'data' => null]; } } }