php
$appid = $this->config->item('appid');
$timestamp = time();
$nonceStr = substr(md5(rand(1000,9999)), 0,16);
$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$str = 'jsapi_ticket='.$this>get_ticket().'&noncestr='.$nonceStr.'×tamp='.$timestamp.'&url='.$url;
$signature = sha1($str);
$data = array();
$data['appid'] = $appid;
$data['nonceStr'] = $nonceStr;
$data['timestamp'] = $timestamp;
$data['signature'] = $signature;
$data['title'] = self::TITLE; // 标题
$data['desc'] = self::DESC; // 摘要
$data['imgUrl'] = self::IMGURL;// 分享图
$data['success'] = "shareDone()"; // 分享成功之后调用的函数
return $data;
html
单拎出来,做个模块,用到的地方直接引用
<!-- 微信分享 -->
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>
$(window).load(function(){
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: "<?=$wechatShare['appid']?>", // 必填,公众号的唯一标识
timestamp: "<?=$wechatShare['timestamp']?>" , // 必填,生成签名的时间戳
nonceStr: "<?=$wechatShare['nonceStr']?>", // 必填,生成签名的随机串
signature: "<?=$wechatShare['signature']?>",// 必填,签名,见附录1
jsApiList: [//需要使用的JS接口列表,分享默认这几个,如果有其他的功能比如图片上传之类的,需要添加对应api进来
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo'
]
});
share_config = {
"share": {
"title" : "<?=$wechatShare['title']?>",//分享卡片标题
"desc" : "<?=$wechatShare['desc']?>",//摘要,如果分享到朋友圈的话,不显示摘要。
"imgUrl": "<?=$wechatShare['imgUrl']?>",//分享图,默认当相对路径处理,所以使用绝对路径的的话,“http://”协议前缀必须在。
"link": "<?=$wechatShare['link']?>",//分享出去后的链接,这里可以将链接设置为另一个页面。
success:function(){ //分享成功后的回调函数
<?=$wechatShare['success']?>;
},
cancel: function () { // 用户取消分享后执行的回调函数
//alert("fail");
}
}
};
wx.ready(function () {
wx.onMenuShareAppMessage(share_config.share);//分享给好友
wx.onMenuShareTimeline(share_config.share);//分享到朋友圈
});
})
</script>
分享成功之后:
function shareDone(){
}