在微擎呼叫微信JSSDK實現分享功能
阿新 • • 發佈:2018-12-11
最近遇到一個專案是基於微擎框架做一個分享功能的,微擎本身是自帶分享功能的,只是這次想要實現自定義分享內容,故進行了以下程式碼處理
* $_W[‘account’][‘jssdkconfig’]是微擎封裝好的jssdk簽名包的內容
* php頁面程式碼
<?php
if (!defined('IN_IA')) {
exit('Access Denied');
}
global $_W, $_GPC;
$shareCon = array(
'title' ='分享標題',
'link' => 'www.baidu.com',
'imgUrl' => '../addons/ewei_shop/static/images/404.png',
'desc' => '測試分享內容'
);
$signPackage = $_W['account']['jssdkconfig'];
echo '<pre>';
print_r($_W['account']['jssdkconfig']);
include $this ->template('diyposter/test');
?>
- html頁面程式碼
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>
wx.config({
debug: false,
appId: '{$signPackage["appId"]}',
timestamp: '{$signPackage["timestamp"]}',
nonceStr: '{$signPackage["nonceStr"]}' ,
signature: '{$signPackage["signature"]}',
jsApiList: [
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
'onMenuShareQZone'
]
});
wx.ready(function(){
// 分享朋友圈
wx.onMenuShareTimeline({
title:'{$shareCon["title"]}',
link:window.location.href,
imgUrl:'{php echo tomedia($shareCon["imgUrl"]);}',
success:function(){
//傳送成功的方法
alert('已分享');
},
cancel:function(){
//傳送取消的方法
alert('已取消');
}
});
// 分享給朋友
wx.onMenuShareAppMessage({
title:'{$shareCon["title"]}',
desc:'{$shareCon["desc"]}',
link:window.location.href,
imgUrl:'{php echo tomedia($shareCon["imgUrl"]);}',
success:function(){
//傳送成功的方法
alert('已分享');
},
cancel:function(){
//傳送取消的方法
alert('已取消');
}
});
})
</script>