123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- <?php
- namespace app\api\controller;
- use app\api\controller\Common;
- use app\common\model\PartyJoin;
- use fast\Random;
- use app\common\controller\RedisLeaderboard;
- use Redis;
- use think\Db;
- use think\Exception;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- use think\Request;
- use app\common\service\RoomService;
- /**
- * 派对礼物接口
- */
- class Partygift extends Common
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = ['*'];
- /**
- * 单独赠送礼物,一键清空背包
- */
- public function giveAllbackGiftToYou(){
- // 接口防并发
- if (!$this->apiLimit(1, 1000)) {
- $this->error(__('Operation frequently'));
- }
- $user_ids = $this->request->request("user_id");// 赠送对象
- $party_id = $this->request->request("party_id",0);// 派对ID
- $room_type = $this->request->request('room_type',1); // 房间类型
- $is_back = 1; // 是否背包赠送: 1=是,0=否
- if (!$user_ids || !in_array($room_type,[1,2]))
- {
- $this->error(__('Invalid parameters'));
- }
- //我所有的背包礼物
- $getMyBackGift = $this->getMyBackGift($this->auth->id);
- if(empty($getMyBackGift)){
- $this->error('背包里没有礼物了');
- }
- //依次走赠送礼物接口
- foreach($getMyBackGift as $backkey => $backval){
- $this->giveGiftToYou_back($user_ids,$backval['id'],$party_id,$room_type,$backval['number'],$is_back);
- }
- }
- /**
- * 获取我的背包礼物
- */
- private function getMyBackGift($userid) {
- $list = \app\common\model\GiftBack::field("id,name,sum(number) as number")
- ->where(["user_id"=>$userid,"is_use"=>0])
- ->group("name")
- ->select();
- return $list;
- }
- //giveGiftToYou 改的,只改了前几行传参,request接收改用变量接收,其他不变,完全复制
- private function giveGiftToYou_back($user_ids,$gift_id,$party_id = 0,$room_type = 1,$number,$is_back = 0) {
- if (!$user_ids || !in_array($is_back,[0,1]) || !$gift_id || !$number || !in_array($room_type,[1,2])) $this->error(__('Invalid parameters'));
- $user_id_arr = explode(",",$user_ids);
- $userCount = count($user_id_arr);
- $userauthid = $this->auth->id;
- $soundCoinRate = config("site.giftCoin"); // 声币兑换比例
- $userModel = new \app\common\model\User();
- $backGiftId = 0;
- $boxgiftInfo = [];
- if($is_back == 1) {
- // 获取背包礼物信息
- $giftInfo = \app\common\model\GiftBack::get($gift_id);
- if(!$giftInfo) $this->error("背包礼物获取失败");
- $backGiftId = $giftInfo->gift_id;
- // 随机获取一个礼物
- $allCount = $number*$userCount;
- $giftbackList = \app\common\model\GiftBack::where(["name"=>$giftInfo->name,"user_id"=>$userauthid,'is_use'=>0])->limit($allCount)->select();
- $giftInfo = isset($giftbackList[0])?$giftbackList[0]:[];
- $giftcount = 0;
- $giftList = [];
- if($giftbackList) foreach($giftbackList as $k => $v) {
- $giftcount = $giftcount + $v["number"];
- $giftList[$k] = $v;
- if($giftcount >= $allCount) {
- break;
- }
- }
- if($giftcount < $allCount) $this->error("背包数量不足");
- $giftValue = $giftInfo["value"] * $number;
- $getValue = $giftInfo["value"] * $number;
- } else {
- // // 不可以赠送给自己
- // if(in_array($userauthid,$user_id_arr)) $this->error("不可以赠送给自己!");
- // 获取礼物信息
- $giftModel = new \app\common\model\Gift();
- $where = [];
- $where["id"] = $gift_id;
- $giftInfo = $giftModel->where($where)->find();
- if (!$giftInfo) $this->error("请选择礼物!");
- $giftValue = $giftInfo["value"] * $number;
- $giftCountValue = $giftInfo["value"] * $number * $userCount;
- $getValue = $giftValue;
- // 判断如果是礼物盒则随机开礼物盒礼物
- if($giftInfo->box_type > 0) {
- $boxgiftInfo = $this->getBoxGift($giftInfo->box_type);
- $getValue = $boxgiftInfo["price"];
- }
- // 判断当前用户余额
- $where = [];
- $where["id"] = $userauthid;
- $userInfo = $userModel->where($where)->lock(true)->find();
- if (!$userInfo) $this->error("用户信息查询失败!");
- if($userInfo["jewel"] < $giftCountValue) $this->error("您的钻石余额不足!");
- }
- $hotValue = $getValue;
- $getValue = round($getValue * ($soundCoinRate/100));
- // 转换统计
- $progetValue = $hotValue - $getValue;
- if($progetValue >0) {
- $data = [];
- $data["user_id"] = $user_ids;
- $data["party_id"] = $party_id?$party_id:0;
- $data["gift_value"] = $hotValue;
- $data["plat_value"] = $hotValue - $getValue;
- $data["createtime"] = time();
- \app\common\model\UserChangeLog::insert($data);
- }
- // 转换声币后 再进行抽点设置
- $partyInfo = null;
- if(!$party_id) {
- $platRate = 10;
- $guilderRate = 30;
- } else {
- $partyInfo = \app\common\model\Party::field("user_id,platRate,guilderRate")->where(["id"=>$party_id])->find();
- // 获取系统配置信息
- $platRate = $partyInfo->platRate; // 平台抽成百分比
- $guilderRate = $partyInfo->guilderRate; // 工会长抽成百分比
- }
- $platValue = bcmul($platRate/100,$getValue);
- $guilderValue = bcmul($guilderRate/100,$getValue);
- $getValue = bcsub(bcsub($getValue,$platValue),$guilderValue);
- // $gif_image = $is_back==1?$giftInfo["gif_image"]:$giftInfo["special"];
- $returnData = [];
- Db::startTrans();
- try {
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"], 86400 * 31);
- if ($redisconfig['redis_pwd']) {
- $redis->auth($redisconfig['redis_pwd']);
- }
- if($redisconfig['redis_selectdb'] > 0){
- $redis->select($redisconfig['redis_selectdb']);
- }
- // 事务处理余额与记录信息
- $userjewellogModel = new \app\common\model\UserJewelLog();
- //$usersoundcoinlogModel = new \app\common\model\UserSoundcoinLog();
- // 获取当天零点
- $day = date("Ymd");
- // 获取本周第一天
- $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
- // 获取本月第一天
- $monthday = date("Ym01");
- $allVal = 0;
- $i = 0;
- foreach($user_id_arr as $user_id) {
- // 获取赠送用户信息
- $where = [];
- $where["id"] = $user_id;
- $touserInfo = $userModel->where($where)->find();
- if($is_back == 1) {
- $b=0;
- foreach($giftList as $k => $v) {
- for($a=1;$a<=$v["number"];$a++) {
- $b++;
- $num = $v["number"] - $a;
- if($num > 0) {
- $res1 = \app\common\model\GiftBack::where(["id"=>$v["id"]])->setDec("number");
- } else {
- $res1 = \app\common\model\GiftBack::update(["is_use"=>1,"use_time"=>time()],["id"=>$v["id"]]);
- }
- if($b == $number) break;
- }
- }
- $res2 = true;
- } else {
- // 扣除当前用户钻石余额
- $where = [];
- $where["id"] = $userauthid;
- $res1 = $userModel->where($where)->setDec("jewel", $giftValue);
- // 添加当前用户钻石流水记录
- $res2 = $userjewellogModel->addUserJewelLog($userauthid, $giftValue, "-", $userInfo["jewel"], "赠送礼物:'" . $giftInfo["name"] . "',扣除" . $giftValue . "钻石!", 3);
- }
- $giftuserpartyModel = new \app\common\model\GiftUserParty();
- // 添加礼物赠送记录表
- $data = [];
- $data["user_id"] = $userauthid;
- $data["user_to_id"] = $user_id;
- $data["party_id"] = $party_id;
- if($boxgiftInfo){
- $data["gift_id"] = $boxgiftInfo['gift_id'];
- $data["gift_give_type"] = 2;
- $data["gift_name"] = $boxgiftInfo["gift_name"];
- $data["gift_gif_image"] = $boxgiftInfo["image"];
- $data["number"] = $number;
- $data["price"] = $boxgiftInfo["price"];
- $data["value"] = $boxgiftInfo["price"] * $number;
- }else{
- $data["gift_id"] = $backGiftId > 0 ? $backGiftId : $gift_id;
- $data["gift_give_type"] = $is_back ? 1 : 2;
- $data["gift_name"] = $giftInfo["name"];
- $data["gift_gif_image"] = $giftInfo["image"];
- $data["number"] = $number;
- $data["price"] = $giftInfo["value"];
- $data["value"] = $giftValue;
- }
- $data["createtime"] = time();
- $res5 = $giftuserpartyModel->allowField(true)->save($data);
- // 添加赠送用户声币余额
- /* $where = [];
- $where["id"] = $user_id;
- $res3 = $userModel->where($where)->setInc("sound_coin", $getValue);
- $getValue == 0 && $res3 = true;*/
- // 添加赠送用户声币流水记录soundCoin
- // xxx送你
- //$res4 = $usersoundcoinlogModel->addUserSoundcoinLog($user_id, $getValue, "+", $touserInfo["sound_coin"], "{$this->auth->nickname}送你{$giftInfo['name']}x{$number}", 1, $giftuserpartyModel->id);
- /*if ($partyInfo && $guilderValue > 0) {
- // 增加房主抽成
- $where = [];
- $where["id"] = $partyInfo->user_id;
- $userModel->where($where)->setInc("sound_coin", $guilderValue);
- // 添加赠送用户声币流水记录soundCoin
- // xxx送礼物给xxx,房间礼物抽成
- $usersoundcoinlogModel->addUserSoundcoinLog($partyInfo->user_id, $guilderValue, "+", $touserInfo["sound_coin"], "{$this->auth->nickname}送礼物{$giftInfo['name']}x{$number}给{$touserInfo['nickname']},房间礼物抽成", 4, $giftuserpartyModel->id);
- }*/
- //总消费增加
- if($is_back != 1){
- $res6 = Db::name('user')->where('id',$user_id)->setInc("renewcount", $giftCountValue);
- }
- if ($res1 && $res2 && $res5) {
- $i++;
- if($party_id > 0) {
- // 添加redis记录做财富排行榜日榜用
- $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $day . "d", $hotValue, $user_id);
- // 添加redis记录做财富排行榜周榜用
- $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $weekday . "w", $hotValue, $user_id);
- // 添加redis记录做财富排行榜月榜用
- $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $monthday . "m", $hotValue, $user_id);
- // 添加redis记录做贡献排行榜日榜用
- $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $day . "d", $giftValue, $userauthid);
- // 添加redis记录做贡献排行榜周榜用
- $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $weekday . "w", $giftValue, $userauthid);
- // 添加redis记录做贡献排行榜月榜用
- $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $monthday . "m", $giftValue, $userauthid);
- // tcp 更新用户魅力值
- $this->updateUserCharm($party_id, $user_id, $hotValue);
- // 如果是主播,则添加魅力值记录做榜单统计
- if($room_type == 2) {
- $data = [];
- $data["user_id"] = $user_id;
- $data["party_id"] = $party_id;
- $data["charm"] = $hotValue;
- $data["createtime"] = time();
- \app\common\model\UserCharmRank::insert($data);
- }
- }
- $getempirical = config("site.getempirical");
- $getempirical = $getempirical * $hotValue;
- // 获取用户贵族信息
- $noble = \app\common\model\User::getUserNoble($this->auth->id);
- if(isset($noble["noble_on"]) && $noble["noble_on"] == 1) {
- $getempirical = $getempirical + $getempirical * ($noble["explain"]/100);
- }
- // 增加用户经验值
- $res = \app\common\model\User::addEmpirical($this->auth->id,$getempirical);
- if ($res){
- $this->auth->level = $res->level;
- }
- // +exp
- \app\common\model\TaskLog::tofinish($this->auth->id,"OBHqCX4g",$number);
- //贵族升级处理
- //$this->checkBeNoble($redis,$this->auth->id,$giftCountValue);
- // +message
- \app\common\model\Message::addMessage($user_id,"礼物通知","收到 ".$this->auth->nickname." 赠送的".$giftInfo["name"]." x".$number);
- $allVal = $allVal + $hotValue;
- // // 剪掉背包礼物
- // if($is_back == 1) {
- // \app\common\model\GiftBack::update(["is_use"=>1],["id"=>$gift_id]);
- // }
- }
- }
- // 获取用户魅力值
- $users = $redis->zRange("hourCharm_".$party_id,0,-1,true);
- $u = [];
- if($users) {
- foreach($users as $k => $v) $u[] = [
- "user_id"=>$k,
- "charm"=>$this->changeW($v)
- ];
- }
- $userCharm = $u;
- // tcp 更新房间热度
- $partyHot = $this->updatePartyHot($party_id, $allVal, $room_type);
- // 如果是派对,则添加派对热度值记录做榜单统计
- if($room_type == 1) {
- $data = [];
- $data["party_id"] = $party_id;
- $data["hot"] = $allVal;
- $data["createtime"] = time();
- \app\common\model\PartyHot::insert($data);
- }
- // tcp 获取房间用户周前三名
- $partyUserTop = $this->getPartyUserTop($party_id, $room_type);
- if($i == $userCount) {
- $returnData["userCharm"] = $userCharm;
- $returnData["partyHot"] = $this->changeW($partyHot);
- $returnData["partyUserTop"] = $partyUserTop;
- if($is_back != 1) {
- if($giftInfo->box_type > 0) { // 不是背包,宝箱中赠送
- $returnData["box_type"] = $giftInfo->box_type;
- $returnData["box_image"] = $giftInfo->image;
- $returnData["image"] = $boxgiftInfo["image"];
- $returnData["name"] = $boxgiftInfo["gift_name"];
- $returnData["gif_image"] = $boxgiftInfo["special"];
- } else {
- $returnData["image"] = $giftInfo["image"];
- $returnData["gif_image"] = $giftInfo["special"];
- }
- } else {
- $returnData["image"] = $giftInfo["image"];
- $returnData["gif_image"] = $giftInfo["gif_image"];
- }
- // 增加抽点记录
- $data = [];
- $data["user_id"] = $user_ids;
- $data["party_id"] = $party_id?$party_id:0;
- $data["gift_value"] = $getValue;
- $data["plat_value"] = $platValue;
- $data["guilder_value"] = $guilderValue;
- $data["createtime"] = time();
- \app\common\model\UserProfitLog::insert($data);
- Db::commit();
- $this->success("赠送成功!",$returnData);
- } else {
- $this->success("赠送失败!");
- }
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- }
- }
|