Livebc.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use Redis;
  6. /**
  7. * 视频直播间
  8. */
  9. class Livebc extends Api
  10. {
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = ['*'];
  13. //直播列表
  14. public function lists(){
  15. $type_id = input('type_id','');
  16. $map = [
  17. 'livebc.is_online' => 1,
  18. 'livebc.room_type' => 2,
  19. ];
  20. if(!empty($type_id)){
  21. $map['livebc.party_type'] = $type_id;
  22. }
  23. $list = Db::name('party')->alias('livebc')->field('livebc.id,livebc.user_id,livebc.party_name as title,livebc.cityname,user.nickname,user.avatar')
  24. ->join('user','livebc.user_id = user.id','LEFT')
  25. ->where($map)
  26. ->autopage()->select();
  27. $list = list_domain_image($list,['avatar']);
  28. $this->success('success',$list);
  29. }
  30. //直播间详情
  31. public function info(){
  32. $id = input('id',0);
  33. if(empty($id)){
  34. $this->error();
  35. }
  36. $map = [
  37. 'livebc.id' => $id,
  38. 'livebc.room_type' => 2,
  39. ];
  40. $info = Db::name('party')->alias('livebc')->field('livebc.id,livebc.user_id,livebc.party_name as title,livebc.cityname,user.nickname,user.avatar')
  41. ->join('user','livebc.user_id = user.id','LEFT')
  42. ->where($map)
  43. ->find();
  44. if(!empty($info)){
  45. $map = [
  46. 'uid' => $this->auth->id,
  47. 'follow_uid' => $info['user_id'],
  48. ];
  49. $is_follow = Db::name('user_follow')->where($map)->find();
  50. $info['is_follow'] = $is_follow ? 1 : 0;
  51. $info = info_domain_image($info,['avatar']);
  52. }
  53. $this->success('success',$info);
  54. }
  55. //直播送礼物
  56. public function givegift() {
  57. // 接口防并发
  58. if (!$this->apiLimit(1, 1000)) {
  59. $this->error(__('Operation frequently'));
  60. }
  61. $user_id = input('user_id');// 赠送对象
  62. $gift_id = input('gift_id');// 礼物ID
  63. $number = input('number',1,'intval');//数量
  64. if (!$user_id || !$gift_id || $number < 1)
  65. {
  66. $this->error();
  67. }
  68. // 不可以赠送给自己
  69. if($this->auth->id == $user_id)
  70. {
  71. $this->error("不可以赠送给自己");
  72. }
  73. // 获取礼物信息
  74. $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
  75. if (!$giftinfo)
  76. {
  77. $this->error("请选择礼物");
  78. }
  79. $giftvalue = bcmul($giftinfo['value'],$number);
  80. //被赠送人信息
  81. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  82. if (!$touserinfo)
  83. {
  84. $this->error("不存在的用户");
  85. }
  86. // 判断当前用户余额
  87. $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
  88. if($user_gold < $giftvalue)
  89. {
  90. $this->error("您的金币余额不足");
  91. }
  92. $partyid = Db::name('party')->where(['user_id'=>$user_id,'room_type'=>2])->value('id');
  93. Db::startTrans();
  94. // 添加礼物赠送记录表
  95. $data = [
  96. 'user_id' => $this->auth->id,
  97. 'user_to_id' => $user_id,
  98. 'gift_id' => $giftinfo['id'],
  99. 'gift_name' => $giftinfo['name'],
  100. 'number' => $number,
  101. 'price' => $giftvalue,
  102. 'createtime' => time(),
  103. ];
  104. $log_id = Db::name('gift_user_livebc')->insertGetId($data);
  105. if(!$log_id){
  106. Db::rollback();
  107. $this->error('赠送失败');
  108. }
  109. if($giftvalue > 0){
  110. // 扣除当前用户余额
  111. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$giftvalue,55,'赠送礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
  112. if($wallet_rs['status'] === false){
  113. Db::rollback();
  114. $this->error($wallet_rs['msg']);
  115. }
  116. // 添加赠送用户声币余额
  117. $money_to_gold = config('site.money_to_gold');
  118. $gift_plat_scale = config('site.gift_plat_scale');
  119. $giftmoney = bcdiv($giftvalue,$money_to_gold,2);
  120. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  121. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,'money',$money,56,'获得礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
  122. if($wallet_rs['status'] === false){
  123. Db::rollback();
  124. $this->error($wallet_rs['msg']);
  125. }
  126. //排行榜
  127. $redis = new Redis();
  128. $redisconfig = config("redis");
  129. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  130. // 添加redis记录做财富排行榜
  131. $redis->zIncrBy("livebc_jewel_get_" . $partyid, $giftvalue, $user_id);
  132. // 添加redis记录做贡献排行榜
  133. $redis->zIncrBy("livebc_jewel_to_" . $partyid, $giftvalue, $this->auth->id);
  134. }
  135. // tcp 获取房间用户周前三名
  136. $partyUserTop = $this->getPartyUserTop($partyid);
  137. $returnData["partyUserTop"] = $partyUserTop;
  138. $returnData["image"] = one_domain_image($giftinfo["image"]);
  139. $returnData["gif_image"] = one_domain_image($giftinfo["special"]);
  140. Db::commit();
  141. $this->success("赠送成功!",$returnData);
  142. }
  143. /**
  144. * 用户赠送礼物后房间内用户排行,贡献榜前三名
  145. */
  146. private function getPartyUserTop($party_id) {
  147. $redis = new Redis();
  148. $redisconfig = config("redis");
  149. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  150. $userModel = new \app\common\model\User();
  151. // 获取条数
  152. $num = 3;
  153. // 获取3条财富排行周记录
  154. $getweek = $redis->zRevRange("livebc_jewel_to_".$party_id,0,$num-1,true);
  155. $userList = $userModel->rankList($getweek);
  156. $avatarArr = [];
  157. if($userList) {
  158. foreach($userList as $k => $v) {
  159. $v["jewel"] > 0 && $avatarArr[] = $v["avatar"];
  160. }
  161. // 加入缓存做备份
  162. $redis->hSet("user_jewel_top3",$party_id,json_encode($avatarArr));
  163. }
  164. return $avatarArr;
  165. }
  166. //准备返回数据,刷礼物的总值,刷礼物人数,榜一信息
  167. public function outlivebc(){
  168. $party_id = Db::name('party')->where(['user_id'=>$this->auth->id,'room_type'=>2])->value('id');
  169. $redis = new Redis();
  170. $redisconfig = config("redis");
  171. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  172. // 获取财富排行 全部
  173. $getweek = $redis->zRevRange("livebc_jewel_to_".$party_id,0,-1,true);
  174. $userinfo = [];
  175. if(!empty($getweek)){
  176. $userid = array_keys($getweek)[0];
  177. $userinfo = Db::name('user')->field('id,username,nickname,avatar,gender')->where('id',$userid)->find();
  178. $userinfo = info_domain_image($userinfo,['avatar']);
  179. }
  180. $rs = [
  181. 'gift_value' => array_sum($getweek),
  182. 'gift_user' => count($getweek),
  183. 'top_userinfo' => $userinfo,
  184. ];
  185. $this->success('success',$rs);
  186. }
  187. }