Receive.php 11 KB

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