<?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 call(){
        $param=array(
            'instanceId'=>'92e13f2c-4ae4-4602-aacb-595253ad20ca',
            'ajaxPath'=>U('Home/Call/ajaxapi','',true,true),
        
        );
        $this->assign($param);
        $this->display();
    }
    public function ajaxapi(){
        $_data=I('post.');
        file_put_contents('uploads/PAY-callpost'.I('get.method').date("YmdHis").'.txt',var_export($_data,true));
        
        $action = $_data['action'];
        $response;
        switch ($action){
         case "GetConfig":
           $response=$this->GetConfig($request);
           break;
         case "RequestLoginInfo":
           $response=$this->RequestLoginInfo($request);
           break;
         case "ListSkillGroupsOfUser":
           $response=$this->ListSkillGroupsOfUser($request);
           break;
         case "GetNumberRegionInfo":
           $response=$this->GetNumberRegionInfo($request);
           break;
         case "GetServiceExtensions":
           $response=$this->GetServiceExtensions($request);
           break;
         case "PickLocalNumber":
           $response=$this->PickLocalNumber($request);
           break;
         case "ListConfig":
           $response=$this->ListConfig($request);
           break;
         case "LaunchAppraise":
           $response=$this->LaunchAppraise($request);
           break;
         case "GenerateAgentStatisticReport":
           $response=$this->GenerateAgentStatisticReport($request);
           break;
         case "ListRecordingsByContactId":
           $response=$this->listRecordingsByContactId($request);
           break;
         case "DownloadRecording":
           $response=$this->DownloadRecording($request);
           break;
         case "ListCallDetailRecords":
           $response=$this->ListCallDetailRecords($request);
           break;
         case "ListRealTimeAgent":
           $response=$this->ListRealTimeAgent($request);
           break;
         case "ListRecentCallRecords":
           $response=$this->ListRecentCallRecords($request);
           break;
        }
        return $response;
        
    } 
    public function GetConfig(Request $request){

      $param = $request->request->get('request');
      $json=json_decode($param,true);
      $name = $json['name'];
      $instanceId = $json['instanceId'];
      $objectType = $json['objectType'];
      $objectId = $json['objectId'];

      $session = $request->getSession();
      $userInfo =$session->get($instanceId);
      $refreshToken = $session->get('refreshToken');


      // var_dump(dirname(__FILE__)."/../../../plugin/aliyun-php-sdk-core/Config.php");
      // die;
      //引入sdk,只做参考，按自己的项目配置来
      include_once(dirname(__FILE__)."/../../../plugin/aliyun-php-sdk-core/Config.php");

      //接口调用过程
      $clientProfile = \DefaultProfile::getBearerTokenProfile("cn-shanghai",$refreshToken); 

      \DefaultProfile::addEndpoint("CCC", "cn-shanghai", "CCC", "ccc.cn-shanghai.aliyuncs.com");

      //引入sdk,只做参考
      include(dirname(__FILE__)."/../../../plugin/aliyun-php-sdk-ccc/CCC/Request/V20170705/GetConfigRequest.php");

      $request=new GetConfigRequest();
      $request->setInstanceId($instanceId);
      $request->setName($name);
      $request->setObjectType($objectType);
      $request->setObjectId($objectId);
    
      $client=new \DefaultAcsClient($clientProfile);
      $response=$client->getAcsResponse($request); 

      $destination = array();

      $this->copyObject($destination,$response);

      return new JsonResponse($destination);

    }
    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;
    		}
    		$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();
    }
}
?>