微信公众号(二)config接口注入权限验证配置-signature无效的情况

官方文档

config接口验证配置
里面的封装的httpGet()请看https://blog.csdn.net/y_z_w123/article/details/81456540

注意点:1.JS接口安全域名 一定要是展示页面的域名(不需要http:// 或者 https://)。2.获取签名的随机字符串和时间戳一定要和config验证配置里面的一样 。3.获取前端页面的地址不用做处理。

第一步:引入必要的文件

JQuery:https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
JSSDK: http://res.wx.qq.com/open/js/jweixin-1.4.0.js

第二步:客户端的书写


第三步:服务端

# 请求是要请求 signature() 这个方法
public function signature()
    {
        $url = $_GET['url'];
        $time = time();
        #生成的随机字符串
        $nonceStr = $this->nonceStr();
        #制作签名的四个参数
        $params = array('url'=>$url,'noncestr'=>$nonceStr,'jsapi_ticket'=>$this->getJsapi(),'timestamp'=>$time);
        $link = $this->getSign($params);
        $signature = sha1($link);
        #前端config需要注入配置的参数
        $data = json_encode(array('debug'=>true,
            'appId'=>'xxxxxxxxxxxx','timestamp'=>$time,
            'nonceStr'=>$nonceStr,'signature'=>$signature,'jsApiList'=>array( "menuItem:share:appMessage")));
        return $data;
    }
    #将取到的值以键值对方式拼接
    public function getSign($params)
    {
        ksort($params);
        $str = "";
        foreach ($params as $k => $v){
            $str .= $k . "=" . $v . "&";
        }
        $data = substr($str,0,-1);
        return $data;
    }
# 随机字符串的生成
    public function nonceStr($length = 16)
    {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str   = "";
        for ($i = 0;$i < $length;$i++){
            $str.= substr($chars,mt_rand(0,strlen($chars)-1),1);
        }
        return $str;
    }
    #获取access_token 不是网页授权的access_token
	[获取access_token的方法](https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183)
    public function signToken()
    {
        $key    = 'xxxxxxxxxxxxxxxxxxx';
        $secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';
        $url =  "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$key."&secret=".$secret."";
        $data = json_decode($this->httpGet($url),true);
        return $data['access_token'];
    }
	#这里的access_token做了存文件的处理 你也可以选择进行存库定时刷新 3-5分钟刷新一次(linux的定时任务)
    public function getToken()
    {
        $dir = dirname($_SERVER['DOCUMENT_ROOT']).'/storage/framework/cache/';
        $filename = 'web_token.json';
        $files = $dir.$filename;
        if(!file_exists($files))
        {
            $data['access_token'] = $this->signToken();
            $data['expires']=time()-7000;
            $jsonStr =  json_encode($data);
            $fp = fopen($files, "w");
            fwrite($fp, $jsonStr);
            fclose($fp);
        }
        $file = file_get_contents($files,true);
        $result = json_decode($file,true);
        if (time() > $result['expires']){
            $data['access_token'] = $this->signToken();
            $data['expires']=time()+7000;
            $jsonStr =  json_encode($data);
            $fp = fopen($files, "w");
            fwrite($fp, $jsonStr);
            fclose($fp);
            return $data['access_token'];
        }else{
            return $result['access_token'];
        }
    }
	#这里的Ticket做了存文件的处理 你也可以选择进行存库定时刷新 3-5分钟刷新一次(linux的定时任务)
    public function getTicket()
    {
        #获取access_token的链接https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183
        $token = $this->getToken();
        $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$token."&type=jsapi";
        $ticket = json_decode($this->httpGet($url),true);
        return $ticket['ticket'];
    }
    public function getJsapi()
    {
        $dir = dirname($_SERVER['DOCUMENT_ROOT']).'/storage/framework/cache/';
        $filename = 'ticket.json';
        $files = $dir.$filename;
        if(!file_exists($files))
        {
            $data['access_token'] = $this->getTicket();
            $data['expires']=time()-7000;
            $jsonStr =  json_encode($data);
            $fp = fopen($files, "w");
            fwrite($fp, $jsonStr);
            fclose($fp);
        }
        $file = file_get_contents($files,true);
        $result = json_decode($file,true);
        if (time() > $result['expires']){
            $data['access_token'] = $this->getTicket();
            $data['expires']=time()+7000;
            $jsonStr =  json_encode($data);
            $fp = fopen($files, "w");
            fwrite($fp, $jsonStr);
            fclose($fp);
            return $data['access_token'];
        }else{
            return $result['access_token'];
        }
    }

效果图: 要把config 的debug 设置为true看打印信息

微信公众号(二)config接口注入权限验证配置-signature无效的情况_第1张图片

你可能感兴趣的:(微信公众号)