<?php
/**
*	作者：信呼开发团队(雨中磐石)
*	官网：http://www.rockoa.com
*	软件：信呼在线客服系统
*	服务端推送
*/
class reimClassModel extends Model
{
	
	//初始化配置读取
	public function initModel()
	{
		$reimhot = $this->getpushhostport(getconfig('server_push'));
		$this->rockqueue_host = $reimhot['host'];
		$this->rockqueue_port = $reimhot['port'];
		$this->server_recid	  = getconfig('server_recid');
		
	}
	public function getpushhostport($str)
	{
		$host = ''; $port = 0;
		$stra = explode('//', $str);
		if(isset($stra[1])){
			$strb 	= explode(':', str_replace('/','', $stra[1]));
			$host	= $strb[0];
			$port	= (int)arrvalue($strb, 1, '0');
		}
		return array('host'=>$host,'port'=>$port);
	}
	
	/**
	*	发送队列信息
	*	$cont 内容可以是http地址，也可以如:cli,run
	*	$param 参数
	*	使用 c('rockqueue')->push('cli,run');
	*/
	public function push($cont, $param=array(), $runtime=0, $id=0)
	{
		$type 	= 'cmd';
		$url	= $cont;
		if(substr($cont,0,4)=='http'){
			$type='url';
		}else{
			if(!contain($url, ','))$url='cli,'.$url.'';
			$phppath = getconfig('phppath');
			if(!contain($this->rockqueue_host, '127.0.0.1') || isempt($phppath)){
				$urla= explode(',', $url);
				$url = URL.'api.php?m='.$urla[0].'&a='.$urla[1].'';
				$type= 'url';
			}else{
				$st1 = '';
				foreach($param as $k=>$v)$st1.=' -'.$k.'='.$v.'';
				$url = ''.$phppath.' '.ROOT_PATH.'/api.php '.$url.''.$st1.'';
			}
		}
		if($type=='url'){
			$jg  = contain($url,'?')?'&':'?';
			$st1 = '';
			foreach($param as $k=>$v)$st1.='&'.$k.'='.$v.'';
			if($st1!='')$url.=''.$jg.''.substr($st1,1).'';
		}
		
		if($id==0)$id = rand(1,99999);
		$rarr[] = array(
			'qtype'		=> $type,
			'runtime'	=> $runtime,
			'url'		=> $url,
			'id'		=> $id
		);
		$barr = $this->pushdata($rarr);
		$barr['cmdurl'] = ''.$type.':'.$url;
		return $barr;
	}
	
	/**
	*	推送数据过去
	*/
	public function pushdata($rarr)
	{
		if(!$rarr)return returneorror('发空内容');
		if(!$this->rockqueue_host || $this->rockqueue_port==0)return returnerror('没有配置服务端');
		if(is_array($rarr))$rarr = json_encode($rarr);
		$url = 'http://'.$this->rockqueue_host.':'.$this->rockqueue_port.'/?atype=send&data='.urlencode($rarr).'';
		$reqult = c('curl')->setTimeout(1)->getcurl($url);
		if($reqult){
			return returnsuccess($reqult);
		}else{
			return returnerror('服务端配置不能用');
		}
	}
	
	/**
	*	服务端推送$atype='reim'
	*/
	public function pushserver($cans=array())
	{

		$carr['from'] 	= $this->server_recid;
		$carr['adminid']= $this->adminid;
		$carr['atype'] 	= 'send';
		$carr['qtype'] 	= 'reim'; //固定说明是聊天消息
		foreach($cans as $k=>$v)$carr[$k]=$v;
		if(isset($carr['mess']))$carr['mess'] 	= $this->rock->jm->base64encode($carr['mess']);
		if(isset($carr['title']))$carr['title'] = $this->rock->jm->base64encode($carr['title']);
		
		return $this->pushdata($carr);
	}
	
	/**
	*	给咨询用户发消息
	*/
	public function pushzixun($sid, $cans=array())
	{
		$receid = 900000+floatval($sid);
		
		$can['sendid'] = $this->adminid;
		$can['receid'] = $receid;
		$can['type']   = 'zixun';
		foreach($cans as $k=>$v)$can[$k]=$v;
		
		return $this->pushserver($can);
	}
	
	//这个没什么用
	public function rocktempcstr()
	{
		
	}
}