New file |
| | |
| | | package com.matrix.system.shopXcx.api; |
| | | |
| | | import com.matrix.component.tools.HttpRequest; |
| | | import com.matrix.component.tools.HttpResponse; |
| | | import com.matrix.core.exception.GlobleException; |
| | | import com.matrix.core.tools.LogUtil; |
| | | import com.matrix.core.tools.PropertiesUtil; |
| | | import com.matrix.core.tools.StringUtils; |
| | | import net.sf.json.JSONObject; |
| | | |
| | | import java.io.IOException; |
| | | |
| | | /*** |
| | | * 公众号api工具 |
| | | */ |
| | | public class WeChatGzhApiTools { |
| | | /** |
| | | * 公众号秘钥 |
| | | */ |
| | | private static final String GZH_SECRET = "gzh_secret"; |
| | | /** |
| | | * 公众号appid |
| | | */ |
| | | private static final String GZH_APPID = "gzh_appid"; |
| | | /** |
| | | * 微信登录url |
| | | */ |
| | | private static final String WECHAT_LOGIN_URL = "wechar_login_url"; |
| | | |
| | | /** |
| | | * 上一次获取时间 |
| | | */ |
| | | private static Long preTime; |
| | | |
| | | /** |
| | | * 当前有效的微信token |
| | | */ |
| | | private static String accessToken = ""; |
| | | |
| | | private static String appid = ""; |
| | | private static String secret = ""; |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 获取公众号APPId |
| | | * @return |
| | | */ |
| | | public static String getAppid(){ |
| | | if(StringUtils.isBlank(appid)){ |
| | | appid = PropertiesUtil.getString(GZH_APPID); |
| | | } |
| | | return appid; |
| | | } |
| | | |
| | | /** |
| | | * 获取公众号秘钥 |
| | | * @return |
| | | */ |
| | | public static String getSecret(){ |
| | | if(StringUtils.isBlank(secret)){ |
| | | secret = PropertiesUtil.getString(GZH_SECRET); |
| | | } |
| | | return secret; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 清空token |
| | | */ |
| | | public static void cleanAccessToken() { |
| | | preTime = null; |
| | | accessToken = ""; |
| | | } |
| | | |
| | | public static String getAccessToken(String gzhAppId, String gzhSecret) { |
| | | |
| | | if (isTokenInvalid()) { |
| | | synchronized (accessToken) { |
| | | if (isTokenInvalid()) { |
| | | LogUtil.info("刷新微信accessToken"); |
| | | HttpRequest reqObj = new HttpRequest(); |
| | | HttpResponse result = null; |
| | | try { |
| | | result = reqObj |
| | | .sendHttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" |
| | | + gzhAppId+ "&secret=" + gzhSecret, null); |
| | | } catch (IOException e) { |
| | | LogUtil.error("获取公众号accessToken失败"+e.getMessage(),e); |
| | | throw new GlobleException(e.getMessage()); |
| | | } |
| | | JSONObject json = JSONObject.fromObject(result.getDataString()); |
| | | |
| | | if(json.has("errmsg")){ |
| | | throw new GlobleException("获取返回刷新的accessToken失败"+json.toString()); |
| | | }else{ |
| | | String access_token = json.getString("access_token"); |
| | | accessToken = access_token; |
| | | preTime = System.currentTimeMillis(); |
| | | LogUtil.info("返回刷新的accessToken={}", accessToken); |
| | | return access_token; |
| | | } |
| | | |
| | | } else { |
| | | return accessToken; |
| | | } |
| | | } |
| | | } else { |
| | | LogUtil.info("返回现有accessToken={}", accessToken); |
| | | return accessToken; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * token是否无效 |
| | | * TODO 由于现在有多个环境,为了防止冲突每次都获取新的token |
| | | * @return |
| | | */ |
| | | private static boolean isTokenInvalid() { |
| | | /** if (StringUtils.isNotBlank(accessToken) && preTime != null) { |
| | | Long now = System.currentTimeMillis() / 1000; |
| | | Long pre = preTime / 1000; |
| | | boolean invalid = (now - pre) > 6200; |
| | | return invalid; |
| | | } else { |
| | | return true; |
| | | }*/ |
| | | return true; |
| | | } |
| | | |
| | | |
| | | } |