之前很少接触微信开发,最近让做分享有点蒙,下面是自己做分享的一些经验仅供参考
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(); 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: 链接 }); });