微信公众平台 入门到精通 《餐厅管理》

index.php

<?php

//define("TOKEN", "weixin");

include("diner/lib/weixin.class.php");


$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {
	$wechatObj->responseMsg();
}else{
    $wechatObj->valid();
}

class wechatCallbackapiTest 
{
    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;
        }
    }

    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)){
            $this->logger("R ".$postStr);
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $RX_TYPE = trim($postObj->MsgType);

            switch ($RX_TYPE)
            {
                case "event":
                    $result = $this->receiveEvent($postObj);
                    break;
                case "text":
                    $result = $this->receiveText($postObj);
                    break;
                case "location":
                    include("diner/model/SendMsgDB.php");
     				$sendmsg = new SendMsgDB();//消息处理模型实例化
                    $sendmsg->saveUserLocation($postObj);
                    $array =  $this->goroute($postObj);
                    $result = $this->transmitNews($postObj, $array);
                    break;
            }
            $this->logger("T ".$result);
            echo $result;
        }else {
            echo "";
            exit;
        }
    }
    
    //接收响应
    private function receiveEvent($object)
    {
        $content = "";
        switch ($object->Event)
        {
            case "subscribe":
                $content = "您好,欢迎关注兔子饭庄。 ";
                if (isset($object->EventKey)){
                    $array = $this->showqr($object, 0);
                 	$result = $this->transmitNews($object, $array);
                }else{
                	$result = $this->transmitText($object, $content);
                }
                
                break;
            case "unsubscribe":
                $content = "取消关注";
                $result = $this->transmitText($object, $content);
                break;
             case 'VIEW':
                break;
             case 'CLICK':
                $array = $this->click($object);
               	if(is_array($array)){
                	$result = $this->transmitNews($object, $array);
               	}else{
                	$result = $this->transmitText($object, $array);
               	}
                
                break;
             case "SCAN":
                 $array = $this->showqr($object, 1);
                 $result = $this->transmitNews($object, $array);
                break;
            default :
             
        }
      
       	return $result;
       
    }
    
    //接收位置消息
    private function receiveLocation($object)
    {
        $content = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label;
        $result = $this->transmitText($object, $content);
        return $result;
    }
    
    /**
     * 显示优惠券页面
     * @param type $data $data->EventKey的值与type有关。当type=0时为qrscene_123123,type=1时为123123
     * @param type $type 0,未关注 1 关注
     */
    private function showqr($data, $type = 0) {
        if ($type == 0) {
            $sceneid = substr($data->EventKey, 8);
        } else if ($type == 1) {
           	$sceneid = $data->EventKey;
        }
        $text = '使用优惠券';
        $content = array();
        $content[] = array(
                  "Title"=>"使用优惠券", 
                  "Description"=> $text, 
                  "PicUrl"=>"http://mmsns.qpic.cn/mmsns/XWia2Xj7RZ8mhQaESostBicFaX2HjVBbJYKKCBk9PkuicKrSZdfNL7XAw/0", 
                  "Url" => "http://1.dq095.applinzi.com/diner/showqr.php?sceneid=" .$sceneid . "&user=" . $data->FromUserName
        		);
        return $content;
    }
    
    /*
    * 分类处理点击事件
    * @param type $data 微信消息体
    */
    
     private function click($object) {
        $eventKey = $object->EventKey;
        switch ($eventKey) {
            case 'CLICK_RESERVE':
				//$array = "CLICK_RESERVE,return".json_encode($eventKey)."。over ";
               $array = $this->goreserve($object);
               
                break;
            case 'CLICK_ROUTE':
                include("diner/model/SendMsgDB.php");
     			$sendmsg = new SendMsgDB();//消息处理模型实例化
                $userLoc = $sendmsg->getUserLocation($object);
                //sae_log(var_export($userLoc, TRUE));
                if (empty($userLoc)) {
                    $array =  "【兔子饭庄路线导航】\n试试发送你的位置,即可为您指引到各个分店线路:\n 1. 点击左下方“小键盘”\n 2. 点击打字窗口旁边的“+号键”\n 3. 选择“位置”图标 \n 4. 完成定位后点击右上角“发送”";
                } else {
                    $array =  $this->goroute($object);
                }
                break;
            default :                
                break;
        }
       return  $array;
    }
    
    /**
     * 显示路线导航页面
     * @param type $data 微信消息体
     */
    private function goroute($data) {
         $text = '最近的兔子饭庄路线';
         $content = array();
         $content[] = array(
                  "Title"=>"路线导航", 
                  "Description"=> $text, 
                  "PicUrl"=>"http://mmbiz.qpic.cn/mmbiz/XWia2Xj7RZ8nxGcFl47qJQjsm1iaqf3SquP9ucVPEoCCBFAibdicKtaCmbEZCLJcE5ib6gEKSZicjHSlySJclicrgicPHQ/0", 
                  "Url" =>"http://1.dq095.applinzi.com/diner/route.php?user=". $data->FromUserName
             );
          return  $content;
    }

    
    /**
     * 显示预约页面
     * @param type $data 微信消息体
     */
    private function goreserve($data) {
        
     include("diner/model/SendMsgDB.php");
     $sendmsg = new SendMsgDB();//消息处理模型实例化
     
        $ret = $sendmsg->getRecentReserve($data);
        $content = array();
        if (!empty($ret) && $ret['dinertime'] > time() - 1800) {
             $content[] = array(
                  "Title"=>"您最近的预约", 
                  "Description"=>'时间:' . date('Y-m-d H:i', $ret['dinertime']) . ',人数:' . $ret['num'], 
                  "PicUrl"=>"http://mmbiz.qpic.cn/mmbiz/XWia2Xj7RZ8nxGcFl47qJQjsm1iaqf3SquWeL0BzAficTdkxo2oeV9PvVqvcUUj14pE4oq5fAZx2s4TGsZIalZFCg/0", 
                  "Url" =>"http://1.dq095.applinzi.com/diner/myreserve.php?user=". $data->FromUserName
             );
        } else {
             $content[] = array(
                  "Title"=>"兔子饭庄精美美食等着你哟", 
                  "Description"=>'现在预约吧', 
                  "PicUrl"=>"http://mmbiz.qpic.cn/mmbiz/XWia2Xj7RZ8nxGcFl47qJQjsm1iaqf3SquWeL0BzAficTdkxo2oeV9PvVqvcUUj14pE4oq5fAZx2s4TGsZIalZFCg/0", 
                  "Url" =>"http://1.dq095.applinzi.com/diner/reserve.php?user=". $data->FromUserName
             );
        }
        
        return  $content;
    }
	
    //回复图文消息
    private function transmitNews($object, $newsArray)
    {
        if(!is_array($newsArray)){
            return;
        }
        $itemTpl = "    <item>
                            <Title><![CDATA[%s]]></Title>
                            <Description><![CDATA[%s]]></Description>
                            <PicUrl><![CDATA[%s]]></PicUrl>
                            <Url><![CDATA[%s]]></Url>
                        </item>
                    ";
        $item_str = "";
        foreach ($newsArray as $item){
            $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
        }
        $newsTpl = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[news]]></MsgType>
                        <Content><![CDATA[]]></Content>
                        <ArticleCount>%s</ArticleCount>
                        <Articles>
                        $item_str</Articles>
                    </xml>";

        $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
        return $result;
    }
  
    private function receiveText($object)
    {
        $keyword = trim($object->Content);
        include("xiaoi.php");
        $content = getXiaoiInfo($object->FromUserName, $keyword);
        $result = $this->transmitText($object, $content);
        return $result;
    }

    private function transmitText($object, $content)
    {
        $textTpl = "<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[text]]></MsgType>
                        <Content><![CDATA[%s]]></Content>
                    </xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        return $result;
    }
    
    private function logger($log_content)
    {
        if(isset($_SERVER['HTTP_BAE_ENV_APPID'])){   //BAE
            require_once "BaeLog.class.php";
            $logger = BaeLog::getInstance();
            $logger ->logDebug($log_content);
        }else if(isset($_SERVER['HTTP_APPNAME'])){   //SAE
            sae_set_display_errors(false);
            sae_debug($log_content);
            sae_set_display_errors(true);
        }else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL
            $max_size = 10000;
            $log_filename = "log.xml";
            if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
            file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND);
        }
    }
}


?>

猜你喜欢

转载自blog.csdn.net/qq_37968920/article/details/83069186
今日推荐