Skip to content

Commit a7846f9

Browse files
committed
重构部分代码,抽取重复代码为单独方法,并调整部分代码顺序
1 parent 30c914f commit a7846f9

File tree

1 file changed

+41
-44
lines changed

1 file changed

+41
-44
lines changed

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

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.http.client.config.RequestConfig;
2222
import org.apache.http.client.methods.CloseableHttpResponse;
2323
import org.apache.http.client.methods.HttpGet;
24+
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
2425
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
2526
import org.apache.http.impl.client.BasicResponseHandler;
2627
import org.apache.http.impl.client.CloseableHttpClient;
@@ -209,6 +210,13 @@ public WxMpMassSendResult massOpenIdsMessageSend(WxMpMassOpenIdsMessage message)
209210
return WxMpMassSendResult.fromJson(responseContent);
210211
}
211212

213+
@Override
214+
public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception {
215+
String url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview";
216+
String responseContent = execute(new SimplePostRequestExecutor(), url, wxMpMassPreviewMessage.toJson());
217+
return WxMpMassSendResult.fromJson(responseContent);
218+
}
219+
212220
@Override
213221
public String shortUrl(String long_url) throws WxErrorException {
214222
String url = "https://api.weixin.qq.com/cgi-bin/shorturl";
@@ -226,11 +234,30 @@ public String templateSend(WxMpTemplateMessage templateMessage) throws WxErrorEx
226234
String responseContent = execute(new SimplePostRequestExecutor(), url, templateMessage.toJson());
227235
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
228236
final JsonObject jsonObject = tmpJsonElement.getAsJsonObject();
229-
if (jsonObject.get("errcode").getAsInt() == 0)
237+
if (jsonObject.get("errcode").getAsInt() == 0){
230238
return jsonObject.get("msgid").getAsString();
239+
}
240+
231241
throw new WxErrorException(WxError.fromJson(responseContent));
232242
}
233243

244+
@Override
245+
public String setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException {
246+
if (null == wxMpIndustry.getPrimaryIndustry() || null == wxMpIndustry.getPrimaryIndustry().getId()
247+
|| null == wxMpIndustry.getSecondIndustry() || null == wxMpIndustry.getSecondIndustry().getId()) {
248+
throw new IllegalArgumentException("industry id is empty");
249+
}
250+
String url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry";
251+
return execute(new SimplePostRequestExecutor(), url, wxMpIndustry.toJson());
252+
}
253+
254+
@Override
255+
public WxMpIndustry getIndustry() throws WxErrorException {
256+
String url = "https://api.weixin.qq.com/cgi-bin/template/get_industry";
257+
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
258+
return WxMpIndustry.fromJson(responseContent);
259+
}
260+
234261
@Override
235262
public WxMpSemanticQueryResult semanticQuery(WxMpSemanticQuery semanticQuery) throws WxErrorException {
236263
String url = "https://api.weixin.qq.com/semantic/semproxy/search";
@@ -253,6 +280,16 @@ public String oauth2buildAuthorizationUrl(String redirectURI, String scope, Stri
253280
return url.toString();
254281
}
255282

283+
private WxMpOAuth2AccessToken getOAuth2AccessToken(StringBuilder url) throws WxErrorException {
284+
try {
285+
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
286+
String responseText = executor.execute(this.getHttpclient(), this.httpProxy, url.toString(), null);
287+
return WxMpOAuth2AccessToken.fromJson(responseText);
288+
} catch (IOException e) {
289+
throw new RuntimeException(e);
290+
}
291+
}
292+
256293
@Override
257294
public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException {
258295
StringBuilder url = new StringBuilder();
@@ -262,13 +299,7 @@ public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorExc
262299
url.append("&code=").append(code);
263300
url.append("&grant_type=authorization_code");
264301

265-
try {
266-
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
267-
String responseText = executor.execute(getHttpclient(), this.httpProxy, url.toString(), null);
268-
return WxMpOAuth2AccessToken.fromJson(responseText);
269-
} catch (IOException e) {
270-
throw new RuntimeException(e);
271-
}
302+
return this.getOAuth2AccessToken(url);
272303
}
273304

274305
@Override
@@ -279,13 +310,7 @@ public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throw
279310
url.append("&grant_type=refresh_token");
280311
url.append("&refresh_token=").append(refreshToken);
281312

282-
try {
283-
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
284-
String responseText = executor.execute(getHttpclient(), this.httpProxy, url.toString(), null);
285-
return WxMpOAuth2AccessToken.fromJson(responseText);
286-
} catch (IOException e) {
287-
throw new RuntimeException(e);
288-
}
313+
return this.getOAuth2AccessToken(url);
289314
}
290315

291316
@Override
@@ -440,10 +465,7 @@ private void initHttpClient() {
440465

441466
if (this.wxMpConfigStorage.getSSLContext() != null){
442467
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
443-
this.wxMpConfigStorage.getSSLContext(),
444-
new String[] { "TLSv1" },
445-
null,
446-
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
468+
this.wxMpConfigStorage.getSSLContext(), new String[] { "TLSv1" }, null, new DefaultHostnameVerifier());
447469
apacheHttpClientBuilder.sslConnectionSocketFactory(sslsf);
448470
}
449471

@@ -460,36 +482,11 @@ public void setRetrySleepMillis(int retrySleepMillis) {
460482
this.retrySleepMillis = retrySleepMillis;
461483
}
462484

463-
464485
@Override
465486
public void setMaxRetryTimes(int maxRetryTimes) {
466487
this.maxRetryTimes = maxRetryTimes;
467488
}
468489

469-
@Override
470-
public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception {
471-
String url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview";
472-
String responseContent = execute(new SimplePostRequestExecutor(), url, wxMpMassPreviewMessage.toJson());
473-
return WxMpMassSendResult.fromJson(responseContent);
474-
}
475-
476-
@Override
477-
public String setIndustry(WxMpIndustry wxMpIndustry) throws WxErrorException {
478-
if (null == wxMpIndustry.getPrimaryIndustry() || null == wxMpIndustry.getPrimaryIndustry().getId()
479-
|| null == wxMpIndustry.getSecondIndustry() || null == wxMpIndustry.getSecondIndustry().getId()) {
480-
throw new IllegalArgumentException("industry id is empty");
481-
}
482-
String url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry";
483-
return execute(new SimplePostRequestExecutor(), url, wxMpIndustry.toJson());
484-
}
485-
486-
@Override
487-
public WxMpIndustry getIndustry() throws WxErrorException {
488-
String url = "https://api.weixin.qq.com/cgi-bin/template/get_industry";
489-
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
490-
return WxMpIndustry.fromJson(responseContent);
491-
}
492-
493490
@Override
494491
public WxMpKefuService getKefuService() {
495492
return this.kefuService;

0 commit comments

Comments
 (0)