Skip to content

Commit 48d3163

Browse files
committed
优化部分代码
1 parent 5bd2d20 commit 48d3163

File tree

13 files changed

+50
-247
lines changed

13 files changed

+50
-247
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceAbstractImpl.java renamed to weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceBaseImpl.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@
1313
import me.chanjar.weixin.common.util.crypto.SHA1;
1414
import me.chanjar.weixin.common.util.http.*;
1515
import me.chanjar.weixin.mp.api.*;
16-
import me.chanjar.weixin.mp.bean.*;
17-
import me.chanjar.weixin.mp.bean.result.*;
16+
import me.chanjar.weixin.mp.bean.WxMpSemanticQuery;
17+
import me.chanjar.weixin.mp.bean.result.WxMpCurrentAutoReplyInfo;
18+
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
19+
import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult;
20+
import me.chanjar.weixin.mp.bean.result.WxMpUser;
1821
import org.apache.commons.lang3.StringUtils;
1922
import org.slf4j.Logger;
2023
import org.slf4j.LoggerFactory;
2124

2225
import java.io.IOException;
2326
import java.util.concurrent.locks.Lock;
2427

25-
public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, RequestHttp<H, P> {
28+
public abstract class WxMpServiceBaseImpl<H, P> implements WxMpService, RequestHttp<H, P> {
2629

2730
private static final JsonParser JSON_PARSER = new JsonParser();
2831

@@ -92,14 +95,14 @@ public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
9295
@Override
9396
public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException {
9497
long timestamp = System.currentTimeMillis() / 1000;
95-
String noncestr = RandomUtils.getRandomStr();
98+
String randomStr = RandomUtils.getRandomStr();
9699
String jsapiTicket = getJsapiTicket(false);
97100
String signature = SHA1.genWithAmple("jsapi_ticket=" + jsapiTicket,
98-
"noncestr=" + noncestr, "timestamp=" + timestamp, "url=" + url);
101+
"noncestr=" + randomStr, "timestamp=" + timestamp, "url=" + url);
99102
WxJsapiSignature jsapiSignature = new WxJsapiSignature();
100103
jsapiSignature.setAppId(this.getWxMpConfigStorage().getAppId());
101104
jsapiSignature.setTimestamp(timestamp);
102-
jsapiSignature.setNonceStr(noncestr);
105+
jsapiSignature.setNonceStr(randomStr);
103106
jsapiSignature.setUrl(url);
104107
jsapiSignature.setSignature(signature);
105108
return jsapiSignature;
@@ -111,10 +114,10 @@ public String getAccessToken() throws WxErrorException {
111114
}
112115

113116
@Override
114-
public String shortUrl(String long_url) throws WxErrorException {
117+
public String shortUrl(String longUrl) throws WxErrorException {
115118
JsonObject o = new JsonObject();
116119
o.addProperty("action", "long2short");
117-
o.addProperty("long_url", long_url);
120+
o.addProperty("long_url", longUrl);
118121
String responseContent = this.post(WxMpService.SHORTURL_API_URL, o.toString());
119122
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
120123
return tmpJsonElement.getAsJsonObject().get("short_url").getAsString();
@@ -161,12 +164,12 @@ public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throw
161164
}
162165

163166
@Override
164-
public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException {
167+
public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken token, String lang) throws WxErrorException {
165168
if (lang == null) {
166169
lang = "zh_CN";
167170
}
168171

169-
String url = String.format(WxMpService.OAUTH2_USERINFO_URL, oAuth2AccessToken.getAccessToken(), oAuth2AccessToken.getOpenId(), lang);
172+
String url = String.format(WxMpService.OAUTH2_USERINFO_URL, token.getAccessToken(), token.getOpenId(), lang);
170173

171174
try {
172175
RequestExecutor<String, String> executor = SimpleGetRequestExecutor.create(this);
@@ -178,8 +181,8 @@ public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, Strin
178181
}
179182

180183
@Override
181-
public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken) {
182-
String url = String.format(WxMpService.OAUTH2_VALIDATE_TOKEN_URL, oAuth2AccessToken.getAccessToken(), oAuth2AccessToken.getOpenId());
184+
public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken token) {
185+
String url = String.format(WxMpService.OAUTH2_VALIDATE_TOKEN_URL, token.getAccessToken(), token.getOpenId());
183186

184187
try {
185188
SimpleGetRequestExecutor.create(this).execute(url, null);
@@ -226,7 +229,7 @@ public String post(String url, String postData) throws WxErrorException {
226229
}
227230

228231
/**
229-
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求
232+
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求.
230233
*/
231234
@Override
232235
public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
@@ -265,6 +268,7 @@ public <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E da
265268
if (uri.contains("access_token=")) {
266269
throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
267270
}
271+
268272
String accessToken = getAccessToken(false);
269273

270274
String uriWithAccessToken = uri + (uri.contains("?") ? "&" : "?") + "access_token=" + accessToken;
@@ -296,7 +300,7 @@ public <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E da
296300
return null;
297301
} catch (IOException e) {
298302
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", uriWithAccessToken, data, e.getMessage());
299-
throw new RuntimeException(e);
303+
throw new WxErrorException(WxError.builder().errorMsg(e.getMessage()).build(), e);
300304
}
301305
}
302306

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceApacheHttpClientImpl.java renamed to weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceHttpClientImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import java.util.concurrent.locks.Lock;
2020

2121
/**
22-
* apache-http方式实现
22+
* apache http client方式实现.
2323
*/
24-
public class WxMpServiceApacheHttpClientImpl extends WxMpServiceAbstractImpl<CloseableHttpClient, HttpHost> {
24+
public class WxMpServiceHttpClientImpl extends WxMpServiceBaseImpl<CloseableHttpClient, HttpHost> {
2525
private CloseableHttpClient httpClient;
2626
private HttpHost httpProxy;
2727

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
*
99
* @author <a href="https://github.com/binarywang">Binary Wang</a>
1010
*/
11-
public class WxMpServiceImpl extends WxMpServiceApacheHttpClientImpl {
11+
public class WxMpServiceImpl extends WxMpServiceHttpClientImpl {
1212
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceJoddHttpImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* jodd-http方式实现
1616
*/
17-
public class WxMpServiceJoddHttpImpl extends WxMpServiceAbstractImpl<HttpConnectionProvider, ProxyInfo> {
17+
public class WxMpServiceJoddHttpImpl extends WxMpServiceBaseImpl<HttpConnectionProvider, ProxyInfo> {
1818
private HttpConnectionProvider httpClient;
1919
private ProxyInfo httpProxy;
2020

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceOkHttpImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
import java.io.IOException;
1212
import java.util.concurrent.locks.Lock;
1313

14-
public class WxMpServiceOkHttpImpl extends WxMpServiceAbstractImpl<OkHttpClient, OkHttpProxyInfo> {
14+
/**
15+
* okhttp实现
16+
*/
17+
public class WxMpServiceOkHttpImpl extends WxMpServiceBaseImpl<OkHttpClient, OkHttpProxyInfo> {
1518
private OkHttpClient httpClient;
1619
private OkHttpProxyInfo httpProxy;
1720

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpBusyRetryTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import me.chanjar.weixin.common.bean.result.WxError;
44
import me.chanjar.weixin.common.exception.WxErrorException;
55
import me.chanjar.weixin.common.util.http.RequestExecutor;
6-
import me.chanjar.weixin.mp.api.impl.WxMpServiceApacheHttpClientImpl;
6+
import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl;
77
import org.testng.annotations.*;
88

99
import java.util.concurrent.ExecutionException;
@@ -16,7 +16,7 @@ public class WxMpBusyRetryTest {
1616

1717
@DataProvider(name = "getService")
1818
public Object[][] getService() {
19-
WxMpService service = new WxMpServiceApacheHttpClientImpl() {
19+
WxMpService service = new WxMpServiceHttpClientImpl() {
2020

2121
@Override
2222
public synchronized <T, E> T executeInternal(

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/demo/WxMpDemoServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
66
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
77
import me.chanjar.weixin.mp.api.WxMpService;
8-
import me.chanjar.weixin.mp.api.impl.WxMpServiceApacheHttpClientImpl;
8+
import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl;
99
import org.eclipse.jetty.server.Server;
1010
import org.eclipse.jetty.servlet.ServletHandler;
1111
import org.eclipse.jetty.servlet.ServletHolder;
@@ -47,7 +47,7 @@ private static void initWeixin() {
4747
.fromXml(is1);
4848

4949
wxMpConfigStorage = config;
50-
wxMpService = new WxMpServiceApacheHttpClientImpl();
50+
wxMpService = new WxMpServiceHttpClientImpl();
5151
wxMpService.setWxMpConfigStorage(config);
5252

5353
WxMpMessageHandler logHandler = new DemoLogHandler();

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/entpay/EntPayRequest.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
public class EntPayRequest extends BaseWxPayRequest {
2525
/**
2626
* <pre>
27-
* 字段名:公众账号appid
27+
* 字段名:公众账号appid.
2828
* 变量名:mch_appid
2929
* 是否必填:是
3030
* 示例值:wx8888888888888888
@@ -37,7 +37,7 @@ public class EntPayRequest extends BaseWxPayRequest {
3737

3838
/**
3939
* <pre>
40-
* 字段名:商户号
40+
* 字段名:商户号.
4141
* 变量名:mchid
4242
* 是否必填:是
4343
* 示例值:1900000109
@@ -50,7 +50,7 @@ public class EntPayRequest extends BaseWxPayRequest {
5050

5151
/**
5252
* <pre>
53-
* 字段名:设备号
53+
* 字段名:设备号.
5454
* 变量名:device_info
5555
* 是否必填:否
5656
* 示例值:13467007045764
@@ -63,7 +63,7 @@ public class EntPayRequest extends BaseWxPayRequest {
6363

6464
/**
6565
* <pre>
66-
* 字段名:商户订单号
66+
* 字段名:商户订单号.
6767
* 变量名:partner_trade_no
6868
* 是否必填:是
6969
* 示例值:10000098201411111234567890
@@ -77,7 +77,7 @@ public class EntPayRequest extends BaseWxPayRequest {
7777

7878
/**
7979
* <pre>
80-
* 字段名:需保持唯一性 用户openid
80+
* 字段名:需保持唯一性 用户openid.
8181
* 变量名:openid
8282
* 是否必填:是
8383
* 示例值:oxTWIuGaIt6gTKsQRLau2M0yL16E
@@ -91,7 +91,7 @@ public class EntPayRequest extends BaseWxPayRequest {
9191

9292
/**
9393
* <pre>
94-
* 字段名:校验用户姓名选项
94+
* 字段名:校验用户姓名选项.
9595
* 变量名:check_name
9696
* 是否必填:是
9797
* 示例值:OPTION_CHECK
@@ -107,7 +107,7 @@ public class EntPayRequest extends BaseWxPayRequest {
107107

108108
/**
109109
* <pre>
110-
* 字段名:收款用户姓名
110+
* 字段名:收款用户姓名.
111111
* 变量名:re_user_name
112112
* 是否必填:可选
113113
* 示例值:马花花
@@ -121,7 +121,7 @@ public class EntPayRequest extends BaseWxPayRequest {
121121

122122
/**
123123
* <pre>
124-
* 字段名:金额
124+
* 字段名:金额.
125125
* 变量名:amount
126126
* 是否必填:是
127127
* 示例值:10099
@@ -135,7 +135,7 @@ public class EntPayRequest extends BaseWxPayRequest {
135135

136136
/**
137137
* <pre>
138-
* 字段名:企业付款描述信息
138+
* 字段名:企业付款描述信息.
139139
* 变量名:desc
140140
* 是否必填:是
141141
* 示例值:理赔
@@ -149,7 +149,7 @@ public class EntPayRequest extends BaseWxPayRequest {
149149

150150
/**
151151
* <pre>
152-
* 字段名:Ip地址
152+
* 字段名:Ip地址.
153153
* 变量名:spbill_create_ip
154154
* 是否必填:是
155155
* 示例值:192.168.0.1
@@ -191,4 +191,8 @@ public String toString() {
191191
return ToStringUtils.toSimpleString(this);
192192
}
193193

194+
@Override
195+
protected boolean ignoreSignType() {
196+
return true;
197+
}
194198
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxEntPayQueryRequest.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)