Livebc.php 7.2 KB

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