前段时间写了一片微信分享的文章,在二次分享时会存在问题,今天进行补充,主要是第五条
1、通过appId和secret获取access_token
public static String getAccessToken(String appid, String secret) {
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
JSONObject jsonObject = httpRequest(url, "GET", null);
RTLogger.getLogger("WechatLog").debug("重新获取微信接口token返回:"+jsonObject);
try {
if(jsonObject.getString("errcode")!=null){
RTLogger.getLogger("WechatLog").debug("重新获取微信接口errcode返回:"+jsonObject.getString("errcode"));
return "false";
}
}catch (Exception e) {
}
return jsonObject.getString("access_token");
}
2、通过access_token获取jsappticket
public static String getJsapiTicket(String token) {
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+token+"&type=jsapi";
JSONObject jsonObject = httpRequest(url, "GET", null);
log.info("获得 jsonObject{}------:"+jsonObject);
try {
if(!"0".equals(jsonObject.getString("errcode"))){
return "false";
}
}catch (Exception e) {
}
return jsonObject.getString("ticket");
}
3、注意点token和ticket都是有默认有效期的,为了提高效率最好放到redis里面,过去时间比默认时间稍小
4、完成关键的两步,就是对数据的封装和编码了
@ModelAttribute("wechatInfo")
public WechatInfo prepareShareInfo(HttpServletRequest request){
String isshare = request.getParameter("isshare");
if(StringUtils.isBlank(isshare)){
return null;
}
WechatInfo wechatInfo =new WechatInfo();
String fullUrl = getFullRequestUrl(request);
// 生成微信签名信息
String appId = WxPayConstant.APP_ID;
if (WxPayConstant.getIsUseTestWx()) {
appId = WxPayConstant.TEST_APP_ID;
}
wechatInfo.setAppId(appId);
wechatInfo.setUrl(fullUrl);
long timestamp = System.currentTimeMillis() / 1000;
wechatInfo.setTimestamp(timestamp);
String nonceStr = UUIDUtil.genUUIDString();
wechatInfo.setNonceStr(nonceStr);
String jsapiTicket = WeiXinUtil.getTicket(jedisClient);
String signParam = "jsapi_ticket=" + jsapiTicket + "&noncestr=" + nonceStr + "×tamp=" + timestamp + "&url=" + fullUrl; //参数一定按照这个字典循序排序,否者验签无法通过
log.info("未编码前:"+signParam);
String signature = Sha.encodeSha1(signParam);
log.info("编码后:"+signature);
wechatInfo.setSignature(signature);
return wechatInfo;
}
5、java获取get请求全参数方法封装(此处解决二次分享问题):
public static String getFullRequestUrl(HttpServletRequest request){ String queryStr=request.getQueryString(); log.info("获取请求参数处理前:"+queryStr); String from = request.getParameter("from"); log.info("from---------->" +from); if(StringUtils.isBlank(from)){ queryStr= queryStr.split("&from=")[0]; } String isappinstalled = request.getParameter("isappinstalled"); log.info("isappinstalled---------->" +isappinstalled); if(StringUtils.isBlank(isappinstalled)){ queryStr = queryStr.split("&isappinstalled=")[0]; } String code = request.getParameter("code"); log.info("code------------>" +code); if(StringUtils.isBlank(code)){ queryStr = queryStr.split("&code=")[0]; } String state = request.getParameter("state"); log.info("state------------>" +state); if(StringUtils.isBlank(state)){ queryStr = queryStr.split("&state=")[0]; } log.info("获取请求参数处理后:"+queryStr); String furl= String.valueOf(request.getRequestURL()); if(StringUtils.isNotBlank(queryStr)){ furl+="?"+queryStr; } return furl; }
到现在后台的配置结束。
前端只需要导入微信jssdk进行相关配置即可
<#--微信公众号JSSDK-->
function weixin() {
alert(appId)
// 微信JSSDK配置
wx.config({
debug: false,
appId:appId,
timestamp:timestamp,
nonceStr: nonceStr,
signature:signature,
jsApiList: [
'checkJsApi',
'onMenuShareWeibo',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareQZone'
]
});
}
wx.ready(function () {
wx.checkJsApi({
jsApiList: [
'onMenuShareWeibo',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareQZone'
],
success: function (res) {
console.log(res);
}
});
wx.onMenuShareWeibo({
title: "测试", desc: '测试', link: url, imgUrl: 链接
});
wx.onMenuShareTimeline({
title: "测试", desc: '测试', link: url, imgUrl: 链接
});
wx.onMenuShareAppMessage({
title: "测试", desc: '测试', link: url, imgUrl: 链接
});
wx.onMenuShareQQ({
title: "测试", desc: '测试', link: url, imgUrl: 链接
});
wx.onMenuShareQZone({
title: "测试", desc: '测试', link: url, imgUrl: 链接
});
});
————————————————
版权声明:本文为CSDN博主「wangc_gogo」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wangc_gogo/article/details/103611498