Skip to content

Commit 9346c17

Browse files
committed
🎨 抽取oauth2相关接口方法到独立类WxOAuth2Service中
1 parent 4bd5b3c commit 9346c17

File tree

5 files changed

+311
-260
lines changed

5 files changed

+311
-260
lines changed

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

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import me.chanjar.weixin.common.api.WxOcrService;
55
import me.chanjar.weixin.common.bean.WxJsapiSignature;
66
import me.chanjar.weixin.common.bean.WxNetCheckResult;
7+
import me.chanjar.weixin.common.enums.TicketType;
78
import me.chanjar.weixin.common.error.WxErrorException;
89
import me.chanjar.weixin.common.service.WxService;
910
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
@@ -15,7 +16,6 @@
1516
import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult;
1617
import me.chanjar.weixin.mp.bean.result.WxMpUser;
1718
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
18-
import me.chanjar.weixin.common.enums.TicketType;
1919
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
2020

2121
import java.util.Map;
@@ -32,9 +32,9 @@ public interface WxMpService extends WxService {
3232
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319&token=&lang=zh_CN
3333
* </pre>
3434
*
35+
* @param timestamp 时间戳
3536
* @param nonce 随机串
3637
* @param signature 签名
37-
* @param timestamp 时间戳
3838
* @return 是否验证通过
3939
*/
4040
boolean checkSignature(String timestamp, String nonce, String signature);
@@ -82,8 +82,8 @@ public interface WxMpService extends WxService {
8282
* 获得时会检查 Token是否过期,如果过期了,那么就刷新一下,否则就什么都不干
8383
* </pre>
8484
*
85-
* @param forceRefresh 强制刷新
8685
* @param type ticket类型
86+
* @param forceRefresh 强制刷新
8787
* @return ticket
8888
* @throws WxErrorException .
8989
*/
@@ -156,12 +156,12 @@ public interface WxMpService extends WxService {
156156
* URL格式为:https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
157157
* </pre>
158158
*
159-
* @param redirectURI 用户授权完成后的重定向链接,无需urlencode, 方法内会进行encode
159+
* @param redirectUri 用户授权完成后的重定向链接,无需urlencode, 方法内会进行encode
160160
* @param scope 应用授权作用域,拥有多个作用域用逗号(,)分隔,网页应用目前仅填写snsapi_login即可
161161
* @param state 非必填,用于保持请求和回调的状态,授权请求后原样带回给第三方。该参数可用于防止csrf攻击(跨站请求伪造攻击),建议第三方带上该参数,可设置为简单的随机数加session进行校验
162-
* @return url
162+
* @return url string
163163
*/
164-
String buildQrConnectUrl(String redirectURI, String scope, String state);
164+
String buildQrConnectUrl(String redirectUri, String scope, String state);
165165

166166
/**
167167
* <pre>
@@ -172,8 +172,10 @@ public interface WxMpService extends WxService {
172172
* @param redirectURI 用户授权完成后的重定向链接,无需urlencode, 方法内会进行encode
173173
* @param scope scope
174174
* @param state state
175-
* @return url
175+
* @return url string
176+
* @deprecated use oauth2Service.buildAuthorizationUrl() instead
176177
*/
178+
@Deprecated
177179
String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state);
178180

179181
/**
@@ -186,6 +188,7 @@ public interface WxMpService extends WxService {
186188
* @return token对象
187189
* @throws WxErrorException .
188190
*/
191+
@Deprecated
189192
WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException;
190193

191194
/**
@@ -197,6 +200,7 @@ public interface WxMpService extends WxService {
197200
* @return 新的token对象
198201
* @throws WxErrorException .
199202
*/
203+
@Deprecated
200204
WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException;
201205

202206
/**
@@ -209,6 +213,7 @@ public interface WxMpService extends WxService {
209213
* @return 用户对象
210214
* @throws WxErrorException .
211215
*/
216+
@Deprecated
212217
WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException;
213218

214219
/**
@@ -219,6 +224,7 @@ public interface WxMpService extends WxService {
219224
* @param oAuth2AccessToken token对象
220225
* @return 是否有效
221226
*/
227+
@Deprecated
222228
boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken);
223229

224230
/**
@@ -276,6 +282,7 @@ public interface WxMpService extends WxService {
276282
* </pre>
277283
*
278284
* @param appid 公众号的APPID
285+
* @throws WxErrorException the wx error exception
279286
*/
280287
void clearQuota(String appid) throws WxErrorException;
281288

@@ -286,9 +293,11 @@ public interface WxMpService extends WxService {
286293
* 可以参考,{@link MediaUploadRequestExecutor}的实现方法
287294
* </pre>
288295
*
289-
* @param data 参数数据
296+
* @param <T> the type parameter
297+
* @param <E> the type parameter
290298
* @param executor 执行器
291299
* @param url 接口地址
300+
* @param data 参数数据
292301
* @return 结果
293302
* @throws WxErrorException 异常
294303
*/
@@ -297,8 +306,8 @@ public interface WxMpService extends WxService {
297306
/**
298307
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求.
299308
*
300-
* @param queryParam 参数
301309
* @param url 请求接口地址
310+
* @param queryParam 参数
302311
* @return 接口响应字符串
303312
* @throws WxErrorException 异常
304313
*/
@@ -307,8 +316,8 @@ public interface WxMpService extends WxService {
307316
/**
308317
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求.
309318
*
310-
* @param postData 请求参数json值
311319
* @param url 请求接口地址
320+
* @param postData 请求参数json值
312321
* @return 接口响应字符串
313322
* @throws WxErrorException 异常
314323
*/
@@ -321,9 +330,11 @@ public interface WxMpService extends WxService {
321330
* 可以参考,{@link MediaUploadRequestExecutor}的实现方法
322331
* </pre>
323332
*
324-
* @param data 参数数据
333+
* @param <T> the type parameter
334+
* @param <E> the type parameter
325335
* @param executor 执行器
326336
* @param url 接口地址
337+
* @param data 参数数据
327338
* @return 结果
328339
* @throws WxErrorException 异常
329340
*/
@@ -597,16 +608,16 @@ public interface WxMpService extends WxService {
597608
/**
598609
* .
599610
*
600-
* @param tagService .
611+
* @param userTagService .
601612
*/
602-
void setTagService(WxMpUserTagService tagService);
613+
void setUserTagService(WxMpUserTagService userTagService);
603614

604615
/**
605616
* .
606617
*
607-
* @param qrCodeService .
618+
* @param qrcodeService .
608619
*/
609-
void setQrCodeService(WxMpQrcodeService qrCodeService);
620+
void setQrcodeService(WxMpQrcodeService qrcodeService);
610621

611622
/**
612623
* .
@@ -712,4 +723,18 @@ public interface WxMpService extends WxService {
712723
* @param commentService .
713724
*/
714725
void setCommentService(WxMpCommentService commentService);
726+
727+
/**
728+
* Gets oauth2 service.
729+
*
730+
* @return the oauth2 service
731+
*/
732+
WxOAuth2Service getOAuth2Service();
733+
734+
/**
735+
* Sets oauth2Service.
736+
*
737+
* @param oAuth2Service the o auth 2 service
738+
*/
739+
void setOAuth2Service(WxOAuth2Service oAuth2Service);
715740
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package me.chanjar.weixin.mp.api;
2+
3+
import me.chanjar.weixin.common.error.WxErrorException;
4+
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
5+
import me.chanjar.weixin.mp.bean.result.WxMpUser;
6+
7+
/**
8+
* oauth2 相关接口.
9+
*
10+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
11+
* @date 2020-08-08
12+
*/
13+
public interface WxOAuth2Service {
14+
/**
15+
* <pre>
16+
* 构造oauth2授权的url连接.
17+
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=网页授权获取用户基本信息
18+
* </pre>
19+
*
20+
* @param redirectURI 用户授权完成后的重定向链接,无需urlencode, 方法内会进行encode
21+
* @param scope scope
22+
* @param state state
23+
* @return url
24+
*/
25+
String buildAuthorizationUrl(String redirectURI, String scope, String state);
26+
27+
/**
28+
* <pre>
29+
* 用code换取oauth2的access token.
30+
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=网页授权获取用户基本信息
31+
* </pre>
32+
*
33+
* @param code code
34+
* @return token对象
35+
* @throws WxErrorException .
36+
*/
37+
WxMpOAuth2AccessToken getAccessToken(String code) throws WxErrorException;
38+
39+
/**
40+
* <pre>
41+
* 刷新oauth2的access token.
42+
* </pre>
43+
*
44+
* @param refreshToken 刷新token
45+
* @return 新的token对象
46+
* @throws WxErrorException .
47+
*/
48+
WxMpOAuth2AccessToken refreshAccessToken(String refreshToken) throws WxErrorException;
49+
50+
/**
51+
* <pre>
52+
* 用oauth2获取用户信息, 当前面引导授权时的scope是snsapi_userinfo的时候才可以.
53+
* </pre>
54+
*
55+
* @param oAuth2AccessToken token对象
56+
* @param lang zh_CN, zh_TW, en
57+
* @return 用户对象
58+
* @throws WxErrorException .
59+
*/
60+
WxMpUser getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException;
61+
62+
/**
63+
* <pre>
64+
* 验证oauth2的access token是否有效.
65+
* </pre>
66+
*
67+
* @param oAuth2AccessToken token对象
68+
* @return 是否有效
69+
*/
70+
boolean validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken);
71+
72+
}

0 commit comments

Comments
 (0)