<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script language="javascript" type="text/javascript">
wx.config({
debug: false,//這里是開啟測試,如果設置為true,則打開每個步驟,都會有提示,是否成功或者失敗
appId: '你的appid',
timestamp: '14999923234',//這個一定要與上面的php代碼里的一樣。
nonceStr: '14999923234',//這個一定要與上面的php代碼里的一樣。
signature: '<?=jssdk();?>',
jsApiList: [
// 所有要調用的 API 都要加到這個列表中
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo'
]
});
wx.ready(function(){
wx.onMenuShareTimeline({
title: "這里是草果網", // 分享標題
link: "http://www.icaoguo.com/", // 分享鏈接
imgUrl: "http://icaoguo.com/upfile/banner1.png", // 分享圖標
success: function () {
alert('成功');
// 用戶確認分享后執行的回調函數
},
cancel: function () {
alert('取消');
// 用戶取消分享后執行的回調函數
}
});
wx.onMenuShareAppMessage({
title: "<?=$act['act_name']?>", // 分享標題
desc: "<?=substr($act['act_stime'],0,10)?><?=$act['act_place']?>", // 分享描述
link: "http://www.brandhd.com/v/events/view/<?=$act['act_id']?>", // 分享鏈接
imgUrl: "http://www.brandhd.com<?=$act['act_poster_small']?>", // 分享圖標
type: '', // 分享類型,music、video或link,不填默認為link
dataUrl: '', // 如果type是music或video,則要提供數據鏈接,默認為空
success: function () {
// 用戶確認分享后執行的回調函數
},
cancel: function () {
// 用戶取消分享后執行的回調函數
}
});
wx.onMenuShareQQ({
title: "<?=$act['act_name']?>", // 分享標題
desc: "<?=substr($act['act_stime'],0,10)?>\n<?=$act['act_place']?>", // 分享描述
link: "http://www.brandhd.com/v/events/view/<?=$act['act_id']?>", // 分享鏈接
imgUrl: "http://www.brandhd.com<?=$act['act_poster_small']?>", // 分享圖標
success: function () {
// 用戶確認分享后執行的回調函數
},
cancel: function () {
// 用戶取消分享后執行的回調函數
}
});
wx.onMenuShareWeibo({
title: "<?=$act['act_name']?>", // 分享標題
desc: "<?=substr($act['act_stime'],0,10)?>\n<?=$act['act_place']?>", // 分享描述
link: "http://www.brandhd.com/v/events/view/<?=$act['act_id']?>", // 分享鏈接
imgUrl: "http://www.brandhd.com<?=$act['act_poster_small']?>", // 分享圖標
success: function () {
// 用戶確認分享后執行的回調函數
},
cancel: function () {
// 用戶取消分享后執行的回調函數
}
});
});
</script>
</body>
</html>
<?php
function jssdk(){
$appid = '你的appid';
$secret = '你的secret';
$_title = '微信';$accesstoken = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."");//獲取access_token
$token = json_decode($accesstoken); //對JSON格式的字符串進行編碼
$t = get_object_vars($token);//轉換成數組
$access_token = $t['access_token'];//輸出access_token
$jsapi = file_get_contents("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi");
$jsapi = json_decode($jsapi);
$j = get_object_vars($jsapi);
$jsapi = $j['ticket'];//get JSAPI
$time = 14999923234;
$noncestr= $time;
$jsapi_ticket= $jsapi;
$timestamp=$time;
$url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$and = "jsapi_ticket=".$jsapi_ticket."&noncestr=".$noncestr."×tamp=".$timestamp."&url=".$url."";
$signature = sha1($and);
return $signature;
}
?>
不生效的話,要去微信公眾號平台設置JS安全域名。
