Test.php 8.5 KB

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