123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- /**
- * 视频直播间
- */
- class Livebc extends Api
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = ['*'];
- //直播列表
- public function lists(){
- $type_id = input('type_id','');
- $map = [
- 'livebc.is_online' => 1,
- ];
- if(!empty($type_id)){
- $map['livebc.type_id'] = $type_id;
- }
- $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')
- ->join('user','livebc.user_id = user.id','LEFT')
- ->where($map)
- ->autopage()->select();
- $list = list_domain_image($list,['avatar']);
- $this->success('success',$list);
- }
- //直播间详情
- public function info(){
- $id = input('id',0);
- if(empty($id)){
- $this->error();
- }
- $map = [
- 'livebc.id' => $id,
- ];
- $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')
- ->join('user','livebc.user_id = user.id','LEFT')
- ->where($map)
- ->find();
- if(!empty($info)){
- $map = [
- 'uid' => $this->auth->id,
- 'follow_uid' => $info['user_id'],
- ];
- $is_follow = Db::name('user_follow')->where($map)->find();
- $info['is_follow'] = $is_follow ? 1 : 0;
- $info = info_domain_image($info,['avatar']);
- }
- $this->success('success',$info);
- }
- //开播
- public function start(){
- $type_id = input('type_id','');
- $title = input('title','');
- $room_no = input('room_no','');
- $cityname = input('cityname','');
- if(empty($type_id) || empty($title) || empty($room_no)){
- $this->error();
- }
- $data = [
- 'type_id' => $type_id,
- 'title' => $title,
- 'room_no' => $room_no,
- 'cityname' => $cityname,
- 'is_online' => 1,
- 'updatetime' => time(),
- ];
- Db::startTrans();
- $find = Db::name('livebc')->where('user_id',$this->auth->id)->lock(true)->find();
- if($find){
- $rs = Db::name('livebc')->where('id',$find['id'])->update($data);
- }else{
- $data['user_id'] = $this->auth->id;
- $data['createtime'] = time();
- $rs = Db::name('livebc')->insertGetId($data);
- }
- if($rs === false){
- Db::rollback();
- $this->error('开播失败');
- }
- //修改用户表直播状态
- $rs_user = Db::name('user')->where('id',$this->auth->id)->update(['is_livebc' => 1]);
- if($rs_user === false){
- Db::rollback();
- $this->error('开播失败');
- }
- Db::commit();
- $this->success('success');
- }
- //直播送礼物
- public function givegift() {
- // 接口防并发
- /*if (!$this->apiLimit(1, 1000)) {
- $this->error(__('Operation frequently'));
- }*/
- $user_id = input('user_id');// 赠送对象
- $gift_id = input('gift_id');// 礼物ID
- if (!$user_id || !$gift_id )
- {
- $this->error();
- }
- // 不可以赠送给自己
- if($this->auth->id == $user_id)
- {
- $this->error("不可以赠送给自己");
- }
- // 获取礼物信息
- $giftinfo = Db::name('gift')->where('id',$gift_id)->find();
- if (!$giftinfo)
- {
- $this->error("请选择礼物");
- }
- //被赠送人信息
- $touserinfo = Db::name('user')->where('id',$user_id)->find();
- if (!$touserinfo)
- {
- $this->error("不存在的用户");
- }
- // 判断当前用户余额
- $user_gold = model('wallet')->getWallet($this->auth->id,'gold');
- if($user_gold < $giftinfo['value'])
- {
- $this->error("您的金币余额不足");
- }
- Db::startTrans();
- // 添加礼物赠送记录表
- $data = [
- 'user_id' => $this->auth->id,
- 'user_to_id' => $user_id,
- 'gift_id' => $giftinfo['id'],
- 'gift_name' => $giftinfo['name'],
- 'number' => 1,
- 'price' => $giftinfo['value'],
- 'createtime' => time(),
- ];
- $log_id = Db::name('gift_user_livebc')->insertGetId($data);
- if(!$log_id){
- Db::rollback();
- $this->error('赠送失败');
- }
- if($giftinfo['value'] > 0){
- // 扣除当前用户余额
- $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$giftinfo['value'],55,'赠送礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
- if($wallet_rs['status'] === false){
- Db::rollback();
- $this->error($wallet_rs['msg']);
- }
- // 添加赠送用户声币余额
- $money_to_gold = config('site.money_to_gold');
- $gift_plat_scale = config('site.gift_plat_scale');
- $giftmoney = bcdiv($giftinfo['value'],$money_to_gold,2);
- $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
- $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,'money',$money,56,'获得礼物:'.$giftinfo["name"],'gift_user_typing',$log_id);
- if($wallet_rs['status'] === false){
- Db::rollback();
- $this->error($wallet_rs['msg']);
- }
- }
- Db::commit();
- $this->success('赠送成功');
- }
- }
|