Livebc.php 7.2 KB

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