后端代码
try {
String nonceStr = WXPayUtil.generateNonceStr();
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
String accessToken = null;
String ticket = null;
boolean tokenHave = redisUtils.hasKey("accessToken");
if(tokenHave){
accessToken = (String) redisUtils.get("accessToken");
}else {
System.out.println("发送获取accessToken");
String requestUrl = "https://api.weixin.qq.com/cgi-bin/token";
String params = "grant_type=client_credential&appid=" + WxConstant.appId + "&secret=" + WxConstant.secret + "";
String result = WxUtil.sendGet(requestUrl, params);
accessToken = com.alibaba.fastjson.JSONObject.parseObject(result).getString("access_token");
boolean flag = redisUtils.set("accessToken", accessToken, 6600);
if(flag == false){
getJsapiTicket();
}
}
boolean ticketHave = redisUtils.hasKey("ticket");
if(ticketHave){
ticket = (String) redisUtils.get("ticket");
}else {
String requestUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
String params = "access_token=" + accessToken + "&type=jsapi";