优化部分代码

This commit is contained in:
Binary Wang
2018-01-27 18:39:37 +08:00
parent 5bd2d209db
commit 48d3163b33
13 changed files with 50 additions and 247 deletions

View File

@@ -13,8 +13,11 @@ import me.chanjar.weixin.common.util.RandomUtils;
import me.chanjar.weixin.common.util.crypto.SHA1;
import me.chanjar.weixin.common.util.http.*;
import me.chanjar.weixin.mp.api.*;
import me.chanjar.weixin.mp.bean.*;
import me.chanjar.weixin.mp.bean.result.*;
import me.chanjar.weixin.mp.bean.WxMpSemanticQuery;
import me.chanjar.weixin.mp.bean.result.WxMpCurrentAutoReplyInfo;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -22,7 +25,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.concurrent.locks.Lock;
public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, RequestHttp<H, P> {
public abstract class WxMpServiceBaseImpl<H, P> implements WxMpService, RequestHttp<H, P> {
private static final JsonParser JSON_PARSER = new JsonParser();
@@ -92,14 +95,14 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
@Override
public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException {
long timestamp = System.currentTimeMillis() / 1000;
String noncestr = RandomUtils.getRandomStr();
String randomStr = RandomUtils.getRandomStr();
String jsapiTicket = getJsapiTicket(false);
String signature = SHA1.genWithAmple("jsapi_ticket=" + jsapiTicket,
"noncestr=" + noncestr, "timestamp=" + timestamp, "url=" + url);
"noncestr=" + randomStr, "timestamp=" + timestamp, "url=" + url);
WxJsapiSignature jsapiSignature = new WxJsapiSignature();
jsapiSignature.setAppId(this.getWxMpConfigStorage().getAppId());
jsapiSignature.setTimestamp(timestamp);
jsapiSignature.setNonceStr(noncestr);
jsapiSignature.setNonceStr(randomStr);
jsapiSignature.setUrl(url);
jsapiSignature.setSignature(signature);
return jsapiSignature;
@@ -111,10 +114,10 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
}
@Override
public String shortUrl(String long_url) throws WxErrorException {
public String shortUrl(String longUrl) throws WxErrorException {
JsonObject o = new JsonObject();
o.addProperty("action", "long2short");
o.addProperty("long_url", long_url);
o.addProperty("long_url", longUrl);
String responseContent = this.post(WxMpService.SHORTURL_API_URL, o.toString());
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
return tmpJsonElement.getAsJsonObject().get("short_url").getAsString();
@@ -161,12 +164,12 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
}
@Override
public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException {
public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken token, String lang) throws WxErrorException {
if (lang == null) {
lang = "zh_CN";
}
String url = String.format(WxMpService.OAUTH2_USERINFO_URL, oAuth2AccessToken.getAccessToken(), oAuth2AccessToken.getOpenId(), lang);
String url = String.format(WxMpService.OAUTH2_USERINFO_URL, token.getAccessToken(), token.getOpenId(), lang);
try {
RequestExecutor<String, String> executor = SimpleGetRequestExecutor.create(this);
@@ -178,8 +181,8 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
}
@Override
public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken) {
String url = String.format(WxMpService.OAUTH2_VALIDATE_TOKEN_URL, oAuth2AccessToken.getAccessToken(), oAuth2AccessToken.getOpenId());
public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken token) {
String url = String.format(WxMpService.OAUTH2_VALIDATE_TOKEN_URL, token.getAccessToken(), token.getOpenId());
try {
SimpleGetRequestExecutor.create(this).execute(url, null);
@@ -226,7 +229,7 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
}
/**
* 向微信端发送请求在这里执行的策略是当发生access_token过期时才去刷新然后重新执行请求而不是全局定时请求
* 向微信端发送请求在这里执行的策略是当发生access_token过期时才去刷新然后重新执行请求而不是全局定时请求.
*/
@Override
public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
@@ -265,6 +268,7 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
if (uri.contains("access_token=")) {
throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
}
String accessToken = getAccessToken(false);
String uriWithAccessToken = uri + (uri.contains("?") ? "&" : "?") + "access_token=" + accessToken;
@@ -296,7 +300,7 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
return null;
} catch (IOException e) {
this.log.error("\n【请求地址】: {}\n【请求参数】{}\n【异常信息】{}", uriWithAccessToken, data, e.getMessage());
throw new RuntimeException(e);
throw new WxErrorException(WxError.builder().errorMsg(e.getMessage()).build(), e);
}
}

View File

@@ -19,9 +19,9 @@ import java.io.IOException;
import java.util.concurrent.locks.Lock;
/**
* apache-http方式实现
* apache http client方式实现.
*/
public class WxMpServiceApacheHttpClientImpl extends WxMpServiceAbstractImpl<CloseableHttpClient, HttpHost> {
public class WxMpServiceHttpClientImpl extends WxMpServiceBaseImpl<CloseableHttpClient, HttpHost> {
private CloseableHttpClient httpClient;
private HttpHost httpProxy;

View File

@@ -8,5 +8,5 @@ package me.chanjar.weixin.mp.api.impl;
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public class WxMpServiceImpl extends WxMpServiceApacheHttpClientImpl {
public class WxMpServiceImpl extends WxMpServiceHttpClientImpl {
}

View File

@@ -14,7 +14,7 @@ import java.util.concurrent.locks.Lock;
/**
* jodd-http方式实现
*/
public class WxMpServiceJoddHttpImpl extends WxMpServiceAbstractImpl<HttpConnectionProvider, ProxyInfo> {
public class WxMpServiceJoddHttpImpl extends WxMpServiceBaseImpl<HttpConnectionProvider, ProxyInfo> {
private HttpConnectionProvider httpClient;
private ProxyInfo httpProxy;

View File

@@ -11,7 +11,10 @@ import okhttp3.*;
import java.io.IOException;
import java.util.concurrent.locks.Lock;
public class WxMpServiceOkHttpImpl extends WxMpServiceAbstractImpl<OkHttpClient, OkHttpProxyInfo> {
/**
* okhttp实现
*/
public class WxMpServiceOkHttpImpl extends WxMpServiceBaseImpl<OkHttpClient, OkHttpProxyInfo> {
private OkHttpClient httpClient;
private OkHttpProxyInfo httpProxy;

View File

@@ -3,7 +3,7 @@ package me.chanjar.weixin.mp.api;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.mp.api.impl.WxMpServiceApacheHttpClientImpl;
import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl;
import org.testng.annotations.*;
import java.util.concurrent.ExecutionException;
@@ -16,7 +16,7 @@ public class WxMpBusyRetryTest {
@DataProvider(name = "getService")
public Object[][] getService() {
WxMpService service = new WxMpServiceApacheHttpClientImpl() {
WxMpService service = new WxMpServiceHttpClientImpl() {
@Override
public synchronized <T, E> T executeInternal(

View File

@@ -5,7 +5,7 @@ import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceApacheHttpClientImpl;
import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
@@ -47,7 +47,7 @@ public class WxMpDemoServer {
.fromXml(is1);
wxMpConfigStorage = config;
wxMpService = new WxMpServiceApacheHttpClientImpl();
wxMpService = new WxMpServiceHttpClientImpl();
wxMpService.setWxMpConfigStorage(config);
WxMpMessageHandler logHandler = new DemoLogHandler();