Receive.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. libxml_disable_entity_loader(true);
  164. $result = (array) simplexml_load_string($post, 'SimpleXMLElement', LIBXML_NOCDATA);
  165. $this->handle($result);
  166. } else {
  167. return false;
  168. }
  169. }
  170. /**
  171. * 对微信的内容做处理,得到最终结果
  172. *
  173. * @return mixed
  174. */
  175. private function handle($result)
  176. {
  177. return;
  178. $config = array
  179. (
  180. 'AppId' => 'add_appid',
  181. 'CreateTime' => 'add_create_date',
  182. 'InfoType' => 'add_type',
  183. 'ComponentVerifyTicket' => 'add_ticket',
  184. );
  185. foreach ($config as $k => $v) {
  186. $this->result($k, $v, $result);
  187. }
  188. # 事件处理
  189. $this->event();
  190. $this->save();
  191. }
  192. /**
  193. * 事件处理
  194. *
  195. * @return mixed
  196. */
  197. private function event()
  198. {
  199. if ($this->result['add_type'] == 'component_verify_ticket') {
  200. }
  201. }
  202. /**
  203. * 入库
  204. *
  205. * @return mixed
  206. */
  207. private function save()
  208. {
  209. $this->result['add_info'] = is_array($this->result['add_info']) ? implode(',', $this->result['add_info']) : $this->result['add_info'];
  210. $this->result['add_site'] = $this->core->site();
  211. # 入库 使用mid排重
  212. $info = array();
  213. if (isset($this->result['add_mid'])) {
  214. $info = Dever::load('weixin/usermsg-getOne', array('where_mid' => $this->result['add_mid'], 'where_site' => $this->result['add_site']));
  215. $this->result['id'] = $info['id'];
  216. }
  217. if (!$info) {
  218. $this->result['id'] = Dever::load('weixin/usermsg-insert', $this->result);
  219. # 记录点对点聊天记录 暂时不记录了
  220. /*
  221. if($this->result['add_type'] == 'text')
  222. {
  223. $update['add_state'] = 1;
  224. $update['add_openid'] = $this->result['add_from'];
  225. $update['add_usermsg_id'] = $this->result['id'];
  226. $update['add_account'] = -1;
  227. $update['add_type'] = $this->result['add_type'];
  228. $update['add_info'] = Dever::load('weixin/user.msg', $this->result['id']);
  229. Dever::load('weixin/usermsg_log-insert', $update);
  230. }
  231. */
  232. }
  233. }
  234. /**
  235. * 得到最终结果
  236. *
  237. * @return mixed
  238. */
  239. private function result($key, $index, $result, $name = '')
  240. {
  241. if (isset($result[$key])) {
  242. $value = trim($result[$key]);
  243. if ($index == 'add_type') {
  244. $this->type($value, $result);
  245. }
  246. if ($index == 'add_content') {
  247. //$key = $name ? $name : $key;
  248. $this->result[$index][0][$key] = $value;
  249. $this->info($key, $value);
  250. } elseif ($index == 'add_ctime') {
  251. $this->result[$index] = $value ? $value : time();
  252. } else {
  253. $this->result[$index] = $value;
  254. }
  255. }
  256. }
  257. /**
  258. * 根据状态得到用户发送的具体内容,为了响应及时,这里不做解析。
  259. *
  260. * @return mixed
  261. */
  262. private function info($key, $value)
  263. {
  264. $this->result['add_info'][] = $value;
  265. }
  266. /**
  267. * 得到type的值
  268. *
  269. * @return mixed
  270. */
  271. private function type($index, $result)
  272. {
  273. $type = $this->config($index);
  274. if (is_array($type)) {
  275. foreach ($type as $k => $v) {
  276. if (is_numeric($k)) {
  277. $k = $v;
  278. }
  279. $this->result($k, 'add_content', $result, $v);
  280. }
  281. } else {
  282. $this->result($type, 'add_content', $result);
  283. }
  284. }
  285. /**
  286. * 得到对应关系
  287. *
  288. * @return mixed
  289. */
  290. private function keywords($type)
  291. {
  292. # 根据keywords的type设定来设置对应关系
  293. $config = array
  294. (
  295. 1 => 'text',
  296. 2 => 'news',
  297. //3 => 'news',
  298. 4 => 'voice',
  299. 5 => 'video',
  300. 6 => 'image',
  301. );
  302. if (!isset($config[$type])) {
  303. return false;
  304. }
  305. $this->result['type'] = $config[$type];
  306. return $config[$type];
  307. }
  308. /**
  309. * 配置type类型
  310. *
  311. * @return mixed
  312. */
  313. private function config($key, $type = 'get')
  314. {
  315. $config = array
  316. (
  317. # 文本
  318. 'text' => array
  319. (
  320. # 设置获取的key 应答
  321. 'get' => 'Content',
  322. # 设置赋值的key 回复
  323. 'set' => 'Content',
  324. ),
  325. # 图片
  326. 'image' => array
  327. (
  328. 'get' => array('PicUrl', 'MediaId'),
  329. 'set' => array
  330. (
  331. 'Image' => array('MediaId' => 'pic')
  332. ),
  333. ),
  334. # 语音
  335. 'voice' => array
  336. (
  337. 'get' => array('MediaId', 'Format'),
  338. 'set' => array
  339. (
  340. 'Voice' => array('MediaId' => 'mp3')
  341. ),
  342. ),
  343. # 视频
  344. 'video' => array
  345. (
  346. 'get' => array('MediaId', 'ThumbMediaId'),
  347. 'set' => array
  348. (
  349. 'Voice' => array('MediaId' => 'mp4', 'Title' => 'title', 'Description' => 'info')
  350. ),
  351. ),
  352. # 小视频
  353. 'shortvideo' => array
  354. (
  355. 'get' => array('MediaId', 'ThumbMediaId'),
  356. 'set' => false,
  357. ),
  358. # 地理位置
  359. 'location' => array
  360. (
  361. 'get' => array('Location_X', 'Location_Y', 'Scale', 'Label'),
  362. 'set' => false,
  363. ),
  364. # 链接消息
  365. 'link' => array
  366. (
  367. 'get' => array('Title', 'Description', 'Url', 'Label'),
  368. 'set' => false,
  369. ),
  370. # 单图文消息
  371. 'new' => array
  372. (
  373. 'get' => false,
  374. 'set' => array
  375. (
  376. 'ArticleCount' => 1,
  377. 'Articles' => array
  378. (
  379. 'item' => array('Title' => 'title', 'Description' => 'info', 'PicUrl' => 'pic', 'Url' => 'link')
  380. )
  381. ),
  382. ),
  383. # 多图文消息
  384. 'news' => array
  385. (
  386. 'get' => false,
  387. 'set' => array
  388. (
  389. 'ArticleCount' => 'total',
  390. 'Articles' => array
  391. (
  392. 'item' => array('Title' => 'title', 'Description' => 'info', 'PicUrl' => 'pic', 'Url' => 'link')
  393. )
  394. ),
  395. ),
  396. # 音乐消息
  397. 'music' => array
  398. (
  399. 'get' => false,
  400. 'set' => false,
  401. ),
  402. # 事件消息,所有事件返回的字段都在此做说明
  403. 'event' => array
  404. (
  405. 'get' => array
  406. (
  407. 'Event' => '事件',
  408. 'EventKey' => '事件ID',
  409. 'Latitude' => '纬度',
  410. 'Longitude' => '经度',
  411. 'Precision' => '精度',
  412. 'Ticket' => '二维码',
  413. 'MsgID' => '消息ID',
  414. 'Status' => '状态',
  415. 'TotalCount' => '粉丝数',
  416. 'FilterCount' => '过滤数',
  417. 'SentCount' => '成功数',
  418. 'ErrorCount' => '失败数',
  419. ),
  420. 'set' => false,
  421. ),
  422. );
  423. return $config[$key][$type];
  424. }
  425. /**
  426. * 获取当前时间
  427. *
  428. * @return mixed
  429. */
  430. private function time()
  431. {
  432. return time();
  433. }
  434. /**
  435. * 拼装xml相应代码
  436. *
  437. * @return mixed
  438. */
  439. private function xml($content)
  440. {
  441. $xml = "<xml>\r\n"
  442. .$this->tag('ToUserName', $this->result['add_from']) . "\r\n"
  443. .$this->tag('FromUserName', $this->result['add_to']) . "\r\n"
  444. .$this->tag('CreateTime', $this->time()) . "\r\n"
  445. .$this->tag('MsgType', $this->result['type']) . "\r\n"
  446. .$content . "\r\n"
  447. .'</xml>';
  448. return $xml;
  449. }
  450. /**
  451. * 生成tag
  452. *
  453. * @return mixed
  454. */
  455. private function tag($tag, $value = '')
  456. {
  457. if (!is_numeric($value) && strpos($value, '<') === false) {
  458. $value = '<![CDATA['.$value.']]>';
  459. }
  460. return '<'.$tag.'>'.$value.'</'.$tag.'>';
  461. }
  462. }