Skip to content

Commit 879c1f5

Browse files
committed
抽取公众号部分微信请求URL到接口类中 binarywang#195
1 parent bb83ead commit 879c1f5

File tree

6 files changed

+84
-110
lines changed

6 files changed

+84
-110
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpApiUrls.java

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

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,70 @@
1111
* 微信API的Service
1212
*/
1313
public interface WxMpService {
14+
/**
15+
* 获取access_token
16+
*/
17+
String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
18+
/**
19+
* 获得jsapi_ticket
20+
*/
21+
String GET_JSAPI_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi";
22+
/**
23+
* 上传群发用的图文消息
24+
*/
25+
String MEDIA_UPLOAD_NEWS_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadnews";
26+
/**
27+
* 上传群发用的视频
28+
*/
29+
String MEDIA_UPLOAD_VIDEO_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadvideo";
30+
/**
31+
* 分组群发消息
32+
*/
33+
String MESSAGE_MASS_SENDALL_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall";
34+
/**
35+
* 按openId列表群发消息
36+
*/
37+
String MESSAGE_MASS_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/send";
38+
/**
39+
* 群发消息预览接口
40+
*/
41+
String MESSAGE_MASS_PREVIEW_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/preview";
42+
/**
43+
* 长链接转短链接接口
44+
*/
45+
String SHORTURL_API_URL = "https://api.weixin.qq.com/cgi-bin/shorturl";
46+
/**
47+
* 语义查询接口
48+
*/
49+
String SEMANTIC_SEMPROXY_SEARCH_URL = "https://api.weixin.qq.com/semantic/semproxy/search";
50+
/**
51+
* 用code换取oauth2的access token
52+
*/
53+
String OAUTH2_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
54+
/**
55+
* 刷新oauth2的access token
56+
*/
57+
String OAUTH2_REFRESH_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s";
58+
/**
59+
* 用oauth2获取用户信息
60+
*/
61+
String OAUTH2_USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=%s";
62+
/**
63+
* 验证oauth2的access token是否有效
64+
*/
65+
String OAUTH2_VALIDATE_TOKEN_URL = "https://api.weixin.qq.com/sns/auth?access_token=%s&openid=%s";
66+
/**
67+
* 获取微信服务器IP地址
68+
*/
69+
String GET_CALLBACK_IP_URL = "https://api.weixin.qq.com/cgi-bin/getcallbackip";
70+
/**
71+
* 第三方使用网站应用授权登录的url
72+
*/
73+
String QRCONNECT_URL = "https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect";
74+
/**
75+
* oauth2授权的url连接
76+
*/
77+
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";
1478

1579
/**
1680
* <pre>

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
7171
}
7272

7373
if (this.getWxMpConfigStorage().isJsapiTicketExpired()) {
74-
String responseContent = execute(new SimpleGetRequestExecutor(), WxMpApiUrls.GET_JSAPI_TICKET_URL, null);
74+
String responseContent = execute(new SimpleGetRequestExecutor(), WxMpService.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,31 +107,31 @@ public String getAccessToken() throws WxErrorException {
107107

108108
@Override
109109
public WxMpMassUploadResult massNewsUpload(WxMpMassNews news) throws WxErrorException {
110-
String responseContent = this.post(WxMpApiUrls.MEDIA_UPLOAD_NEWS_URL, news.toJson());
110+
String responseContent = this.post(WxMpService.MEDIA_UPLOAD_NEWS_URL, news.toJson());
111111
return WxMpMassUploadResult.fromJson(responseContent);
112112
}
113113

114114
@Override
115115
public WxMpMassUploadResult massVideoUpload(WxMpMassVideo video) throws WxErrorException {
116-
String responseContent = this.post(WxMpApiUrls.MEDIA_UPLOAD_VIDEO_URL, video.toJson());
116+
String responseContent = this.post(WxMpService.MEDIA_UPLOAD_VIDEO_URL, video.toJson());
117117
return WxMpMassUploadResult.fromJson(responseContent);
118118
}
119119

120120
@Override
121121
public WxMpMassSendResult massGroupMessageSend(WxMpMassTagMessage message) throws WxErrorException {
122-
String responseContent = this.post(WxMpApiUrls.MESSAGE_MASS_SENDALL_URL, message.toJson());
122+
String responseContent = this.post(WxMpService.MESSAGE_MASS_SENDALL_URL, message.toJson());
123123
return WxMpMassSendResult.fromJson(responseContent);
124124
}
125125

126126
@Override
127127
public WxMpMassSendResult massOpenIdsMessageSend(WxMpMassOpenIdsMessage message) throws WxErrorException {
128-
String responseContent = this.post(WxMpApiUrls.MESSAGE_MASS_SEND_URL, message.toJson());
128+
String responseContent = this.post(WxMpService.MESSAGE_MASS_SEND_URL, message.toJson());
129129
return WxMpMassSendResult.fromJson(responseContent);
130130
}
131131

132132
@Override
133133
public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception {
134-
String responseContent = this.post(WxMpApiUrls.MESSAGE_MASS_PREVIEW_URL, wxMpMassPreviewMessage.toJson());
134+
String responseContent = this.post(WxMpService.MESSAGE_MASS_PREVIEW_URL, wxMpMassPreviewMessage.toJson());
135135
return WxMpMassSendResult.fromJson(responseContent);
136136
}
137137

@@ -140,26 +140,26 @@ public String shortUrl(String long_url) throws WxErrorException {
140140
JsonObject o = new JsonObject();
141141
o.addProperty("action", "long2short");
142142
o.addProperty("long_url", long_url);
143-
String responseContent = this.post(WxMpApiUrls.SHORTURL_API_URL, o.toString());
143+
String responseContent = this.post(WxMpService.SHORTURL_API_URL, o.toString());
144144
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
145145
return tmpJsonElement.getAsJsonObject().get("short_url").getAsString();
146146
}
147147

148148
@Override
149149
public WxMpSemanticQueryResult semanticQuery(WxMpSemanticQuery semanticQuery) throws WxErrorException {
150-
String responseContent = this.post(WxMpApiUrls.SEMANTIC_SEMPROXY_SEARCH_URL, semanticQuery.toJson());
150+
String responseContent = this.post(WxMpService.SEMANTIC_SEMPROXY_SEARCH_URL, semanticQuery.toJson());
151151
return WxMpSemanticQueryResult.fromJson(responseContent);
152152
}
153153

154154
@Override
155155
public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) {
156-
return String.format(WxMpApiUrls.CONNECT_OAUTH2_AUTHORIZE_URL,
156+
return String.format(WxMpService.CONNECT_OAUTH2_AUTHORIZE_URL,
157157
this.getWxMpConfigStorage().getAppId(), URIUtil.encodeURIComponent(redirectURI), scope, StringUtils.trimToEmpty(state));
158158
}
159159

160160
@Override
161161
public String buildQrConnectUrl(String redirectURI, String scope, String state) {
162-
return String.format(WxMpApiUrls.QRCONNECT_URL,
162+
return String.format(WxMpService.QRCONNECT_URL,
163163
this.getWxMpConfigStorage().getAppId(), URIUtil.encodeURIComponent(redirectURI), scope, StringUtils.trimToEmpty(state));
164164
}
165165

@@ -175,13 +175,13 @@ private WxMpOAuth2AccessToken getOAuth2AccessToken(String url) throws WxErrorExc
175175

176176
@Override
177177
public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException {
178-
String url = String.format(WxMpApiUrls.OAUTH2_ACCESS_TOKEN_URL, this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret(), code);
178+
String url = String.format(WxMpService.OAUTH2_ACCESS_TOKEN_URL, this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret(), code);
179179
return this.getOAuth2AccessToken(url);
180180
}
181181

182182
@Override
183183
public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException {
184-
String url = String.format(WxMpApiUrls.OAUTH2_REFRESH_TOKEN_URL, this.getWxMpConfigStorage().getAppId(), refreshToken);
184+
String url = String.format(WxMpService.OAUTH2_REFRESH_TOKEN_URL, this.getWxMpConfigStorage().getAppId(), refreshToken);
185185
return this.getOAuth2AccessToken(url);
186186
}
187187

@@ -191,7 +191,7 @@ public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, Strin
191191
lang = "zh_CN";
192192
}
193193

194-
String url = String.format(WxMpApiUrls.OAUTH2_USERINFO_URL, oAuth2AccessToken.getAccessToken(), oAuth2AccessToken.getOpenId(), lang);
194+
String url = String.format(WxMpService.OAUTH2_USERINFO_URL, oAuth2AccessToken.getAccessToken(), oAuth2AccessToken.getOpenId(), lang);
195195

196196
try {
197197
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
@@ -204,7 +204,7 @@ public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, Strin
204204

205205
@Override
206206
public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken) {
207-
String url = String.format(WxMpApiUrls.OAUTH2_VALIDATE_TOKEN_URL, oAuth2AccessToken.getAccessToken(), oAuth2AccessToken.getOpenId());
207+
String url = String.format(WxMpService.OAUTH2_VALIDATE_TOKEN_URL, oAuth2AccessToken.getAccessToken(), oAuth2AccessToken.getOpenId());
208208

209209
try {
210210
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
@@ -219,7 +219,7 @@ public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken
219219

220220
@Override
221221
public String[] getCallbackIP() throws WxErrorException {
222-
String responseContent = this.get(WxMpApiUrls.GET_CALLBACK_IP_URL, null);
222+
String responseContent = this.get(WxMpService.GET_CALLBACK_IP_URL, null);
223223
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
224224
JsonArray ipList = tmpJsonElement.getAsJsonObject().get("ip_list").getAsJsonArray();
225225
String[] ipArray = new String[ipList.size()];

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import me.chanjar.weixin.common.exception.WxErrorException;
66
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
77
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
8-
import me.chanjar.weixin.mp.api.WxMpApiUrls;
98
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
9+
import me.chanjar.weixin.mp.api.WxMpService;
1010
import me.chanjar.weixin.mp.api.impl.AbstractWxMpServiceImpl;
1111
import org.apache.http.HttpHost;
1212
import org.apache.http.client.config.RequestConfig;
@@ -66,7 +66,7 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
6666
}
6767

6868
if (this.getWxMpConfigStorage().isAccessTokenExpired()) {
69-
String url = String.format(WxMpApiUrls.GET_ACCESS_TOKEN_URL,
69+
String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
7070
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
7171
try {
7272
HttpGet httpGet = new HttpGet(url);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
5151
}
5252

5353
if (this.getWxMpConfigStorage().isAccessTokenExpired()) {
54-
String url = String.format(WxMpApiUrls.GET_ACCESS_TOKEN_URL,
54+
String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
5555
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
5656

5757
HttpRequest request = HttpRequest.get(url);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import me.chanjar.weixin.common.bean.result.WxError;
55
import me.chanjar.weixin.common.exception.WxErrorException;
66
import me.chanjar.weixin.common.util.http.okhttp.OkhttpProxyInfo;
7-
import me.chanjar.weixin.mp.api.WxMpApiUrls;
87
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
8+
import me.chanjar.weixin.mp.api.WxMpService;
99
import me.chanjar.weixin.mp.api.impl.AbstractWxMpServiceImpl;
1010
import okhttp3.*;
1111

@@ -37,7 +37,7 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
3737
}
3838

3939
if (this.getWxMpConfigStorage().isAccessTokenExpired()) {
40-
String url = String.format(WxMpApiUrls.GET_ACCESS_TOKEN_URL,
40+
String url = String.format(WxMpService.GET_ACCESS_TOKEN_URL,
4141
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
4242

4343
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().connectionPool(httpClient);

0 commit comments

Comments
 (0)