绑定域名和引入js的就不说了
废话不说直接上代码
1 public void share(HttpServletRequest request) throws Exception { 2 3 StringBuffer homeUrl = request.getRequestURL(); 4 String queryString =request.getQueryString(); 5 if(StringUtils.isNotBlank(queryString)){ 6 homeUrl.append("?").append(queryString); 7 }
8 long timestamp = System.currentTimeMillis() / 1000; 9 10 String nonceStr = UUID.randomUUID().toString(); 11 12 String signature = SignUtil.getSignature( 13 weiXinBaseService.getJsTicket(), nonceStr, timestamp, 14 homeUrl.toString()); 15 logger.info("url="+homeUrl); 16 logger.info("nonceStr=" + nonceStr); 17 logger.info("timestamp=" + timestamp); 18 logger.info("signature=" + signature); 19 logger.info("appid=" + WebConfig.get("weixin.appid")); 20 request.setAttribute("appid", WebConfig.get("weixin.appid")); 21 request.setAttribute("timestamp", timestamp); 22 request.setAttribute("nonceStr", nonceStr); 23 request.setAttribute("signature", signature); 24 }
此方法主要有几个地方需要注意:1.获取的是请求的全路径,如果get请求有跟参数需要做个拼接。要不然签名会有错误。
2.随机数直接通过uuid随机数取的。
3.签名。微信的签名是个挺麻烦的事。但是又是必不可少的。签名代码奉上。
/** * 获得分享链接的签名。 * @param ticket * @param nonceStr * @param timeStamp * @param url * @return * @throws Exception */ public static String getSignature(String ticket, String nonceStr, long timeStamp, String url) throws Exception { String sKey = "jsapi_ticket=" + ticket + "&noncestr=" + nonceStr + "×tamp=" + timeStamp + "&url=" + url; return getSignature(sKey); }
具体签名方法如下
/** * 验证签名。 * * @param signature * @param timestamp * @param nonce * @return */ public static String getSignature(String sKey) throws Exception { String ciphertext = null; MessageDigest md = MessageDigest.getInstance("SHA-1"); byte[] digest = md.digest(sKey.toString().getBytes()); ciphertext = byteToStr(digest); return ciphertext.toLowerCase(); } /** * 将字节数组转换为十六进制字符串 * * @param byteArray * @return */ private static String byteToStr(byte[] byteArray) { String strDigest = ""; for (int i = 0; i < byteArray.length; i++) { strDigest += byteToHexStr(byteArray[i]); } return strDigest; }
/** * 将字节转换为十六进制字符串 * * @param mByte * @return */ private static String byteToHexStr(byte mByte) { char[] Digit = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘ }; char[] tempArr = new char[2]; tempArr[0] = Digit[(mByte >>> 4) & 0X0F]; tempArr[1] = Digit[mByte & 0X0F]; String s = new String(tempArr); return s; }
前端页面中引入微信js文件并配置config
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/jweixin-1.0.0.js"></script> <script> var shareTitle = "法国游已经近在咫尺了,是朋友就帮我一把吧!"; var shareImg = "http://" + domain +"${pageContext.request.contextPath}/static/img/shareImage.jpg" wx.config({ debug: false, // appId: ‘${appid}‘, // 必填,公众号的唯一标识 timestamp: ‘${timestamp}‘, // 必填,生成签名的时间戳 nonceStr: ‘${nonceStr}‘, // 必填,生成签名的随机串 signature: ‘${signature}‘,// 必填,签名,见附录1 jsApiList: [ ‘onMenuShareTimeline‘, ‘onMenuShareAppMessage‘, ‘showOptionMenu‘ ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 });
wx.ready(function () {
share();
});
function share(){
wx.showOptionMenu();
wx.onMenuShareTimeline({
title: shareTitle, // 分享标题
link: $("#shareLink").val(), // 分享链接
imgUrl:shareImg,
success: function () {
}
});
wx.onMenuShareAppMessage({
title: shareTitle, // 分享标题
link: $("#shareLink").val(), // 分享链接
imgUrl:shareImg,
desc: ‘xxx‘, // 分享描述
success: function () {
}
});
}
</script>