Skip to content

Commit

Permalink
🎨 抽取oauth2相关接口方法到独立类WxOAuth2Service中
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Aug 9, 2020
1 parent 4bd5b3c commit 9346c17
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 260 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.chanjar.weixin.common.api.WxOcrService;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.WxNetCheckResult;
import me.chanjar.weixin.common.enums.TicketType;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.service.WxService;
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
Expand All @@ -15,7 +16,6 @@
import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.common.enums.TicketType;
import me.chanjar.weixin.mp.enums.WxMpApiUrl;

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

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

/**
Expand All @@ -186,6 +188,7 @@ public interface WxMpService extends WxService {
* @return token对象
* @throws WxErrorException .
*/
@Deprecated
WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException;

/**
Expand All @@ -197,6 +200,7 @@ public interface WxMpService extends WxService {
* @return 新的token对象
* @throws WxErrorException .
*/
@Deprecated
WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException;

/**
Expand All @@ -209,6 +213,7 @@ public interface WxMpService extends WxService {
* @return 用户对象
* @throws WxErrorException .
*/
@Deprecated
WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException;

/**
Expand All @@ -219,6 +224,7 @@ public interface WxMpService extends WxService {
* @param oAuth2AccessToken token对象
* @return 是否有效
*/
@Deprecated
boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken);

/**
Expand Down Expand Up @@ -276,6 +282,7 @@ public interface WxMpService extends WxService {
* </pre>
*
* @param appid 公众号的APPID
* @throws WxErrorException the wx error exception
*/
void clearQuota(String appid) throws WxErrorException;

Expand All @@ -286,9 +293,11 @@ public interface WxMpService extends WxService {
* 可以参考,{@link MediaUploadRequestExecutor}的实现方法
* </pre>
*
* @param data 参数数据
* @param <T> the type parameter
* @param <E> the type parameter
* @param executor 执行器
* @param url 接口地址
* @param data 参数数据
* @return 结果
* @throws WxErrorException 异常
*/
Expand All @@ -297,8 +306,8 @@ public interface WxMpService extends WxService {
/**
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求.
*
* @param queryParam 参数
* @param url 请求接口地址
* @param queryParam 参数
* @return 接口响应字符串
* @throws WxErrorException 异常
*/
Expand All @@ -307,8 +316,8 @@ public interface WxMpService extends WxService {
/**
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求.
*
* @param postData 请求参数json值
* @param url 请求接口地址
* @param postData 请求参数json值
* @return 接口响应字符串
* @throws WxErrorException 异常
*/
Expand All @@ -321,9 +330,11 @@ public interface WxMpService extends WxService {
* 可以参考,{@link MediaUploadRequestExecutor}的实现方法
* </pre>
*
* @param data 参数数据
* @param <T> the type parameter
* @param <E> the type parameter
* @param executor 执行器
* @param url 接口地址
* @param data 参数数据
* @return 结果
* @throws WxErrorException 异常
*/
Expand Down Expand Up @@ -597,16 +608,16 @@ public interface WxMpService extends WxService {
/**
* .
*
* @param tagService .
* @param userTagService .
*/
void setTagService(WxMpUserTagService tagService);
void setUserTagService(WxMpUserTagService userTagService);

/**
* .
*
* @param qrCodeService .
* @param qrcodeService .
*/
void setQrCodeService(WxMpQrcodeService qrCodeService);
void setQrcodeService(WxMpQrcodeService qrcodeService);

/**
* .
Expand Down Expand Up @@ -712,4 +723,18 @@ public interface WxMpService extends WxService {
* @param commentService .
*/
void setCommentService(WxMpCommentService commentService);

/**
* Gets oauth2 service.
*
* @return the oauth2 service
*/
WxOAuth2Service getOAuth2Service();

/**
* Sets oauth2Service.
*
* @param oAuth2Service the o auth 2 service
*/
void setOAuth2Service(WxOAuth2Service oAuth2Service);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package me.chanjar.weixin.mp.api;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;

/**
* oauth2 相关接口.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @date 2020-08-08
*/
public interface WxOAuth2Service {
/**
* <pre>
* 构造oauth2授权的url连接.
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=网页授权获取用户基本信息
* </pre>
*
* @param redirectURI 用户授权完成后的重定向链接,无需urlencode, 方法内会进行encode
* @param scope scope
* @param state state
* @return url
*/
String buildAuthorizationUrl(String redirectURI, String scope, String state);

/**
* <pre>
* 用code换取oauth2的access token.
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=网页授权获取用户基本信息
* </pre>
*
* @param code code
* @return token对象
* @throws WxErrorException .
*/
WxMpOAuth2AccessToken getAccessToken(String code) throws WxErrorException;

/**
* <pre>
* 刷新oauth2的access token.
* </pre>
*
* @param refreshToken 刷新token
* @return 新的token对象
* @throws WxErrorException .
*/
WxMpOAuth2AccessToken refreshAccessToken(String refreshToken) throws WxErrorException;

/**
* <pre>
* 用oauth2获取用户信息, 当前面引导授权时的scope是snsapi_userinfo的时候才可以.
* </pre>
*
* @param oAuth2AccessToken token对象
* @param lang zh_CN, zh_TW, en
* @return 用户对象
* @throws WxErrorException .
*/
WxMpUser getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException;

/**
* <pre>
* 验证oauth2的access token是否有效.
* </pre>
*
* @param oAuth2AccessToken token对象
* @return 是否有效
*/
boolean validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken);

}
Loading

0 comments on commit 9346c17

Please sign in to comment.