WechatPublic.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. namespace app\common\util;
  3. class WechatPublic
  4. {
  5. var $_conf;
  6. function __construct($config){
  7. $this->_conf = $config;
  8. }
  9. public function valid() {
  10. if($this->checkSignature()){
  11. echo htmlspecialchars(strip_tags($_GET["echostr"]), ENT_QUOTES);
  12. exit;
  13. }
  14. }
  15. private function checkSignature()
  16. {
  17. $signature = $_GET["signature"];
  18. $timestamp = $_GET["timestamp"];
  19. $nonce = $_GET["nonce"];
  20. $token = $this->_conf['token'];
  21. $tmpArr = array($token, $timestamp, $nonce);
  22. sort($tmpArr);
  23. $tmpStr = implode( $tmpArr );
  24. $tmpStr = sha1( $tmpStr );
  25. if( $tmpStr == $signature ){
  26. return true;
  27. }else{
  28. return false;
  29. }
  30. }
  31. public function responseMsg() {
  32. $postStr = @file_get_contents("php://input");
  33. if (!empty($postStr)) {
  34. libxml_disable_entity_loader(true);
  35. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  36. $postType = trim($postObj->MsgType);
  37. switch ($postType) {
  38. case 'text':
  39. $res = $this->receiveText($postObj);
  40. break;
  41. case 'image':
  42. $res = $this->receiveImage($postObj);
  43. break;
  44. case 'location':
  45. $res = $this->receiveLocation($postObj);
  46. break;
  47. case 'voice':
  48. $res = $this->receiveVoice($postObj);
  49. break;
  50. case 'video':
  51. $res = $this->receiveVideo($postObj);
  52. break;
  53. case 'link':
  54. $res = $this->receiveLink($postObj);
  55. break;
  56. case 'event':
  57. $res = $this->receiveEvent($postObj);
  58. break;
  59. default:
  60. $res = 'unknow msg type: '.$postType;
  61. break;
  62. }
  63. echo $res;
  64. }
  65. else{
  66. echo 'other msg';
  67. exit;
  68. }
  69. }
  70. private function receiveLink($object) {
  71. $msg = '你发送的是链接已收到,请等待处理';
  72. $res = $this->transmitText($object, $msg);
  73. return $res;
  74. }
  75. private function receiveText($object) {
  76. $content = trim($object->Content);
  77. $txt = '请点击下方链接:'. "\n";
  78. if ($this->_conf['gjc1'] <> '' && strstr($content, $this->_conf['gjc1'])) {
  79. $data = array();
  80. $txt .= '<a href="'.$this->_conf['gjcl1'].'">'.$this->_conf['gjcm1'].'</a>' . "\n";
  81. $data[] = array('Title'=>$this->_conf['gjcm1'], 'Description'=>'', 'PicUrl'=>$this->_conf['gjci1'], 'Url'=>$this->_conf['gjcl1']);
  82. }
  83. elseif ($this->_conf['gjc2'] <> '' && strstr($content, $this->_conf['gjc2'])) {
  84. $data = array();
  85. $txt .= '<a href="'.$this->_conf['gjcl2'].'">'.$this->_conf['gjcm2'].'</a>' . "\n";
  86. $data[] = array('Title'=>$this->_conf['gjcm2'], 'Description'=>'', 'PicUrl'=>$this->_conf['gjci2'], 'Url'=>$this->_conf['gjcl2']);
  87. }
  88. elseif ($this->_conf['gjc3'] <> '' && strstr($content, $this->_conf['gjc3'])) {
  89. $data = array();
  90. $txt .= '<a href="'.$this->_conf['gjcl3'].'">'.$this->_conf['gjcm3'].'</a>' . "\n";
  91. $data[] = array('Title'=>$this->_conf['gjcm3'], 'Description'=>'', 'PicUrl'=>$this->_conf['gjci3'], 'Url'=>$this->_conf['gjcl3']);
  92. }
  93. elseif ($this->_conf['gjc4'] <> '' && strstr($content, $this->_conf['gjc4'])) {
  94. $data = array();
  95. $txt .= '<a href="'.$this->_conf['gjcl4'].'">'.$this->_conf['gjcm4'].'</a>' . "\n";
  96. $data[] = array('Title'=>$this->_conf['gjcm4'], 'Description'=>'', 'PicUrl'=>$this->_conf['gjci4'], 'Url'=>$this->_conf['gjcl4']);
  97. }
  98. else {
  99. $param =[];
  100. $param['wd'] = $content;
  101. $param['num']=7;
  102. if($this->_conf['msgtype'] !=1){
  103. $param['num'] =1;
  104. }
  105. if (substr($this->_conf['sousuo'], 0, 4) != 'http') {
  106. $this->_conf['sousuo'] = 'http://' . $this->_conf['sousuo'];
  107. }
  108. $res = model('Vod')->listCacheData($param);
  109. $data = [];
  110. if($res['code']>1 || empty($res['list'])){
  111. $txt .= '<a href="'.$this->_conf['wuziyuanlink'].'">'.$this->_conf['wuziyuan'].'</a>' . "\n";
  112. $data[] = array('Title'=>$this->_conf['wuziyuan'], 'Description'=>'', 'PicUrl'=>'', 'Url'=>$this->_conf['wuziyuanlink']);
  113. }
  114. else{
  115. if($this->_conf['bofang'] ==2){
  116. $search_url = $this->_conf['sousuo'] .mac_url('vod/search',['wd'=>$content]); //'/index.php/vod/search/wd/' . urlencode($content);
  117. $txt .= '<a href="'.$search_url.'">点击进入搜索页面查看</a>' . "\n";
  118. $data[] = array('Title'=>'点击进入搜索页面查看', 'Description'=>'恭喜您找到了相关资源,由于微信限制请进入搜索页查看', 'PicUrl'=>'', 'Url'=>$search_url );
  119. }
  120. else {
  121. foreach ($res['list'] as $k => $v) {
  122. $url = $this->_conf['sousuo'] . mac_url_vod_detail($v);
  123. if ($this->_conf['bofang'] > 0) {
  124. $url = $this->_conf['sousuo'] . mac_url_vod_play($v, ['sid' => 1, 'nid' => 1]);
  125. }
  126. if (substr($v['vod_pic'], 0, 4) == 'http' || substr($v['vod_pic'], 0, 4) == 'mac:') {
  127. $picUrl = mac_url_img($v['vod_pic']);
  128. } else {
  129. $picUrl = $this->_conf['sousuo'] . "/" . $v['vod_pic'];
  130. }
  131. $txt .= '<a href="' . $url . '">' . ($k + 1) . ',' . $v['vod_name'] . ' ' . $v['vod_remarks'] . '</a>' . "\n";
  132. $data[] = array('Title' => $v['vod_name'], 'Description' => mac_substring(strip_tags($v["vod_content"]), 20), 'PicUrl' => $picUrl, 'Url' => $url);
  133. }
  134. }
  135. }
  136. }
  137. if (is_array($data)){
  138. if ( $this->_conf['msgtype'] !=1 && isset($data[0])){
  139. $r = $this->transmitNews($object, $data);
  140. }
  141. else{
  142. $r = $this->transmitText($object, $txt);
  143. }
  144. }
  145. return $r;
  146. }
  147. private function receiveEvent($object) {
  148. $guanzhu = $this->_conf['guanzhu'];
  149. $msg = '';
  150. switch ($object->Event) {
  151. case 'subscribe':
  152. $msg = $guanzhu;
  153. break;
  154. case 'unsubscribe':
  155. $msg = '拜拜了您内~';
  156. break;
  157. case 'CLICK':
  158. switch ($object->EventKey) {
  159. default:
  160. $res = '你点击了: '.$object->EventKey;
  161. break;
  162. }
  163. break;
  164. default:
  165. $msg = 'receive a new event: '.$object->Event;
  166. break;
  167. }
  168. $res = $this->transmitText($object, $msg);
  169. return $res;
  170. }
  171. private function transmitText($object, $content) {
  172. $xmlTpl = '<xml>
  173. <ToUserName><![CDATA[%s]]></ToUserName>
  174. <FromUserName><![CDATA[%s]]></FromUserName>
  175. <CreateTime>%s</CreateTime>
  176. <MsgType><![CDATA[text]]></MsgType>
  177. <Content><![CDATA[%s]]></Content>
  178. </xml>';
  179. $res = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content);
  180. return $res;
  181. }
  182. private function transmitNews($object, $newsArray) {
  183. if (!is_array($newsArray)) {
  184. return;
  185. }
  186. $itemTpl = '<item>
  187. <Title><![CDATA[%s]]></Title>
  188. <Description><![CDATA[%s]]></Description>
  189. <PicUrl><![CDATA[%s]]></PicUrl>
  190. <Url><![CDATA[%s]]></Url>
  191. </item>';
  192. $item_str = '';
  193. foreach($newsArray as $item) {
  194. $item_str.= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
  195. }
  196. $xmlTpl = '<xml>
  197. <ToUserName><![CDATA[%s]]></ToUserName>
  198. <FromUserName><![CDATA[%s]]></FromUserName>
  199. <CreateTime>%s</CreateTime>
  200. <MsgType><![CDATA[news]]></MsgType>
  201. <ArticleCount>%s</ArticleCount>
  202. <Articles>%s</Articles>
  203. </xml>';
  204. $res = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray),$item_str);
  205. return $res;
  206. }
  207. private function transmitImage($object, $imageArray) {
  208. $xmlTpl = '<xml>
  209. <ToUserName><![CDATA[%s]]></ToUserName>
  210. <FromUserName><![CDATA[%s]]></FromUserName>
  211. <CreateTime>%s</CreateTime>
  212. <MsgType><![CDATA[image]]></MsgType>
  213. <Image>
  214. <MediaId><![CDATA[%s]]></MediaId>
  215. </Image>
  216. </xml>';
  217. $res = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $imageArray['MediaId']);
  218. return $res;
  219. }
  220. }