原文
微信分享網(wǎng)頁的時候,希望分享出來的鏈接是標題+描述+縮略圖,微信開發(fā)代碼示例里已提供了方法,但只適用于動態(tài)頁面。由于dedecms是生成了靜態(tài)文件,其實我想使用ajax獲取jssdk參數(shù)也能也能實現(xiàn)微信分享功能了,在這里分享給大家。
jssdk的步驟業(yè)務流程是這樣滴:
1:在微信公眾號后臺配置js 安全域名,即需要引入jssdk的頁面域名。 需要在域名根目錄下放置微信的檢測文件。
2:配置出ip白名單(可參考:http://www.idcxx.com/wx-125-1-1.html)
3:后臺開發(fā)人員生成簽名傳遞給前臺
4:前端頁面引入script方式 jssdk文件
5:通過ajax獲取config 配置,完成config 配置后即可使用jssdk的各項功能了。
再補充啰嗦一句:
確保你獲取用來簽名的url是動態(tài)獲取的,動態(tài)頁面可參見實例代碼中php的實現(xiàn)方式。
如果是html的靜態(tài)頁面在前端通過ajax將url傳到后臺簽名,前端需要用js獲取當前頁面除去'#'hash部分的鏈接(可用location.href.split('#')[0]獲取,
而且需要encodeURIComponent,后臺decodeURIComponent解碼),
因為頁面一旦分享,微信客戶端會在你的鏈接末尾加入其它參數(shù),
如果不是動態(tài)獲取當前鏈接,將導致分享后的頁面簽名失敗。
前臺HTML代碼示例:
<script src="http://外部站點引入/js/jquery-1.11.1.min.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
var url=encodeURIComponent(window.location.href.split('#')[0]);//分享成功之后再次分享,微信會再url附帶額外參數(shù)造成二次分享簽名錯誤,這里處理下避免config:invalid signature簽名失效
$.ajax({
type : "get",
url : "http://xxx/jssdk.php?url="+url,//替換網(wǎng)址,xxx根據(jù)自己jssdk文件位置修改
dataType : "jsonp",
jsonp: "callback",
jsonpCallback:"success_jsonpCallback",
success : function(data){
wx.config({
debug: true, // 開啟調(diào)試模式,調(diào)用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數(shù),可以在pc端打開,參數(shù)信息會通過log打出,僅在pc端時才會打印
appId: data.appId,
timestamp: data.timestamp,
nonceStr: data.nonceStr,
signature: data.signature,
jsApiList: [
"onMenuShareTimeline", //分享給好友
"onMenuShareAppMessage", //分享到朋友圈
"onMenuShareQQ", //分享到QQ
"onMenuShareWeibo" //分享到微博
]
});
},
error:function(data){
alert(JSON.stringify(data));
//alert("連接失??!");
}
});
wx.ready(function (){
var shareData = {
title: '標題',
desc: '簡介',//這里請?zhí)貏e注意是要去除html
link: '鏈接',
imgUrl: '標題圖'
};
wx.onMenuShareAppMessage(shareData);
wx.onMenuShareTimeline(shareData);
wx.onMenuShareQQ(shareData);
wx.onMenuShareWeibo(shareData);
});
</script>
修改服務端文件jssdk.php
<?php
$url = urldecode($_GET['url']);
class JSSDK {
private $appId;
private $appSecret;
private $url;
public function __construct($appId, $appSecret,$url) {
$this->appId = $appId;
$this->appSecret = $appSecret;
$this->url = $url;
}
public function getSignPackage() {
$jsapiTicket = $this->getJsApiTicket();
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
// $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url =$this->url;
$timestamp = time();
$nonceStr = $this->createNonceStr();
// 這里參數(shù)的順序要按照 key 值 ASCII 碼升序排序
$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() {
// jsapi_ticket 應該全局存儲與更新,以下代碼以寫入到文件中做示例
$data = json_decode(file_get_contents("jsapi_ticket.json"));
if ($data->expire_time < time()) {
$accessToken = $this->getAccessToken();
// 如果是企業(yè)號用以下 URL 獲取 ticket
// $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
$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->expire_time = time() + 7000;
$data->jsapi_ticket = $ticket;
$fp = fopen("jsapi_ticket.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
} else {
$ticket = $data->jsapi_ticket;
}
return $ticket;
}
private function getAccessToken() {
// access_token 應該全局存儲與更新,以下代碼以寫入到文件中做示例
$data = json_decode(file_get_contents("access_token.json"));
if ($data->expire_time < time()) {
// 如果是企業(yè)號用以下URL獲取access_token
// $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
$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->expire_time = time() + 7000;
$data->access_token = $access_token;
$fp = fopen("access_token.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
} else {
$access_token = $data->access_token;
}
return $access_token;
}
private function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
}
$jssdk = new JSSDK("公眾號ID", "公眾號密鑰",$url);//按照自己的公眾號填寫
$signPackage = $jssdk->GetSignPackage();
$tmp=json_encode(array ('appId'=>$signPackage["appId"],'timestamp'=>$signPackage["timestamp"],'nonceStr'=>$signPackage["nonceStr"],'signature'=>$signPackage["signature"],'url'=>$signPackage["url"]));
$callback = $_GET['callback'];
echo $callback.'('.$tmp.')';
exit;
?>
其它的按照動態(tài)頁面參數(shù)設置即可。
?
?
?
?
本文摘自 :https://blog.51cto.com/u