Merge branch 'tianmu' into develop

This commit is contained in:
Binary Wang 2016-06-18 21:56:09 +08:00
commit d64f5020c4
6 changed files with 286 additions and 286 deletions

View File

@ -199,18 +199,18 @@ public class WxMpMessageRouter {
protected boolean isDuplicateMessage(WxMpXmlMessage wxMessage) { protected boolean isDuplicateMessage(WxMpXmlMessage wxMessage) {
String messageId = ""; StringBuffer messageId = new StringBuffer();
if (wxMessage.getMsgId() == null) { if (wxMessage.getMsgId() == null) {
messageId = String.valueOf(wxMessage.getCreateTime()) messageId.append(wxMessage.getCreateTime())
+ "-" + wxMessage.getFromUserName() .append("-").append(wxMessage.getFromUserName())
+ "-" + String.valueOf(wxMessage.getEventKey() == null ? "" : wxMessage.getEventKey()) .append("-").append(wxMessage.getEventKey() == null ? "" : wxMessage.getEventKey())
+ "-" + String.valueOf(wxMessage.getEvent() == null ? "" : wxMessage.getEvent()) .append("-").append(wxMessage.getEvent() == null ? "" : wxMessage.getEvent())
; ;
} else { } else {
messageId = String.valueOf(wxMessage.getMsgId()); messageId.append(wxMessage.getMsgId());
} }
if (messageDuplicateChecker.isDuplicate(messageId)) { if (messageDuplicateChecker.isDuplicate(messageId.toString())) {
return true; return true;
} }
return false; return false;

View File

@ -105,9 +105,10 @@ public class WxMpServiceImpl implements WxMpService {
if (wxMpConfigStorage.isAccessTokenExpired()) { if (wxMpConfigStorage.isAccessTokenExpired()) {
synchronized (globalAccessTokenRefreshLock) { synchronized (globalAccessTokenRefreshLock) {
if (wxMpConfigStorage.isAccessTokenExpired()) { if (wxMpConfigStorage.isAccessTokenExpired()) {
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" String url = new StringBuffer()
+ "&appid=" + wxMpConfigStorage.getAppId() .append("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential")
+ "&secret=" + wxMpConfigStorage.getSecret(); .append("&appid=").append(wxMpConfigStorage.getAppId())
.append("&secret=").append(wxMpConfigStorage.getSecret()).toString();
try { try {
HttpGet httpGet = new HttpGet(url); HttpGet httpGet = new HttpGet(url);
if (httpProxy != null) { if (httpProxy != null) {
@ -522,29 +523,31 @@ public class WxMpServiceImpl implements WxMpService {
@Override @Override
public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) { public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) {
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?"; StringBuffer url = new StringBuffer();
url += "appid=" + wxMpConfigStorage.getAppId(); url.append("https://open.weixin.qq.com/connect/oauth2/authorize?");
url += "&redirect_uri=" + URIUtil.encodeURIComponent(redirectURI); url.append("appid=").append(wxMpConfigStorage.getAppId());
url += "&response_type=code"; url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectURI));
url += "&scope=" + scope; url.append("&response_type=code");
url.append("&scope=").append(scope);
if (state != null) { if (state != null) {
url += "&state=" + state; url.append("&state=").append(state);
} }
url += "#wechat_redirect"; url.append("#wechat_redirect");
return url; return url.toString();
} }
@Override @Override
public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException { public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException {
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?"; StringBuffer url = new StringBuffer();
url += "appid=" + wxMpConfigStorage.getAppId(); url.append("https://api.weixin.qq.com/sns/oauth2/access_token?");
url += "&secret=" + wxMpConfigStorage.getSecret(); url.append("appid=").append(wxMpConfigStorage.getAppId());
url += "&code=" + code; url.append("&secret=").append(wxMpConfigStorage.getSecret());
url += "&grant_type=authorization_code"; url.append("&code=").append(code);
url.append("&grant_type=authorization_code");
try { try {
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
String responseText = executor.execute(getHttpclient(), httpProxy, url, null); String responseText = executor.execute(getHttpclient(), httpProxy, url.toString(), null);
return WxMpOAuth2AccessToken.fromJson(responseText); return WxMpOAuth2AccessToken.fromJson(responseText);
} catch (ClientProtocolException e) { } catch (ClientProtocolException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -555,14 +558,15 @@ public class WxMpServiceImpl implements WxMpService {
@Override @Override
public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException { public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException {
String url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?"; StringBuffer url = new StringBuffer();
url += "appid=" + wxMpConfigStorage.getAppId(); url.append("https://api.weixin.qq.com/sns/oauth2/refresh_token?");
url += "&grant_type=refresh_token"; url.append("appid=").append(wxMpConfigStorage.getAppId());
url += "&refresh_token=" + refreshToken; url.append("&grant_type=refresh_token");
url.append("&refresh_token=").append(refreshToken);
try { try {
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
String responseText = executor.execute(getHttpclient(), httpProxy, url, null); String responseText = executor.execute(getHttpclient(), httpProxy, url.toString(), null);
return WxMpOAuth2AccessToken.fromJson(responseText); return WxMpOAuth2AccessToken.fromJson(responseText);
} catch (ClientProtocolException e) { } catch (ClientProtocolException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -573,18 +577,19 @@ public class WxMpServiceImpl implements WxMpService {
@Override @Override
public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException { public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException {
String url = "https://api.weixin.qq.com/sns/userinfo?"; StringBuffer url = new StringBuffer();
url += "access_token=" + oAuth2AccessToken.getAccessToken(); url.append("https://api.weixin.qq.com/sns/userinfo?");
url += "&openid=" + oAuth2AccessToken.getOpenId(); url.append("access_token=").append(oAuth2AccessToken.getAccessToken());
url.append("&openid=").append(oAuth2AccessToken.getOpenId());
if (lang == null) { if (lang == null) {
url += "&lang=zh_CN"; url.append("&lang=zh_CN");
} else { } else {
url += "&lang=" + lang; url.append("&lang=").append(lang);
} }
try { try {
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
String responseText = executor.execute(getHttpclient(), httpProxy, url, null); String responseText = executor.execute(getHttpclient(), httpProxy, url.toString(), null);
return WxMpUser.fromJson(responseText); return WxMpUser.fromJson(responseText);
} catch (ClientProtocolException e) { } catch (ClientProtocolException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -595,13 +600,14 @@ public class WxMpServiceImpl implements WxMpService {
@Override @Override
public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken) { public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken) {
String url = "https://api.weixin.qq.com/sns/auth?"; StringBuffer url = new StringBuffer();
url += "access_token=" + oAuth2AccessToken.getAccessToken(); url.append("https://api.weixin.qq.com/sns/auth?");
url += "&openid=" + oAuth2AccessToken.getOpenId(); url.append("access_token=").append(oAuth2AccessToken.getAccessToken());
url.append("&openid=").append(oAuth2AccessToken.getOpenId());
try { try {
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor(); RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
executor.execute(getHttpclient(), httpProxy, url, null); executor.execute(getHttpclient(), httpProxy, url.toString(), null);
} catch (ClientProtocolException e) { } catch (ClientProtocolException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} catch (IOException e) { } catch (IOException e) {

View File

@ -30,15 +30,15 @@ import java.util.UUID;
public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTicket> { public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTicket> {
@Override @Override
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpQrCodeTicket ticket) throws WxErrorException, ClientProtocolException, IOException { public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri,
WxMpQrCodeTicket ticket) throws WxErrorException, ClientProtocolException, IOException {
if (ticket != null) { if (ticket != null) {
if (uri.indexOf('?') == -1) { if (uri.indexOf('?') == -1) {
uri += '?'; uri += '?';
} }
uri += uri.endsWith("?") ? uri += uri.endsWith("?")
"ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8") ? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8")
: : "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8");
"&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8");
} }
HttpGet httpGet = new HttpGet(uri); HttpGet httpGet = new HttpGet(uri);
@ -59,7 +59,7 @@ public class QrCodeRequestExecutor implements RequestExecutor<File, WxMpQrCodeTi
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response); InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
}finally { } finally {
httpGet.releaseConnection(); httpGet.releaseConnection();
} }

View File

@ -1,18 +1,15 @@
package me.chanjar.weixin.mp.util.json; package me.chanjar.weixin.mp.util.json;
import java.lang.reflect.Type;
import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer; import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
import me.chanjar.weixin.common.util.json.GsonHelper; import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.mp.bean.WxMpCard; import me.chanjar.weixin.mp.bean.WxMpCard;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Type;
import java.util.List;
/** /**
* Created by YuJian on 15/11/11. * Created by YuJian on 15/11/11.

View File

@ -1,20 +1,17 @@
package me.chanjar.weixin.mp.util.json; package me.chanjar.weixin.mp.util.json;
import java.lang.reflect.Type;
import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer; import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import me.chanjar.weixin.common.util.json.GsonHelper; import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.mp.bean.WxMpCard; import me.chanjar.weixin.mp.bean.WxMpCard;
import me.chanjar.weixin.mp.bean.result.WxMpCardResult; import me.chanjar.weixin.mp.bean.result.WxMpCardResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.util.List;
/** /**
* Created by YuJian on 15/11/11. * Created by YuJian on 15/11/11.