<?php
namespace Home\Controller;
class CallController extends BaseController {
    public function auth(){
        $_data=I('get.');
        $call= new \Common\Vendor\Aliyuncall;
        if($_data['state']!='hanchaocall'){
            $return=array('status'=>0,'info'=>'state is error');
        }else{
            $return=$call->getTokenBycode($_data['code']);
        }
        if($return['status']){
            $this->success('授权成功');
        }else{
            $this->error($return['info']);
        }
    }
    public function notify(){
    	$_data=I('get.');
    	// file_put_contents('uploads/PAY-call'.I('get.method').date("YmdHis").'.txt',var_export($_data,true));
    	if(!empty($_data['callNumber']) && !empty($_data['calledNumber']) && !empty($_data['callType'])){
    		$map=$add_data=$shop_map=array();
    		if($_data['callType']=='Inbound'){//呼入
    			$map['mobile']=$_data['callNumber'];
                $shop_map['shops_call']=$_data['calledNumber'];
    			$add_data['call_type']=2;
    		}else if($_data['callType']=='Outbound'){//呼出
    			$map['mobile']=$_data['calledNumber'];
                $shop_map['shops_call']=$_data['callNumber'];
    			$add_data['call_type']=1;
    		}
    		$uid=M('Member')->where($map)->getField('id');
    		$shop_id=M('Shops')->where($shop_map)->getField('id');
    		if($uid){
    			$add_data['uid']=$uid;
    		}
    		if($shop_id){
    			$add_data['shop_id']=$shop_id;
    		}
            if($add_data['call_type']=2 && $shop_id){//呼叫中心推送                
                $content="您有新的来电请注意接听";
                $paramdata=array('type'=>1,'txt'=>$content);
                $ids=getpushids(5,$shop_id);
                if(!empty($ids[1]))$result = pushSend($ids[1],$content,'app',1,$paramdata);
                if(!empty($ids[2]))$result = pushSend($ids[2],$content,'app',2,$paramdata);
            }
    		$add_data['caller']=$_data['callNumber'];
    		$add_data['calles']=$_data['calledNumber'];
            $add_data['callid']=$_data['contactId'];
    		$add_data['addtime']=NOW_TIME;
    		$add_data['status']=1;
    		M('CallLog')->add($add_data);
            $this->assign($_data);
    	}
        $this->display();
    }
}
?>