Livebc.php 7.3 KB

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