1.在網(wǎng)頁中引入微信的sdk
<script src="https://res2.wx.qq.com/open/js/jweixin-1.4.0.js" charset="utf-8"></script>
2.這里需要后臺提供一個接口粱锐,來獲取設(shè)置的一系列值
后臺計算方法:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
(簽名算法見文末的[附錄1]眼虱,所有JS接口列表見文末的[附錄2])
//otitle:轉(zhuǎn)發(fā)標題
//desc:轉(zhuǎn)發(fā)介紹
//olink:轉(zhuǎn)發(fā)鏈接
//oimgUrl: 轉(zhuǎn)發(fā)圖片
function wxSdk(otitle, desc, olink, oimgUrl) {
$.ajax({
type: "GET",
url: requsetUrl + "?url=" + encodeURIComponent(location.href.split('#')[0]),//encodeURIComponent(location.href.split('#')[0])這個要加,截掉轉(zhuǎn)碼后的參數(shù)迹恐,不截會報錯
data: {},
success: function(res) {
//這里的debug=true時,可以在頁面進行調(diào)試
wx.config({
debug: false,
appId: res.appId,
timestamp: res.timestamp,
nonceStr: res.nonceStr,
signature: res.signature,
jsApiList: [
'onMenuShareAppMessage', 'onMenuShareTimeline', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone', 'updateAppMessageShareData', 'updateTimelineShareData','chooseWXPay'
]
});
//請注意靡羡,原有的 wx.onMenuShareTimeline拣度、wx.onMenuShareAppMessage而晒、wx.onMenuShareQQ蝇狼、wx.onMenuShareQZone 接口,即將廢棄倡怎。轉(zhuǎn)移至 wx.updateAppMessageShareData迅耘、updateTimelineShareData 接口。但是有些手機上新接口不生效监署,保險起見全都加上比較合適(也可以不加)
wx.ready(function(res) {
wx.onMenuShareTimeline({
title: otitle, //標題
desc: desc,//形容
link: olink,//跳轉(zhuǎn)頁面
imgUrl: oimgUrl,//圖片
success: function() {
}
});
wx.onMenuShareAppMessage({
title: otitle,
desc: desc,
link: olink,
imgUrl: oimgUrl,
success: function() {}
});
wx.onMenuShareQQ({
title: otitle,
desc: desc,
link: olink,
imgUrl: oimgUrl,
success: function() {}
});
wx.onMenuShareWeibo({
title: otitle,
desc: desc,
link: olink,
imgUrl: oimgUrl,
success: function() {}
});
wx.onMenuShareQZone({
title: otitle,
desc: desc,
link: olink,
imgUrl: oimgUrl,
success: function() {}
});
wx.updateAppMessageShareData({
title: otitle,
desc: desc,
link: olink,
imgUrl: oimgUrl,
success: function() {}
});
wx.updateTimelineShareData({
title: otitle,
link: olink,
imgUrl: oimgUrl,
success: function() {}
});
//附帶接入支付
wx.checkJsApi({
jsApiList: ['chooseWXPay','onMenuShareTimeline'],
success: res => {},
fail: err => {
alert('check api fail:', err)
}
})
});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
// alert("neizhi---->error--");
}
});
}
3.微信公眾平臺接口調(diào)試工具(用來判斷后臺簽名算法是否出錯)
https://mp.weixin.qq.com/debug/