关注回复和自定义回复(tp3.2)

注意:无论关注还是关键词回复 触发的都是咱们最开始填写的那个服务器地址
流程:
	1.当我们进行关注或者在公众号中发送消息的时候,微信服务器填写的地址都会接收到,并且把信息传送到我们填写的服务器地址
	2.我们在类中写一个方法用于接收他们的信息
	3.对接收的信息进行处理查看是那种类型情况是关注还是事件
	4.对不同的事件对数据库进行查找并返回给微信端即可,微信端会根据你的返回和携带的xml等数据返回给用户不同回应
代码如下:
	/*下面的代码不仅试用于关注和自定义关键字回复也试用于菜单的*/
	//  直接通过连接调用相应的方法即可
	 public function handleEvent($object)  
    {  
        $contentStr = "";  
        switch ($object->Event)  
        {  
            case "subscribe":  
            // 这个是关注事件m先触发客服接口,然后在输出自动回复的内容  
                //$to = $this->actoken($object->FromUserName); 
                $wxid = $_GET['wxid']; 
                $res = M('replayset')->where('wxid = '.$wxid .'  and replaytypeid = 1')->find();
                if(!empty($res['imgtext']) && $res['imgtext'] != 0){
                    $getData = M('imgtext')->field("title,description,picurl,url")->where('mark ="'.$res['imgtext'].'"')->select();
                    $articleCount = count($getData);// 个数超过十个不允许
                    $resultStr = $this->responseImgtext($object,$articleCount,$getData);  
                    return $resultStr;  
                    exit();
                }else{
                    $contentStr = /*$object->FromUserName.*/$res['text'];  
                }
                break;  
            case "CLICK":
                $EventKey = $object->EventKey;
                $sql = 'wxid ='.$_GET['wxid'].'  and keyMark ="'. $EventKey .'"  and type = 1';
                $res = M('menu')->where($sql)->find();
      
                if(!empty($res['imgtextMark']) && $res['imgtextMark'] != 0){
                    // 去查此时的内容
                    // 1526361251_NgrGcqU9
                    $getData = M('imgtext')->field("title,description,picurl,url")->where('mark ="'.$res['imgtextMark'].'"')->select();
                
                    $articleCount = count($getData);// 个数超过十个不允许
                    $resultStr = $this->responseImgtext($object,$articleCount,$getData);  
                    return $resultStr;  
                    exit();
                }else{
                    $contentStr = $res['menudata'];
                }
               
                break;
            default :  
                // 再次进入公众号的时候会触发这条信息
               // $contentStr = "Unknow Event: ".$object->Event;  
                exit;
                break;  
        }  
        $resultStr = $this->responseText($object, $contentStr);  
        return $resultStr;  
    }  

	// 自定义回复设置
    public function replayAuto(){
        $postStr = file_get_contents('php://input');// php7 用这个方法否则获取不到
        if (!empty($postStr)){  
                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);  
                $RX_TYPE = trim($postObj->MsgType);  
                switch($RX_TYPE)  
                {  
                    case "text":  
                        $resultStr = $this->handleText($postObj);  
                        break;  
                    case "event":  
                        $resultStr = $this->handleEvent($postObj);  
                        break;  
                    default:  
                        $resultStr = "Unknow msg type: ".$RX_TYPE;  
                        break;  
                }  
                echo $resultStr;  
        }else {  
            echo "";  
        }  

    }
     public function handleText($postObj)  
    {  
        $fromUsername = $postObj->FromUserName;  
        $toUsername = $postObj->ToUserName;  
        $keyword = trim($postObj->Content);  
        $time = time();  
        $textTpl = "  
                      
                      
                    %s  
                      
                      
                    0  
                    ";               
        if(!empty( $keyword ))  
        {  
            $msgType = "text";  
            // 说明有客户输入
            // 则直接去查找此时的后台配置信息进行处理
            $data = M('replayset')->where('wxid='.$_GET['wxid'].'   and  replaytypeid =2' )->select();
            $contentStr = "我们的联系电话:18201383130";

            // 默认是文本,这个位置上进行判断是否是图文回复如果是图文回复则优先图文
            foreach($data as $key=>$value){
                if($value['keyword'] == $keyword){
                    // 图文
                    if(!empty($value['imgtext'])){
                        $getData = M('imgtext')->field("title,description,picurl,url")->where('mark ="'.$value['imgtext'].'"')->select();
                        $articleCount = count($getData);// 个数超过十个不允许
                        $resultStr = $this->responseImgtext($postObj,$articleCount,$getData); 
                        echo $resultStr;  
                        exit(); 
                    }
                    // 文本
                    else{
                        $contentStr = $value['text'];
                    }
                   
                    break;
                }
            }
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);  
            echo $resultStr;  
        }else{  
            echo "Input something...";  
        }  
    }  
     // 图文信息回复
    public function responseImgtext($object,$articleCount,$content, $flag=0){
        $ToUserName = $object->FromUserName;
        $fromUsername = $object->ToUserName;
        $imgText =  " 
                          
                          
                        ".time()."  
                         
                        
                        ";
                    foreach($content as $key=>$vaule){
                        $imgText .= "
                            
                                <![CDATA[".$vaule['title']."]]> 
                                
                                
                                
                            
                        ";
                    }
                        
        $imgText .=     "
                    "; 
        $resultStr =  $imgText;
         //$resultStr = sprintf($imgText, $object->FromUserName, $object->ToUserName, time(), 1,'this is a demo','gao is demo','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1526019694278&di=2688fd1d8e0685be4f30d729589b2f7a&imgtype=0&src=http%3A%2F%2Fimg.bimg.126.net%2Fphoto%2FVANc10nr7f7GxevHxi3JNg%3D%3D%2F5788251421878021509.jpg','www.baidu.com',$flag);  
        return $resultStr;  
    }

后台设置:

关注回复和自定义回复(tp3.2)_第1张图片

客户端效果:

关注回复和自定义回复(tp3.2)_第2张图片


你可能感兴趣的:(thinkphp,微信公众号管理后台搭建和开发)