呼叫微信JSSDK介面,實現自定義分享功能
1,你需有一個公眾號,沒有不用談自定義分享內容了;
2,公眾號上把你的域名增加到“JS介面安全域名”:
3,我這功能是基於thinkphp3.1實現的,按下圖上網下包放在對應該目錄下:
4,修改核心公共檔案:functions.php
加入程式碼:
/** * 微信分享初始化 * @return array * @author simon */
if(!function_exists('wx_share_init')){ function wx_share_init(){ $wxconfig = array(); vendor('Wxjssdk.Jssdk'); $config = array('appid'=>WXAPPID,'appsecret'=>WXAPPSECRET); //這裡配置了微信公眾號的AppId和AppSecret $jssdk =new JSSDK($config['appid'], $config['appsecret']); $wxconfig = $jssdk->GetSignPackage(); return $wxconfig; } }
5,在專案配置檔案:config.php
加入程式碼:(星為公眾號的appid和appsecret)
define("WXAPPID", "******"); define("WXAPPSECRET", "*****");
6,在對應要呼叫的頁面控制器里加入程式碼:
public function _initialize() { //呼叫微信JSSDK介面,為自定義分享功能提供驗證引數---- $this->wxconfig = wx_share_init(); $this->assign('wxconfig', $this->wxconfig); }
6,在對應要呼叫的頁面控制器的模板檔案里加入程式碼:
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script> <script type="application/javascript" language="javascript"> //alert("sdfasfasf"); var shareimg='http://{$Think.server.http_host}/Public/images/choujiang-ad.jpg'; var appId="{$wxconfig['appId']}"; var timestamp="{$wxconfig['timestamp']}"; var nonceStr="{$wxconfig['nonceStr']}"; var signature="{$wxconfig['signature']}"; //微信分享------------------------- wx.config({ debug:false,// 是否開啟除錯模式 appId:appId,// 必填,微訊號AppID timestamp:timestamp,// 必填,生成簽名的時間戳 nonceStr:nonceStr,// 必填,生成簽名的隨機串 signature:signature,// 必填,簽名,見附錄1 jsApiList: [ 'checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone' ] });
wx.ready(function(){ //alert("出現"); // 1 判斷當前版本是否支援指定 JS 介面,支援批量判斷 wx.checkJsApi({ jsApiList: [ 'getNetworkType', 'previewImage', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone' ], }); var shareData ={ title:'{$title}',// 分享標題 link:'{$wxconfig.url}',// 分享連結,記得使用絕對路徑 imgUrl:shareimg,// 分享圖示,記得使用絕對路徑 desc:'{$art.description}',// 分享描述 type: 'link', success:function(){ console.info('分享成功!'); // 使用者確認分享後執行的回撥函式 },
cancel:function(){ console.info('取消分享!'); // 使用者取消分享後執行的回撥函式 } } wx.onMenuShareAppMessage(shareData); wx.onMenuShareTimeline(shareData); wx.onMenuShareQQ(shareData); wx.onMenuShareWeibo(shareData);
});
wx.error(function(res){ console.info(res); }); </script>
搞掂,不是程式設計師的勿看,不會tp的自學去,不瞭解微信JS介面勿做