Receive.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | receive.php 用于接收微信发过来的信息
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace Component\Src;
  8. use Dever;
  9. use Main\Lib\Wechat;
  10. class Receive
  11. {
  12. /**
  13. * wechat
  14. *
  15. * @var Wechat
  16. */
  17. private $wechat;
  18. /**
  19. * result
  20. *
  21. * @var array
  22. */
  23. private $result;
  24. /**
  25. * output
  26. *
  27. * @var string
  28. */
  29. private $output;
  30. /**
  31. * 构造函数 初始化
  32. *
  33. * @return mixed
  34. */
  35. public function __construct()
  36. {
  37. $this->wechat = new Wechat();
  38. }
  39. /**
  40. * 业务推送接口 微信服务器会将所有请求都推送到这里
  41. *
  42. * @return mixed
  43. */
  44. public function main()
  45. {
  46. $this->output = '';
  47. # 获取微信消息
  48. $this->request();
  49. # 对消息进行验证,并根据类型得到本平台的配置数据
  50. //$this->response();
  51. # 输出
  52. $this->output();
  53. }
  54. /**
  55. * 输出
  56. *
  57. * @return mixed
  58. */
  59. private function output()
  60. {
  61. if (!$this->output) {
  62. $this->output = 'success';
  63. }
  64. echo $this->output;die;
  65. }
  66. /**
  67. * 据类型得到本平台的配置数据
  68. *
  69. * @return mixed
  70. */
  71. private function response()
  72. {
  73. if ($this->output) {
  74. return;
  75. }
  76. if ($this->result && isset($this->result['add_type'])) {
  77. if (isset($this->result['subscribe'])) {
  78. if ($this->result['subscribe'] == true) {
  79. # 订阅成功 发送消息
  80. $this->msg('msg1');
  81. # 发送消息之后,往用户表更新信息
  82. $this->user();
  83. $this->setSubscribe(1);
  84. }
  85. if ($this->result['subscribe'] == false) {
  86. # 取消订阅 不发送消息
  87. //$this->msg('msg1');
  88. # 改变当前用户的订阅状态
  89. $this->setSubscribe(2);
  90. }
  91. } elseif (isset($this->result['add_content'][0]['Content'])) {
  92. $this->match($this->result['add_content'][0]['Content']);
  93. } else {
  94. # 如果没有匹配到,则直接返回默认回复消息
  95. $this->msg('msg2');
  96. }
  97. }
  98. }
  99. /**
  100. * 获取微信发送的内容
  101. *
  102. * @return mixed
  103. */
  104. private function request()
  105. {
  106. //$post = $_GET ? $_GET : $_POST;
  107. $post = file_get_contents('php://input');
  108. /*
  109. $post = ' <xml>
  110. <ToUserName><![CDATA[toUser]]></ToUserName>
  111. <FromUserName><![CDATA[ow29VvzICifpOTq_9EDGT3AhYpqg]]></FromUserName>
  112. <CreateTime>1442067297</CreateTime>
  113. <MsgType><![CDATA[text]]></MsgType>
  114. <Content><![CDATA[dfd]]></Content>
  115. <MsgId>1311112322222222</MsgId>
  116. </xml>';
  117. /*
  118. $post = '<xml>
  119. <ToUserName><![CDATA[toUser]]></ToUserName>
  120. <FromUserName><![CDATA[FromUser]]></FromUserName>
  121. <CreateTime>123456789</CreateTime>
  122. <MsgType><![CDATA[event]]></MsgType>
  123. <Event><![CDATA[subscribe]]></Event>
  124. </xml>';
  125. $post = ' <xml>
  126. <ToUserName><![CDATA[toUser]]></ToUserName>
  127. <FromUserName><![CDATA[ow29VvzICifpOTq_9EDGT3AhYpqg]]></FromUserName>
  128. <CreateTime>1442067297</CreateTime>
  129. <MsgType><![CDATA[image]]></MsgType>
  130. <PicUrl><![CDATA[http://mmbiz.qpic.cn/mmbiz/OxUBpiaYgpHiaDaiccrsX3MrFEVtjIfEAZk8zoJDZ4gXWWW2CIFic6Dicqndu23wMgA4Icg3yE4SRZg6L5jqanu9kvQ/0]]></PicUrl>
  131. <MediaId><![CDATA[media_id]]></MediaId>
  132. <MsgId>111</MsgId>
  133. </xml>';
  134. $post = '<xml>
  135. <ToUserName><![CDATA[toUser]]></ToUserName>
  136. <FromUserName><![CDATA[ow29VvzICifpOTq_9EDGT3AhYpqg]]></FromUserName>
  137. <CreateTime>1442067297</CreateTime>
  138. <MsgType><![CDATA[voice]]></MsgType>
  139. <MediaId><![CDATA[media_id]]></MediaId>
  140. <Format><![CDATA[Format]]></Format>
  141. <MsgId>23123213</MsgId>
  142. </xml>';
  143. $post = '<xml>
  144. <ToUserName><![CDATA[toUser]]></ToUserName>
  145. <FromUserName><![CDATA[FromUser]]></FromUserName>
  146. <CreateTime>123456789</CreateTime>
  147. <MsgType><![CDATA[event]]></MsgType>
  148. <Event><![CDATA[CLICK]]></Event>
  149. <EventKey><![CDATA[EVENTKEY]]></EventKey>
  150. </xml>';
  151. */
  152. Dever::log(time());
  153. if (!empty($post)) {
  154. $post = json_encode($post);
  155. Dever::log($post);
  156. return;
  157. libxml_disable_entity_loader(true);
  158. $result = (array) simplexml_load_string($post, 'SimpleXMLElement', LIBXML_NOCDATA);
  159. $this->handle($result);
  160. } else {
  161. return false;
  162. }
  163. }
  164. /**
  165. * 对微信的内容做处理,得到最终结果
  166. *
  167. * @return mixed
  168. */
  169. private function handle($result)
  170. {
  171. return;
  172. $config = array
  173. (
  174. 'AppId' => 'add_appid',
  175. 'CreateTime' => 'add_create_date',
  176. 'InfoType' => 'add_type',
  177. 'ComponentVerifyTicket' => 'add_ticket',
  178. );
  179. foreach ($config as $k => $v) {
  180. $this->result($k, $v, $result);
  181. }
  182. # 事件处理
  183. $this->event();
  184. $this->save();
  185. }
  186. /**
  187. * 事件处理
  188. *
  189. * @return mixed
  190. */
  191. private function event()
  192. {
  193. if ($this->result['add_type'] == 'component_verify_ticket') {
  194. }
  195. }
  196. /**
  197. * 入库
  198. *
  199. * @return mixed
  200. */
  201. private function save()
  202. {
  203. $this->result['add_info'] = is_array($this->result['add_info']) ? implode(',', $this->result['add_info']) : $this->result['add_info'];
  204. $this->result['add_site'] = $this->core->site();
  205. # 入库 使用mid排重
  206. $info = array();
  207. if (isset($this->result['add_mid'])) {
  208. $info = Dever::load('weixin/usermsg-getOne', array('where_mid' => $this->result['add_mid'], 'where_site' => $this->result['add_site']));
  209. $this->result['id'] = $info['id'];
  210. }
  211. if (!$info) {
  212. $this->result['id'] = Dever::load('weixin/usermsg-insert', $this->result);
  213. # 记录点对点聊天记录 暂时不记录了
  214. /*
  215. if($this->result['add_type'] == 'text')
  216. {
  217. $update['add_state'] = 1;
  218. $update['add_openid'] = $this->result['add_from'];
  219. $update['add_usermsg_id'] = $this->result['id'];
  220. $update['add_account'] = -1;
  221. $update['add_type'] = $this->result['add_type'];
  222. $update['add_info'] = Dever::load('weixin/user.msg', $this->result['id']);
  223. Dever::load('weixin/usermsg_log-insert', $update);
  224. }
  225. */
  226. }
  227. }
  228. /**
  229. * 得到最终结果
  230. *
  231. * @return mixed
  232. */
  233. private function result($key, $index, $result, $name = '')
  234. {
  235. if (isset($result[$key])) {
  236. $value = trim($result[$key]);
  237. if ($index == 'add_type') {
  238. $this->type($value, $result);
  239. }
  240. if ($index == 'add_content') {
  241. //$key = $name ? $name : $key;
  242. $this->result[$index][0][$key] = $value;
  243. $this->info($key, $value);
  244. } elseif ($index == 'add_ctime') {
  245. $this->result[$index] = $value ? $value : time();
  246. } else {
  247. $this->result[$index] = $value;
  248. }
  249. }
  250. }
  251. /**
  252. * 根据状态得到用户发送的具体内容,为了响应及时,这里不做解析。
  253. *
  254. * @return mixed
  255. */
  256. private function info($key, $value)
  257. {
  258. $this->result['add_info'][] = $value;
  259. }
  260. /**
  261. * 得到type的值
  262. *
  263. * @return mixed
  264. */
  265. private function type($index, $result)
  266. {
  267. $type = $this->config($index);
  268. if (is_array($type)) {
  269. foreach ($type as $k => $v) {
  270. if (is_numeric($k)) {
  271. $k = $v;
  272. }
  273. $this->result($k, 'add_content', $result, $v);
  274. }
  275. } else {
  276. $this->result($type, 'add_content', $result);
  277. }
  278. }
  279. /**
  280. * 得到对应关系
  281. *
  282. * @return mixed
  283. */
  284. private function keywords($type)
  285. {
  286. # 根据keywords的type设定来设置对应关系
  287. $config = array
  288. (
  289. 1 => 'text',
  290. 2 => 'news',
  291. //3 => 'news',
  292. 4 => 'voice',
  293. 5 => 'video',
  294. 6 => 'image',
  295. );
  296. if (!isset($config[$type])) {
  297. return false;
  298. }
  299. $this->result['type'] = $config[$type];
  300. return $config[$type];
  301. }
  302. /**
  303. * 配置type类型
  304. *
  305. * @return mixed
  306. */
  307. private function config($key, $type = 'get')
  308. {
  309. $config = array
  310. (
  311. # 文本
  312. 'text' => array
  313. (
  314. # 设置获取的key 应答
  315. 'get' => 'Content',
  316. # 设置赋值的key 回复
  317. 'set' => 'Content',
  318. ),
  319. # 图片
  320. 'image' => array
  321. (
  322. 'get' => array('PicUrl', 'MediaId'),
  323. 'set' => array
  324. (
  325. 'Image' => array('MediaId' => 'pic')
  326. ),
  327. ),
  328. # 语音
  329. 'voice' => array
  330. (
  331. 'get' => array('MediaId', 'Format'),
  332. 'set' => array
  333. (
  334. 'Voice' => array('MediaId' => 'mp3')
  335. ),
  336. ),
  337. # 视频
  338. 'video' => array
  339. (
  340. 'get' => array('MediaId', 'ThumbMediaId'),
  341. 'set' => array
  342. (
  343. 'Voice' => array('MediaId' => 'mp4', 'Title' => 'title', 'Description' => 'info')
  344. ),
  345. ),
  346. # 小视频
  347. 'shortvideo' => array
  348. (
  349. 'get' => array('MediaId', 'ThumbMediaId'),
  350. 'set' => false,
  351. ),
  352. # 地理位置
  353. 'location' => array
  354. (
  355. 'get' => array('Location_X', 'Location_Y', 'Scale', 'Label'),
  356. 'set' => false,
  357. ),
  358. # 链接消息
  359. 'link' => array
  360. (
  361. 'get' => array('Title', 'Description', 'Url', 'Label'),
  362. 'set' => false,
  363. ),
  364. # 单图文消息
  365. 'new' => array
  366. (
  367. 'get' => false,
  368. 'set' => array
  369. (
  370. 'ArticleCount' => 1,
  371. 'Articles' => array
  372. (
  373. 'item' => array('Title' => 'title', 'Description' => 'info', 'PicUrl' => 'pic', 'Url' => 'link')
  374. )
  375. ),
  376. ),
  377. # 多图文消息
  378. 'news' => array
  379. (
  380. 'get' => false,
  381. 'set' => array
  382. (
  383. 'ArticleCount' => 'total',
  384. 'Articles' => array
  385. (
  386. 'item' => array('Title' => 'title', 'Description' => 'info', 'PicUrl' => 'pic', 'Url' => 'link')
  387. )
  388. ),
  389. ),
  390. # 音乐消息
  391. 'music' => array
  392. (
  393. 'get' => false,
  394. 'set' => false,
  395. ),
  396. # 事件消息,所有事件返回的字段都在此做说明
  397. 'event' => array
  398. (
  399. 'get' => array
  400. (
  401. 'Event' => '事件',
  402. 'EventKey' => '事件ID',
  403. 'Latitude' => '纬度',
  404. 'Longitude' => '经度',
  405. 'Precision' => '精度',
  406. 'Ticket' => '二维码',
  407. 'MsgID' => '消息ID',
  408. 'Status' => '状态',
  409. 'TotalCount' => '粉丝数',
  410. 'FilterCount' => '过滤数',
  411. 'SentCount' => '成功数',
  412. 'ErrorCount' => '失败数',
  413. ),
  414. 'set' => false,
  415. ),
  416. );
  417. return $config[$key][$type];
  418. }
  419. /**
  420. * 获取当前时间
  421. *
  422. * @return mixed
  423. */
  424. private function time()
  425. {
  426. return time();
  427. }
  428. /**
  429. * 拼装xml相应代码
  430. *
  431. * @return mixed
  432. */
  433. private function xml($content)
  434. {
  435. $xml = "<xml>\r\n"
  436. .$this->tag('ToUserName', $this->result['add_from']) . "\r\n"
  437. .$this->tag('FromUserName', $this->result['add_to']) . "\r\n"
  438. .$this->tag('CreateTime', $this->time()) . "\r\n"
  439. .$this->tag('MsgType', $this->result['type']) . "\r\n"
  440. .$content . "\r\n"
  441. .'</xml>';
  442. return $xml;
  443. }
  444. /**
  445. * 生成tag
  446. *
  447. * @return mixed
  448. */
  449. private function tag($tag, $value = '')
  450. {
  451. if (!is_numeric($value) && strpos($value, '<') === false) {
  452. $value = '<![CDATA['.$value.']]>';
  453. }
  454. return '<'.$tag.'>'.$value.'</'.$tag.'>';
  455. }
  456. }