PHP封装推送实例

'FAIL',
					'code'=>0,
					'status_desc'=>'参数有误!',
				);	
			}
			$push_params	= array(
				'pushPlatUse'		=> 'jpush',		// 默认参数,不需要传
				'productCode'		=> 'yl1001',	// 默认参数,不需要传
				
				"time_to_live"		=> 60*60*24,	// 默认参数,不需要传
				"platform"			=> '',			// 默认参数,不需要传
				"apns_production"	=> true,		// 默认参数,不需要传
			);
			$app_push_class	= new AppPush($push_params);
			
			$this->title	= $title;
			$this->content	= $content;
			$this->params	= $params;
			$this->extras	= $extras;
			
			if ( !isset($this->params['receiver_type']) ) {
				$this->params['receiver_type']	= 4;	// 广播
			}
			if ( !isset($this->params['send_description']) ) {
				$this->params['send_description']	= '新的推送';
			}
			
			// 编码转换(推送时)
			$this->changeCoding();
			
			// 通知
			$this->send_result	= $app_push_class->sendNotice($this->title, $this->content, $this->params, $this->extras);
			// 推送结果
			$this->setResultInfo(1);
			// 推送日志
			$this->addPushLog(1);
			
			// 消息
			$this->send_result	= $app_push_class->sendMessage($this->title, $this->content, $this->params, $this->extras);
			// 推送结果
			$this->setResultInfo(0);
			// 推送日志
			$this->addPushLog(0);
			
			return array(
				'status'=>'OK',
				'code'=>200,
				'status_desc'=>'推送成功!',
			);
		}
		
		/**
		 * 添加推送任务
		 * @param	$push_type	string	推送类型 0消息,1通知,可以同时添加消息和通知的任务,用逗号分隔
		 * @param	$title		string	推送标题
		 * @param	$content	string	推送内容
		 * @param	$params		array	推送参数
		 * 			$params[receiver_value]	string	推送的接收人,此参数和$otherArr[push_person_id]一样的作用
		 * @param	$extras		array	推送扩展
		 * 			$extras[type]			integer	推送的消息类型,此参数和$otherArr[msg_type]一样的作用
		 * @param	$otherArr	array	其他参数,yl_app_push中的字段
		 * 			$otherArr['trigger_id']			string	触发ID
		 * 			$otherArr['trigger_id_two']		string	二级触发ID
		 * 			$otherArr['trigger_person_id']	string	触发人ID
		 * @author	sugang
		 * @date	2014-9-18
		 **/
		public function addPushTask($push_type, $title, $content, $params, $extras, $otherArr = array(), $conditionArr = array()) {
			if( !isset($push_type) || empty($title) || empty($content) ) {
				return array(
					'status'=>'FAIL',
					'code'=>0,
					'status_desc'=>'参数有误!',
				);	
			}
			
			// 推送的消息类型
			if ( !isset($extras['type']) && $otherArr['msg_type'] ) {
				return array(
					'status'=>'FAIL',
					'code'=>2,
					'status_desc'=>'推送消息类型有误!',
				);
			}
			
			// 消息类型
			$msg_type	= isset($extras['type'])	? $extras['type'] : $otherArr['msg_type'];
			// 推送接收人
			$push_person_id	= isset($params['receiver_value'])	? $params['receiver_value'] : 0;
			if ( $params['receiver_type'] == 4 ) {
				// 广播
				$push_person_id	= 0;
			}
			
			$insertArr	= array(
				'yap_product'			=> 1,
				'yap_trigger_id'		=> isset($otherArr['trigger_id']) ? $otherArr['trigger_id'] : '',
				'yap_trigger_id_two'	=> isset($otherArr['trigger_id_two']) ? $otherArr['trigger_id_two'] : '',
				'yap_trigger_person_id'	=> isset($otherArr['trigger_person_id']) ? $otherArr['trigger_person_id'] : '',
				'yap_push_person_id'	=> $push_person_id,
				'yap_push_type'		=> $push_type,
				'yap_msg_type'		=> $msg_type,
				'yap_title'			=> $title,
				'yap_content'		=> $content,
				'yap_status'		=> 2,
				'yap_extra'			=> serialize(array('params'=>$params,'extras'=>$extras)),
				'yap_slave'			=> '',
				'idatetime'			=> isset($otherArr['idatetime']) ? $otherArr['idatetime'] : date('Y-m-d H:i:s', time()+60),
			);
			
			// 同时添加消息和通知
			if ( !is_null($push_type) ) {
				$push_typeArr	= explode(',', $push_type);
				$push_typeArr	= array_unique($push_typeArr);
			}
			foreach( $push_typeArr as $type ) {
				if ( $type == 0 || $type == 1 ) {
					$insertArr['yap_id']		= $this->ado->getSerId();
					$insertArr['yap_push_type']	= $type;
					$this->insert($insertArr);
				}
			}
			return array(
				'status'=>'OK',
				'code'=>200,
				'status_desc'=>'添加成功!',
			);
		}
		
		/////Start 不同类型的推送函数/////////////////////////////
		
		/**
		 * 发表文章时向听众推送
		 * @param	$article_id	string	文章ID
		 **/
		public function pushShareArticle($article_id, $conditionArr = array()) {
			
		}
		
		/**
		 * 社群发表文章时向成员推送
		 * @param	$article_id	string	文章ID
		 * @author	sugang
		 * @date	2014-9-16
		 **/
		public function pushGroupArticle($article_id, $conditionArr = array()) {
			$comm_article_class	= new Comm_article('Comm_articleBase');
			$articleInfo		= $comm_article_class->select("`article_id`='{$article_id}'");
			if ( $articleInfo['article_id'] ) {
				// if( $articleInfo['pro_id']==29 ){
					// 文档资料不推
					// return false;
				// }
				$group_id	= $articleInfo['qi_id'];
				if ( $group_id!='' ) {
					// 获得社群的所有成员
					$groups_person_class	= new Groups_person('Groups_personBase');
					$memberList				= $groups_person_class->getList('allList', " and group_id='{$group_id}'", '', '', '', array('fields'=>'person_id'));
					$member_ids	= DimeTwoToOne($memberList, 'person_id', 'person_id');
					// 剔除发布者
					unset($member_ids[$articleInfo['own_id']]);
					
					// 开启推送设置
					$filterReceiver	= $this->filterPushReceiver($member_ids, 200);
					if ( empty($filterReceiver) ) {
						return false;
					}
					
					// 社群信息
					$groups_class		= new Groups('GroupsBase');
					$groupInfo			= $groups_class->getGroupInfoById($group_id);
					$group_name			= $groupInfo['group_name'];
					// 发布人信息
					$job1001user_class 	= new Job1001user('Job1001userBase');
					$job1001user_arr 	= $job1001user_class->getBasicInfo(10,$articleInfo['own_id']);
					$personInfo			= $job1001user_arr['info']['basic'];
					
					$this->extras = array(
						'aid'			=> $article_id,
						'gid'			=> $group_id,
						'type'			=> '200',
						'tit'			=> $personInfo['person_iname'],
					);
					$this->params = array(
						"receiver_type"		=> 3,
						"send_description"	=> "社群发表话题时推送给成员",
					);
					$this->title			= $this->yl_title;
					$this->content			= $group_name.'中发表了:'.$articleInfo['title'];
					$this->extras['mid']	= $this->ado->getSerId();	// 当前推送的日志id
					
					$this->openDebugReceiver();
					
					$app_push_class		= new AppPush();
					$this->changeCoding();
					$this->send_result	= $app_push_class->sendNotice($this->title, $this->content, $this->params, $this->extras);
					// 存放推送结果
					$this->setResultInfo(1);
					// 保存推送日志
					$this->addPushLog(1, array('trigger_id'=>$article_id,'trigger_id_two'=>$groupInfo['group_id']));
					
					$this->extras['time']	= time();// 发表时间
					$this->extras['mid']	= $this->ado->getSerId();	// 当前推送的日志id
					// $this->changeCoding();
					$this->send_result		= $app_push_class->sendMessage($this->title, $this->content, $this->params, $this->extras);
					// 存放推送结果
					$this->setResultInfo(0);
					// 保存推送日志
					$this->addPushLog(0, array('trigger_id'=>$article_id,'trigger_id_two'=>$groupInfo['group_id']));
				}
			}
		}
		
		/////End 不同类型的推送函数/////////////////////////////
		
		/**
		 * 过滤未开启推送的用户,当开启调试用户时,直接初始化调试数据(程序中有这样的判断:过滤后没有用户了,就不用推送了)
		 * @param	$receiver_id	string/array	需要过滤的用户,多个用户用逗号隔开
		 * @param	$msg_type		integer			消息类型
		 **/
		public function filterPushReceiver($receiver_id, $msg_type) {
			if ( !$this->is_debug ) {
				$yl_app_push_setting_class	= new Yl_app_push_setting('Yl_app_push_settingBase');
				if ( is_array($receiver_id) ) {
					$receiver_id	= implode(',', $receiver_id);
				}
				$receiver_id	= trim($receiver_id, ',');
				if ( $receiver_id ) {
					$receiver_id	= $yl_app_push_setting_class->filterPushArr($receiver_id, $msg_type);
					$this->params['receiver_value']	= $receiver_id;
				}
			} else {
				$this->openDebugReceiver();
			}
			return $receiver_id;
		}
		
		/**
		 * 转换编码函数
		 **/
		public static function convertEncoding($val, $in = 'gbk', $out = 'utf-8') {
			if( $val == '' || empty( $val ) )
				return $val;
			if( !is_array( $val ) ){
				if( is_string( $val ) ){
					if ( function_exists('mb_convert_encoding') ) {
						return mb_convert_encoding($val, $out, $in);
					} else {
						return iconv($in, $out, $val);
					}
				}else{
					return $val;
				}
			}else{
				foreach( $val as $k=>$v ){
					$val[$k] = self::convertEncoding($v, $in, $out);
				}
				return $val;
			}
		}
		
		/**
		 * 编码转换(推送前和添加日志前调用)
		 **/
		public function changeCoding($in = 'gbk', $out = 'utf-8') {
			// $this->title	= self::convertEncoding($this->title, $in, $out);
			// $this->content	= self::convertEncoding($this->content, $in, $out);
			// $this->params	= self::convertEncoding($this->params, $in, $out);
			$this->extras	= self::convertEncoding($this->extras, $in, $out);
		}
		
		/**
		 * 设置推送日志
		 * @param	$insertArr[trigger_id]			string	一级触发源
		 * @param	$insertArr[trigger_id_two]		string	二级触发源
		 * @param	$insertArr[trigger_person_id]	string	触发人ID
		 * @param	$insertArr[push_type]			integer	推送的类型
		 **/
		public function addPushLog($push_type, $insertArr = array()) {
			$this->changeCoding('utf-8', 'gbk');
			
			$insert_arr	= array();
			$insert_arr['yap_id']				= $this->extras['mid'] ? $this->extras['mid'] : $this->ado->getSerId();
			$insert_arr['yap_product']			= $this->product;
			$insert_arr['yap_trigger_id']		= $insertArr['trigger_id'];
			$insert_arr['yap_trigger_id_two']	= $insertArr['trigger_id_two'];
			$insert_arr['yap_trigger_person_id']= $insertArr['trigger_person_id'];
			$insert_arr['yap_push_type']		= $push_type;
			// 广播推送时记录为0,其他为接收用户
			$insert_arr['yap_push_person_id']	= $this->params['receiver_type'] == 4	? '0' : $this->params['receiver_value'];
			$insert_arr['yap_msg_type']			= $this->extras['type'];
			$insert_arr['yap_title']			= $this->title;
			$insert_arr['yap_content']			= $this->content;
			// 推送结果0失败,1成功,2未发送
			$insert_arr['yap_status']		= 1;	// 数据库默认为1
			if( empty($this->send_result) ){
				$insert_arr['yap_status']	= 2;	// 未发送
			} else if( $this->send_result['status'] != 'OK' ){
				$insert_arr['yap_status']	= 0;
			}
			$insert_arr['yap_extra']	= serialize(array('params'=>$this->params, 'extras'=>$this->extras));
			$insert_arr['yap_slave']	= empty($this->send_result)	? '' : serialize($this->send_result);
			$insert_arr['idatetime']	= date('Y-m-d H:i:s');
			$this->insert($insert_arr);
		}
		
		/**
		 * 设置成功接收日志
		 **/
		public function addReceiverLog() {
			
		}
		
		/**
		 * 开启调试方法
		 **/
		public function openDebug($debug_receiver = '') {
			$this->is_debug	= true;
			if( $debug_receiver ){
				$this->debug_receiver	= $debug_receiver;
			}
		}
		
		/**
		 * 开始调试时,设置推送接收人
		 **/
		public function openDebugReceiver() {
			if ( $this->is_debug ) {
				$this->params['receiver_value']	= $this->debug_receiver;
			}
		}
		
		/**
		 * 设置调试结果
		 **/
		public function setResultInfo($push_type) {
			$push_type	= intval($push_type);
			$this->debug_result[$push_type]	= $this->send_result;
		}
		
		/**
		 * 获取调试结果
		 **/
		public function getResultInfo() {
			if( $this->is_debug ){
				return $this->debug_result;
			}
		}
		
	}
?>

你可能感兴趣的:(PHP)