1.微信分享接口的開發分幾個步驟
a.得到ticket
b.根據ticket和url(#號後不要)得到signature,nonceStr,timestamp
c.根據得到的signature,nonceStr,timestamp 和appId 去通過config接口注入權限驗證配置
d.通過ws.ready去實現分享接口,接口名稱參考http://mp.weixin.qq.com/wiki/11/74ad127cc054f6b80759c40f77ec03db.html
官方校驗簽名網站:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign
示例如下:
a.引入(必要的)<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
· 可異步得到config接口需要的參數,開發的時候將這個改爲true
- debug : true
- <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
- <script type="text/javascript">
- var targetUrl = location.href.split("#")[0];
- $.ajax({
- url : "logonAction.do?proc=initWeiXin&targetUrl=" + targetUrl,
- type : "get",
- datatype : 'html',
- cache : true,
- success : function(html) {
- wx.config({
- debug : false,// 開啓調試模式,調用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數,可以在pc端打開,參數信息會通過log打出,僅在pc端時纔會打印。
- appId : eval('(' + html + ')').appId,// 必填,公衆號的唯一標識
- timestamp : eval('(' + html + ')').timestamp,// 必填,生成簽名的時間戳
- nonceStr : eval('(' + html + ')').nonceStr,// 必填,生成簽名的隨機串
- signature : eval('(' + html + ')').signature,// 必填,簽名,
- jsApiList : [
- // 所有要調用的 API 都要加到這個列表中
- 'onMenuShareTimeline' ]
- });
- }
- });<pre name="code" class="javascript"><span style="white-space:pre"> </span>wx.ready(function() {
- //隱藏分享按鈕
- //WeixinJSBridge.call('hideOptionMenu');
- wx.onMenuShareTimeline({
- title: '標題',
- link: 'https://www.baidu.com',
- imgUrl: 'https://www.baidu.com/img/bd_logo1.png',
- success: function () {
- alert('分享成功');
- },
- cancel: function () {
- alert('取消分享');
- }
- });
- });</pre><br>
- <pre></pre>
- 分享結果如下
- <p></p>
- <p><span style="font-size:14px"><img src="" alt=""><br>
- </span></p>
- <p><span style="font-size:14px"><br>
- </span></p>
- <p><span style="font-size:14px"><br>
- </span></p>
- <p><span style="font-size:14px">注意:如果微信彈出 errormsg config invalid signature</span></p>
- <p><span style="font-size:14px">,請確保</span><span style="font-size:14px; white-space:pre">url</span><span style="white-space:pre; color:rgb(51,51,51); font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:24px; background-color:rgb(245,245,245)"><span style="font-size:14px">(#號後不要)的正確性,</span><strong><span style="font-size:18px">url和微信中複製鏈接地址是一樣的</span></strong><span style="font-size:14px">(</span><span style="font-size:14px; color:rgb(51,51,51); font-family:Helvetica,Tahoma,Arial,sans-serif; line-height:24px; white-space:pre; background-color:rgb(245,245,245)">#號後不要</span><span style="font-size:14px">)</span></span></p>
- <p><span style="font-size:14px"><br>
- </span></p>
- <p><span style="font-size:14px"><span style="white-space:pre"></span></span></p>