流程 :
服务端请求微信生成config相关参数
客户端ajax请求服务器获取参数
config.php
index.php
url为当前调用jssdk的完整网址,前端url编码
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
appId = $appId;
$this->appSecret = $appSecret;
}
public function getSignPackage($url) {
$jsapiTicket = $this->getJsApiTicket();
$timestamp = time();
$nonceStr = $this->createNonceStr();
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$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接口列表
});
},
})