LewaimaiWeixinMessage.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. require_once "WXBizMsgCrypt/WXBizMsgCrypt.php";
  3. //这个类是用来处理微信消息回复相关的问题
  4. class LewaimaiWeixinMessage {
  5. public static function getResponse($xmlMsg)
  6. {
  7. $msgObj = simplexml_load_string($xmlMsg, 'SimpleXMLElement', LIBXML_NOCDATA);
  8. if ($msgObj == false)
  9. {
  10. LewaimaiDebug::Log("Parsel XML Failed!");
  11. return false;
  12. }
  13. //公共属性
  14. $FromUserName = $msgObj->FromUserName;
  15. $ToUserName = $msgObj->ToUserName;
  16. $CreateTime = $msgObj->CreateTime;
  17. $MsgType = $msgObj->MsgType;
  18. $MsgId = $msgObj->MsgId;
  19. if ($MsgType == "text")
  20. {
  21. $TextContent = trim($msgObj->Content);
  22. if (strlen($TextContent) == 0)
  23. {
  24. $newResponse = "success";
  25. }
  26. else
  27. {
  28. $newTextMsgHandle = new TextMsgHandle;
  29. $newTextMsgHandle->SetMsgInfo($FromUserName, $ToUserName, $CreateTime, $MsgType, $MsgId);
  30. $newTextMsgHandle->m_TextContent = $TextContent;
  31. $newResponse = $newTextMsgHandle->GetResponse();
  32. }
  33. }
  34. elseif ($MsgType == "event")
  35. {
  36. $Event = trim($msgObj->Event);
  37. $EventKey = trim($msgObj->EventKey);
  38. $Latitude = trim($msgObj->Latitude);
  39. $Longitude = trim($msgObj->Longitude);
  40. $Precision = trim($msgObj->Precision);
  41. $Ticket = trim($msgObj->Ticket);
  42. if (!empty($Event))
  43. {
  44. $newEventMsgHandle = new EventMsgHandle();
  45. $newEventMsgHandle->SetMsgInfo($FromUserName, $ToUserName, $CreateTime, $MsgType, $MsgId);
  46. $newEventMsgHandle->m_Event = $Event;
  47. $newEventMsgHandle->m_EventKey = $EventKey;
  48. $newEventMsgHandle->m_Latitude = $Latitude;
  49. $newEventMsgHandle->m_Longitude = $Longitude;
  50. $newEventMsgHandle->m_Precision = $Precision;
  51. $newEventMsgHandle->m_Ticket = $Ticket;
  52. $newResponse = $newEventMsgHandle->GetResponse();
  53. }
  54. else
  55. {
  56. $newResponse = "success";
  57. }
  58. }
  59. elseif ($MsgType == "location")
  60. {
  61. $Location_X = trim($msgObj->Location_X); //腾讯接口里面这里X是维度,Y是经度
  62. $Location_Y = trim($msgObj->Location_Y);
  63. $Scale = trim($msgObj->Scale);
  64. $Label = trim($msgObj->Label);
  65. if (!empty($Location_X) && !empty($Location_Y))
  66. {
  67. $newLocationMsgHandle = new LocationMsgHandle();
  68. $newLocationMsgHandle->SetMsgInfo($FromUserName, $ToUserName, $CreateTime, $MsgType, $MsgId);
  69. $newLocationMsgHandle->m_Location_X = $Location_X;
  70. $newLocationMsgHandle->m_Location_Y = $Location_Y;
  71. $newLocationMsgHandle->m_Scale = $Scale;
  72. $newLocationMsgHandle->m_Label = $Label;
  73. $newResponse = $newLocationMsgHandle->GetResponse();
  74. }
  75. else
  76. {
  77. $newResponse = "success";
  78. }
  79. }
  80. elseif ($MsgType == "image")
  81. {
  82. $PicUrl = trim($msgObj->PicUrl);
  83. $MediaId = trim($msgObj->MediaId);
  84. if(!empty($PicUrl) && !empty($MediaId))
  85. {
  86. $picturereplytage = PictureReplyTage::model()->find('picurl="'.$PicUrl.'"');
  87. if($picturereplytage)
  88. {
  89. if($picturereplytage->time + 30 > time())
  90. {
  91. exit;
  92. }
  93. else
  94. {
  95. $picturereplytage->time = time();
  96. $picturereplytage->update();
  97. }
  98. }
  99. else
  100. {
  101. $picturereplytage = new PictureReplyTage();
  102. $picturereplytage->picurl = $PicUrl;
  103. $picturereplytage->time = time();
  104. $picturereplytage->save();
  105. }
  106. $newImageMsgHandle = new ImageMsgHandle();
  107. $newImageMsgHandle->SetMsgInfo($FromUserName, $ToUserName, $CreateTime, $MsgType, $MsgId);
  108. $newResponse = $newImageMsgHandle->GetResponse();
  109. }
  110. else
  111. {
  112. $newResponse = "success";
  113. }
  114. }
  115. elseif ($MsgType == "voice")
  116. {
  117. $format = trim($msgObj->Format);
  118. $MediaId = trim($msgObj->MediaId);
  119. if(!empty($format) && !empty($MediaId))
  120. {
  121. $voicereplytage = VoiceReplyTage::model()->find('media_id="'.$MediaId.'"');
  122. if($voicereplytage)
  123. {
  124. if($voicereplytage->time + 30 > time())
  125. {
  126. exit;
  127. }
  128. else
  129. {
  130. $voicereplytage->time = time();
  131. $voicereplytage->update();
  132. }
  133. }
  134. else
  135. {
  136. $voicereplytage = new VoiceReplyTage();
  137. $voicereplytage->media_id = $MediaId;
  138. $voicereplytage->format = $format;
  139. $voicereplytage->time = time();
  140. $voicereplytage->save();
  141. }
  142. $newVoiceMsgHandle = new VoiceMsgHandle();
  143. $newVoiceMsgHandle->SetMsgInfo($FromUserName, $ToUserName, $CreateTime, $MsgType, $MsgId);
  144. $newResponse = $newVoiceMsgHandle->GetResponse();
  145. }
  146. else
  147. {
  148. $newResponse = "success";
  149. }
  150. }
  151. else
  152. {
  153. $newResponse = "success";
  154. }
  155. if (!isset($newResponse))
  156. {
  157. Yii::log("MsgType:" . $MsgType);
  158. }
  159. return $newResponse;
  160. }
  161. public static function getDecryptMsg()
  162. {
  163. $msg_signature = $_GET["msg_signature"];
  164. $timestamp = $_GET["timestamp"];
  165. $nonce = $_GET["nonce"];
  166. $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
  167. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  168. if ($postObj == false)
  169. {
  170. Yii::log("Parsel XML Failed!");
  171. echo "are u kidding?";
  172. exit();
  173. }
  174. $encrypt = $postObj->Encrypt;
  175. $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
  176. $from_xml = sprintf($format, $encrypt);
  177. $appId = LEWAIMAI_PINGTAI_APPID;
  178. $token = LEWAIMAI_PINGTAI_TOKEN;
  179. $encodingAesKey = LEWAIMAI_PINGTAI_ENCODINGAESKEY;
  180. $pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId);
  181. //第三方收到公众号平台发送的消息
  182. $msg = '';
  183. $errCode = $pc->decryptMsg($msg_signature, $timestamp, $nonce, $from_xml, $msg);
  184. return $msg;
  185. }
  186. }