这个我们就重写weixin的类,因为我之前是直接做的,看最近博客上来路这些的东西比较多就放出思路大家来探讨研究和使用吧,不多说了,估计来的用户都看怎么使用我就直接上代码了!

<?php
define("TOKEN","");

function load($classname){
		//程序需要优化 需要定义一个静态变量 如果这个对象被加载了 就直接使用从
	    //程序池里 否则就加载这个程序
		require_once $classname.'.class.php';
	}
spl_autoload_register('load');
主入口程序
<?php
require_once "config.php";
class WeiXin{
                    private $classname = "";
                     public function __construct($classname){
                       $this->classname = $classname;
                     }

			public function response(){
				//装饰者模式加载各种东西,这里可以做业务判断
				$handler = new WXHandler(new $this->classname());
				$handler->responseMessage();
			}

			//第一步检测微信使用的
			public  function valid(){
		        $echoStr = $_GET["echostr"];
		        if($this->checkSignature()){
		        	echo $echoStr;
		        	exit;
		        	}
    		}

    		private function checkSignature(){
		        $signature = $_GET["signature"];
		        $timestamp = $_GET["timestamp"];
		        $nonce = $_GET["nonce"];	
		        		
				$token = TOKEN;
				$tmpArr = array($token,$timestamp,$nonce);
				sort($tmpArr);
				$tmpStr = implode($tmpArr);
				$tmpStr = sha1($tmpStr);
				
				if($tmpStr == $signature){
					return true;
				}else{
					return false;
				}
			}
	}

$weixin = new WeiXin(TestDemo);
$weixin->response();
抽象类,用多态实现加载多个模块
<?php
	abstract class WXResponseFormat {
		protected $fromUserName = '';
		protected $toUserName = '';
		protected $keyword = '';
		protected $time = '';
		protected $model = null;
		protected $msgType = '';
		protected $event = '';
		protected $textTpl = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							<FuncFlag>0<FuncFlag>
						</xml>";

		protected function getDatas(){
			$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
			if (!empty($postStr)){
              	$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $this->fromUserName = $fromUsername = $postObj->FromUserName;
                $this->toUserName = $toUsername = $postObj->ToUserName;
                $this->keyword = $keyword = trim($postObj->Content);
                $this->time = $time = time();
                $this->msgType = $postObj->MsgType;
                $this->event = $postObj->Event;
			}
		}
		
		
		abstract public function responseMessage();
	}
模拟实现装饰者模式
<?php
	class WXHandler {
		private $wxResponseFormat = null;
		
		public function __construct(WXResponseFormat $wxResponseFormat){
			$this->wxResponseFormat = $wxResponseFormat;
		}
		
		public function responseMessage(){
			return $this->wxResponseFormat->responseMessage();
		}
	}
一个testdemo实现类,大家可以自己加载自己的业务逻辑
<?php
	class TestDemo extends WXResponseFormat {
		private $modelstatus;
		private $modelname = array(1=>'search_content',2=>'search_word',3=>'search_music',4=>'search_location',5=>'search_help',6=>'search_no',7=>'search_mtalk');

		public function responseMessage(){
			$this->getDatas();
			$this->JudgeModel();
			$msgType = 'text';
			$contentStr = '';
			
			if($this->msgType=='event' && $this->event == 'subscribe'){
				$contentStr = "Hello,欢迎您关注我们\n";
				$resultStr = sprintf($this->textTpl,$this->fromUserName,$this->toUserName,$this->time,'text',$contentStr);
				echo $resultStr;
				exit;
			}
			
			$this->keyword = strtolower(trim($this->keyword));

			//没有输入任何关键字的时候
			if(empty($this->keyword)){
				$contentStr.= "亲~ 您还没有输入内容噢~~ ^_^";
				$resultStr = sprintf($this->textTpl,$this->fromUserName,$this->toUserName,$this->time,'text',$contentStr);
				echo $resultStr;
				exit;
			}

			//判断状态加载模块扩展写加载某些东西
			if(empty($this->modelstatus)){
				$notype = new search_no();
				$result = $notype ->responseMessage();
			}else{
				$modeltype = $this->modelname[$this->modelstatus];
				$type = new $modeltype();
				$type->responseMessage();
			}
		}

		//根据输入的关键字判断是哪个模式,譬如输入1 就是对应search_content
		private function JudgeModel(){
			$data = array('s'=>1,'c'=>2,'m'=>3,'l'=>4,'h'=>5);
			$keyword = strtolower(trim($this->keyword));
			if(array_key_exists($keyword,$data)){
				if(isset($data[$keyword])){
					$id = $data[$keyword];
				}
			}	
			return $this->modelstatus = $id;
		}
	
}
加载的实现的不同的方法
<?php

class search_no extends WXResponseFormat{

	public function responseMessage(){
			$this->getDatas();
			$contentStr.= "这个功能我们还没有开发";
			$resultStr = sprintf($this->textTpl,$this->fromUserName,$this->toUserName,$this->time,'text',$contentStr);
			echo $resultStr;
			exit;
		
	}
	
}

?>
这个代码,直接从我自己开发的东西玻璃出来的,比较破碎,因为整个项目里边都是业务逻辑,加载的比较复杂,让大家更简单的理解就这样出来了,有不懂得大家可以留言!

点赞(0) 打赏

Comment list 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部