Livebc.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 视频直播间
  7. */
  8. class Livebc extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. //直播列表
  13. public function lists(){
  14. $type_id = input('type_id','');
  15. $map = [
  16. 'livebc.is_online' => 1,
  17. ];
  18. if(!empty($type_id)){
  19. $map['livebc.type_id'] = $type_id;
  20. }
  21. $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')
  22. ->join('user','livebc.user_id = user.id','LEFT')
  23. ->where($map)
  24. ->autopage()->select();
  25. $list = list_domain_image($list,['avatar']);
  26. $this->success('success',$list);
  27. }
  28. //直播间详情
  29. public function info(){
  30. $id = input('id',0);
  31. if(empty($id)){
  32. $this->error();
  33. }
  34. $map = [
  35. 'livebc.id' => $id,
  36. ];
  37. $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')
  38. ->join('user','livebc.user_id = user.id','LEFT')
  39. ->where($map)
  40. ->find();
  41. if(!empty($info)){
  42. $map = [
  43. 'uid' => $this->auth->id,
  44. 'follow_uid' => $info['user_id'],
  45. ];
  46. $is_follow = Db::name('user_follow')->where($map)->find();
  47. $info['is_follow'] = $is_follow ? 1 : 0;
  48. $info = info_domain_image($info,['avatar']);
  49. }
  50. $this->success('success',$info);
  51. }
  52. //开播
  53. public function start(){
  54. $type_id = input('type_id','');
  55. $title = input('title','');
  56. $room_no = input('room_no','');
  57. $cityname = input('cityname','');
  58. if(empty($type_id) || empty($title) || empty($room_no)){
  59. $this->error();
  60. }
  61. $data = [
  62. 'type_id' => $type_id,
  63. 'title' => $title,
  64. 'room_no' => $room_no,
  65. 'cityname' => $cityname,
  66. 'is_online' => 1,
  67. 'updatetime' => time(),
  68. ];
  69. Db::startTrans();
  70. $find = Db::name('livebc')->where('user_id',$this->auth->id)->lock(true)->find();
  71. if($find){
  72. $rs = Db::name('livebc')->where('id',$find['id'])->update($data);
  73. }else{
  74. $data['user_id'] = $this->auth->id;
  75. $data['createtime'] = time();
  76. $rs = Db::name('livebc')->insertGetId($data);
  77. }
  78. if($rs === false){
  79. Db::rollback();
  80. $this->error('开播失败');
  81. }
  82. //修改用户表直播状态
  83. $rs_user = Db::name('user')->where('id',$this->auth->id)->update(['is_livebc' => 1]);
  84. if($rs_user === false){
  85. Db::rollback();
  86. $this->error('开播失败');
  87. }
  88. Db::commit();
  89. $this->success('success');
  90. }
  91. //直播送礼物
  92. public function givefift() {
  93. // 接口防并发
  94. /*if (!$this->apiLimit(1, 1000)) {
  95. $this->error(__('Operation frequently'));
  96. }*/
  97. $user_id = input('user_id');// 赠送对象
  98. $gift_id = input('gift_id');// 礼物ID
  99. if (!$user_id || !$gift_id )
  100. {
  101. $this->error();
  102. }
  103. // 不可以赠送给自己
  104. if($this->auth->id == $user_id)
  105. {
  106. $this->error("不可以赠送给自己");
  107. }
  108. // 获取礼物信息
  109. $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
  110. if (!$giftinfo)
  111. {
  112. $this->error("请选择礼物");
  113. }
  114. //被赠送人信息
  115. $touserinfo = Db::name('user')->where('id',$user_id)->find();
  116. if (!$touserinfo)
  117. {
  118. $this->error("不存在的用户");
  119. }
  120. // 判断当前用户余额
  121. $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
  122. if($user_gold < $giftinfo['value'])
  123. {
  124. $this->error("您的金币余额不足");
  125. }
  126. Db::startTrans();
  127. // 添加礼物赠送记录表
  128. $data = [
  129. 'user_id' => $this->auth->id,
  130. 'user_to_id' => $user_id,
  131. 'gift_id' => $giftinfo['id'],
  132. 'gift_name' => $giftinfo['name'],
  133. 'number' => 1,
  134. 'price' => $giftinfo['value'],
  135. 'createtime' => time(),
  136. ];
  137. $log_id = Db::name('gift_user_livebc')->insertGetId($data);
  138. if(!$log_id){
  139. Db::rollback();
  140. $this->error('赠送失败');
  141. }
  142. if($giftinfo['value'] > 0){
  143. // 扣除当前用户余额
  144. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$giftinfo['value'],55,'赠送礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
  145. if($wallet_rs['status'] === false){
  146. Db::rollback();
  147. $this->error($wallet_rs['msg']);
  148. }
  149. // 添加赠送用户声币余额
  150. $money_to_gold = config('site.money_to_gold');
  151. $gift_plat_scale = config('site.gift_plat_scale');
  152. $giftmoney = bcdiv($giftinfo['value'],$money_to_gold,2);
  153. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  154. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,'money',$money,56,'获得礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
  155. if($wallet_rs['status'] === false){
  156. Db::rollback();
  157. $this->error($wallet_rs['msg']);
  158. }
  159. }
  160. Db::commit();
  161. $this->success('赠送成功');
  162. }
  163. }