php生成微信jssdk wx.config參數,調用js接口

流程 :

服務端請求微信生成config相關參數

客戶端ajax請求服務器獲取參數

config.php

<?php
$appid = 'wx8e66f8993d';
$appsecret = '42190267910b765d37';

        
        

index.php

url爲當前調用jssdk的完整網址,前端url編碼

<?php
include 'config.php';
include 'JSSDK.php';
$jssdk = new JSSDK($appid, $appsecret);
$url = $_GET['url'];
$signPackage = $jssdk->getSignPackage($url);
$config =  array(
    'debug' => true,
    'appId' => $signPackage['appId'],
    'timestamp' => $signPackage['timestamp'],
    'nonceStr' => $signPackage['nonceStr'],
    'signature' => $signPackage['signature'],
    'jsApiList' => array(
        'checkJsApi',
        'updateTimelineShareData',
        'hideOptionMenu',
        'updateAppMessageShareData',
        'hideMenuItems',
        'showMenuItems'
    )
);
echo json_encode(['code' => 0,'data' => $config ,'msg' => 'ok']);
exit;

JSSDK.php

<?php
/**
 * @auther: xxf
 * Date: 2019/11/8
 * Time: 15:59
 */
class JSSDK {
    private $appId;
    private $appSecret;
    public function __construct($appId, $appSecret) {
        $this->appId = $appId;
        $this->appSecret = $appSecret;
    }

    public function getSignPackage($url) {
        $jsapiTicket = $this->getJsApiTicket();
        $timestamp = time();
        $nonceStr = $this->createNonceStr();
        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";

        $signature = sha1($string);

        $signPackage = array(
            "appId"     => $this->appId,
            "nonceStr"  => $nonceStr,
            "timestamp" => $timestamp,
            "url"       => $url,
            "signature" => $signature,
            "rawString" => $string
        );
        return $signPackage;
    }

    private function createNonceStr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    private function getJsApiTicket() {
        $file = 'jsapi_ticket.json';
        if(!file_exists($file)){
            file_put_contents($file, '');
        }
        $data = json_decode(file_get_contents($file));
        if (empty($data) || $data->expire_time < time()) {
            $accessToken = $this->getAccessToken();
            $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
            $res = json_decode($this->httpGet($url));
            $ticket = $res->ticket;
            if ($ticket) {
                $data = new stdClass();
                $data->expire_time = time() + 4000;
                $data->jsapi_ticket = $ticket;
                file_put_contents($file, json_encode($data));
            }
        } else {
            $ticket = $data->jsapi_ticket;
        }

        return $ticket;
    }

    private function getAccessToken() {
        $file = 'access_token.json';
        if(!file_exists($file)){
            file_put_contents($file, '');
        }
        $data = json_decode(file_get_contents($file));
        if (empty($data) || $data->expire_time < time()) {
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
            $res = json_decode($this->httpGet($url));
            $access_token = $res->access_token;
            if ($access_token) {
                $data = new stdClass();
                $data->expire_time = time() + 4000;
                $data->access_token = $access_token;
                file_put_contents($file, json_encode($data));
            }
        } else {
            $access_token = $data->access_token;
        }
        return $access_token;
    }

    private function httpGet($url) {
        return file_get_contents($url);
    }
}

 

客戶端調用示例

$.ajax({
                        type: "GET",
                        url: "/jssdk/?url=" + encodeURI(location.href),
                        dataType: "json",
                        data: {},
                        success: function (res) {
                            console.log(res);

                            wx.config({
                                debug: true, // 開啓調試模式,調用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數,可以在pc端打開,參數信息會通過log打出,僅在pc端時纔會打印。
                                appId: res.data.appId, // 必填,公衆號的唯一標識
                                timestamp: res.data.timestamp, // 必填,生成簽名的時間戳
                                nonceStr: res.data.nonceStr, // 必填,生成簽名的隨機串
                                signature: res.data.signature,// 必填,簽名
                                jsApiList: res.data.jsApiList// 必填,需要使用的JS接口列表
                            });

                        },
                    })

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章