Test.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace app\index\controller;
  3. use think\Db;
  4. class Index
  5. {
  6. /**
  7. * 全麦/单独赠送礼物
  8. */
  9. public function giveGiftToYou() {
  10. // 接口防并发
  11. if (!$this->apiLimit(1, 1000)) {
  12. $this->error(__('Operation frequently'));
  13. }
  14. //
  15. $user_ids = $this->request->request("user_id");// 赠送对象
  16. $gift_id = $this->request->request("gift_id");// 礼物ID
  17. $party_id = $this->request->request("party_id",0);// 派对ID
  18. $number = $this->request->request("number");// 赠送数量
  19. $room_type = 1; // 房间类型
  20. $is_back = 0;// 是否背包赠送: 1=是,0=否
  21. if (!$user_ids || !$gift_id || !$number)
  22. {
  23. $this->error(__('Invalid parameters'));
  24. }
  25. //
  26. $user_id_arr = explode(',',$user_ids);
  27. $userCount = count($user_id_arr);
  28. $userauthid = $this->auth->id;
  29. // 获取礼物信息
  30. $giftInfo = Db::name('gift')->where('id',$gift_id)->find();
  31. if (!$giftInfo) {$this->error("请选择礼物!");}
  32. $giftValue = $giftInfo["value"] * $number;
  33. $giftCountValue = $giftInfo["value"] * $number * $userCount;
  34. // 判断当前用户余额
  35. $user_gold = model('wallet')->getWallet($userauthid,'gold');
  36. if($user_gold < $giftCountValue) {$this->error("您的金币余额不足!");}
  37. if($party_id){
  38. $partyInfo = \app\common\model\Party::field("user_id,platRate,guilderRate")->where(["id"=>$party_id])->find();
  39. if(!$partyInfo){
  40. $this->error('不存在的语聊间');
  41. }
  42. }
  43. $returnData = [];
  44. Db::startTrans();
  45. try {
  46. $redis = new Redis();
  47. $redisconfig = config("redis");
  48. $redis->connect($redisconfig["host"], $redisconfig["port"], 86400 * 31);
  49. // 获取当天零点
  50. $day = date("Ymd");
  51. // 获取本周第一天
  52. $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  53. // 获取本月第一天
  54. $monthday = date("Ym01");
  55. $allVal = 0;
  56. $i = 0;
  57. foreach($user_id_arr as $user_id) {
  58. // 添加礼物赠送记录表
  59. $data = [];
  60. $data["user_id"] = $userauthid;
  61. $data["user_to_id"] = $user_id;
  62. $data["party_id"] = $party_id;
  63. $data["gift_id"] = $gift_id;
  64. $data["gift_give_type"] = 2;
  65. $data["gift_name"] = $giftInfo["name"];
  66. $data["gift_gif_image"] = $giftInfo["image"];
  67. $data["number"] = $number;
  68. $data["price"] = $giftInfo["value"];
  69. $data["value"] = $giftValue;
  70. $data["createtime"] = time();
  71. $log_id = Db::name('gift_user_party')->insertGetId($data);
  72. if(!$log_id){
  73. Db::rollback();
  74. $this->error('赠送失败');
  75. }
  76. if($giftValue > 0){
  77. // 扣除当前用户钻石余额
  78. $wallet_rs = model('wallet')->lockChangeAccountRemain($userauthid,'gold',-$giftValue,51,'赠送礼物:'.$giftInfo["name"],'gift_user_party',$log_id);
  79. if($wallet_rs['status'] === false){
  80. Db::rollback();
  81. $this->error($wallet_rs['msg']);
  82. }
  83. // 添加赠送用户余额
  84. $money_to_gold = config('site.money_to_gold');
  85. $gift_plat_scale = config('site.gift_plat_scale');
  86. $giftmoney = bcdiv($giftValue,$money_to_gold,2);
  87. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  88. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,'money',$money,52,'获得礼物:'.$giftInfo["name"],'gift_user_party',$log_id);
  89. if($wallet_rs['status'] === false){
  90. Db::rollback();
  91. $this->error($wallet_rs['msg']);
  92. }
  93. }
  94. $res6 = true;
  95. if ($res6) {
  96. $i++;
  97. if($party_id > 0) {
  98. // 添加redis记录做财富排行榜日榜用
  99. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $day . "d", $giftValue, $user_id);
  100. // 添加redis记录做财富排行榜周榜用
  101. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $weekday . "w", $giftValue, $user_id);
  102. // 添加redis记录做财富排行榜月榜用
  103. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $monthday . "m", $giftValue, $user_id);
  104. // 添加redis记录做贡献排行榜日榜用
  105. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $day . "d", $giftValue, $userauthid);
  106. // 添加redis记录做贡献排行榜周榜用
  107. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $weekday . "w", $giftValue, $userauthid);
  108. // 添加redis记录做贡献排行榜月榜用
  109. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $monthday . "m", $giftValue, $userauthid);
  110. // tcp 更新用户魅力值
  111. $this->updateUserCharm($party_id, $user_id, $giftValue);
  112. }
  113. $allVal = $allVal + $giftValue;
  114. }
  115. }
  116. // 获取用户魅力值
  117. $users = $redis->zRange("hourCharm_".$party_id,0,-1,true);
  118. $u = [];
  119. if($users) {
  120. foreach($users as $k => $v) $u[] = [
  121. "user_id"=>$k,
  122. "charm"=>$this->changeW($v)
  123. ];
  124. }
  125. $userCharm = $u;
  126. // tcp 更新房间热度
  127. $partyHot = $this->updatePartyHot($party_id, $allVal, $room_type);
  128. // 如果是派对,则添加派对热度值记录做榜单统计
  129. if($room_type == 1) {
  130. $data = [];
  131. $data["party_id"] = $party_id;
  132. $data["hot"] = $allVal;
  133. $data["createtime"] = time();
  134. \app\common\model\PartyHot::insert($data);
  135. }
  136. // tcp 获取房间用户周前三名
  137. $partyUserTop = $this->getPartyUserTop($party_id, $room_type);
  138. if($i == $userCount) {
  139. $returnData["userCharm"] = $userCharm;
  140. $returnData["partyHot"] = $this->changeW($partyHot);
  141. $returnData["partyUserTop"] = $partyUserTop;
  142. $returnData["image"] = $giftInfo["image"];
  143. $returnData["gif_image"] = $giftInfo["special"];
  144. Db::commit();
  145. $this->success("赠送成功!",$returnData);
  146. } else {
  147. $this->success("赠送失败!");
  148. }
  149. } catch (ValidateException $e) {
  150. Db::rollback();
  151. $this->error($e->getMessage());
  152. } catch (PDOException $e) {
  153. Db::rollback();
  154. $this->error($e->getMessage());
  155. } catch (Exception $e) {
  156. Db::rollback();
  157. $this->error($e->getMessage());
  158. }
  159. }
  160. }