微信jssdk的官方文檔 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
JSSDK使用步驟
步驟一:綁定域名
先登錄微信公眾平臺(tái)進(jìn)入“公眾號(hào)設(shè)置”的“功能設(shè)置”里填寫“JS接口安全域名”慎玖。
備注:登錄后可在“開發(fā)者中心”查看對(duì)應(yīng)的接口權(quán)限碉哑。
步驟二:引入JS文件
在需要調(diào)用JS接口的頁面引入如下JS文件,(支持https):http://res.wx.qq.com/open/js/jweixin-1.4.0.js
如需進(jìn)一步提升服務(wù)穩(wěn)定性碍沐,當(dāng)上述資源不可訪問時(shí)身隐,可改訪問:http://res2.wx.qq.com/open/js/jweixin-1.4.0.js (支持https)。
備注:支持使用 AMD/CMD 標(biāo)準(zhǔn)模塊加載方法加載
步驟三:通過config接口注入權(quán)限驗(yàn)證配置
所有需要使用JS-SDK的頁面必須先注入配置信息,否則將無法調(diào)用(同一個(gè)url僅需調(diào)用一次,對(duì)于變化url的SPA的web app可在每次url變化時(shí)進(jìn)行調(diào)用,目前Android微信客戶端不支持pushState的H5新特性吮便,所以使用pushState來實(shí)現(xiàn)web app的頁面會(huì)導(dǎo)致簽名失敗,此問題會(huì)在Android6.2中修復(fù))幢踏。
wx.config({
debug: true, // 開啟調(diào)試模式,調(diào)用的所有api的返回值會(huì)在客戶端alert出來髓需,若要查看傳入的參數(shù),可以在pc端打開惑折,參數(shù)信息會(huì)通過log打出授账,僅在pc端時(shí)才會(huì)打印枯跑。
appId: '', // 必填惨驶,公眾號(hào)的唯一標(biāo)識(shí)
timestamp: , // 必填,生成簽名的時(shí)間戳
nonceStr: '', // 必填敛助,生成簽名的隨機(jī)串
signature: '',// 必填粗卜,簽名
jsApiList: [] // 必填,需要使用的JS接口列表
});
php代碼:
public function actionSqrcode()
{
//時(shí)間戳
$wx['timestamp'] = time();
//生成簽名的隨機(jī)串
$wx['noncestr'] = md5(time());
//jsapi_ticket是公眾號(hào)用于調(diào)用微信JS接口的臨時(shí)票據(jù)纳击。正常情況下续扔,jsapi_ticket的有效期為7200秒攻臀,通過access_token來獲取。
$wx['jsapi_ticket'] = $this->actionTicket();
//分享的地址纱昧,注意:這里是指當(dāng)前網(wǎng)頁的URL刨啸,不包含#及其后面部分,曾經(jīng)的我就在這里被坑了识脆,所以小伙伴們要小心了
$wx['url'] = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$string = sprintf("jsapi_ticket=%s&noncestr=%s×tamp=%s&url=%s", $wx['jsapi_ticket'], $wx['noncestr'], $wx['timestamp'], $wx['url']);
//生成簽名
$wx['signature'] = sha1($string);
return $wx;
}
public function actionAccessToken(){
$file = file_get_contents('./token');
$info = json_decode($file,1);
if ($info && $info['time'] > time())
return $info['access_token'];
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的微信秘鑰";
$info = file_get_contents($url);
$info = json_decode($info,1);
if ($info){
$info['time'] = time()+$info['expires_in'];
file_put_contents('./token',json_encode($info));
return $info['access_token'];
}else{
return '失敗';
}
}
public function actionTicket()
{
$file = file_get_contents('./ticket');
$info = json_decode($file,1);
if ($info && $info['time'] > time())
return $info['ticket'];
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$this->actionAccessToken()."&type=jsapi";
$info = file_get_contents($url);
$info = json_decode($info,1);
if ($info){
$info['time'] = time()+$info['expires_in'];
file_put_contents('./ticket',json_encode($info));
return $info['ticket'];
}else{
return '失敗';
}
}
public function actionCurl($url,$data,$type = 'json'){
if($type=='json'){//json $_POST=json_decode(file_get_contents('php://input'), TRUE);
$headers = array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
$data=json_encode($data);
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
js代碼:
<script>
$(function(){
$.ajax({
type:"get",
url:"actionSqrcode",
async:true,
success:function(res){
wx.config({
debug : false,
appId : 'res.appid',
timestamp : "res.timestamp",
nonceStr : "res.noncestr",
signature : "res.signature",
jsApiList : [
'updateTimelineShareData',
]
});
wx.ready(function () { //需在用戶可能點(diǎn)擊分享按鈕前就先調(diào)用
wx.updateTimelineShareData({
title: 'xx注冊(cè)領(lǐng)好禮', // 分享標(biāo)題
link: "http://www.xxx.com", // 分享鏈接设联,該鏈接域名或路徑必須與當(dāng)前頁面對(duì)應(yīng)的公眾號(hào)JS安全域名一致
imgUrl: 'http://www.xxx.com/123.jpg', // 分享圖標(biāo)
success: function (r) {
// 設(shè)置成功
}
});
});
}
});
})
</script>