Skip to content

Commit

Permalink
binarywang#1720 增加群机器人消息推送接口服务、不需要自动带accessToken的post请求接口
Browse files Browse the repository at this point in the history
  • Loading branch information
yang ran committed Aug 21, 2020
1 parent 23e0b72 commit 04349ed
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ public interface WxCpService {
*/
String post(String url, String postData) throws WxErrorException;

/**
* 当不需要自动带accessToken的时候,可以用这个发起post请求
*
* @param url 接口地址
* @param postData 请求body字符串
*/
String postWithoutToken(String url, String postData) throws WxErrorException;

/**
* <pre>
* Service没有实现某个API的时候,可以用这个,
Expand Down Expand Up @@ -328,6 +336,13 @@ public interface WxCpService {

WxCpOaService getOAService();

/**
* 获取群机器人消息推送服务
*
* @return 群机器人消息推送服务
*/
WxCpGroupRobotService getGroupRobotService();

/**
* http请求对象
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
private WxCpOaService oaService = new WxCpOaServiceImpl(this);
private WxCpTaskCardService taskCardService = new WxCpTaskCardServiceImpl(this);
private WxCpExternalContactService externalContactService = new WxCpExternalContactServiceImpl(this);
private WxCpGroupRobotService groupRobotService = new WxCpGroupRobotServiceImpl(this);

/**
* 全局的是否正在刷新access token的锁.
Expand Down Expand Up @@ -217,6 +218,11 @@ public String post(String url, String postData) throws WxErrorException {
return execute(SimplePostRequestExecutor.create(this), url, postData);
}

@Override
public String postWithoutToken(String url, String postData) throws WxErrorException {
return this.executeNormal(SimplePostRequestExecutor.create(this), url, postData);
}

/**
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求.
*/
Expand Down Expand Up @@ -296,6 +302,27 @@ protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E
}
}

/**
* 普通请求,不自动带accessToken
*/
private <T, E> T executeNormal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
try {
T result = executor.execute(uri, data, WxType.CP);
log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", uri, data, result);
return result;
} catch (WxErrorException e) {
WxError error = e.getError();
if (error.getErrorCode() != 0) {
log.error("\n【请求地址】: {}\n【请求参数】:{}\n【错误信息】:{}", uri, data, error);
throw new WxErrorException(error, e);
}
return null;
} catch (IOException e) {
log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", uri, data, e.getMessage());
throw new RuntimeException(e);
}
}

@Override
public void setWxCpConfigStorage(WxCpConfigStorage wxConfigProvider) {
this.configStorage = wxConfigProvider;
Expand Down Expand Up @@ -412,6 +439,11 @@ public WxCpOaService getOAService() {
return oaService;
}

@Override
public WxCpGroupRobotService getGroupRobotService() {
return groupRobotService;
}

@Override
public WxCpTaskCardService getTaskCardService() {
return taskCardService;
Expand Down

0 comments on commit 04349ed

Please sign in to comment.