Wechat interface document address: https://mp.weixin.qq.com/wiki? T = resource / res ˊ main & id = mp1421141115 & token = & lang = zh cn
Implementation steps:
1. Bind domain name
First login WeChat public platform into the "official account settings" function settings to fill in the "JS interface security domain name". (special tips: you don't need to add http or https. You have suffered losses)
2. Import js file into the page
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
3. Inject permission to verify configuration through config interface
wx.config({
debug: true, / / turn on the debugging mode. The return values of all the APIs you call will come out in the client alert. To view the parameters passed in, you can turn them on on the pc side. The parameter information will be printed out through the log, and will only be printed on the pc side.
appId: '/ / / required, official account number only identifier.
Timestamp:, / / required, generate signature timestamp
nonceStr: '', / / required, generate random string of signature
Signature: '', / / required, signature, see Appendix 1
jsApiList: [] / / required. Please refer to Appendix 2 for the list of JS interfaces to be used
});
4. Successful verification through the ready interface processing
wx.ready(function(){
/ / detailed code
});
Handling failed validation through the error interface
wx.error(function(res){});
Detailed page code
-
<script>
-
//Wechat sharing circle of friends
-
$(function(){
-
/***User clicks to share to wechat circle and then loads interface interface*******/
-
var url=window.location.href.split('#')[0];
-
url = url.replace(/&/g, '%26');
-
console.log("url:"+url);
-
$.ajax({
-
url: "<%=basePath%>/lottery/shareToFriend.action?url="+url,
-
type: "POST",
-
async:true,
-
cache: false,
-
dataType: "json",
-
success: function(data){
-
wx.config({
-
debug: false,
-
appId: 'wx2948dfef9ef421ee',
-
timestamp:data.timeStamp,
-
nonceStr:data.nonceStr,
-
signature:data.signature,
-
jsApiList: [
-
'checkJsApi',
-
'onMenuShareTimeline',
-
'hideOptionMenu',
-
'onMenuShareAppMessage'
-
]
-
});
-
-
wx.ready(function(){
-
//wx.hideOptionMenu(); / * * * hide sharing menu
-
wx.checkJsApi({
-
jsApiList: [
-
'getLocation',
-
'onMenuShareTimeline',
-
'onMenuShareAppMessage'
-
],
-
success: function (res) {
-
//alert(res.errMsg);
-
}
-
});
-
-
wx.onMenuShareAppMessage({
-
title: 'Squeegee',
-
desc: 'Scratch is on',
-
link: '<%=basePath%>/lottery/lottery.action?lottery.id=${lottery.id}',
-
imgUrl: '<%=basePath%>/resources/qjc/img/start.png',
-
trigger: function (res) {
-
//alert('users Click to send to friends')
-
},
-
success: function (res) {
-
alert('You've got the chance to draw. Hurry to win the prize~~');
-
//Increase the number of games after sharing
-
$.ajax({
-
url: "<%=basePath%>/lottery/rewardPlayCount.action?openId=${openId}&lotteryId=${lottery.id}&shareType=friend",
-
type: "POST",
-
async:true,
-
cache: false,
-
dataType: "json",
-
success: function(data){
-
-
}
-
});
-
},
-
cancel: function (res) {
-
//alert('cancelled ')
-
},
-
fail: function (res) {
-
alert(res.errMsg);
-
}
-
});
-
-
//2.2 monitor the "share to friends" button click, customize the share content and share result interface
-
wx.onMenuShareTimeline({
-
title: 'Squeegee',
-
desc: 'Scratch is on',
-
link: '<%=basePath%>/lottery/lottery.action?lottery.id=${lottery.id}',
-
imgUrl: '<%=basePath%>/resources/qjc/img/start.png',
-
trigger: function (res) {
-
//alert('users Click to share to their friends circle ')
-
},
-
success: function (res) {
-
alert('You've got the chance to draw. Hurry to win the prize~~');
-
//Increase the number of games after sharing
-
$.ajax({
-
url: "<%=basePath%>/lottery/rewardPlayCount.action?openId=${openId}&lotteryId=${lottery.id}&shareType=friendCircle",
-
type: "POST",
-
async:true,
-
cache: false,
-
dataType: "json",
-
success: function(data){
-
-
}
-
});
-
},
-
cancel: function (res) {
-
//alert('cancelled ')
-
},
-
fail: function (res) {
-
alert(res.errMsg);
-
}
-
});
-
-
wx.error(function (res) {
-
alert(res.errMsg);
-
});
-
});
-
},
-
error: function() {
-
alert('ajax request failed!!!!');
-
return;
-
}
-
});
-
});
-
-
</script>
java background action code:
-
-
public void shareToFriend(){
-
HttpServletRequest request = ServletActionContext.getRequest();
-
String timeStamp = Sha1Util.getTimeStamp();
-
String nonceStr = WxConfig.getUUID();
-
String url=request.getParameter("url");
-
String signature = WxConfig.getSignature("APPId", "APP_secret", url, timeStamp, nonceStr);
-
request.setAttribute("timeStamp", timeStamp);
-
request.setAttribute("nonceStr", nonceStr);
-
request.setAttribute("url", url);
-
request.setAttribute("signature", signature);
-
WXjssdk result = new WXjssdk(timeStamp,nonceStr,signature,url);
-
CommonUtil.returnMsg(ServletActionContext.getResponse(), new Gson().toJson(result));
-
}
-
<pre name="code" class="java">
-
public final static String WEIXIN_JSAPI_TICKET_URL ="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
-
-
-
public static String getAccessToken(String appId,String appSecret){
-
String access_token;
-
access_token = mapToken.get("accessToken");
-
if(access_token==null){
-
String url = HttpUtil.WEIXIN_HOST_API + "/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+appSecret;
-
String menuJsonStr = HttpUtil.get(url);
-
final Type type = new TypeToken<Map<String, Object>>() {}.getType();
-
final Map<Object, Object> accessTokenInfo = new Gson().fromJson(menuJsonStr, type);
-
try{
-
access_token = accessTokenInfo.get("access_token").toString();
-
Object expires_in = accessTokenInfo.get("expires_in");
-
mapToken.put("accessToken", access_token);
-
logger.info("access_token:"+access_token+";expires_in:"+expires_in);
-
}catch (JSONException e) {
-
access_token = null;
-
e.printStackTrace();
-
logger.error("errcode:{}:"+accessTokenInfo.get("errcode")+"errmsg:{}:"+accessTokenInfo.get("errmsg"));
-
}
-
}
-
return access_token;
-
}
-
-
-
public static String getJsapiTicket(String accessToken){
-
String ticket;
-
ticket = mapTicket.get("ticket");
-
if(ticket==null){
-
String url = HttpUtil.WEIXIN_HOST_API + "/cgi-bin/ticket/getticket?access_token="+accessToken+"&type=jsapi";
-
String menuJsonStr = HttpUtil.get(url);
-
final Type type = new TypeToken<Map<String, Object>>() {}.getType();
-
final Map<Object, Object> ticketInfo = new Gson().fromJson(menuJsonStr, type);
-
try{
-
ticket = ticketInfo.get("ticket").toString();
-
String expires_in = ticketInfo.get("expires_in").toString();
-
mapTicket.put("ticket", ticket);
-
logger.info("jsapi_ticket:"+ticket+";expires_in:"+expires_in);
-
}catch (JSONException e) {
-
ticket = null;
-
e.printStackTrace();
-
logger.error("ticket errcode:{}:"+ticketInfo.get("errcode")+"errmsg:{}:"+ticketInfo.get("errmsg"));
-
}
-
}
-
return ticket;
-
}
-
-
-
public static String getUUID(){
-
String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
-
return uuid;
-
}
-
-
-
public static String getSignature(String appId,String appSecret,String url,String timeStamp,String nonceStr){
-
String accessToken = getAccessToken(appId,appSecret);
-
String jsapi_ticket = getJsapiTicket(accessToken);
-
logger.info("accessToken==="+accessToken);
-
String signValue = "jsapi_ticket="+jsapi_ticket+"&noncestr="+nonceStr+"×tamp="+timeStamp+"&url="+url;
-
logger.info("WeChat JS-SDK Signature string for permission validation:"+signValue);
-
-
String signature = Sha1Util.getSha1((signValue));
-
logger.info("WeChat JS-SDK Signature for permission validation:"+signature);
-
return signature;
-
}
Other projects
-
Sha1Util.java and MD5Util.java can be downloaded directly from the platform.