<?php
/** App登录控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <xiaoq@570035208.com>
 */

namespace Wxsmall\Controller;
class LiveController extends BaseController {
	public function _initialize(){
        parent::_initialize ();
       
    }

    /**
     * wxsmall 端及时通信接口
     */
    public function ajaxrun(){
        $return = array('status'=>0,'msg'=>'数据获取失败');
        $user_info=$this->checkuserauth();
        if(empty($user_info)){
            $return['status'] = 3;
            $return['msg'] = '请先登录';
            $this->ajaxReturn($return);exit();
        }
        $param = $result = I('param.');
        if(empty($param['type']))$this->ajaxReturn($return);
        $result = D('Chat')->index($param,$user_info);
        if($result['status']!=1)$result['status'] = 3;
        $this->ajaxReturn($result);
    }
	//获取主播信息
    public function live_anchor_info(){
        $return = array('status'=>1);
        $getdata = I('get.');
        $user_info=$this->checkuserauth();
        $uid=$user_info['id'];
        if(empty($uid)){
            $return['status'] = 3;
            $return['msg'] = '缺少必要参数';    
            $this->ajaxReturn($return);
        }
        $return['data']['user_info'] = $user_info;
        $LiveAnchor=D("LiveAnchor")->get_userby_live($uid);


        // $LiveAnchor=D("LiveAnchor")->get_userby_live(11);
        
        $is_off = C('SHOW_LIVE_MODEL');
		$return['data']['is_shenh']=C('LIVE_APPLY_OFF');
        if($LiveAnchor!=false && $is_off==1){
            $return['data']['live_anchor']=$LiveAnchor;
        }else{
            $return['data']['live_anchor']='';
        }

        $this->ajaxReturn($return);
    }
    //主播个人信息
    public function liver_info(){
        $return=array('status'=>1,'msg'=>'');
        $memberinfo = $this->checkuserauth();//获取网站会员信息
        if(empty($memberinfo)){
            $return['status']=3;
            $return['msg']='您没登陆，请登录';
            $this->ajaxReturn($return);
        }
        $map = array();
		$map['user_id']=$memberinfo['id'];
		// $map['user_id']=11;
        $map['is_delete']=0;
        $live_info=M("LiveAnchor")->where($map)->field(true)->find();
        if(empty($live_info)){
			$return['status']=3;
            $live_info['status'] = 0;
        }else{
            $live_info['index_img']=thumb($live_info['index_img']);
            $live_info['front_card']=thumb($live_info['front_card']);
            $live_info['negative_card']=thumb($live_info['negative_card']);
            $live_info['handheld_card']=thumb($live_info['handheld_card']);
            $live_info['province']=$live_info['province'];
            $live_info['city_id']=$live_info['city_id'];
            $live_info['district']=$live_info['district'];
			
            $regions['province']=idtoname($live_info['province'],"Area",'name');
            $regions['city_id']=idtoname($live_info['city_id'],"Area",'name');
            $regions['district']=idtoname($live_info['district'],"Area",'name');
            $live_info['region']=$regions['province'].$regions['city_id'].$regions['district'];
            $live_info['cate_name'] = idtoname($live_info['cate_id'],'LiveCategory','name','id',false);
			$live_info['shop_name'] = idtoname($live_info['shop_id'],'Shops','name','id',false);
        }
        //获取分类
        $cate_info=D("LiveAnchor")->getcate();
        // if($cate_info){
            // foreach ($cate_info as $v) {
                // $cate_info[0]='请选择分类';
                // $cate_info[$v['id']] = $v['name'];
            // }
        // }
        $return['data']['live_info']=$live_info;
        $return['data']['cate_info']=$cate_info;
        //1126新加
        // $go_offline_time = idtoname('go_offline_time','LiveSet','value','name');

        $go_offline_time = getconfig('GO_OFFLINE_TIME');
        $return['data']['go_offline_time'] = $go_offline_time;
        $return['data']['bg_img'] = C('WXSMALL_LIVE_BG');
        //end
        $this->ajaxReturn($return);
    }
	public function get_shops_list(){
		$return=array('status'=>3,'msg'=>'');
		$map = array();$map['status'] = 1;
		$shops_list = M('Shops')->where($map)->field('id,name')->select();
		if($shops_list){
			$return['status'] = 1;
			$return['data']['shops_list']=$shops_list;
		}
        $this->ajaxReturn($return);
	}
	public function sub_anchor_info(){
		$return=array('status'=>3,'msg'=>'');
		$getdata = I('param.');
        $user_info=$this->checkuserauth();
        $uid=$user_info['id'];
        // $uid=11;
        if(empty($uid)){
            $return['status'] = 3;
            $return['msg'] = '缺少必要参数';    
            $this->ajaxReturn($return);
        }
		if(!$getdata['id'] && !$getdata['user_id']){
			$getdata['user_id'] = $uid;
			$getdata['status'] = 0;
			$getdata['addtime'] = NOW_TIME;
		}
		$getdata['updatetime'] = NOW_TIME;
        $return = D("LiveAnchor")->sub_anchor_info($getdata);
        $this->ajaxReturn($return);
	}
    /**
     * 获取分类或者店铺信息
     */
    public function get_param_info(){
        $return=array('status'=>1);
        $param=I('param.');
        $return['param'] = $param;
        // $this->ajaxReturn($return);
        $keyword=I('search_key');
        $type=I("cate_key");
        $info=D("LiveAnchor")->get_param_info($keyword,$type);
        if($info['status']==1){
            $return['data']=$info['data'];
        }else{
            $return['msg']=$info['msg'];
            $return['status']=3;
        }
        $this->ajaxReturn($return);
    }

    /**
     * 模糊查询商品
     */
    public function get_search_goods(){
        $return = array('status'=>3,'msg'=>'未搜索到相关商品');
        $param = I('param.');

        if(empty($param['search_key']))$this->ajaxReturn($return);

        $user_info=$this->checkuserauth();
        $param['uid']=$user_info['id'];
        $param['shop_id']=$user_info['HCshop']['bind_shop_id'];
        $result = D('LiveAnchor')->getgoods(0,$param,1,0,$user_info,$param['room_id'],$param);
        if($result['status']!=1){
            $result = $return;
        }
        $this->ajaxReturn($result);
    }

     /**
     * 获取主播推荐的商品
     */
    public function get_live_goods(){
        $param=I('param.');
        $return=array('status'=>1,'msg'=>'');  
        $memberinfo = $this->checkuserauth();//获取网站会员信息

        // $param['map'] = 1;
        //获取购买的商品
        $live_info=D("Chat")->runHistory('query','',$param['room_id'],0,$param,$memberinfo);
        if($live_info) {
            $goods_info = unserialize($live_info['goods_info']);
            $goods_info = array_filter($goods_info);
            $goods_count = count($goods_info);
            foreach ($goods_info as $k => $v) {
                $edition[] = $v['sort'];
                $goods_info[$k]['goods_id']=$v['id'];
                $goods_info[$k]['img']=thumb($v['img'],200,200);
                $goods_info[$k]['shop_price']=format_price($v['shop_price']);
                $data=array();
                $data = D("Common/GoodsAttr")->get_shop_attr($v['id']);
                $goods_info[$k]['goods_attrs']=$data['newarr'];
            }
            array_multisort($edition, SORT_DESC, $goods_info);           
            
            if($goods_info){
                $return['data']['goods_info']=$goods_info;
            }
            
            $return['data']['goods_count']=$goods_count?$goods_count:0;
        }else{
            $return['data']['goods_info']='';
            $return['data']['goods_count']=0;
        }
        $return['data']['live_history_id']=$live_info['id'];//直播记录id
        $return['data']['live_anchor_id']=idtoname($live_info['roomnumber'],'LiveAnchor','id','roomnumber',false);//主播id
        $return['data']['user_info'] = $memberinfo;
        $this->ajaxReturn($return);
    }

    //会员查看直播列表页
    public function get_live_list(){
        $is_off = C('SHOW_LIVE_MODEL');
        if($is_off!=1){
            $return['status'] = 3;
            $return['msg'] = '系统未开启直播';
            $this->ajaxReturn($return);exit();
        }
        $map=array();
        // $memberinfo = $this->checkuserauth();//获取网站会员信息
        $return=array('status'=>2,'msg'=>'已加载全部数据');
        $param=I('param.');
        $cate_id=$param['cate_id'];
        if($cate_id && $cate_id !='undefined') $map['cate_id']=$cate_id;   

        $map['status'] = 1;
        //112新加
        $map['is_live'] = 1;
        //end
        $order = 'is_live desc,id desc';

        $livelist = page_list('LiveAnchor', $map, $order);
        // $online_people_rate = idtoname('online_people_rate','LiveSet','value','name');  //在线观看人数基数

        $online_people_rate = getconfig('ONLINE_PEOPLE_RATE');
        if($livelist['list']){
            foreach($livelist['list'] as $key=>$val){
                $l_arr=$u_map=array();
                $l_arr['roomnumber'] = $val['roomnumber'];
                $l_arr['is_effect'] = 1;
                $live_history = M('LiveHistory')->where($l_arr)->field('id,liveonlinecount,liveimg,livebrief,praise_number')->order('id desc')->select();
                $livelist['list'][$key]['cate_name']=idtoname($val['cate_id'],'LiveCategory','name','id',false);

                $livelist['list'][$key]['index_img'] =thumb(format_url($val['index_img']),355,355); 
                if($live_history){
                    $hlive_arr = $live_history[0];
                    $livelist['list'][$key]['number']=intval($online_people_rate + $hlive_arr['liveonlinecount']);
                    $livelist['list'][$key]['liveimg'] =thumb(format_url($hlive_arr['liveimg']),355,355); 
                    $livelist['list'][$key]['livebrief'] =$hlive_arr['livebrief'];
                    $livelist['list'][$key]['praise_number'] = $hlive_arr['praise_number'];

                       
                }
                //图片
                if($livelist['list'][$key]['liveimg']) $livelist['list'][$key]['live_img'] =$livelist['list'][$key]['liveimg'];
                else $livelist['list'][$key]['live_img'] = $livelist['list'][$key]['index_img']; 
                //标题
                if($livelist['list'][$key]['livebrief']) $livelist['list'][$key]['live_title'] =$livelist['list'][$key]['livebrief'];
                else $livelist['list'][$key]['live_title'] = $livelist['list'][$key]['brief']; 
            
                
                
                           
                $u_map['id']=$val['user_id'];
                $member_author=M("Member")->where($u_map)->field('nickname,username,mobile,header_img')->find();
                if($member_author){
                    $livelist['list'][$key]['name']=D('Chat')->getnickname($member_author);
                    if(empty($livelist['list'][$key]['name'])) $livelist['list'][$key]['name'] = trim(C('WEB_LIVES_NAME'));

                    if($member_author['header_img']) $livelist['list'][$key]['header_img']=$member_author['header_img'];
                    else $livelist['list'][$key]['header_img']='';
                }
                if($val['city_id']) $livelist['list'][$key]['city']=idtoname(intval($val['city_id']),"area");
                else $livelist['list'][$key]['city'] = '石家庄';

                if($val['is_live']!=1) $livelist['list'][$key]['live_type'] = 1;
                $return['status'] = 1;
            
            }
        }

       

        //end
        

       //  if($param['list_hub'] && $param['list_hub'] !='undefined'){
       //      $list_hub = json_decode(htmlspecialchars_decode($param['list_hub']),true);
       //      $hub_arr=array();
       //      foreach($list_hub as $kk=>$vv){
       //          $hub_arr[] = $vv['key'];
       //      }
       //      $map['lh.roomnumber'] = array('IN',$hub_arr);
            
       // }else{
       //      $map['live_type'] =1;
       // }
       // $list=D("LiveAnchor")->get_live_list(0,$map);
        // $anchor_list=D("LiveAnchor")->get_live_list(0,$map);
       //1219获取回放
        // $live_type = 1; 
        // $list = D("LiveAnchor")->get_live_list(0,$map,'', '', 1,0,$live_type);
        // $anchor_list = $list;
            
        // $return['anchor_list'] = $anchor_list;
        $return['data']['cate_id'] = $cate_id;
        $return['data']['list'] = $livelist;
        $return['data']['live_list'] = $livelist['list'];
        // if($anchor_list['list']){
        //     // $return['data']['anchor_list'] = $anchor_list;
        //     $return['data']['anchor_list'] = $list;
        // }else $return['data']['anchor_list'] = '';
        //end
        $this->ajaxReturn($return);
    }

    //获取直播分类
    public function get_live_cate(){
        $is_off = C('SHOW_LIVE_MODEL');
        if($is_off!=1){
            $return['status'] = 3;
            $return['msg'] = '系统未开启直播';
            $this->ajaxReturn($return);exit();
        }
        $return=array('status'=>1,'msg'=>'');
        $param=I('param.');

        $cate=D("LiveAnchor")->getcate();
        $return['data']['cate'] = $cate;
        //1117新加
        // $return['data']['web_name'] = C('WEB_SITE_TITLE')?C('WEB_SITE_TITLE'):'汉潮直播';
        $this->ajaxReturn($return);

    }
    //end
    /**
     * 点击查看直播
     * 直播流地址
     * type 1推流（开播）2拉流（观看）3结束直播
     */
    public function liveindex(){
        $return=array('status'=>1,'msg'=>'');
        $param = I('param.');
        $memberinfo = $this->checkuserauth();//获取网站会员信息
        if(empty($param['type'])||empty($param['room_id'])){
            $return['msg'] = '缺少必要参数';
            $this->ajaxReturn($return);
        }
        $room_id = $param['room_id'];
        $live_type = $param['type'];
        if($param['type']==1){
            $checkinfo = D('Chat')->checkAnchor($memberinfo);//判断当前登录会员是否为当前房间主播
            if($checkinfo['status']!=1){
                $return['msg'] = $checkinfo['msg'];
                $this->ajaxReturn($return);
            }
            //主播开播
            $param['type'] = 'start_play';
            $result = D('Chat')->index($param);
            if($result['status']!=1){
                $return['msg'] = $result['msg'];
                $this->ajaxReturn($return);
            }  
        }
        if($param['type']==3){
            //关闭直播
            $param['type'] = 'stop_play';
            $result = D('Chat')->index($param);
            $this->ajaxReturn($result);
        }
        $anchorinfo = idtoname($param['room_id'],'LiveAnchor','default_video_source,is_play_default','roomnumber');

        $return['anchorinfo']=$anchorinfo;
        //1121获取拉流地址
        $live= new \Common\Vendor\QiniuLive;
        $streamKey = $room_id;
        $url=$live->RTMPPlayURL($streamKey);
        if($url){
            $resulturl = $url;
        }else{
            $resulturl = $anchorinfo['default_video_source'];
        }

        //end

        $live_info=D("Chat")->runHistory('query','',$room_id,0,$param,$memberinfo,'');//是否开播
        $is_live = false;
        if($live_info){
            $is_live = true;
        }
        if($resulturl){
            $return['status'] = 1;
            $return['data']['url'] = $resulturl;
            $return['data']['is_live'] = $is_live;
        }
        $this->ajaxReturn($return);
    }

    /**
     * 获取主播信息
     */
    public function get_live_info(){
        $return=array('status'=>1,'msg'=>'');
        $param=I('param.');
        $memberinfo = $this->checkuserauth();
        $username =$memberinfo['id']<10?'hc0'.$memberinfo['id']:'hc'.$memberinfo['id'];
        $memberinfo['nickname']=D("Chat")->getnickname($memberinfo);
        $roomnumber=$param['room_id'];
        $map=$u_map=array();
        $map['roomnumber']=$roomnumber;
        $author=M("LiveAnchor")->where($map)->field('brief,user_id,content,city_id,group_id')->find();
        if(empty($author['content'])) $author['content']='暂无简介';        
        if($author['city_id']) $author['city']=idtoname($author['city_id'],"area");
        
        $u_map['id']=$author['user_id'];
        $member=M("Member")->where($u_map)->field('nickname,username,mobile,header_img')->find();

        if($member){
            $author['name']=D('Chat')->getnickname($member);
            // if($member_author['header_img']) $member['header_img'] = $member_author['header_img'];
            // else $member['header_img'] = '';
            $author['header_img']=thumb($member['header_img'],50,50);
        }
        //获取赞数
        $live_info=D("Chat")->runHistory('query','',$roomnumber,0,$param,$memberinfo,'');
        if(empty($live_info['group_id'])) $live_info['group_id'] = $author['group_id'];

        $author['live_info'] = $live_info;
        if($live_info){
            $goods_info=unserialize($live_info['goods_info']);
            $goods_info=array_filter($goods_info);
            $goods_count=count($goods_info);
            $praise_number=$live_info['praise_number'];
        }else{
            $praise_number=0;
        }
        $author['praise_number']=$praise_number;
        $author['goods_count']=$goods_count?$goods_count:0;
        //判断该会员是否关注主播
        $user_id=$memberinfo['id']?$memberinfo['id']:0;
        $map=$f_map=array();
        $map['roomnumber']=$roomnumber;
        $author_id=M("LiveAnchor")->where($map)->getField('id');
        $f_map['user_id']=$user_id;
        $f_map['anchor_id']=$author_id;
        $f_map['status']=1;
        $count=M("LiveFollow")->where($f_map)->count();
        $user_map=array();
        $user_map['user_id']=$user_id;
        $user_room=M("LiveAnchor")->where($user_map)->getField('roomnumber');
        if(empty($user_room)){
            $user_room=0;
        }
        $author['count']=$count?$count:0;
        $author['user_room']=$user_room;
        $author['share_url']=geturl('Live/Mobilechat/index/',array('roomnumber'=>$roomnumber,'eid'=>$user_id),true,true);
        $author['live_name'] = C('WEB_LIVES_NAME')?C('WEB_LIVES_NAME'):'汉潮直播';
        //1110
        // $author['sdkAppID'] = $author['appIDAt3rd'] = C('WEBIM_SDKAPPID');
        // $author['accountType'] = C('WEBIM_ACCOUNT_TYPE');

        // $author['web_name'] = C('WEB_SITE_TITLE')?C('WEB_SITE_TITLE'):'汉潮直播';
        $author['web_name'] = $author['name'].'的直播间';

        $author['dz_time'] = idtoname('praise_limit_time','LiveSet','value','name');
        //1204新加
        $words = C('SENSITIVE_WORDS');
        if($words) $arr_wo = explode(',',$words);
        $author['words'] = $arr_wo;
        $author['user_info'] = $memberinfo;
        //end
        if($author){
            $return['status']=1;
            $return['data']=$author;
        }else{
            $return['status']=2;
            $return['msg']='';
        }
		
        //获取微信商城小程序appid        
        $wx_shop_appid=D('LiveAnchor')->get_small_appid('SHOP');
        $return['wx_shop_appid']=$wx_shop_appid;

       //325新加
        $return['data']['username'] = $username;
        $password='hanhcao';
        $return['data']['password'] = $password;
        $im= new \Common\Vendor\JimIM;
        $signature=$im->signature();//获取客户端调用参数
        $return['data']['signature'] = $signature;
        if($memberinfo['id']<10) $usernameh ='hc0'.$memberinfo['id'];
        else $usernameh ='hc'.$memberinfo['id'];

        $is_show = $im->show($usernameh);
        $return['data']['show'] = $is_show;
        if(!$is_show['username']){
            $password='hanhcao';
            $reg=$im->register($usernameh,$password,$memberinfo['nickname']);
            $return['data']['reg'] = $reg;
        }
        
        $members[] = $usernameh;
        if($live_info['group_id']){
            $room_ret=$im->addMembersRoom($live_info['group_id'],$members);
            $return['data']['room_ret'] = $room_ret;
        }
        
      //end
		
        
        $this->ajaxReturn($return);
    }

    /**
     * app端获取直播配置信息接口
     */
    public function config(){
        $return = array('status'=>1);
        $memberinfo = $this->checkuserauth();//获取网站会员信息
        // $return['data']['emoticon'] = D('Chat')->emoticon();//输出表情
        // $return['data']['gift'] = D('Chat')->gift();//输出礼物
        // $return['data']['praise_limit_time'] = idtoname('praise_limit_time','LiveSet','value','name');//输出点赞点击间隔
        $this->ajaxReturn($return);
    }

     /**
     * 主播个人主页
     */
    public function anchor_info(){
        $return=array('status'=>1,'msg'=>'');
        $memberinfo = $this->checkuserauth();
        // $online_people_rate = idtoname('online_people_rate','LiveSet','value','name');

        $online_people_rate = getconfig('ONLINE_PEOPLE_RATE');
        $param=I('param.');
        $roomnumber=$param['room_id'];
        $user_id=$param['uid'];
        if(empty($roomnumber)){
            $return['status']=3;
            $return['msg']='参数错误';
            $this->ajaxReturn($return);
        }
        $user_map=$map=array();
        $user_map['user_id']=$user_id;
        $user_room=M("LiveAnchor")->where($user_map)->getField('roomnumber');
        if(empty($user_room)){
            $user_room=0;
        }
        $anchor_info['user_room']=$user_room;
        $map['roomnumber']=$roomnumber;
        $author_id=M("LiveAnchor")->where($map)->getField('id');
        $anchor_info=D("LiveAnchor")->anchor_info($author_id,$roomnumber,$memberinfo);        
        if(empty($anchor_info)){
            $return['status']=2;
            $return['msg']='没有数据';
        }else{
            $anchor_info['header_img']=thumb($anchor_info['header_img'],100,100);
            $anchor_info['now_live_number']=$anchor_info['now_live']['number']?$anchor_info['now_live']['number']:0;
            
            // $anchor_info['now_live_number'] = intval($anchor_info['now_live_number']+$online_people_rate);
            //1106
            if ($anchor_info['collect_count'] > 0) {
                $anchor_info['gz_type'] = 1;
                $anchor_info['is_gz']= '已关注';
            }else{
                $anchor_info['gz_type'] =0;
                $anchor_info['is_gz']= '关注';
            }
            //end
            $return['data']['info']=$anchor_info;
        }
        //获取回放
        $history_info=D("LiveHistory")->getrecordlist($roomnumber);
        if($history_info['data']){
            foreach($history_info['data'] as $key=>$val){
                $history_info['data'][$key]['liveimg']=thumb($val['liveimg'],432,432,'noimg_432x432.png');
                $history_info['data'][$key]['liveonlinecount'] = intval($val['liveonlinecount']+$online_people_rate);
            }
            $return['data']['history_info']=$history_info['data'];
            $return['data']['recordcount']=$history_info['recordcount'];
        }else{
            $return['status'] = $history_info['status'];
            $return['msg'] = $history_info['msg'];
            $return['data']['history_info']='';
            $return['data']['recordcount']=0;

        }
        $this->ajaxReturn($return);
    }

    //关注/取消关注主播
    public function collect_live(){
        $return=array('status'=>1,'msg'=>'');
        $memberinfo = $this->checkuserauth();
        if(empty($memberinfo)){
            $return['status'] = 3;
            $return['msg'] = '请先登录';
            $this->ajaxReturn($return);
        }
        $param=I('param.');
        $id=$param['room_id'];
        $info=D("LiveAnchor")->follow_live($id,$memberinfo);
        if($info['status']>=1){
            $return['status']=1;
            $return['data']=$info['data'];
            $return['msg']=$info['msg'];
        }else{
            $return['status']=3;
            $return['msg']=$info['msg'];
        }
        $this->ajaxReturn($return);

    }
    //个人中心我的关注
    public function myfollow_list(){
        $return=array('status'=>1,'msg'=>'');
        $memberinfo = $this->checkuserauth();
		// $memberinfo = array('id'=>1,'status'=>1);
        $collectlive = D('LiveAnchor')->get_collect_livenew($memberinfo);
        if($collectlive['list']){
            foreach($collectlive['list'] as $key=>$val){
                $collectlive['list'][$key]['header_img']=format_url($val['header_img']);
                $collectlive['list'][$key]['is_active']=1;
            }
            $return['data']=$collectlive['list'];
            $return['num']=count($collectlive['list']);
        }else{
            $return['status']=2;
            $return['msg']='没有更多数据';
        }
		$wx_zb_appid=D('LiveAnchor')->get_small_appid('LIVE');
        $return['wx_zb_appid']=$wx_zb_appid;
        $this->ajaxReturn($return);
    }
    //移除我的关注
    public function delete_collect(){
        $memberinfo = $this->checkuserauth();
        if(empty($memberinfo)){
            $return['msg']='您没登陆，请登录！';
            $return['status']=3;
            $this->ajaxReturn($return);
        }
        $return=array('status'=>1,'msg'=>'');
        $param = I('param.');
        $id=$param['id'];
        $data = D('LiveAnchor')->delete_collect($id,$memberinfo);
        if($data['status']==0){
            $return['status']=3;
            $return['msg']=$data['msg'];
        }else{
            $return['msg']=$data['msg'];
        }
        $this->ajaxReturn($return);
    }
	//个人中心我的粉丝
    public function fans_list(){
        $return=array('status'=>1,'msg'=>'');
        $memberinfo = $this->checkuserauth();
        $collectlive = D('LiveAnchor')->get_fans_list($memberinfo);
        if($collectlive['list']){
            foreach($collectlive['list'] as $key=>$val){
                $collectlive['list'][$key]['header_img']=format_url($val['header_img']);
                $collectlive['list'][$key]['is_active']=1;
            }
            $return['data']=$collectlive['list'];
            $return['num']=count($collectlive['list']);
        }else{
            $return['status']=2;
            $return['msg']='没有更多数据';
        }
        $this->ajaxReturn($return);
    }
    /**
     * 播放历史
     */
    public function live_history(){
        $return=array('status'=>1,'msg'=>'');
        $memberinfo = $this->checkuserauth();//获取网站会员信息
        if(empty($memberinfo)){
            $return['msg']='您没登陆，请登录！';
            $return['status']=3;
            $this->ajaxReturn($return);
        }
        $map = array();$map['user_id']=$memberinfo['id'];
        $live_info=M("LiveAnchor")->where($map)->field(true)->find();
        $collectlive = D('LiveAnchor')->get_live_history($live_info['roomnumber']);
        if($collectlive['list']){
            foreach($collectlive['list'] as $key=>$val){
                if($val['is_effect']>0){
                    $collectlive['list'][$key]['effect']='有效';
                }else{
                    $collectlive['list'][$key]['effect']='无效';
                }
            }
            $return['data']=$collectlive['list'];
        }else{
            $return['msg']='没有更多数据';
            $return['status']=2;
        }
        $this->ajaxReturn($return);
    }
	/**
     * 主播收益
     */
    public function live_divided(){
        $return=array('status'=>1,'msg'=>'');
        $memberinfo = $this->checkuserauth();//获取网站会员信息
        if(empty($memberinfo)){
            $return['msg']='您没登陆，请登录！';
            $return['status']=3;
            $this->ajaxReturn($return);
        }
        $map = array();$map['user_id']=$memberinfo['id'];
        $room=M("LiveAnchor")->where($map)->getField('roomnumber');
        if(empty($room)){
            $return['status']=3;
			$return['msg']='参数错误';
			$this->ajaxReturn($return);
        }
		$collectlive = D('LiveAnchor')->get_live_divided($room);
        if($collectlive['list']){
            $return['data']=$collectlive['list'];
        }else{
            $return['msg']='没有更多数据';
            $return['status']=2;
        }
        $this->ajaxReturn($return);
    }
	/**
     * 主播收益详情页
     */
    public function divided_content(){
		$return=array('status'=>1,'msg'=>'');
        $id=I('id');
        $map=array();
        $map['id']=$id;
        $info=M("LiveDivided")->where($map)->field(true)->find();
        $info['goods_attr']=D("Common/GoodsAttr")->goods_to_attrname($info['goods_attr']);
        if($info){
            $return['data']=$info;
        }else{
            $return['msg']='没有更多数据';
            $return['status']=2;
        }
        $this->ajaxReturn($return);
    }
    //获取商品属性
    public function get_attrs(){
        $return=array('status'=>1,'msg'=>'');
        $memberinfo = $this->checkuserauth();//获取网站会员信息
        $param = I('param.');
        if(empty($param['goods_id'])){
            $return['msg']='参数错误！';
            $return['status']=3;
            $this->ajaxReturn($return);
        }
        $goods_id = $param['goods_id'];
        $param['id'] = $goods_id;
        $param['uid'] = $memberinfo['id'];
        $param['shop_id']=$memberinfo['HCshop']['bind_shop_id'];//追加当前会员匹配的门店
        if(empty($param['shop_id']))$param['shop_id']=idtoname($param['live_anchor_id'],'LiveAnchor','shop_id');
        $result = D('LiveAnchor')->cont($param);



        //获取微信商城小程序appid        
        $wx_shop_appid=D('LiveAnchor')->get_small_appid('SHOP');
        $result['wx_shop_appid']=$wx_shop_appid;
        $this->ajaxReturn($result);
    }
    //加入购物车
    public function add_cart(){
        $param=I('param.');
        $memberinfo=$this->checkuserauth();
        $param['uid']=$memberinfo['id'];
        $param['shop_id']=$memberinfo['HCshop']['bind_shop_id'];;//追加当前会员匹配的门店
        if(empty($param['shop_id']))$param['shop_id']=idtoname($param['live_anchor_id'],'LiveAnchor','shop_id');
        $data=D("LiveAnchor")->add_cart($param);
        if($data['status']==1 && (empty($param['live_type']) || $param['live_type']!='huifang')){
            //328换接口
            $groupid=trim(idtoname($param['live_anchor_id'],'LiveAnchor','group_id','id',false));
            $name = D('Chat')->getnickname($memberinfo);
            $im= new \Common\Vendor\JimIM;
            $admin = getconfig('USERSIG');
            if(!$admin || empty($admin)){
                $info = ['username' => 'admin','password' => 'password'];
                $ret=$im->registerAdmin($info);
                if($ret){
                    $save=$sarr=array();
                    $save['value'] = 'admin';
                    $sarr['name'] = array('like','%USERSIG%');
                    $sd = M('Config')->where($sarr)->save($save);
                }
            }
            // if(empty($ret['body'])){
            $from=$target=$msg=array();
            $from['type'] = 'admin';
            $from['id'] = 'admin';
            $target['type'] = 'chatroom';
            $target['id'] = $groupid;
            $msg['text'] = $name."正在购买";
            $res = $im->sendCustom($from,$target,$msg);
            // }
            //end
        }
        $this->ajaxReturn($data);
    } 
    
    /*
    *获取微信商城小程序appid
    */
    public function get_small_appid(){
        $result=array('status'=>1);
        $param=I('param.');
        $morecode=$param['more_code'];
        if(empty($morecode))$morecode='SHOP';//微信商城小程序标识
        $wx_shop_appid=D('LiveAnchor')->get_small_appid($morecode);
        $result['morecode']=$morecode;
        $result['wx_shop_appid']=$wx_shop_appid;
        $this->ajaxReturn($result);
    }
    //获取拉流地址
    public function get_live_playurl(){
        $return=array('status'=>1,'msg'=>'');
        $memberinfo = $this->checkuserauth();//获取网站会员信息
        $param = I('param.');
        if(empty($memberinfo)){
            $return['msg']='请先登录';
            $return['status']=3;
            $this->ajaxReturn($return);
        }

        //获取拉流地址
        $live= new \Common\Vendor\QiniuLive;
        $streamKey=$param['room_id'];
        $url=$live->RTMPPlayURL($streamKey);
        if($url){
            $return['data'] = $url;
            $return['chat'] =D('LiveAnchor')->getchatinit($memberinfo,$param['room_id']);
        }else{
            $return['status'] = 3;
            $return['msg'] = '播放地址获取错误';
        }
        $this->ajaxReturn($return);

    }
    //开始直播
    public function get_live_pushurl(){
        $return=array('status'=>1,'msg'=>'');
        $memberinfo = $this->checkuserauth();//获取网站会员信息
        $param = I('param.');
        if(empty($memberinfo)){
            $return['msg']='请先登录';
            $return['status']=3;
            $this->ajaxReturn($return);
        }
        $live= new \Common\Vendor\QiniuLive;
        $streamKey=$param['room_id'];
        $url=$live->RTMPPublishURL($streamKey);
        if($url) {
            $return['data'] = $url;            
            $return['chat'] =D('LiveAnchor')->getchatinit($memberinfo,$param['room_id'],1);
        } else{
            $return['status'] = 3;
            $return['msg'] = '开播地址获取错误';
        }
        $this->ajaxReturn($return);
    }
    //获取正在推流的列表
    public function get_live_fetch(){
        $return=array();
        $live= new \Common\Vendor\QiniuLive;
        // $room_number = trim(idtoname('room_number','LiveSet','value','name'));
        $room_number=getconfig('ROOM_NUMBER');
        $result=$live->listStreams(true,$room_number);
        // $result=$live->listStreams();
        if($result['items']){
            $return['status'] = 1;
            $return['data'] = $result;
        }else{
            $return['status'] = 3;
            $return['data'] = '';
        }
        
        $this->ajaxReturn($return);

    }
    //1126新加
    //看这个主播一小时内，是否发起过直播，是，到商品列表，否，填写直播信息
    public function is_livehis(){
        //1.看该会员是否有主播的权限
        $return=array('status'=>1,'msg'=>'');
        $memberinfo = $this->checkuserauth();//获取网站会员信息
        if(empty($memberinfo)){
            $return['status'] = 3;
            $return['msg'] = '登录信息错误';
            $this->ajaxReturn($return);
        }
        $param = I('param.');
        // $return['param'] = $param;
        $room_id = $param['room_id'];
        $checkinfo = D('Chat')->checkAnchor($memberinfo);//检查当前主播权限
        if($checkinfo['status']!=1){
            $return['status'] = 3;
            $return['msg'] = $checkinfo['msg'];
            $this->ajaxReturn($return);
        }
        //2.看这个主播一小时之内有没有发起过直播
        $is_livehis = D('LiveAnchor')->launch_lives($room_id,$memberinfo);  
        $this->ajaxReturn($is_livehis);
        
    }

    //存储本次直播在线观看人数
    public function set_live_num(){
        $return=array('status'=>3,'msg'=>'数据获取错误');
        $memberinfo = $this->checkuserauth();//获取网站会员信息
        $param = I('param.');
        $number_num = $param['member_num'];
        if(empty($memberinfo)) $this->ajaxReturn($return);       
        if(empty($number_num)) $this->ajaxReturn($return);
        $data = array('liveonlinecount'=>$number_num);
        D('Chat')->runHistory('update',$data,$param['room_id'],0,'',$memberinfo);
    }
    //删除直播商品
    public function del_anchor_goods(){
        $return=array('status'=>1,'msg'=>'');
        $memberinfo = $this->checkuserauth();//获取网站会员信息
        if(empty($memberinfo)){
            $return['status'] = 3;
            $return['msg'] = '登录信息错误';
            $this->ajaxReturn($return);
        }
        $param = I('param.');
        $live_info=D("Chat")->runHistory('query','',$param['room_id'],0,$param,$memberinfo);
        if($live_info) {
            $goods_info = unserialize($live_info['goods_info']);
            if($goods_info){
				foreach($goods_info as $k=>$v){
					if($v['id'] == $param['goods_id']){
						unset($goods_info[$k]);
					}
				}				
			}
			if($goods_info){
				$goods_info = serialize($goods_info);
			}else{
				$goods_info = '';
			}
			$data = array();$data['goods_info'] = $goods_info;
			D('Chat')->runHistory('update',$data,$param['room_id'],0,$param,$memberinfo);
			
			$live_info_new=D("Chat")->runHistory('query','',$param['room_id'],0,$param,$memberinfo);
			if($live_info_new['goods_info'] == $goods_info){
				$return['msg'] = '删除成功';
			}else{
				$return['status'] = 3;
				$return['msg'] = '删除失败';
			}
        }else{
            $return['status'] = 3;
			$return['msg'] = '没有要删除的商品';
        }
        $this->ajaxReturn($return);
    }
    //end
	//省市县三级联动
	public function get_link(){
		$result=array('status'=>1);
		$regiondata = D('Member')->linkagearea();
		if($regiondata['status']==1){
			$result['region'] = $regiondata['data']['region'];
			$result['area'] = $regiondata['data']['area'];
		}
		$this->ajaxReturn($result);
	}
    //停止推流保存数据函数
    public function stop_live(){
        $result=array('status'=>3,'msg'=>'数据获取失败');
        $param=I('param.');
        if(empty($param['room_id'])) $this->ajaxReturn($result);
        $memberinfo = $this->checkuserauth();//获取网站会员信息
        if(empty($memberinfo)){
            $return['status'] = 3;
            $return['msg'] = '登录信息错误';
            $this->ajaxReturn($return);
        }
        $where=array();
        $where['stream'] = $param['room_id'];
        $where['stop_time'] = array('eq',0);
        $notify = D('Chat')->get_notify($where,$param['room_id'],$memberinfo);
        $return['data']['res'] = $notify;
        $his_data = array('end_time'=>NOW_TIME);
        D('Chat')->runHistory('update',$his_data,$param['room_id'],0,'',$memberinfo);

        //修改主播表直播状态
        $anarr=$sanarr=array();
        $sanarr['is_live'] = 0;
        $anarr['roomnumber'] = $param['room_id'];
        $is_stop = M('LiveAnchor')->where($anarr)->save($sanarr);
        //end
       
        $this->ajaxReturn($return);
    }
    //实时获取点赞数量
    public function get_live_praise(){
        $param=I('param.');
        $map=array();
        $map['roomnumber'] = $param['room_id'];
        $map['end_time'] = array('eq',0);
        $count = M('LiveHistory')->where($map)->getField('praise_number');
        $return['status'] = 1;
        $return['data'] = $count;  
        $this->ajaxReturn($return);

    }

    //
    public function get_link_url(){
        $result=array('status'=>3,'msg'=>'数据获取失败');
        $param=I('param.');
        if(empty($param['id'])){
            $this->ajaxReturn($return);
            exit();
        }
        $where=$map=$arr=array();
        $where['id'] = $param['id'];
        $link_info = M('LiveHistory')->where($where)->field(true)->find();
        $map['a.id']=$param['id'];
        $map['a.roomnumber'] = $link_info['roomnumber'];
        $live_info = M('LiveHistory')->alias('a')->where($map)->JOIN('LEFT JOIN __LIVE_ANCHOR__ hl ON a.roomnumber=hl.roomnumber')->field('a.*,hl.*,hl.id as a_id,a.id as id')->find();
        if($live_info){
            $arr['id'] = $live_info['user_id'];
            $anchor_info = M('Member')->where($arr)->field('username,nickname,mobile,header_img')->find();
            $live_info['name'] = D('Chat')->getnickname($anchor_info);
            $live_info['header_img'] = $anchor_info['header_img'];
            if(empty($live_info['name'])) $live_info['name'] = '汉潮会员';
            if($live_info['city_id'] != 0) $live_info['city']=idtoname($live_info['city_id'],"area");
            else $live_info['city'] = '石家庄';
            $live_info['live_name'] = C('WEB_LIVES_NAME')?C('WEB_LIVES_NAME'):'汉潮直播';
        }
        if($live_info['goods_info']){            
            $goods_info=unserialize($live_info['goods_info']);
            $goods_info=array_filter($goods_info);
            foreach ($goods_info as $k => $v) {
                $edition[] = $v['sort'];
                $goods_info[$k]['goods_id']=$v['id'];
                $goods_info[$k]['img']=thumb($v['img'],200,200);
                $goods_info[$k]['shop_price']=format_price($v['shop_price']);
                $data=array();
                $data = D("Common/GoodsAttr")->get_shop_attr($v['id']);
                $goods_info[$k]['goods_attrs']=$data['newarr'];
            }
            array_multisort($edition, SORT_DESC, $goods_info); 
            
            $goods_count=count($goods_info);
            $live_info['goods_count'] = $goods_count;
        }else{
            $goods_info = '';
        }
        $return['newgoods_info'] = $goods_info;  
        $return['status'] = 1;
        $return['data'] = $live_info;  

        
        //获取微信商城小程序appid
        //获取微信商城小程序appid        
        $wx_shop_appid=D('LiveAnchor')->get_small_appid('SHOP');
        $return['wx_shop_appid']=$wx_shop_appid;
        $this->ajaxReturn($return);

    }
}