123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- <?php
- /*
- |--------------------------------------------------------------------------
- | receive.php 用于接收微信发过来的信息
- |--------------------------------------------------------------------------
- */
- namespace Component\Service;
- use Dever;
- use Main\Lib\Wechat;
- class Receive
- {
- const TYPE = 5;
- /**
- * wechat
- *
- * @var Wechat
- */
- private $wechat;
-
- /**
- * result
- *
- * @var array
- */
- private $result;
-
- /**
- * output
- *
- * @var string
- */
- private $output;
-
- /**
- * 构造函数 初始化
- *
- * @return mixed
- */
- public function __construct()
- {
- $appid = Dever::input('appid');
- $project = false;
- if ($appid) {
- $info = Dever::db('main/project')->one(array('option_type' => self::TYPE, 'option_appid' => $appid));
- if ($info) {
- $project = $info['id'];
- }
- }
- $this->wechat = new Wechat('component', $project);
- }
-
- /**
- * 业务推送接口 微信服务器会将所有请求都推送到这里
- *
- * @return mixed
- */
- public function main()
- {
- $this->output = '';
-
- # 获取微信消息
- $this->request();
-
- # 对消息进行验证,并根据类型得到本平台的配置数据
- //$this->response();
-
- # 输出
- $this->output();
- }
-
- /**
- * 输出
- *
- * @return mixed
- */
- private function output()
- {
- if (!$this->output) {
- $this->output = 'success';
- }
- echo $this->output;die;
- }
-
- /**
- * 据类型得到本平台的配置数据
- *
- * @return mixed
- */
- private function response()
- {
- if ($this->output) {
- return;
- }
-
- if ($this->result && isset($this->result['add_type'])) {
- if (isset($this->result['subscribe'])) {
- if ($this->result['subscribe'] == true) {
- # 订阅成功 发送消息
- $this->msg('msg1');
- # 发送消息之后,往用户表更新信息
- $this->user();
- $this->setSubscribe(1);
- }
- if ($this->result['subscribe'] == false) {
- # 取消订阅 不发送消息
- //$this->msg('msg1');
- # 改变当前用户的订阅状态
- $this->setSubscribe(2);
- }
- } elseif (isset($this->result['add_content'][0]['Content'])) {
- $this->match($this->result['add_content'][0]['Content']);
- } else {
- # 如果没有匹配到,则直接返回默认回复消息
- $this->msg('msg2');
- }
- }
- }
- /**
- * 获取微信发送的内容
- *
- * @return mixed
- */
- private function request()
- {
- $post = isset($GLOBALS["HTTP_RAW_POST_DATA"]) ? $GLOBALS["HTTP_RAW_POST_DATA"] : false;
- /*
- $post = ' <xml>
- <ToUserName><![CDATA[toUser]]></ToUserName>
- <FromUserName><![CDATA[ow29VvzICifpOTq_9EDGT3AhYpqg]]></FromUserName>
- <CreateTime>1442067297</CreateTime>
- <MsgType><![CDATA[text]]></MsgType>
- <Content><![CDATA[dfd]]></Content>
- <MsgId>1311112322222222</MsgId>
- </xml>';
-
- /*
- $post = '<xml>
- <ToUserName><![CDATA[toUser]]></ToUserName>
- <FromUserName><![CDATA[FromUser]]></FromUserName>
- <CreateTime>123456789</CreateTime>
- <MsgType><![CDATA[event]]></MsgType>
- <Event><![CDATA[subscribe]]></Event>
- </xml>';
- $post = ' <xml>
- <ToUserName><![CDATA[toUser]]></ToUserName>
- <FromUserName><![CDATA[ow29VvzICifpOTq_9EDGT3AhYpqg]]></FromUserName>
- <CreateTime>1442067297</CreateTime>
- <MsgType><![CDATA[image]]></MsgType>
- <PicUrl><![CDATA[http://mmbiz.qpic.cn/mmbiz/OxUBpiaYgpHiaDaiccrsX3MrFEVtjIfEAZk8zoJDZ4gXWWW2CIFic6Dicqndu23wMgA4Icg3yE4SRZg6L5jqanu9kvQ/0]]></PicUrl>
- <MediaId><![CDATA[media_id]]></MediaId>
- <MsgId>111</MsgId>
- </xml>';
- $post = '<xml>
- <ToUserName><![CDATA[toUser]]></ToUserName>
- <FromUserName><![CDATA[ow29VvzICifpOTq_9EDGT3AhYpqg]]></FromUserName>
- <CreateTime>1442067297</CreateTime>
- <MsgType><![CDATA[voice]]></MsgType>
- <MediaId><![CDATA[media_id]]></MediaId>
- <Format><![CDATA[Format]]></Format>
- <MsgId>23123213</MsgId>
- </xml>';
- $post = '<xml>
- <ToUserName><![CDATA[toUser]]></ToUserName>
- <FromUserName><![CDATA[FromUser]]></FromUserName>
- <CreateTime>123456789</CreateTime>
- <MsgType><![CDATA[event]]></MsgType>
- <Event><![CDATA[CLICK]]></Event>
- <EventKey><![CDATA[EVENTKEY]]></EventKey>
- </xml>';
- */
- Dever::log(time());
- if (!empty($post)) {
- Dever::log($post);
- libxml_disable_entity_loader(true);
- $result = (array) simplexml_load_string($post, 'SimpleXMLElement', LIBXML_NOCDATA);
- $this->handle($result);
- } else {
- return false;
- }
- }
-
- /**
- * 对微信的内容做处理,得到最终结果
- *
- * @return mixed
- */
- private function handle($result)
- {
- return;
- $config = array
- (
- 'AppId' => 'add_appid',
- 'CreateTime' => 'add_create_date',
- 'InfoType' => 'add_type',
- 'ComponentVerifyTicket' => 'add_ticket',
- );
-
- foreach ($config as $k => $v) {
- $this->result($k, $v, $result);
- }
-
- # 事件处理
- $this->event();
-
- $this->save();
- }
-
- /**
- * 事件处理
- *
- * @return mixed
- */
- private function event()
- {
- if ($this->result['add_type'] == 'component_verify_ticket') {
-
- }
- }
-
- /**
- * 入库
- *
- * @return mixed
- */
- private function save()
- {
- $this->result['add_info'] = is_array($this->result['add_info']) ? implode(',', $this->result['add_info']) : $this->result['add_info'];
- $this->result['add_site'] = $this->core->site();
-
- # 入库 使用mid排重
- $info = array();
- if (isset($this->result['add_mid'])) {
- $info = Dever::load('weixin/usermsg-getOne', array('where_mid' => $this->result['add_mid'], 'where_site' => $this->result['add_site']));
- $this->result['id'] = $info['id'];
- }
- if (!$info) {
- $this->result['id'] = Dever::load('weixin/usermsg-insert', $this->result);
- # 记录点对点聊天记录 暂时不记录了
- /*
- if($this->result['add_type'] == 'text')
- {
- $update['add_state'] = 1;
- $update['add_openid'] = $this->result['add_from'];
- $update['add_usermsg_id'] = $this->result['id'];
- $update['add_account'] = -1;
- $update['add_type'] = $this->result['add_type'];
- $update['add_info'] = Dever::load('weixin/user.msg', $this->result['id']);
- Dever::load('weixin/usermsg_log-insert', $update);
- }
- */
- }
- }
-
- /**
- * 得到最终结果
- *
- * @return mixed
- */
- private function result($key, $index, $result, $name = '')
- {
- if (isset($result[$key])) {
- $value = trim($result[$key]);
- if ($index == 'add_type') {
- $this->type($value, $result);
- }
- if ($index == 'add_content') {
- //$key = $name ? $name : $key;
- $this->result[$index][0][$key] = $value;
- $this->info($key, $value);
- } elseif ($index == 'add_ctime') {
- $this->result[$index] = $value ? $value : time();
- } else {
- $this->result[$index] = $value;
- }
- }
- }
- /**
- * 根据状态得到用户发送的具体内容,为了响应及时,这里不做解析。
- *
- * @return mixed
- */
- private function info($key, $value)
- {
- $this->result['add_info'][] = $value;
- }
-
- /**
- * 得到type的值
- *
- * @return mixed
- */
- private function type($index, $result)
- {
- $type = $this->config($index);
- if (is_array($type)) {
- foreach ($type as $k => $v) {
- if (is_numeric($k)) {
- $k = $v;
- }
- $this->result($k, 'add_content', $result, $v);
- }
- } else {
- $this->result($type, 'add_content', $result);
- }
- }
-
- /**
- * 得到对应关系
- *
- * @return mixed
- */
- private function keywords($type)
- {
- # 根据keywords的type设定来设置对应关系
- $config = array
- (
- 1 => 'text',
- 2 => 'news',
- //3 => 'news',
- 4 => 'voice',
- 5 => 'video',
- 6 => 'image',
- );
-
- if (!isset($config[$type])) {
- return false;
- }
-
- $this->result['type'] = $config[$type];
-
- return $config[$type];
- }
- /**
- * 配置type类型
- *
- * @return mixed
- */
- private function config($key, $type = 'get')
- {
- $config = array
- (
- # 文本
- 'text' => array
- (
- # 设置获取的key 应答
- 'get' => 'Content',
- # 设置赋值的key 回复
- 'set' => 'Content',
- ),
- # 图片
- 'image' => array
- (
- 'get' => array('PicUrl', 'MediaId'),
- 'set' => array
- (
- 'Image' => array('MediaId' => 'pic')
- ),
- ),
- # 语音
- 'voice' => array
- (
- 'get' => array('MediaId', 'Format'),
- 'set' => array
- (
- 'Voice' => array('MediaId' => 'mp3')
- ),
- ),
- # 视频
- 'video' => array
- (
- 'get' => array('MediaId', 'ThumbMediaId'),
- 'set' => array
- (
- 'Voice' => array('MediaId' => 'mp4', 'Title' => 'title', 'Description' => 'info')
- ),
- ),
- # 小视频
- 'shortvideo' => array
- (
- 'get' => array('MediaId', 'ThumbMediaId'),
- 'set' => false,
- ),
- # 地理位置
- 'location' => array
- (
- 'get' => array('Location_X', 'Location_Y', 'Scale', 'Label'),
- 'set' => false,
- ),
- # 链接消息
- 'link' => array
- (
- 'get' => array('Title', 'Description', 'Url', 'Label'),
- 'set' => false,
- ),
- # 单图文消息
- 'new' => array
- (
- 'get' => false,
- 'set' => array
- (
- 'ArticleCount' => 1,
- 'Articles' => array
- (
- 'item' => array('Title' => 'title', 'Description' => 'info', 'PicUrl' => 'pic', 'Url' => 'link')
- )
- ),
- ),
- # 多图文消息
- 'news' => array
- (
- 'get' => false,
- 'set' => array
- (
- 'ArticleCount' => 'total',
- 'Articles' => array
- (
- 'item' => array('Title' => 'title', 'Description' => 'info', 'PicUrl' => 'pic', 'Url' => 'link')
- )
- ),
- ),
- # 音乐消息
- 'music' => array
- (
- 'get' => false,
- 'set' => false,
- ),
-
- # 事件消息,所有事件返回的字段都在此做说明
- 'event' => array
- (
- 'get' => array
- (
- 'Event' => '事件',
- 'EventKey' => '事件ID',
- 'Latitude' => '纬度',
- 'Longitude' => '经度',
- 'Precision' => '精度',
- 'Ticket' => '二维码',
- 'MsgID' => '消息ID',
- 'Status' => '状态',
- 'TotalCount' => '粉丝数',
- 'FilterCount' => '过滤数',
- 'SentCount' => '成功数',
- 'ErrorCount' => '失败数',
- ),
- 'set' => false,
- ),
- );
-
- return $config[$key][$type];
- }
- /**
- * 获取当前时间
- *
- * @return mixed
- */
- private function time()
- {
- return time();
- }
- /**
- * 拼装xml相应代码
- *
- * @return mixed
- */
- private function xml($content)
- {
- $xml = "<xml>\r\n"
- .$this->tag('ToUserName', $this->result['add_from']) . "\r\n"
- .$this->tag('FromUserName', $this->result['add_to']) . "\r\n"
- .$this->tag('CreateTime', $this->time()) . "\r\n"
- .$this->tag('MsgType', $this->result['type']) . "\r\n"
- .$content . "\r\n"
- .'</xml>';
- return $xml;
- }
- /**
- * 生成tag
- *
- * @return mixed
- */
- private function tag($tag, $value = '')
- {
- if (!is_numeric($value) && strpos($value, '<') === false) {
- $value = '<![CDATA['.$value.']]>';
- }
- return '<'.$tag.'>'.$value.'</'.$tag.'>';
- }
- }
|