Skip to content

Commit bb83ead

Browse files
committed
抽取公众号部分微信请求URL到常量类中 binarywang#195
1 parent 091e478 commit bb83ead

File tree

5 files changed

+139
-101
lines changed

5 files changed

+139
-101
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package me.chanjar.weixin.mp.api;
2+
3+
/**
4+
* <pre>
5+
* 公众号相关接口URL常量类
6+
* Created by Binary Wang on 2017-4-28.
7+
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
8+
* </pre>
9+
*/
10+
public class WxMpApiUrls {
11+
/**
12+
* 获取access_token
13+
*/
14+
public static final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
15+
16+
/**
17+
* 获得jsapi_ticket
18+
*/
19+
public static final String GET_JSAPI_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi";
20+
21+
/**
22+
* 上传群发用的图文消息
23+
*/
24+
public static final String MEDIA_UPLOAD_NEWS_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadnews";
25+
26+
/**
27+
* 上传群发用的视频
28+
*/
29+
public static final String MEDIA_UPLOAD_VIDEO_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadvideo";
30+
31+
/**
32+
* 分组群发消息
33+
*/
34+
public static final String MESSAGE_MASS_SENDALL_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall";
35+
36+
/**
37+
* 按openId列表群发消息
38+
*/
39+
public static final String MESSAGE_MASS_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/send";
40+
41+
/**
42+
* 群发消息预览接口
43+
*/
44+
public static final String MESSAGE_MASS_PREVIEW_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/preview";
45+
46+
/**
47+
* 长链接转短链接接口
48+
*/
49+
public static final String SHORTURL_API_URL = "https://api.weixin.qq.com/cgi-bin/shorturl";
50+
51+
/**
52+
* 语义查询接口
53+
*/
54+
public static final String SEMANTIC_SEMPROXY_SEARCH_URL = "https://api.weixin.qq.com/semantic/semproxy/search";
55+
56+
/**
57+
* 用code换取oauth2的access token
58+
*/
59+
public static final String OAUTH2_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
60+
61+
/**
62+
* 刷新oauth2的access token
63+
*/
64+
public static final String OAUTH2_REFRESH_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s";
65+
66+
/**
67+
* 用oauth2获取用户信息
68+
*/
69+
public static final String OAUTH2_USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=%s";
70+
71+
/**
72+
* 验证oauth2的access token是否有效
73+
*/
74+
public static final String OAUTH2_VALIDATE_TOKEN_URL = "https://api.weixin.qq.com/sns/auth?access_token=%s&openid=%s";
75+
76+
/**
77+
* 获取微信服务器IP地址
78+
*/
79+
public static final String GET_CALLBACK_IP_URL = "https://api.weixin.qq.com/cgi-bin/getcallbackip";
80+
81+
/**
82+
* 第三方使用网站应用授权登录的url
83+
*/
84+
public static final String QRCONNECT_URL = "https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect";
85+
86+
/**
87+
* oauth2授权的url连接
88+
*/
89+
public static final String CONNECT_OAUTH2_AUTHORIZE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect";
90+
}

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

Lines changed: 25 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import me.chanjar.weixin.mp.api.*;
1616
import me.chanjar.weixin.mp.bean.*;
1717
import me.chanjar.weixin.mp.bean.result.*;
18+
import org.apache.commons.lang3.StringUtils;
1819
import org.slf4j.Logger;
1920
import org.slf4j.LoggerFactory;
2021

@@ -70,8 +71,7 @@ public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
7071
}
7172

7273
if (this.getWxMpConfigStorage().isJsapiTicketExpired()) {
73-
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi";
74-
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
74+
String responseContent = execute(new SimpleGetRequestExecutor(), WxMpApiUrls.GET_JSAPI_TICKET_URL, null);
7575
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
7676
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
7777
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
@@ -107,93 +107,66 @@ public String getAccessToken() throws WxErrorException {
107107

108108
@Override
109109
public WxMpMassUploadResult massNewsUpload(WxMpMassNews news) throws WxErrorException {
110-
String url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews";
111-
String responseContent = this.post(url, news.toJson());
110+
String responseContent = this.post(WxMpApiUrls.MEDIA_UPLOAD_NEWS_URL, news.toJson());
112111
return WxMpMassUploadResult.fromJson(responseContent);
113112
}
114113

115114
@Override
116115
public WxMpMassUploadResult massVideoUpload(WxMpMassVideo video) throws WxErrorException {
117-
String url = "https://api.weixin.qq.com/cgi-bin/media/uploadvideo";
118-
String responseContent = this.post(url, video.toJson());
116+
String responseContent = this.post(WxMpApiUrls.MEDIA_UPLOAD_VIDEO_URL, video.toJson());
119117
return WxMpMassUploadResult.fromJson(responseContent);
120118
}
121119

122120
@Override
123121
public WxMpMassSendResult massGroupMessageSend(WxMpMassTagMessage message) throws WxErrorException {
124-
String url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall";
125-
String responseContent = this.post(url, message.toJson());
122+
String responseContent = this.post(WxMpApiUrls.MESSAGE_MASS_SENDALL_URL, message.toJson());
126123
return WxMpMassSendResult.fromJson(responseContent);
127124
}
128125

129126
@Override
130127
public WxMpMassSendResult massOpenIdsMessageSend(WxMpMassOpenIdsMessage message) throws WxErrorException {
131-
String url = "https://api.weixin.qq.com/cgi-bin/message/mass/send";
132-
String responseContent = this.post(url, message.toJson());
128+
String responseContent = this.post(WxMpApiUrls.MESSAGE_MASS_SEND_URL, message.toJson());
133129
return WxMpMassSendResult.fromJson(responseContent);
134130
}
135131

136132
@Override
137133
public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception {
138-
String url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview";
139-
String responseContent = this.post(url, wxMpMassPreviewMessage.toJson());
134+
String responseContent = this.post(WxMpApiUrls.MESSAGE_MASS_PREVIEW_URL, wxMpMassPreviewMessage.toJson());
140135
return WxMpMassSendResult.fromJson(responseContent);
141136
}
142137

143138
@Override
144139
public String shortUrl(String long_url) throws WxErrorException {
145-
String url = "https://api.weixin.qq.com/cgi-bin/shorturl";
146140
JsonObject o = new JsonObject();
147141
o.addProperty("action", "long2short");
148142
o.addProperty("long_url", long_url);
149-
String responseContent = this.post(url, o.toString());
143+
String responseContent = this.post(WxMpApiUrls.SHORTURL_API_URL, o.toString());
150144
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
151145
return tmpJsonElement.getAsJsonObject().get("short_url").getAsString();
152146
}
153147

154148
@Override
155149
public WxMpSemanticQueryResult semanticQuery(WxMpSemanticQuery semanticQuery) throws WxErrorException {
156-
String url = "https://api.weixin.qq.com/semantic/semproxy/search";
157-
String responseContent = this.post(url, semanticQuery.toJson());
150+
String responseContent = this.post(WxMpApiUrls.SEMANTIC_SEMPROXY_SEARCH_URL, semanticQuery.toJson());
158151
return WxMpSemanticQueryResult.fromJson(responseContent);
159152
}
160153

161154
@Override
162155
public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) {
163-
StringBuilder url = new StringBuilder();
164-
url.append("https://open.weixin.qq.com/connect/oauth2/authorize?");
165-
url.append("appid=").append(this.getWxMpConfigStorage().getAppId());
166-
url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectURI));
167-
url.append("&response_type=code");
168-
url.append("&scope=").append(scope);
169-
if (state != null) {
170-
url.append("&state=").append(state);
171-
}
172-
url.append("#wechat_redirect");
173-
return url.toString();
156+
return String.format(WxMpApiUrls.CONNECT_OAUTH2_AUTHORIZE_URL,
157+
this.getWxMpConfigStorage().getAppId(), URIUtil.encodeURIComponent(redirectURI), scope, StringUtils.trimToEmpty(state));
174158
}
175159

176160
@Override
177-
public String buildQrConnectUrl(String redirectURI, String scope,
178-
String state) {
179-
StringBuilder url = new StringBuilder();
180-
url.append("https://open.weixin.qq.com/connect/qrconnect?");
181-
url.append("appid=").append(this.getWxMpConfigStorage().getAppId());
182-
url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectURI));
183-
url.append("&response_type=code");
184-
url.append("&scope=").append(scope);
185-
if (state != null) {
186-
url.append("&state=").append(state);
187-
}
188-
189-
url.append("#wechat_redirect");
190-
return url.toString();
161+
public String buildQrConnectUrl(String redirectURI, String scope, String state) {
162+
return String.format(WxMpApiUrls.QRCONNECT_URL,
163+
this.getWxMpConfigStorage().getAppId(), URIUtil.encodeURIComponent(redirectURI), scope, StringUtils.trimToEmpty(state));
191164
}
192165

193-
private WxMpOAuth2AccessToken getOAuth2AccessToken(StringBuilder url) throws WxErrorException {
166+
private WxMpOAuth2AccessToken getOAuth2AccessToken(String url) throws WxErrorException {
194167
try {
195168
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
196-
String responseText = executor.execute(this, url.toString(), null);
169+
String responseText = executor.execute(this, url, null);
197170
return WxMpOAuth2AccessToken.fromJson(responseText);
198171
} catch (IOException e) {
199172
throw new RuntimeException(e);
@@ -202,42 +175,27 @@ private WxMpOAuth2AccessToken getOAuth2AccessToken(StringBuilder url) throws WxE
202175

203176
@Override
204177
public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException {
205-
StringBuilder url = new StringBuilder();
206-
url.append("https://api.weixin.qq.com/sns/oauth2/access_token?");
207-
url.append("appid=").append(this.getWxMpConfigStorage().getAppId());
208-
url.append("&secret=").append(this.getWxMpConfigStorage().getSecret());
209-
url.append("&code=").append(code);
210-
url.append("&grant_type=authorization_code");
211-
178+
String url = String.format(WxMpApiUrls.OAUTH2_ACCESS_TOKEN_URL, this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret(), code);
212179
return this.getOAuth2AccessToken(url);
213180
}
214181

215182
@Override
216183
public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException {
217-
StringBuilder url = new StringBuilder();
218-
url.append("https://api.weixin.qq.com/sns/oauth2/refresh_token?");
219-
url.append("appid=").append(this.getWxMpConfigStorage().getAppId());
220-
url.append("&grant_type=refresh_token");
221-
url.append("&refresh_token=").append(refreshToken);
222-
184+
String url = String.format(WxMpApiUrls.OAUTH2_REFRESH_TOKEN_URL, this.getWxMpConfigStorage().getAppId(), refreshToken);
223185
return this.getOAuth2AccessToken(url);
224186
}
225187

226188
@Override
227189
public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException {
228-
StringBuilder url = new StringBuilder();
229-
url.append("https://api.weixin.qq.com/sns/userinfo?");
230-
url.append("access_token=").append(oAuth2AccessToken.getAccessToken());
231-
url.append("&openid=").append(oAuth2AccessToken.getOpenId());
232190
if (lang == null) {
233-
url.append("&lang=zh_CN");
234-
} else {
235-
url.append("&lang=").append(lang);
191+
lang = "zh_CN";
236192
}
237193

194+
String url = String.format(WxMpApiUrls.OAUTH2_USERINFO_URL, oAuth2AccessToken.getAccessToken(), oAuth2AccessToken.getOpenId(), lang);
195+
238196
try {
239197
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
240-
String responseText = executor.execute(this, url.toString(), null);
198+
String responseText = executor.execute(this, url, null);
241199
return WxMpUser.fromJson(responseText);
242200
} catch (IOException e) {
243201
throw new RuntimeException(e);
@@ -246,14 +204,11 @@ public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, Strin
246204

247205
@Override
248206
public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken) {
249-
StringBuilder url = new StringBuilder();
250-
url.append("https://api.weixin.qq.com/sns/auth?");
251-
url.append("access_token=").append(oAuth2AccessToken.getAccessToken());
252-
url.append("&openid=").append(oAuth2AccessToken.getOpenId());
207+
String url = String.format(WxMpApiUrls.OAUTH2_VALIDATE_TOKEN_URL, oAuth2AccessToken.getAccessToken(), oAuth2AccessToken.getOpenId());
253208

254209
try {
255210
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
256-
executor.execute(this, url.toString(), null);
211+
executor.execute(this, url, null);
257212
} catch (IOException e) {
258213
throw new RuntimeException(e);
259214
} catch (WxErrorException e) {
@@ -264,8 +219,7 @@ public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken
264219

265220
@Override
266221
public String[] getCallbackIP() throws WxErrorException {
267-
String url = "https://api.weixin.qq.com/cgi-bin/getcallbackip";
268-
String responseContent = get(url, null);
222+
String responseContent = this.get(WxMpApiUrls.GET_CALLBACK_IP_URL, null);
269223
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
270224
JsonArray ipList = tmpJsonElement.getAsJsonObject().get("ip_list").getAsJsonArray();
271225
String[] ipArray = new String[ipList.size()];

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
package me.chanjar.weixin.mp.api.impl.apache;
22

3-
import java.io.IOException;
4-
import java.util.concurrent.locks.Lock;
5-
6-
import org.apache.http.HttpHost;
7-
import org.apache.http.client.config.RequestConfig;
8-
import org.apache.http.client.methods.CloseableHttpResponse;
9-
import org.apache.http.client.methods.HttpGet;
10-
import org.apache.http.impl.client.BasicResponseHandler;
11-
import org.apache.http.impl.client.CloseableHttpClient;
12-
133
import me.chanjar.weixin.common.bean.WxAccessToken;
144
import me.chanjar.weixin.common.bean.result.WxError;
155
import me.chanjar.weixin.common.exception.WxErrorException;
166
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
177
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
8+
import me.chanjar.weixin.mp.api.WxMpApiUrls;
189
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
1910
import me.chanjar.weixin.mp.api.impl.AbstractWxMpServiceImpl;
11+
import org.apache.http.HttpHost;
12+
import org.apache.http.client.config.RequestConfig;
13+
import org.apache.http.client.methods.CloseableHttpResponse;
14+
import org.apache.http.client.methods.HttpGet;
15+
import org.apache.http.impl.client.BasicResponseHandler;
16+
import org.apache.http.impl.client.CloseableHttpClient;
17+
18+
import java.io.IOException;
19+
import java.util.concurrent.locks.Lock;
2020

2121
/**
2222
* apache-http方式实现
@@ -66,9 +66,8 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
6666
}
6767

6868
if (this.getWxMpConfigStorage().isAccessTokenExpired()) {
69-
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
70-
"&appid=" + this.getWxMpConfigStorage().getAppId() + "&secret="
71-
+ this.getWxMpConfigStorage().getSecret();
69+
String url = String.format(WxMpApiUrls.GET_ACCESS_TOKEN_URL,
70+
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
7271
try {
7372
HttpGet httpGet = new HttpGet(url);
7473
if (this.getRequestHttpProxy() != null) {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
5151
}
5252

5353
if (this.getWxMpConfigStorage().isAccessTokenExpired()) {
54-
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
55-
"&appid=" + this.getWxMpConfigStorage().getAppId() + "&secret="
56-
+ this.getWxMpConfigStorage().getSecret();
54+
String url = String.format(WxMpApiUrls.GET_ACCESS_TOKEN_URL,
55+
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
5756

5857
HttpRequest request = HttpRequest.get(url);
5958

0 commit comments

Comments
 (0)