Receive.php 11 KB

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