首先用layuide的页面进行改造。
再微信进行token口令配置
实现功能的类
public function index(){
$mp = M('mp')->order('id,name')->select();
$this->assign('data',$mp);
$this->display();
}
public function addMp(){
if (IS_GET) {
$this->display();
}else{
$arr = I('post.');
$User = D("mp");
if($User->create()){
$result = $User->add(); // 写入数据到数据库
if($result){
$this->success("数据添加成功");
}else{
$this->error("数据添加false");
}
}else{
$this->error($User->getError());
}
}
}
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->ajaxReturn(array('code'=>1,'msg'=>$upload->getError()));
}else{// 上传成功
$url = '/Uploads/'.$info['savepath'].$info['savename'];
$this->ajaxReturn(array('code'=>0,'msg'=>"upload success",'url'=>$url));
}
}
public function enter($id){
$mp = M('mp')->find($id);
$mp['url'] = 'http://'.$_SERVER['HTTP_HOST'].U('Mp/index/index',array('id'=>$mp['id']));
$this->assign('mp',$mp);
$this->display();
}
//
public function usemp($id){
$mp=M('mp');
$where['id']=$id;
$con1['is_use']=1;
$con2['is_use']=0;
$p['id']=array('neq',$id);
$data=$mp->where($where)->save($con1);
$data1=$mp->where($p)->save($con2);
$data=$mp->select();
$this->assign('data',$data);
$this->display('index');
公众号接口
//公众号接入的接口
//$id公众号的主键
// private $token;
public function index($id=''){
$mp = M('mp')->find($id);
$this->token = $mp['token'];
$echostr = I('get.echostr');
if(!empty($echostr)){
if($this->checkSignature()){
echo $echostr;
exit;
}
}else{
$xml = (array) simplexml_load_string(file_get_contents('php://input'), 'SimpleXMLElement', LIBXML_NOCDATA);
//将数组键名转换为小写
$request = array_change_key_case($xml, CASE_LOWER);
echo WechatRequest::switchType($request);
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = $this->token;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}