微信公眾平臺 入門到精通 《餐廳管理》
阿新 • • 發佈:2018-12-15
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); } } } ?>