同一个页面动态 动态对接验证(tp3.2)

版权声明:一切都是为了学习记录,随便转载。 https://blog.csdn.net/Gjanuary/article/details/80364605

要想使用微信公众号的一些功能前提是要对接,考虑到多个公众号的问题,所以需要动态根据url的中的参数进行服务器地址设置进行对接

 

就像这个一样 我们需要填写服务器地址才行。代码如下:

我这里面的wxid就是公众号添加到我这后台之后产生的id

<?php
namespace Home\Controller;
use Think\Controller;
// 连锁医院模块
// /index.php?m=home&c=validate&a=index访问格式
//define("TOKEN", "wxcode");
const TOKEN =  "wxcode";
class WxserverController extends Controller {
	// 所有的登陆和权限验证都在这里处理
	/*protected function _initialize(){
		// 登陆验证
		$object = new LoginandvalidateController();
        $flag = $object->index();
        if($flag){
        	$ss = $object->auth();
        	// 没有权限则显示权限认证页面
        	if(!$ss){
        		exit("请进行权限认证,联系电话:18201383130");
        	}else{
                $this->assign("userinfo",session("userinfo"));
            }
        }
    }*/

    // 用来分发处理逻辑
    public function distribute(){
        // 进行绑定验证
        // 进行查看是否已经验证如果已经验证则不需要重新验证
       
       
        // 获取此时的wxid 进行自定义菜单推送

        if(!isset($_GET['wxid']) || empty($_GET['wxid'])){
            exit('非法访问途径.');
        }else{
            $wxid = $_GET['wxid'];
            // 查找是否已经验证如果已经验证则直接直接进行下面的功能,没有验证的需要进行验证
            $res = M('userbindingwxconfig')->where('id ='.$wxid)->find();
            // 没有验证过
            if($res['validateStatus'] == 0 || empty($res['validateStatus'])){
                $this->valid();
            }else{
                // 关注回复信息推送
                $this->replayAuto();
                
            }
           
        }
      
    }
    public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }
 private function checkSignature()
    {
        // you must define TOKEN by yourself
    /*    if (!defined("TOKEN")) {
            throw new Exception('TOKEN is not defined!');
        }*/
        
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
                
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        
        if( $tmpStr == $signature ){
            // 表明验证成功需要验证状态进行更改成1
            $dataxn['validateStatus'] = 1;
            M('userbindingwxconfig')->where('id='.$_GET['wxid'])->save($dataxn);
            return true;
        }else{
            return false;
        }
    }
}

我的数据库是这个样子的:


当填写地址的时候直接这么填写就行:
1.服务器地址(URL)中输入:http://XXX.com/index.php?m=zz&c=wx&a=distribute&wxid=1111
2.令牌(Token)中输入:wxcode
3.消息加解密密钥(EncodingAESKey):点击随机生成一个即可
4.点击提交即可



猜你喜欢

转载自blog.csdn.net/Gjanuary/article/details/80364605