| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- require_once "WXBizMsgCrypt/WXBizMsgCrypt.php";
- //这个类是用来处理微信消息回复相关的问题
- class LewaimaiWeixinMessage {
- public static function getResponse($xmlMsg)
- {
- $msgObj = simplexml_load_string($xmlMsg, 'SimpleXMLElement', LIBXML_NOCDATA);
- if ($msgObj == false)
- {
- LewaimaiDebug::Log("Parsel XML Failed!");
- return false;
- }
-
- //公共属性
- $FromUserName = $msgObj->FromUserName;
- $ToUserName = $msgObj->ToUserName;
- $CreateTime = $msgObj->CreateTime;
- $MsgType = $msgObj->MsgType;
- $MsgId = $msgObj->MsgId;
-
- if ($MsgType == "text")
- {
- $TextContent = trim($msgObj->Content);
- if (strlen($TextContent) == 0)
- {
- $newResponse = "success";
- }
- else
- {
- $newTextMsgHandle = new TextMsgHandle;
- $newTextMsgHandle->SetMsgInfo($FromUserName, $ToUserName, $CreateTime, $MsgType, $MsgId);
-
- $newTextMsgHandle->m_TextContent = $TextContent;
-
- $newResponse = $newTextMsgHandle->GetResponse();
- }
- }
- elseif ($MsgType == "event")
- {
- $Event = trim($msgObj->Event);
- $EventKey = trim($msgObj->EventKey);
-
- $Latitude = trim($msgObj->Latitude);
- $Longitude = trim($msgObj->Longitude);
- $Precision = trim($msgObj->Precision);
-
- $Ticket = trim($msgObj->Ticket);
-
- if (!empty($Event))
- {
- $newEventMsgHandle = new EventMsgHandle();
- $newEventMsgHandle->SetMsgInfo($FromUserName, $ToUserName, $CreateTime, $MsgType, $MsgId);
-
- $newEventMsgHandle->m_Event = $Event;
- $newEventMsgHandle->m_EventKey = $EventKey;
- $newEventMsgHandle->m_Latitude = $Latitude;
- $newEventMsgHandle->m_Longitude = $Longitude;
- $newEventMsgHandle->m_Precision = $Precision;
- $newEventMsgHandle->m_Ticket = $Ticket;
-
- $newResponse = $newEventMsgHandle->GetResponse();
- }
- else
- {
- $newResponse = "success";
- }
- }
- elseif ($MsgType == "location")
- {
- $Location_X = trim($msgObj->Location_X); //腾讯接口里面这里X是维度,Y是经度
- $Location_Y = trim($msgObj->Location_Y);
- $Scale = trim($msgObj->Scale);
- $Label = trim($msgObj->Label);
-
- if (!empty($Location_X) && !empty($Location_Y))
- {
- $newLocationMsgHandle = new LocationMsgHandle();
- $newLocationMsgHandle->SetMsgInfo($FromUserName, $ToUserName, $CreateTime, $MsgType, $MsgId);
-
- $newLocationMsgHandle->m_Location_X = $Location_X;
- $newLocationMsgHandle->m_Location_Y = $Location_Y;
- $newLocationMsgHandle->m_Scale = $Scale;
- $newLocationMsgHandle->m_Label = $Label;
-
- $newResponse = $newLocationMsgHandle->GetResponse();
- }
- else
- {
- $newResponse = "success";
- }
- }
- elseif ($MsgType == "image")
- {
- $PicUrl = trim($msgObj->PicUrl);
- $MediaId = trim($msgObj->MediaId);
-
- if(!empty($PicUrl) && !empty($MediaId))
- {
- $picturereplytage = PictureReplyTage::model()->find('picurl="'.$PicUrl.'"');
- if($picturereplytage)
- {
- if($picturereplytage->time + 30 > time())
- {
- exit;
- }
- else
- {
- $picturereplytage->time = time();
- $picturereplytage->update();
- }
- }
- else
- {
- $picturereplytage = new PictureReplyTage();
- $picturereplytage->picurl = $PicUrl;
- $picturereplytage->time = time();
- $picturereplytage->save();
- }
-
- $newImageMsgHandle = new ImageMsgHandle();
- $newImageMsgHandle->SetMsgInfo($FromUserName, $ToUserName, $CreateTime, $MsgType, $MsgId);
-
- $newResponse = $newImageMsgHandle->GetResponse();
- }
- else
- {
- $newResponse = "success";
- }
- }
- elseif ($MsgType == "voice")
- {
- $format = trim($msgObj->Format);
- $MediaId = trim($msgObj->MediaId);
-
- if(!empty($format) && !empty($MediaId))
- {
- $voicereplytage = VoiceReplyTage::model()->find('media_id="'.$MediaId.'"');
- if($voicereplytage)
- {
- if($voicereplytage->time + 30 > time())
- {
- exit;
- }
- else
- {
- $voicereplytage->time = time();
- $voicereplytage->update();
- }
- }
- else
- {
- $voicereplytage = new VoiceReplyTage();
- $voicereplytage->media_id = $MediaId;
- $voicereplytage->format = $format;
- $voicereplytage->time = time();
- $voicereplytage->save();
- }
-
- $newVoiceMsgHandle = new VoiceMsgHandle();
- $newVoiceMsgHandle->SetMsgInfo($FromUserName, $ToUserName, $CreateTime, $MsgType, $MsgId);
-
- $newResponse = $newVoiceMsgHandle->GetResponse();
- }
- else
- {
- $newResponse = "success";
- }
- }
- else
- {
- $newResponse = "success";
- }
-
- if (!isset($newResponse))
- {
- Yii::log("MsgType:" . $MsgType);
- }
-
- return $newResponse;
- }
-
- public static function getDecryptMsg()
- {
- $msg_signature = $_GET["msg_signature"];
- $timestamp = $_GET["timestamp"];
- $nonce = $_GET["nonce"];
-
- $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
- $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
- if ($postObj == false)
- {
- Yii::log("Parsel XML Failed!");
- echo "are u kidding?";
- exit();
- }
-
- $encrypt = $postObj->Encrypt;
-
- $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
- $from_xml = sprintf($format, $encrypt);
-
- $appId = LEWAIMAI_PINGTAI_APPID;
- $token = LEWAIMAI_PINGTAI_TOKEN;
- $encodingAesKey = LEWAIMAI_PINGTAI_ENCODINGAESKEY;
- $pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId);
-
- //第三方收到公众号平台发送的消息
- $msg = '';
- $errCode = $pc->decryptMsg($msg_signature, $timestamp, $nonce, $from_xml, $msg);
-
- return $msg;
- }
- }
|