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 = '
1442067297
1311112322222222
';
/*
$post = '
123456789
';
$post = '
1442067297
111
';
$post = '
1442067297
23123213
';
$post = '
123456789
';
*/
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 = "\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"
.'';
return $xml;
}
/**
* 生成tag
*
* @return mixed
*/
private function tag($tag, $value = '')
{
if (!is_numeric($value) && strpos($value, '<') === false) {
$value = '';
}
return '<'.$tag.'>'.$value.''.$tag.'>';
}
}