rh_mp_reply_image(用来存放回复的图片)
rh_mp_reply_news(用来存放回复的消息)
rh_mp_rule(用来存放关键字,回复的类型等)
应先写三种方法(replytext,replyimage,replynews),将一些图文素材上传到数据库
当然再次在前还需要写 upload方法
public function upload(){
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 3145728 ;// 设置附件上传大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->rootPath = './Uploads/'; // 设置附件上传根目录
$upload->savePath = ''; // 设置附件上传(子)目录
// 上传文件
$info = $upload->uploadOne($_FILES['file']);
if(!$info) {// 上传错误提示错误信息
// $this->error($upload->getError());
$this->ajaxReturn(array('code'=>1,'msg'=>$upload->getError()));
}else{// 上传成功
// $this->success('上传成功!');
$file = '/Uploads/'.$info['savepath'].$info['savename'];
$this->ajaxReturn(array('code'=>0,'msg'=>'上传成功','url'=>$file));
}
下面是回复文本,图片以及消息的方法,一起来看一下吧!
//回复文本信息
public function replytext(){
if(IS_GET){
$this->display();
}else{
$content=I('post.content');
$keyword=I('post.keyword');
$model=M('mp_reply_text');
$data['content']=$content;
$reply_id=$model->add($data);
if(isset($reply_id)){
$mp=getCurrentMp();
$data['mp_id']=$mp['id'];
$data['type']='text';
$data['keyword']=$keyword;
$data['reply_id']=$reply_id;
M('mp_rule')->add($data);
$this->ajaxReturn(array('msg'=>'添加成功!'));
}else{
$this->ajaxReturn(array('msg'=>'添加失败!'));
}
}
}
//选择本地图片,需上传到数据库
public function replyimage(){
if(IS_GET){
$this->display();
}else{
$url = I('post.url');//图片在本服务器上传路径
$file = realpath('.'.$url); //相对路径->绝对路径
$accessToken = getAccess_token();
include APP_PATH .'LaneWeChat/lanewechat.php';
//永久素材
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image";
$data['media'] = Curl::addFile($file);
$ret = Curl::callWebServer($url,$data,'post',true,false);
if(isset($ret['media_id'])){
$mp=getCurrentMp();
$data['url']=$url;
$data['media_id']=$ret['media_id'];
$model=M('mp_reply_image');
$reply_id=$model->add($data);
$keyword=I('post.keyword');
if(isset($reply_id)){
$data['mp_id']=$mp['id'];
$data['type']='image';
$data['keyword']=$keyword;
$data['reply_id']=$reply_id;
M('mp_rule')->add($data);
$this->ajaxReturn(array('msg'=>'添加成功!'));
}
else{
$this->ajaxReturn(array('msg'=>'添加失败!'));
}
}else{
$this->ajaxReturn(array('msg'=>'添加失败!'));
}
$this->ajaxReturn($ret);
}
}
public function replynews(){
if(IS_GET){
$this->display();
}else{
$picurl = I('post.url');//图片在本服务器上传路径
$file = realpath('.'.$picurl); //相对路径->绝对路径
// echo $file;
// exit;
$accessToken = getAccess_token();
include APP_PATH .'LaneWeChat/lanewechat.php';
//永久素材
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image";
$data['media'] = Curl::addFile($file);
$ret = Curl::callWebServer($url,$data,'post',true,false);
if(isset($ret['media_id'])){
$mp=getCurrentMp();
$data['title']=I('post.title');
$data['description']=I('post.content');
$data['picurl']=$url;
$data['url']=I('post.content_source_url');
$data['media_id']=$ret['media_id'];
$model=M('mp_reply_news');
$reply_id=$model->add($data);
$keyword=I('post.keyword');
if(isset($reply_id)){
$data['mp_id']=$mp['id'];
$data['type']='news';
$data['keyword']=$keyword;
$data['reply_id']=$reply_id;
M('mp_rule')->add($data);
$this->ajaxReturn(array('msg'=>'添加成功!'));
}
else{
$this->ajaxReturn(array('msg'=>'添加失败!'));
}
}else{
$this->ajaxReturn(array('msg'=>'添加失败!'));
}
$this->ajaxReturn($ret);
}
}