|
@@ -213,6 +213,86 @@ public class WeixinUtil {
|
|
|
return accessToken;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取access_token
|
|
|
+ *
|
|
|
+ * @param appid
|
|
|
+ * 凭证
|
|
|
+ * @param appsecret
|
|
|
+ * 密钥
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static AccessToken getAccessToken(String appid, String appsecret,String pubNo) {
|
|
|
+
|
|
|
+ // 根据public No查询access_token
|
|
|
+ Token token = cache.get(pubNo);
|
|
|
+
|
|
|
+ AccessToken accessToken = null;
|
|
|
+ //判断数据库中是否存在token
|
|
|
+ if (token != null) {
|
|
|
+ //判断token是否失效
|
|
|
+ Date date = new Date();
|
|
|
+ if (date.getTime() < token.getEndDate().getTime()) {
|
|
|
+ accessToken = new AccessToken();
|
|
|
+ accessToken.setExpiresIn(7200);
|
|
|
+ accessToken.setToken(token.getToken());
|
|
|
+ return accessToken;
|
|
|
+ } else {
|
|
|
+ String requestUrl = NameUtils.access_token_url.replace("APPID", NameUtils.getConfig("appId")).replace("APPSECRET", NameUtils.getConfig("appSecret"));
|
|
|
+ JSONObject jsonObject = httpRequest(requestUrl, "GET", null);
|
|
|
+ // 如果请求成功
|
|
|
+ if (null != jsonObject) {
|
|
|
+ try {
|
|
|
+ accessToken = new AccessToken();
|
|
|
+ accessToken.setToken(jsonObject.getString("access_token"));
|
|
|
+ accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
|
|
|
+ token.setToken(accessToken.getToken());
|
|
|
+ token.setEndDate(new Date(date.getTime() + 6500000));
|
|
|
+ token.setAppid(NameUtils.getConfig("pubNo"));
|
|
|
+ try {
|
|
|
+ cache.put(NameUtils.getConfig("pubNo"), token);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error(e.getMessage());
|
|
|
+ }
|
|
|
+ return accessToken;
|
|
|
+ } catch (Exception e) {
|
|
|
+ accessToken = null;
|
|
|
+ // 获取token失败
|
|
|
+ logger.error("获取token失败 errcode:{} errmsg:{}" + jsonObject.getInt("errcode") + jsonObject.getString("errmsg"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ String requestUrl = NameUtils.access_token_url.replace("APPID", appid).replace("APPSECRET", appsecret);
|
|
|
+ JSONObject jsonObject = httpRequest(requestUrl, "GET", null);
|
|
|
+ // 如果请求成功
|
|
|
+ if (null != jsonObject) {
|
|
|
+ try {
|
|
|
+ accessToken = new AccessToken();
|
|
|
+ accessToken.setToken(jsonObject.getString("access_token"));
|
|
|
+ accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
|
|
|
+
|
|
|
+ token = new Token();
|
|
|
+ token.setToken(accessToken.getToken());
|
|
|
+ token.setEndDate(new Date(new Date().getTime() + 6500000));
|
|
|
+ token.setAppid(NameUtils.getConfig("pubNo"));
|
|
|
+
|
|
|
+ try {
|
|
|
+ cache.put(NameUtils.getConfig("pubNo"), token);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 更新失败
|
|
|
+ logger.error(e.getMessage());
|
|
|
+ }
|
|
|
+ return accessToken;
|
|
|
+ } catch (Exception e) {
|
|
|
+ accessToken = null;
|
|
|
+ // 获取token失败
|
|
|
+ logger.error("获取token失败 errcode:{} errmsg:{}" + jsonObject.getInt("errcode") + jsonObject.getString("errmsg"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -242,4 +322,31 @@ public class WeixinUtil {
|
|
|
return qrcJson;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据传入的公众号id创建二维码
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static QRCJson createQrcodeByWechatMp(String json,String appId,String appSecret,String pubNo) {
|
|
|
+ QRCJson qrcJson = new QRCJson();
|
|
|
+ try {
|
|
|
+ AccessToken at = getAccessToken(appId,appSecret,pubNo);
|
|
|
+ if (at != null) {
|
|
|
+ String token = at.getToken();
|
|
|
+ JSONObject jsonObject = httpRequest(NameUtils.get_token_url.replaceAll("TOKEN", token), "POST", json);
|
|
|
+ if (jsonObject != null) {
|
|
|
+ String ticket = URLEncoder.encode(jsonObject.getString("ticket"), "UTF-8");
|
|
|
+ qrcJson.setTicket(ticket);
|
|
|
+ qrcJson.setUrl(NameUtils.show_qrcode_url + ticket);
|
|
|
+ if (json.contains("expire_seconds")) {// 临时二维码才有过期时间
|
|
|
+ qrcJson.setExpire_seconds(jsonObject.getString("expire_seconds"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return qrcJson;
|
|
|
+ }
|
|
|
+
|
|
|
}
|