文本回复有个xml结构,根据对应的位置填写内容,然后格式化输出,
ToUserName 位置上填写的是$fromUsername = $postObj->FromUserName,就是把消息返回给发送信息过来的用户,即接收方账号。
FromUserName 位置上填写的是$toUsername = $postObj->ToUserName,既是开发者微信号。
实例化它的responseMsg() 方法就可以回复自定义的消息了。
/**
* @descrpition 文本
* @param $request
* @return array
*/
public static function text(&$request){
// $content = '收到文本消息';
// return ResponsePassive::text($request['fromusername'], $request['tousername'], $content);
$mp_id = $_GET['id'];
$content = $request['content'];
$where['mp_id'] = $mp_id;
$where['keyword'] = $content;
$data = M('mp_rule')->where($where)->find();
if($data){
$reply_id = $data['reply_id'];
$type = $data['type'];
switch ($type) {
case 'text':
$reply = M('mp_reply_text')->find($reply_id);
if($reply){
$reply_text = $reply['content'];
}else{
$reply_text = "出错啦";
}
return ResponsePassive::text($request['fromusername'], $request['tousername'], $reply_text);
break;
case 'image':
$reply = M('mp_reply_image')->find($reply_id);
if($reply){
$media_id = $reply['media_id'];
return ResponsePassive::image($request['fromusername'],$request['tousername'],$media_id);
}else{
$reply_text = "出错啦";
return ResponsePassive::text($request['fromusername'], $request['tousername'], $reply_text);
}
break;
default:
return 'success';
break;
}
}
return 'success';
}
添加关键字
public function addText(){
$content = I('post.content');
$data['content'] = $content;
$ret = M('mp_reply_text')->add($data);
if(isset($ret)){
$mp = getCurrentMp();
$arr['mp_id'] = $mp['id'];
$arr['reply_id'] = $ret;
$keyword = I('post.keyword');
$arr['keyword'] = $keyword;
$arr['type'] = 'text';
$ret = M('mp_rule')->add($arr);
$this->ajaxReturn(array('msg'=>'添加成功'));
exit;
}else{
$this->ajaxReturn(array('msg'=>'添加失败'));
}
}
测试代码
在主文件中引入回复文本的函数文件
图片