1、首先下载SDK包:
下载路径:https://pan.baidu.com/s/10LTgdf6dIsyD8aSb6lawlw
提取码:nq3h
2、登录微信公众平台:
设置JS接口安全域名:
3、以Thinkphp3.2.3 为例,将包放入Verdor 文件夹中,并将插件取名为:jssdk
4、控制器:
public function read(){
Vendor ('jssdk.jssdk');
$jssdk = new \JSSDK ('你的APPID','你的APPserver');
$signPackage = $jssdk->GetsignPackage ();
// 获取文章主键 ID
$Int_id = I('id',0,'intval');
if($Int_id === 0){
$this->error('ID 错误');
}
$ArticleModel = D('NewsArticle');
// 文章点击数自增一
$ArticleModel->SetInc(['id'=>$Int_id]);
$data = $ArticleModel->getOne(['id'=>$Int_id]);
$this->assign('data',$data);
$this->assign ( 'signPackage', $signPackage );
$this->display('article');
}
5、前端页面
<?php
$arr = array(
'appId' => $signPackage["appId"],
'nonceStr' => $signPackage["nonceStr"],
'timestamp' => $signPackage["timestamp"],
'signature' => $signPackage["signature"]);
?>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
var url=window.location.href;
var img = "{$data['thumb']}";
wx.config({
debug:false,
appId: "{$arr['appId']}",
timestamp: "{$arr['timestamp']}",
nonceStr: "{$arr['nonceStr']}",
signature: "{$arr['signature']}",
url:url,
jsApiList: [
"onMenuShareTimeline", //分享给好友
"onMenuShareAppMessage", //分享到朋友圈
"onMenuShareQQ", //分享到QQ
"onMenuShareWeibo", //分享到微博
]
});
wx.ready(function (){
var shareData = {
title: "{$data['post_title']}",
desc: "{$data['post_excerpt']}",
link: url,
imgUrl: img,
};
wx.onMenuShareAppMessage(shareData);
wx.onMenuShareTimeline(shareData);
wx.onMenuShareQQ(shareData);
wx.onMenuShareWeibo(shareData);
});
</script>