Receive.php 11 KB

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