public static final String wxGetJsapiTicket() { Cache cache = Cache.getInstance(); long now = new Date().getTime() / 1000; if (!cache.exist("wxJsapiTicket") || now - (long) cache.get("wxJsapiTicketTime") > Constant.W_EXPIRE_IN) { String accessToken = Constant.wxGetAccessToken(); if (accessToken == null) { return null; } String url = Constant.W_GET_JSAPI_TICKET + "?access_token=" + accessToken + "&type=jsapi"; String ret = Web.sendGet(url); JSONObject json = JSONObject.fromObject(ret); if (json.containsKey("errcode") && json.getInt("errcode") != 0) { return null; } cache.set("wxJsapiTicket", json.getString("ticket")); cache.set("wxJsapiTicketTime", now); Constant.W_EXPIRE_IN = json.getInt("expires_in") - 200; } return (String) cache.get("wxJsapiTicket"); }
// 获取微信accessToken和JsapiTicket缓存 public static final String wxGetAccessToken() { Cache cache = Cache.getInstance(); long now = new Date().getTime() / 1000; if (!cache.exist("wxAccessToken") || now - (long) cache.get("wxAccessTokenTime") > Constant.W_EXPIRE_IN) { String url = Constant.W_GET_ACCESS_TOKEN_URL + "?grant_type=client_credential&appid=" + Constant.W_APP_ID + "&secret=" + Constant.W_APP_SECRET; String ret = Web.sendGet(url); JSONObject json = JSONObject.fromObject(ret); if (json.containsKey("errcode") && json.getInt("errcode") != 0) { return null; } cache.set("wxAccessToken", json.getString("access_token")); cache.set("wxAccessTokenTime", now); Constant.W_EXPIRE_IN = json.getInt("expires_in") - 200; } return (String) cache.get("wxAccessToken"); }