You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.9 KiB

  1. <?php
  2. namespace App\Common;
  3. /**
  4. * 微信自定义菜单-响应菜单点击事件
  5. */
  6. class WechatCallbackApi
  7. {
  8. public function valid()
  9. {
  10. $echoStr = $_GET["echostr"];
  11. if($this->checkSignature())
  12. {
  13. echo $echoStr;
  14. exit;
  15. }
  16. }
  17. private function checkSignature()
  18. {
  19. $signature = $_GET["signature"];
  20. $timestamp = $_GET["timestamp"];
  21. $nonce = $_GET["nonce"];
  22. $token = TOKEN;
  23. $tmpArr = array($token, $timestamp, $nonce);
  24. sort($tmpArr);
  25. $tmpStr = implode( $tmpArr );
  26. $tmpStr = sha1( $tmpStr );
  27. if( $tmpStr == $signature )
  28. {
  29. return true;
  30. }
  31. else
  32. {
  33. return false;
  34. }
  35. }
  36. public function responseMsg()
  37. {
  38. $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
  39. if (!empty($postStr))
  40. {
  41. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  42. $RX_TYPE = trim($postObj->MsgType);
  43. switch ($RX_TYPE)
  44. {
  45. case "text":
  46. $resultStr = $this->receiveText($postObj);
  47. break;
  48. case "event":
  49. $resultStr = $this->receiveEvent($postObj);
  50. break;
  51. default:
  52. $resultStr = "";
  53. break;
  54. }
  55. echo $resultStr;
  56. }
  57. else
  58. {
  59. echo "";
  60. exit;
  61. }
  62. }
  63. private function receiveText($object)
  64. {
  65. $funcFlag = 0;
  66. $contentStr = "你发送的内容为:".$object->Content;
  67. $resultStr = $this->transmitText($object, $contentStr, $funcFlag);
  68. return $resultStr;
  69. }
  70. private function receiveEvent($object)
  71. {
  72. $contentStr = "";
  73. switch ($object->Event)
  74. {
  75. case "subscribe":
  76. $contentStr = "欢迎洋洋博客";
  77. case "unsubscribe":
  78. break;
  79. case "CLICK":
  80. switch ($object->EventKey)
  81. {
  82. case "company":
  83. $contentStr[] = array("Title" =>"公司简介",
  84. "Description" =>"洋洋的博客",
  85. "PicUrl" =>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg",
  86. "Url" =>"weixin://addfriend/pondbaystudio");
  87. break;
  88. default:
  89. $contentStr[] = array("Title" =>"默认菜单回复",
  90. "Description" =>"您正在使用的是<span style="font-family: Arial, Helvetica, sans-serif;">洋洋的博客</span><span style="font-family: Arial, Helvetica, sans-serif;">", </span>
  91. "PicUrl" =>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg",
  92. "Url" =>"weixin://addfriend/pondbaystudio");
  93. break;
  94. }
  95. break;
  96. default:
  97. break;
  98. }
  99. if (is_array($contentStr))
  100. {
  101. $resultStr = $this->transmitNews($object, $contentStr);
  102. }
  103. else
  104. {
  105. $resultStr = $this->transmitText($object, $contentStr);
  106. }
  107. return $resultStr;
  108. }
  109. private function transmitText($object, $content, $funcFlag = 0)
  110. {
  111. $textTpl = "<xml>
  112. <ToUserName><![CDATA[%s]]></ToUserName>
  113. <FromUserName><![CDATA[%s]]></FromUserName>
  114. <CreateTime>%s</CreateTime>
  115. <MsgType><![CDATA[text]]></MsgType>
  116. <Content><![CDATA[%s]]></Content>
  117. <FuncFlag>%d</FuncFlag>
  118. </xml>";
  119. $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $funcFlag);
  120. return $resultStr;
  121. }
  122. private function transmitNews($object, $arr_item, $funcFlag = 0)
  123. {
  124. //首条标题28字,其他标题39字
  125. if(!is_array($arr_item))
  126. return;
  127. $itemTpl = "<item>
  128. <Title><![CDATA[%s]]></Title>
  129. <Description><![CDATA[%s]]></Description>
  130. <PicUrl><![CDATA[%s]]></PicUrl>
  131. <Url><![CDATA[%s]]></Url>
  132. </item>
  133. ";
  134. $item_str = "";
  135. foreach ($arr_item as $item)
  136. $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
  137. $newsTpl = "<xml>
  138. <ToUserName><![CDATA[%s]]></ToUserName>
  139. <FromUserName><![CDATA[%s]]></FromUserName>
  140. <CreateTime>%s</CreateTime>
  141. <MsgType><![CDATA[news]]></MsgType>
  142. <Content><![CDATA[]]></Content>
  143. <ArticleCount>%s</ArticleCount>
  144. <Articles>
  145. $item_str</Articles>
  146. <FuncFlag>%s</FuncFlag>
  147. </xml>";
  148. $resultStr = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item), $funcFlag);
  149. return $resultStr;
  150. }
  151. }