微信公众号开发

首先用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;
        }

 







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值