页面代码, 导入 微信的JS
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
$.ajax({
url : ******,
type : "post",
data : 'id=********&url='+ location.href.split('#')[0],
dataType:"json",
success : function(json) {
wx.config
}
})
ajax 请求返回 wx.config 的参数
ajax 的data 需要传 触发分享时的页面路径 location.href.split('#')[0]
后台 需要 将
paramMap.put("jsapi_ticket", jsApiTicket);
paramMap.put("noncestr", nonceStr);
paramMap.put("timestamp", Long.toString(timestame));
paramMap.put("url", url);
生成签名
jsapi_ticket 通过String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accessToken + "&type=jsapi";
noncestr 和 timestamp 需要给前端 的值 一致
url 就是 触发分享时的页面路径 location.href.split('#')[0]
注意:签名时 noncestr timestamp 都小写
但是 wx.config 中 nonceStr 中S大写
wx.config({
debug : true,
appId : appId,
timestamp : timestamp,
nonceStr : nonceStr,
signature : signature ,
jsApiList: ["onMenuShareTimeline"
,"onMenuShareQQ"
,"onMenuShareAppMessage"] // 必填,需要使用的JS接口列表
});
wx.ready(function(){
wx.onMenuShareAppMessage({
title: '标题',
desc: '描述',
link: url,
imgUrl: '图片路径',
success: function (res) {
alert('成功');
},
cancel: function (res) {
//alert('已取消');
},
fail: function (res) {
alert(res.errMsg);
}
});
})
如果要分享其他页面地址 更改onMenuShareAppMessage 里的 url 就行
这里的 url 与获得wx.config的 url 不是一个