Skip to content

Commit 72d6aad

Browse files
committed
修复 #80 menuCreate()方法代码的问题,and reformat code
1 parent e4d5545 commit 72d6aad

File tree

1 file changed

+63
-68
lines changed

1 file changed

+63
-68
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpServiceImpl.java

Lines changed: 63 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
2424
import org.apache.commons.lang3.StringUtils;
2525
import org.apache.http.HttpHost;
26-
import org.apache.http.client.ClientProtocolException;
2726
import org.apache.http.client.config.RequestConfig;
2827
import org.apache.http.client.methods.CloseableHttpResponse;
2928
import org.apache.http.client.methods.HttpGet;
@@ -69,7 +68,7 @@ public class WxCpServiceImpl implements WxCpService {
6968
public boolean checkSignature(String msgSignature, String timestamp, String nonce, String data) {
7069
try {
7170
return SHA1.gen(this.configStorage.getToken(), timestamp, nonce, data)
72-
.equals(msgSignature);
71+
.equals(msgSignature);
7372
} catch (Exception e) {
7473
return false;
7574
}
@@ -95,18 +94,18 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
9594
synchronized (this.globalAccessTokenRefreshLock) {
9695
if (this.configStorage.isAccessTokenExpired()) {
9796
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
98-
+ "&corpid=" + this.configStorage.getCorpId()
99-
+ "&corpsecret=" + this.configStorage.getCorpSecret();
97+
+ "&corpid=" + this.configStorage.getCorpId()
98+
+ "&corpsecret=" + this.configStorage.getCorpSecret();
10099
try {
101100
HttpGet httpGet = new HttpGet(url);
102101
if (this.httpProxy != null) {
103102
RequestConfig config = RequestConfig.custom()
104-
.setProxy(this.httpProxy).build();
103+
.setProxy(this.httpProxy).build();
105104
httpGet.setConfig(config);
106105
}
107106
String resultContent = null;
108107
try (CloseableHttpClient httpclient = getHttpclient();
109-
CloseableHttpResponse response = httpclient.execute(httpGet)) {
108+
CloseableHttpResponse response = httpclient.execute(httpGet)) {
110109
resultContent = new BasicResponseHandler().handleResponse(response);
111110
} finally {
112111
httpGet.releaseConnection();
@@ -117,9 +116,7 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
117116
}
118117
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
119118
this.configStorage.updateAccessToken(
120-
accessToken.getAccessToken(), accessToken.getExpiresIn());
121-
} catch (ClientProtocolException e) {
122-
throw new RuntimeException(e);
119+
accessToken.getAccessToken(), accessToken.getExpiresIn());
123120
} catch (IOException e) {
124121
throw new RuntimeException(e);
125122
}
@@ -149,7 +146,7 @@ public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
149146
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
150147
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
151148
this.configStorage.updateJsapiTicket(jsapiTicket,
152-
expiresInSeconds);
149+
expiresInSeconds);
153150
}
154151
}
155152
}
@@ -162,10 +159,10 @@ public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException
162159
String noncestr = RandomUtils.getRandomStr();
163160
String jsapiTicket = getJsapiTicket(false);
164161
String signature = SHA1.genWithAmple(
165-
"jsapi_ticket=" + jsapiTicket,
166-
"noncestr=" + noncestr,
167-
"timestamp=" + timestamp,
168-
"url=" + url
162+
"jsapi_ticket=" + jsapiTicket,
163+
"noncestr=" + noncestr,
164+
"timestamp=" + timestamp,
165+
"url=" + url
169166
);
170167
WxJsapiSignature jsapiSignature = new WxJsapiSignature();
171168
jsapiSignature.setTimestamp(timestamp);
@@ -193,7 +190,7 @@ public void menuCreate(WxMenu menu) throws WxErrorException {
193190
@Override
194191
public void menuCreate(Integer agentId, WxMenu menu) throws WxErrorException {
195192
String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?agentid="
196-
+ this.configStorage.getAgentId();
193+
+ this.configStorage.getAgentId();
197194
post(url, menu.toJson());
198195
}
199196

@@ -230,7 +227,7 @@ public WxMenu menuGet(Integer agentId) throws WxErrorException {
230227

231228
@Override
232229
public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream)
233-
throws WxErrorException, IOException {
230+
throws WxErrorException, IOException {
234231
return mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType));
235232
}
236233

@@ -244,19 +241,19 @@ public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErr
244241
public File mediaDownload(String media_id) throws WxErrorException {
245242
String url = "https://qyapi.weixin.qq.com/cgi-bin/media/get";
246243
return execute(
247-
new MediaDownloadRequestExecutor(
248-
this.configStorage.getTmpDirFile()),
249-
url, "media_id=" + media_id);
244+
new MediaDownloadRequestExecutor(
245+
this.configStorage.getTmpDirFile()),
246+
url, "media_id=" + media_id);
250247
}
251248

252249

253250
@Override
254251
public Integer departCreate(WxCpDepart depart) throws WxErrorException {
255252
String url = "https://qyapi.weixin.qq.com/cgi-bin/department/create";
256253
String responseContent = execute(
257-
new SimplePostRequestExecutor(),
258-
url,
259-
depart.toJson());
254+
new SimplePostRequestExecutor(),
255+
url,
256+
depart.toJson());
260257
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
261258
return GsonHelper.getAsInteger(tmpJsonElement.getAsJsonObject().get("id"));
262259
}
@@ -283,11 +280,11 @@ public List<WxCpDepart> departGet() throws WxErrorException {
283280
*/
284281
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
285282
return WxCpGsonBuilder.INSTANCE.create()
286-
.fromJson(
287-
tmpJsonElement.getAsJsonObject().get("department"),
288-
new TypeToken<List<WxCpDepart>>() {
289-
}.getType()
290-
);
283+
.fromJson(
284+
tmpJsonElement.getAsJsonObject().get("department"),
285+
new TypeToken<List<WxCpDepart>>() {
286+
}.getType()
287+
);
291288
}
292289

293290
@Override
@@ -313,8 +310,8 @@ public void userDelete(String[] userids) throws WxErrorException {
313310
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete";
314311
JsonObject jsonObject = new JsonObject();
315312
JsonArray jsonArray = new JsonArray();
316-
for (int i = 0; i < userids.length; i++) {
317-
jsonArray.add(new JsonPrimitive(userids[i]));
313+
for (String userid : userids) {
314+
jsonArray.add(new JsonPrimitive(userid));
318315
}
319316
jsonObject.add("useridlist", jsonArray);
320317
post(url, jsonObject.toString());
@@ -343,11 +340,11 @@ public List<WxCpUser> userList(Integer departId, Boolean fetchChild, Integer sta
343340
String responseContent = get(url, params);
344341
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
345342
return WxCpGsonBuilder.INSTANCE.create()
346-
.fromJson(
347-
tmpJsonElement.getAsJsonObject().get("userlist"),
348-
new TypeToken<List<WxCpUser>>() {
349-
}.getType()
350-
);
343+
.fromJson(
344+
tmpJsonElement.getAsJsonObject().get("userlist"),
345+
new TypeToken<List<WxCpUser>>() {
346+
}.getType()
347+
);
351348
}
352349

353350
@Override
@@ -366,11 +363,11 @@ public List<WxCpUser> departGetUsers(Integer departId, Boolean fetchChild, Integ
366363
String responseContent = get(url, params);
367364
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
368365
return WxCpGsonBuilder.INSTANCE.create()
369-
.fromJson(
370-
tmpJsonElement.getAsJsonObject().get("userlist"),
371-
new TypeToken<List<WxCpUser>>() {
372-
}.getType()
373-
);
366+
.fromJson(
367+
tmpJsonElement.getAsJsonObject().get("userlist"),
368+
new TypeToken<List<WxCpUser>>() {
369+
}.getType()
370+
);
374371
}
375372

376373
@Override
@@ -404,11 +401,11 @@ public List<WxCpTag> tagGet() throws WxErrorException {
404401
String responseContent = get(url, null);
405402
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
406403
return WxCpGsonBuilder.INSTANCE.create()
407-
.fromJson(
408-
tmpJsonElement.getAsJsonObject().get("taglist"),
409-
new TypeToken<List<WxCpTag>>() {
410-
}.getType()
411-
);
404+
.fromJson(
405+
tmpJsonElement.getAsJsonObject().get("taglist"),
406+
new TypeToken<List<WxCpTag>>() {
407+
}.getType()
408+
);
412409
}
413410

414411
@Override
@@ -417,11 +414,11 @@ public List<WxCpUser> tagGetUsers(String tagId) throws WxErrorException {
417414
String responseContent = get(url, null);
418415
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
419416
return WxCpGsonBuilder.INSTANCE.create()
420-
.fromJson(
421-
tmpJsonElement.getAsJsonObject().get("userlist"),
422-
new TypeToken<List<WxCpUser>>() {
423-
}.getType()
424-
);
417+
.fromJson(
418+
tmpJsonElement.getAsJsonObject().get("userlist"),
419+
new TypeToken<List<WxCpUser>>() {
420+
}.getType()
421+
);
425422
}
426423

427424
@Override
@@ -460,14 +457,14 @@ public void tagRemoveUsers(String tagId, List<String> userIds) throws WxErrorExc
460457
}
461458

462459
@Override
463-
public String oauth2buildAuthorizationUrl(String state) {
464-
return this.oauth2buildAuthorizationUrl(
465-
this.configStorage.getOauth2redirectUri(),
466-
state
467-
);
468-
}
460+
public String oauth2buildAuthorizationUrl(String state) {
461+
return this.oauth2buildAuthorizationUrl(
462+
this.configStorage.getOauth2redirectUri(),
463+
state
464+
);
465+
}
469466

470-
@Override
467+
@Override
471468
public String oauth2buildAuthorizationUrl(String redirectUri, String state) {
472469
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?";
473470
url += "appid=" + this.configStorage.getCorpId();
@@ -489,8 +486,8 @@ public String[] oauth2getUserInfo(String code) throws WxErrorException {
489486
@Override
490487
public String[] oauth2getUserInfo(Integer agentId, String code) throws WxErrorException {
491488
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?"
492-
+ "code=" + code
493-
+ "&agentid=" + agentId;
489+
+ "code=" + code
490+
+ "&agentid=" + agentId;
494491
String responseText = get(url, null);
495492
JsonElement je = new JsonParser().parse(responseText);
496493
JsonObject jo = je.getAsJsonObject();
@@ -544,14 +541,14 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
544541
return executeInternal(executor, uri, data);
545542
} catch (WxErrorException e) {
546543
WxError error = e.getError();
547-
/**
544+
/*
548545
* -1 系统繁忙, 1000ms后重试
549546
*/
550547
if (error.getErrorCode() == -1) {
551548
int sleepMillis = this.retrySleepMillis * (1 << retryTimes);
552549
try {
553550
this.log.debug("微信系统繁忙,{}ms 后重试(第{}次)", sleepMillis,
554-
retryTimes + 1);
551+
retryTimes + 1);
555552
Thread.sleep(sleepMillis);
556553
} catch (InterruptedException e1) {
557554
throw new RuntimeException(e1);
@@ -566,7 +563,7 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
566563
}
567564

568565
protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
569-
if (uri.indexOf("access_token=") != -1) {
566+
if (uri.contains("access_token=")) {
570567
throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
571568
}
572569
String accessToken = getAccessToken(false);
@@ -576,7 +573,7 @@ protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor,
576573

577574
try {
578575
return executor.execute(getHttpclient(), this.httpProxy,
579-
uriWithAccessToken, data);
576+
uriWithAccessToken, data);
580577
} catch (WxErrorException e) {
581578
WxError error = e.getError();
582579
/*
@@ -593,8 +590,6 @@ protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor,
593590
throw new WxErrorException(error);
594591
}
595592
return null;
596-
} catch (ClientProtocolException e) {
597-
throw new RuntimeException(e);
598593
} catch (IOException e) {
599594
throw new RuntimeException(e);
600595
}
@@ -608,15 +603,15 @@ protected CloseableHttpClient getHttpclient() {
608603
public void setWxCpConfigStorage(WxCpConfigStorage wxConfigProvider) {
609604
this.configStorage = wxConfigProvider;
610605
ApacheHttpClientBuilder apacheHttpClientBuilder = this.configStorage
611-
.getApacheHttpClientBuilder();
606+
.getApacheHttpClientBuilder();
612607
if (null == apacheHttpClientBuilder) {
613608
apacheHttpClientBuilder = DefaultApacheHttpClientBuilder.get();
614609
}
615610

616611
apacheHttpClientBuilder.httpProxyHost(this.configStorage.getHttpProxyHost())
617-
.httpProxyPort(this.configStorage.getHttpProxyPort())
618-
.httpProxyUsername(this.configStorage.getHttpProxyUsername())
619-
.httpProxyPassword(this.configStorage.getHttpProxyPassword());
612+
.httpProxyPort(this.configStorage.getHttpProxyPort())
613+
.httpProxyUsername(this.configStorage.getHttpProxyUsername())
614+
.httpProxyPassword(this.configStorage.getHttpProxyPassword());
620615

621616
if (this.configStorage.getHttpProxyHost() != null && this.configStorage.getHttpProxyPort() > 0) {
622617
this.httpProxy = new HttpHost(this.configStorage.getHttpProxyHost(), this.configStorage.getHttpProxyPort());

0 commit comments

Comments
 (0)