Test.php 8.1 KB

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