|
@@ -1,4 +1,4 @@
|
|
-package com.iamberry.wechat.service;
|
|
|
|
|
|
+package com.iamberry.wechat.tools;
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedReader;
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
@@ -7,8 +7,6 @@ import java.io.OutputStream;
|
|
import java.net.ConnectException;
|
|
import java.net.ConnectException;
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
-import java.security.KeyManagementException;
|
|
|
|
-import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
import java.util.logging.Logger;
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
@@ -19,90 +17,58 @@ import javax.net.ssl.TrustManager;
|
|
|
|
|
|
import net.sf.json.JSONObject;
|
|
import net.sf.json.JSONObject;
|
|
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
|
-import com.iamberry.app.tool.log.RatFWLogger;
|
|
|
|
|
|
+import com.iamberry.cache.LocalCache;
|
|
import com.iamberry.wechat.core.entity.wx.AccessToken;
|
|
import com.iamberry.wechat.core.entity.wx.AccessToken;
|
|
import com.iamberry.wechat.core.entity.wx.ITTempLate;
|
|
import com.iamberry.wechat.core.entity.wx.ITTempLate;
|
|
import com.iamberry.wechat.core.entity.wx.Menu;
|
|
import com.iamberry.wechat.core.entity.wx.Menu;
|
|
import com.iamberry.wechat.core.entity.wx.QRCJson;
|
|
import com.iamberry.wechat.core.entity.wx.QRCJson;
|
|
import com.iamberry.wechat.core.entity.wx.Ticket;
|
|
import com.iamberry.wechat.core.entity.wx.Ticket;
|
|
import com.iamberry.wechat.core.entity.wx.Token;
|
|
import com.iamberry.wechat.core.entity.wx.Token;
|
|
-import com.iamberry.wechat.service.mapper.TokenMapper;
|
|
|
|
-import com.iamberry.wechat.tools.MyX509TrustManager;
|
|
|
|
-import com.iamberry.wechat.tools.NameUtils;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* 公众平台通用接口工具类
|
|
* 公众平台通用接口工具类
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @author fzh
|
|
* @author fzh
|
|
- *
|
|
|
|
|
|
+ *
|
|
*/
|
|
*/
|
|
-@Component
|
|
|
|
public class WeixinUtil {
|
|
public class WeixinUtil {
|
|
-
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- private RatFWLogger logger;
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- private TokenMapper tokenMapper;
|
|
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ // 日志
|
|
|
|
+ private static org.slf4j.Logger logger = LoggerFactory.getLogger(WeixinUtil.class);
|
|
|
|
+
|
|
|
|
+ // 发送短信模板的URL
|
|
private static String sendTemplate = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
|
|
private static String sendTemplate = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
|
|
- public void setTokenMapper(TokenMapper tokenMapper) {
|
|
|
|
- this.tokenMapper = tokenMapper;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ // token缓存
|
|
|
|
+ private static LocalCache<String, Token> cache = new LocalCache<>(2);
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 网页授权获取access_token
|
|
* 网页授权获取access_token
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @param code
|
|
* @param code
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public JSONObject getParamByOauth2(String code) {
|
|
|
|
|
|
+ public static JSONObject getParamByOauth2(String code) {
|
|
JSONObject jsonObject = null;
|
|
JSONObject jsonObject = null;
|
|
try {
|
|
try {
|
|
// 得到access_token和openid
|
|
// 得到access_token和openid
|
|
- jsonObject = httpRequest(NameUtils.oauth2_getToken_url.replaceAll("CODE", code).replaceAll("APPID", NameUtils.appId).replaceAll("SECRET", NameUtils.appSecret), "GET", null);
|
|
|
|
|
|
+ jsonObject = httpRequest(NameUtils.oauth2_getToken_url.replaceAll("CODE", code).replaceAll("APPID", NameUtils.getConfig("appId")).replaceAll("SECRET", NameUtils.getConfig("appSecret")), "GET", null);
|
|
if (null != jsonObject) {
|
|
if (null != jsonObject) {
|
|
return jsonObject;
|
|
return jsonObject;
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
// 获取token失败
|
|
// 获取token失败
|
|
- logger.error(this, "获取token失败 errcode:{} errmsg:{}" + jsonObject.getInt("errcode") + jsonObject.getString("errmsg"));
|
|
|
|
|
|
+ logger.error("获取token失败 errcode:{} errmsg:{}" + jsonObject.getInt("errcode") + jsonObject.getString("errmsg"));
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
- private SSLContext context = null;
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取SSLContext实例
|
|
|
|
- * @return SSLContext实例
|
|
|
|
- * @throws KeyManagementException
|
|
|
|
- * @throws NoSuchAlgorithmException
|
|
|
|
- * @author 献
|
|
|
|
- * @Time 2016年11月17日
|
|
|
|
- */
|
|
|
|
- public SSLContext getSSLContext() throws KeyManagementException, NoSuchAlgorithmException {
|
|
|
|
- if (context != null) {
|
|
|
|
- return context;
|
|
|
|
- }
|
|
|
|
- synchronized (context) {
|
|
|
|
- if (context != null) {
|
|
|
|
- return context;
|
|
|
|
- }
|
|
|
|
- // 创建SSLContext,不向对方提供安全证书,并且校验对方安全的的实现为空
|
|
|
|
- TrustManager[] tm = { new MyX509TrustManager() };
|
|
|
|
- SSLContext sslContext = SSLContext.getInstance("SSL");
|
|
|
|
- sslContext.init(null, tm, new java.security.SecureRandom());
|
|
|
|
- context = sslContext;
|
|
|
|
- return context;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* 发起https请求并获取结果
|
|
* 发起https请求并获取结果
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @param requestUrl
|
|
* @param requestUrl
|
|
* 请求地址
|
|
* 请求地址
|
|
* @param requestMethod
|
|
* @param requestMethod
|
|
@@ -114,32 +80,31 @@ public class WeixinUtil {
|
|
* sun.net.www.protocol.http.HttpURLConnection cannot be cast to
|
|
* sun.net.www.protocol.http.HttpURLConnection cannot be cast to
|
|
* javax.net.ssl.HttpsURLConnection
|
|
* javax.net.ssl.HttpsURLConnection
|
|
*/
|
|
*/
|
|
- public JSONObject httpRequest(String requestUrl,
|
|
|
|
- String requestMethod, String outputStr) {
|
|
|
|
|
|
+ public static JSONObject httpRequest(String requestUrl,
|
|
|
|
+ String requestMethod, String outputStr) {
|
|
JSONObject jsonObject = null;
|
|
JSONObject jsonObject = null;
|
|
- StringBuilder buffer = new StringBuilder();
|
|
|
|
|
|
+ StringBuffer buffer = new StringBuffer();
|
|
try {
|
|
try {
|
|
- // 获取SSLContext实例
|
|
|
|
- SSLContext sslContext = getSSLContext();
|
|
|
|
|
|
+ // 创建SSLContext对象,并使用我们指定的信任管理器初始化
|
|
|
|
+ TrustManager[] tm = { new MyX509TrustManager() };
|
|
|
|
+ SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
|
|
|
|
+ sslContext.init(null, tm, new java.security.SecureRandom());
|
|
// 从上述SSLContext对象中得到SSLSocketFactory对象
|
|
// 从上述SSLContext对象中得到SSLSocketFactory对象
|
|
SSLSocketFactory ssf = sslContext.getSocketFactory();
|
|
SSLSocketFactory ssf = sslContext.getSocketFactory();
|
|
|
|
|
|
- // 请求工具初始化
|
|
|
|
URL url = new URL(requestUrl);
|
|
URL url = new URL(requestUrl);
|
|
- HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
|
|
|
|
|
|
+ HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
|
|
|
|
+ .openConnection();
|
|
httpUrlConn.setSSLSocketFactory(ssf);
|
|
httpUrlConn.setSSLSocketFactory(ssf);
|
|
|
|
|
|
httpUrlConn.setDoOutput(true);
|
|
httpUrlConn.setDoOutput(true);
|
|
httpUrlConn.setDoInput(true);
|
|
httpUrlConn.setDoInput(true);
|
|
httpUrlConn.setUseCaches(false);
|
|
httpUrlConn.setUseCaches(false);
|
|
- httpUrlConn.setConnectTimeout(15000);
|
|
|
|
|
|
|
|
// 设置请求方式(GET/POST)
|
|
// 设置请求方式(GET/POST)
|
|
httpUrlConn.setRequestMethod(requestMethod);
|
|
httpUrlConn.setRequestMethod(requestMethod);
|
|
- if ("GET".equalsIgnoreCase(requestMethod)) {
|
|
|
|
- // 如果是GET请求,直接发起,因为参数已经存在于URL中
|
|
|
|
|
|
+ if ("GET".equalsIgnoreCase(requestMethod))
|
|
httpUrlConn.connect();
|
|
httpUrlConn.connect();
|
|
- }
|
|
|
|
|
|
|
|
// 当有数据需要提交时
|
|
// 当有数据需要提交时
|
|
if (null != outputStr) {
|
|
if (null != outputStr) {
|
|
@@ -176,15 +141,18 @@ public class WeixinUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 获取access_token
|
|
* 获取access_token
|
|
- * @param appid 凭证
|
|
|
|
- * @param appsecret 密钥
|
|
|
|
|
|
+ *
|
|
|
|
+ * @param appid
|
|
|
|
+ * 凭证
|
|
|
|
+ * @param appsecret
|
|
|
|
+ * 密钥
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public AccessToken getAccessToken(String appid, String appsecret) {
|
|
|
|
-
|
|
|
|
|
|
+ public static AccessToken getAccessToken(String appid, String appsecret) {
|
|
|
|
+
|
|
// 根据public No查询access_token
|
|
// 根据public No查询access_token
|
|
- Token token = tokenMapper.selectOne(NameUtils.pubNo);
|
|
|
|
-
|
|
|
|
|
|
+ Token token = cache.get(NameUtils.getConfig("pubNo"));
|
|
|
|
+
|
|
AccessToken accessToken = null;
|
|
AccessToken accessToken = null;
|
|
//判断数据库中是否存在token
|
|
//判断数据库中是否存在token
|
|
if (token != null) {
|
|
if (token != null) {
|
|
@@ -196,7 +164,7 @@ public class WeixinUtil {
|
|
accessToken.setToken(token.getToken());
|
|
accessToken.setToken(token.getToken());
|
|
return accessToken;
|
|
return accessToken;
|
|
} else {
|
|
} else {
|
|
- String requestUrl = NameUtils.access_token_url.replace("APPID", NameUtils.appId).replace("APPSECRET", NameUtils.appSecret);
|
|
|
|
|
|
+ String requestUrl = NameUtils.access_token_url.replace("APPID", NameUtils.getConfig("appId")).replace("APPSECRET", NameUtils.getConfig("appSecret"));
|
|
JSONObject jsonObject = httpRequest(requestUrl, "GET", null);
|
|
JSONObject jsonObject = httpRequest(requestUrl, "GET", null);
|
|
// 如果请求成功
|
|
// 如果请求成功
|
|
if (null != jsonObject) {
|
|
if (null != jsonObject) {
|
|
@@ -206,18 +174,17 @@ public class WeixinUtil {
|
|
accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
|
|
accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
|
|
token.setToken(accessToken.getToken());
|
|
token.setToken(accessToken.getToken());
|
|
token.setEndDate(new Date(date.getTime() + 6500000));
|
|
token.setEndDate(new Date(date.getTime() + 6500000));
|
|
- token.setAppid(NameUtils.pubNo);
|
|
|
|
|
|
+ token.setAppid(NameUtils.getConfig("pubNo"));
|
|
try {
|
|
try {
|
|
- tokenMapper.updateToken(token);
|
|
|
|
|
|
+ cache.put(NameUtils.getConfig("pubNo"), token);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
- logger.error(this,"更新token失败:" + e.getMessage());
|
|
|
|
|
|
+ logger.error(e.getMessage());
|
|
}
|
|
}
|
|
return accessToken;
|
|
return accessToken;
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
- e.printStackTrace();
|
|
|
|
accessToken = null;
|
|
accessToken = null;
|
|
// 获取token失败
|
|
// 获取token失败
|
|
- Logger.getAnonymousLogger().info("获取token失败 errcode:{} errmsg:{}" + jsonObject.getInt("errcode") + jsonObject.getString("errmsg"));
|
|
|
|
|
|
+ logger.error("获取token失败 errcode:{} errmsg:{}" + jsonObject.getInt("errcode") + jsonObject.getString("errmsg"));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -230,22 +197,23 @@ public class WeixinUtil {
|
|
accessToken = new AccessToken();
|
|
accessToken = new AccessToken();
|
|
accessToken.setToken(jsonObject.getString("access_token"));
|
|
accessToken.setToken(jsonObject.getString("access_token"));
|
|
accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
|
|
accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
|
|
-
|
|
|
|
|
|
+
|
|
token = new Token();
|
|
token = new Token();
|
|
token.setToken(accessToken.getToken());
|
|
token.setToken(accessToken.getToken());
|
|
token.setEndDate(new Date(new Date().getTime() + 6500000));
|
|
token.setEndDate(new Date(new Date().getTime() + 6500000));
|
|
- token.setAppid(NameUtils.pubNo);
|
|
|
|
-
|
|
|
|
|
|
+ token.setAppid(NameUtils.getConfig("pubNo"));
|
|
|
|
+
|
|
try {
|
|
try {
|
|
- tokenMapper.updateToken(token);
|
|
|
|
|
|
+ cache.put(NameUtils.getConfig("pubNo"), token);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
// 更新失败
|
|
// 更新失败
|
|
|
|
+ logger.error(e.getMessage());
|
|
}
|
|
}
|
|
return accessToken;
|
|
return accessToken;
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
accessToken = null;
|
|
accessToken = null;
|
|
// 获取token失败
|
|
// 获取token失败
|
|
- Logger.getAnonymousLogger().info("获取token失败 errcode:{} errmsg:{}" + jsonObject.getInt("errcode") + jsonObject.getString("errmsg"));
|
|
|
|
|
|
+ logger.error("获取token失败 errcode:{} errmsg:{}" + jsonObject.getInt("errcode") + jsonObject.getString("errmsg"));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -254,11 +222,11 @@ public class WeixinUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 获取jsapi_ticket(jsapi_ticket的有效期为7200秒)
|
|
* 获取jsapi_ticket(jsapi_ticket的有效期为7200秒)
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @param token
|
|
* @param token
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public Ticket getTicket(String token) {
|
|
|
|
|
|
+ public static Ticket getTicket(String token) {
|
|
Ticket ticket = null;
|
|
Ticket ticket = null;
|
|
String requestUrl = NameUtils.jsapi_ticket_url.replace("ACCESS_TOKEN", token);
|
|
String requestUrl = NameUtils.jsapi_ticket_url.replace("ACCESS_TOKEN", token);
|
|
JSONObject jsonObject = httpRequest(requestUrl, "GET", null);
|
|
JSONObject jsonObject = httpRequest(requestUrl, "GET", null);
|
|
@@ -273,23 +241,23 @@ public class WeixinUtil {
|
|
ticket.setExpiresIn(jsonObject.getInt("expires_in"));
|
|
ticket.setExpiresIn(jsonObject.getInt("expires_in"));
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
// 获取token失败
|
|
// 获取token失败
|
|
- Logger.getAnonymousLogger().info("获取ticket失败 errcode:{} errmsg:{}" + jsonObject.getInt("errcode") + jsonObject.getString("errmsg"));
|
|
|
|
-
|
|
|
|
|
|
+ logger.error("获取ticket失败 errcode:{} errmsg:{}" + jsonObject.getInt("errcode") + jsonObject.getString("errmsg"));
|
|
|
|
+
|
|
// 判断错误信息是否为 access_token 失效的问题,只有access失效,我们才重新获取,否则每次获取可能导致access_token资源耗尽
|
|
// 判断错误信息是否为 access_token 失效的问题,只有access失效,我们才重新获取,否则每次获取可能导致access_token资源耗尽
|
|
- if (jsonObject.getInt("errcode") != 40001)
|
|
|
|
|
|
+ if (jsonObject.getInt("errcode") != 40001)
|
|
return null;
|
|
return null;
|
|
-
|
|
|
|
|
|
+
|
|
// 强制刷新access_token,存在多线程安全隐患!
|
|
// 强制刷新access_token,存在多线程安全隐患!
|
|
- requestUrl = NameUtils.access_token_url.replace("APPID", NameUtils.appId).replace("APPSECRET", NameUtils.appSecret);
|
|
|
|
|
|
+ requestUrl = NameUtils.access_token_url.replace("APPID", NameUtils.getConfig("appId")).replace("APPSECRET", NameUtils.getConfig("appSecret"));
|
|
JSONObject accessTokenJsonObject = httpRequest(requestUrl, "GET", null);
|
|
JSONObject accessTokenJsonObject = httpRequest(requestUrl, "GET", null);
|
|
-
|
|
|
|
|
|
+
|
|
// 报错access_token信息,不管报错与否
|
|
// 报错access_token信息,不管报错与否
|
|
Token wechatToken = new Token();
|
|
Token wechatToken = new Token();
|
|
wechatToken.setEndDate(new Date(new Date().getTime() + 6500000));
|
|
wechatToken.setEndDate(new Date(new Date().getTime() + 6500000));
|
|
- wechatToken.setAppid(NameUtils.pubNo);
|
|
|
|
|
|
+ wechatToken.setAppid(NameUtils.getConfig("pubNo"));
|
|
wechatToken.setToken(accessTokenJsonObject.getString("access_token"));
|
|
wechatToken.setToken(accessTokenJsonObject.getString("access_token"));
|
|
- tokenMapper.updateToken(wechatToken);
|
|
|
|
-
|
|
|
|
|
|
+ cache.put(NameUtils.getConfig("pubNo"), wechatToken);
|
|
|
|
+
|
|
// 重新获取一次
|
|
// 重新获取一次
|
|
requestUrl = NameUtils.jsapi_ticket_url.replace("ACCESS_TOKEN", wechatToken.getToken());
|
|
requestUrl = NameUtils.jsapi_ticket_url.replace("ACCESS_TOKEN", wechatToken.getToken());
|
|
jsonObject = httpRequest(requestUrl, "GET", null);
|
|
jsonObject = httpRequest(requestUrl, "GET", null);
|
|
@@ -297,20 +265,20 @@ public class WeixinUtil {
|
|
ticket.setTicket(jsonObject.getString("ticket"));
|
|
ticket.setTicket(jsonObject.getString("ticket"));
|
|
ticket.setExpiresIn(jsonObject.getInt("expires_in"));
|
|
ticket.setExpiresIn(jsonObject.getInt("expires_in"));
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
return ticket;
|
|
return ticket;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 创建菜单
|
|
* 创建菜单
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @param menu
|
|
* @param menu
|
|
* 菜单实例
|
|
* 菜单实例
|
|
* @param accessToken
|
|
* @param accessToken
|
|
* 有效的access_token
|
|
* 有效的access_token
|
|
* @return 0表示成功,其他值表示失败
|
|
* @return 0表示成功,其他值表示失败
|
|
*/
|
|
*/
|
|
- public int createMenu(Menu menu, String accessToken) {
|
|
|
|
|
|
+ public static int createMenu(Menu menu, String accessToken) {
|
|
int result = 0;
|
|
int result = 0;
|
|
// 拼装创建菜单的url
|
|
// 拼装创建菜单的url
|
|
String url = NameUtils.menu_create_url.replace("ACCESS_TOKEN", accessToken);
|
|
String url = NameUtils.menu_create_url.replace("ACCESS_TOKEN", accessToken);
|
|
@@ -329,11 +297,11 @@ public class WeixinUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 创建菜单
|
|
* 创建菜单
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @param jsonMenu
|
|
* @param jsonMenu
|
|
* @param accessToken
|
|
* @param accessToken
|
|
*/
|
|
*/
|
|
- public int createMenu(String jsonMenu, String accessToken) {
|
|
|
|
|
|
+ public static int createMenu(String jsonMenu, String accessToken) {
|
|
int result = 0;
|
|
int result = 0;
|
|
// 拼装创建菜单的url
|
|
// 拼装创建菜单的url
|
|
String url = NameUtils.menu_create_url.replace("ACCESS_TOKEN", accessToken);
|
|
String url = NameUtils.menu_create_url.replace("ACCESS_TOKEN", accessToken);
|
|
@@ -350,14 +318,14 @@ public class WeixinUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 创建二维码
|
|
* 创建二维码
|
|
- *
|
|
|
|
- * @param qrCode
|
|
|
|
|
|
+ *
|
|
|
|
+ * @param json
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public QRCJson createQrcode(String json) {
|
|
|
|
|
|
+ public static QRCJson createQrcode(String json) {
|
|
QRCJson qrcJson = new QRCJson();
|
|
QRCJson qrcJson = new QRCJson();
|
|
try {
|
|
try {
|
|
- AccessToken at = this.getAccessToken(NameUtils.appId, NameUtils.appSecret);
|
|
|
|
|
|
+ AccessToken at = getAccessToken(NameUtils.getConfig("appId"), NameUtils.getConfig("appSecret"));
|
|
if (at != null) {
|
|
if (at != null) {
|
|
String token = at.getToken();
|
|
String token = at.getToken();
|
|
JSONObject jsonObject = httpRequest(NameUtils.get_token_url.replaceAll("TOKEN", token), "POST", json);
|
|
JSONObject jsonObject = httpRequest(NameUtils.get_token_url.replaceAll("TOKEN", token), "POST", json);
|
|
@@ -378,18 +346,18 @@ public class WeixinUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 生成网页授权链接
|
|
* 生成网页授权链接
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @param type
|
|
* @param type
|
|
* @param redirectURI
|
|
* @param redirectURI
|
|
*/
|
|
*/
|
|
- public String getOauth2Url(String type, String redirectURI) {
|
|
|
|
|
|
+ public static String getOauth2Url(String type, String redirectURI) {
|
|
String url = "";
|
|
String url = "";
|
|
try {
|
|
try {
|
|
redirectURI = URLEncoder.encode(redirectURI, "utf-8");
|
|
redirectURI = URLEncoder.encode(redirectURI, "utf-8");
|
|
if ("snsapi_base".equals(type)) {
|
|
if ("snsapi_base".equals(type)) {
|
|
- url = NameUtils.oauth2_url.replace("SCOPE", "snsapi_base").replace("APPID", NameUtils.appId).replace("REDIRECT_URI", redirectURI);
|
|
|
|
|
|
+ url = NameUtils.oauth2_url.replace("SCOPE", "snsapi_base").replace("APPID", NameUtils.getConfig("appId")).replace("REDIRECT_URI", redirectURI);
|
|
} else {
|
|
} else {
|
|
- url = NameUtils.oauth2_url.replace("SCOPE", "snsapi_userinfo").replace("APPID", NameUtils.appId).replace("REDIRECT_URI", redirectURI);
|
|
|
|
|
|
+ url = NameUtils.oauth2_url.replace("SCOPE", "snsapi_userinfo").replace("APPID", NameUtils.getConfig("appId")).replace("REDIRECT_URI", redirectURI);
|
|
}
|
|
}
|
|
return url;
|
|
return url;
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -400,17 +368,17 @@ public class WeixinUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* (网页授权)拉取用户信息
|
|
* (网页授权)拉取用户信息
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @param code
|
|
* @param code
|
|
* @param type
|
|
* @param type
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public JSONObject getUserInfoByOauth2(String code, String type) {
|
|
|
|
|
|
+ public static JSONObject getUserInfoByOauth2(String code, String type) {
|
|
JSONObject jsonObject = null;
|
|
JSONObject jsonObject = null;
|
|
String openid = "", access_token = "";
|
|
String openid = "", access_token = "";
|
|
try {
|
|
try {
|
|
// 得到access_token和openid
|
|
// 得到access_token和openid
|
|
- jsonObject = httpRequest(NameUtils.oauth2_getToken_url.replaceAll("CODE", code).replaceAll("APPID", NameUtils.appId).replaceAll("SECRET", NameUtils.appSecret), "GET", null);
|
|
|
|
|
|
+ jsonObject = httpRequest(NameUtils.oauth2_getToken_url.replaceAll("CODE", code).replaceAll("APPID", NameUtils.getConfig("appId")).replaceAll("SECRET", NameUtils.getConfig("appSecret")), "GET", null);
|
|
if (null != jsonObject) {
|
|
if (null != jsonObject) {
|
|
openid = jsonObject.getString("openid");
|
|
openid = jsonObject.getString("openid");
|
|
access_token = jsonObject.getString("access_token");
|
|
access_token = jsonObject.getString("access_token");
|
|
@@ -426,19 +394,19 @@ public class WeixinUtil {
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
access_token = null;
|
|
access_token = null;
|
|
// 获取token失败
|
|
// 获取token失败
|
|
-System.out.println("获取token失败 errcode:{} errmsg:{}" + jsonObject.getInt("errcode") + jsonObject.getString("errmsg"));
|
|
|
|
|
|
+ logger.error("获取token失败 errcode:{} errmsg:{}" + jsonObject.getInt("errcode") + jsonObject.getString("errmsg"));
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 拉取用户信息
|
|
* 拉取用户信息
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @param code
|
|
* @param code
|
|
* @param appid
|
|
* @param appid
|
|
* @param secret
|
|
* @param secret
|
|
*/
|
|
*/
|
|
- public String[] getOpenId(String code, String appid, String secret) {
|
|
|
|
|
|
+ public static String[] getOpenId(String code, String appid, String secret) {
|
|
String[] strs = new String[2];
|
|
String[] strs = new String[2];
|
|
String url = "", openid = "", access_token = "";
|
|
String url = "", openid = "", access_token = "";
|
|
Logger.getAnonymousLogger().info("code = " + code);
|
|
Logger.getAnonymousLogger().info("code = " + code);
|
|
@@ -463,12 +431,12 @@ System.out.println("获取token失败 errcode:{} errmsg:{}" + jsonObject.getInt(
|
|
|
|
|
|
/**
|
|
/**
|
|
* 根据openid拉取用户信息
|
|
* 根据openid拉取用户信息
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @param openid
|
|
* @param openid
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public JSONObject getUserInfo(String openid) {
|
|
|
|
- AccessToken token = this.getAccessToken(NameUtils.appId, NameUtils.appSecret);
|
|
|
|
|
|
+ public static JSONObject getUserInfo(String openid) {
|
|
|
|
+ AccessToken token = getAccessToken(NameUtils.getConfig("appId"), NameUtils.getConfig("appSecret"));
|
|
if (token == null) {
|
|
if (token == null) {
|
|
// 此处如果出错,只能说明程序逻辑改变,存在多线程安全隐患
|
|
// 此处如果出错,只能说明程序逻辑改变,存在多线程安全隐患
|
|
return null;
|
|
return null;
|
|
@@ -485,24 +453,24 @@ System.out.println("获取token失败 errcode:{} errmsg:{}" + jsonObject.getInt(
|
|
try {
|
|
try {
|
|
jsonObject.getString("sex");
|
|
jsonObject.getString("sex");
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
-
|
|
|
|
|
|
+
|
|
// 记录日志
|
|
// 记录日志
|
|
- logger.error(this, "getUserInfo method throw exception:" + e.getMessage() + "[" + jsonObject + "]");
|
|
|
|
-
|
|
|
|
|
|
+ logger.error("getUserInfo method throw exception:" + e.getMessage() + "[" + jsonObject + "]");
|
|
|
|
+
|
|
// 判断错误信息是否为 access_token 失效的问题,只有access失效,我们才重新获取,否则每次获取可能导致access_token资源耗尽
|
|
// 判断错误信息是否为 access_token 失效的问题,只有access失效,我们才重新获取,否则每次获取可能导致access_token资源耗尽
|
|
- if (jsonObject.getInt("errcode") != 40001)
|
|
|
|
|
|
+ if (jsonObject.getInt("errcode") != 40001)
|
|
return jsonObject;
|
|
return jsonObject;
|
|
-
|
|
|
|
|
|
+
|
|
// 强制刷新access_token,存在多线程安全隐患!
|
|
// 强制刷新access_token,存在多线程安全隐患!
|
|
- String requestUrl = NameUtils.access_token_url.replace("APPID", NameUtils.appId).replace("APPSECRET", NameUtils.appSecret);
|
|
|
|
|
|
+ String requestUrl = NameUtils.access_token_url.replace("APPID", NameUtils.getConfig("appId")).replace("APPSECRET", NameUtils.getConfig("appSecret"));
|
|
JSONObject accessTokenJsonObject = httpRequest(requestUrl, "GET", null);
|
|
JSONObject accessTokenJsonObject = httpRequest(requestUrl, "GET", null);
|
|
-
|
|
|
|
|
|
+
|
|
// 报错access_token信息,不管报错与否
|
|
// 报错access_token信息,不管报错与否
|
|
Token wechatToken = new Token();
|
|
Token wechatToken = new Token();
|
|
wechatToken.setEndDate(new Date(new Date().getTime() + 6500000));
|
|
wechatToken.setEndDate(new Date(new Date().getTime() + 6500000));
|
|
- wechatToken.setAppid(NameUtils.pubNo);
|
|
|
|
|
|
+ wechatToken.setAppid(NameUtils.getConfig("pubNo"));
|
|
wechatToken.setToken(accessTokenJsonObject.getString("access_token"));
|
|
wechatToken.setToken(accessTokenJsonObject.getString("access_token"));
|
|
- tokenMapper.updateToken(wechatToken);
|
|
|
|
|
|
+ cache.put(NameUtils.getConfig("pubNo"), wechatToken);
|
|
// 再次获取用户信息
|
|
// 再次获取用户信息
|
|
return httpRequest(NameUtils.userinfo_url.replaceAll("ACCESS_TOKEN", wechatToken.getToken()).replaceAll("OPENID", openid), "GET", null);
|
|
return httpRequest(NameUtils.userinfo_url.replaceAll("ACCESS_TOKEN", wechatToken.getToken()).replaceAll("OPENID", openid), "GET", null);
|
|
}
|
|
}
|
|
@@ -512,11 +480,11 @@ System.out.println("获取token失败 errcode:{} errmsg:{}" + jsonObject.getInt(
|
|
/**
|
|
/**
|
|
* 发送模板消息 appId 公众账号的唯一标识 appSecret 公众账号的密钥 openId 用户标识
|
|
* 发送模板消息 appId 公众账号的唯一标识 appSecret 公众账号的密钥 openId 用户标识
|
|
*/
|
|
*/
|
|
- public void sendTemplateMessage(String appId, String appSecret,
|
|
|
|
- String openId, String template_id, Object data, String redirectUrl) {
|
|
|
|
|
|
+ public static void sendTemplateMessage(String appId, String appSecret,
|
|
|
|
+ String openId, String template_id, Object data, String redirectUrl) {
|
|
|
|
|
|
// 准备数据
|
|
// 准备数据
|
|
- AccessToken token = this.getAccessToken(appId, appSecret);
|
|
|
|
|
|
+ AccessToken token = getAccessToken(appId, appSecret);
|
|
String access_token = token.getToken();
|
|
String access_token = token.getToken();
|
|
String url = sendTemplate + access_token;
|
|
String url = sendTemplate + access_token;
|
|
|
|
|
|
@@ -525,7 +493,7 @@ System.out.println("获取token失败 errcode:{} errmsg:{}" + jsonObject.getInt(
|
|
temp.setTouser(openId);
|
|
temp.setTouser(openId);
|
|
temp.setTemplate_id(template_id);
|
|
temp.setTemplate_id(template_id);
|
|
temp.setUrl(redirectUrl);
|
|
temp.setUrl(redirectUrl);
|
|
- temp.setData(data);
|
|
|
|
|
|
+ temp.setData(JSONObject.fromObject(data));
|
|
|
|
|
|
// format
|
|
// format
|
|
String jsonString = JSONObject.fromObject(temp).toString();
|
|
String jsonString = JSONObject.fromObject(temp).toString();
|