1
0
mirror of synced 2025-12-22 09:58:07 +08:00

🎨 优化企业微信消息发送接口代码,引入moco模拟测试组件,方便测试代码

This commit is contained in:
Binary Wang
2020-08-30 17:41:56 +08:00
parent 7cfabab628
commit c01347cac6
13 changed files with 297 additions and 118 deletions

View File

@@ -0,0 +1,25 @@
package me.chanjar.weixin.cp.api;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
/**
* 消息推送接口.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @date 2020 -08-30
*/
public interface WxCpMessageService {
/**
* <pre>
* 发送消息
* 详情请见: http://qydev.weixin.qq.com/wiki/index.php?title=%E5%8F%91%E9%80%81%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
* </pre>
*
* @param message 要发送的消息对象
* @return the wx cp message send result
* @throws WxErrorException the wx error exception
*/
WxCpMessageSendResult messageSend(WxCpMessage message) throws WxErrorException;
}

View File

@@ -29,13 +29,16 @@ public interface WxCpService {
* @param timestamp 时间戳
* @param nonce 随机数
* @param data 微信传输过来的数据有可能是echoStr有可能是xml消息
* @return the boolean
*/
boolean checkSignature(String msgSignature, String timestamp, String nonce, String data);
/**
* 获取access_token, 不强制刷新access_token
*
* @see #getAccessToken(boolean)
* @return the access token
* @throws WxErrorException the wx error exception
* @see #getAccessToken(boolean) #getAccessToken(boolean)
*/
String getAccessToken() throws WxErrorException;
@@ -49,13 +52,17 @@ public interface WxCpService {
* </pre>
*
* @param forceRefresh 强制刷新
* @return the access token
* @throws WxErrorException the wx error exception
*/
String getAccessToken(boolean forceRefresh) throws WxErrorException;
/**
* 获得jsapi_ticket,不强制刷新jsapi_ticket
*
* @see #getJsapiTicket(boolean)
* @return the jsapi ticket
* @throws WxErrorException the wx error exception
* @see #getJsapiTicket(boolean) #getJsapiTicket(boolean)
*/
String getJsapiTicket() throws WxErrorException;
@@ -68,6 +75,8 @@ public interface WxCpService {
* </pre>
*
* @param forceRefresh 强制刷新
* @return the jsapi ticket
* @throws WxErrorException the wx error exception
*/
String getJsapiTicket(boolean forceRefresh) throws WxErrorException;
@@ -78,7 +87,9 @@ public interface WxCpService {
* 签名的jsapi_ticket必须使用以下接口获取。且必须用wx.agentConfig中的agentid对应的应用secret去获取access_token。
* 签名用的noncestr和timestamp必须与wx.agentConfig中的nonceStr和timestamp相同。
*
* @see #getJsapiTicket(boolean)
* @return the agent jsapi ticket
* @throws WxErrorException the wx error exception
* @see #getJsapiTicket(boolean) #getJsapiTicket(boolean)
*/
String getAgentJsapiTicket() throws WxErrorException;
@@ -96,6 +107,8 @@ public interface WxCpService {
* </pre>
*
* @param forceRefresh 强制刷新
* @return the agent jsapi ticket
* @throws WxErrorException the wx error exception
*/
String getAgentJsapiTicket(boolean forceRefresh) throws WxErrorException;
@@ -107,23 +120,18 @@ public interface WxCpService {
* </pre>
*
* @param url url
* @return the wx jsapi signature
* @throws WxErrorException the wx error exception
*/
WxJsapiSignature createJsapiSignature(String url) throws WxErrorException;
/**
* <pre>
* 发送消息
* 详情请见: http://qydev.weixin.qq.com/wiki/index.php?title=%E5%8F%91%E9%80%81%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
* </pre>
*
* @param message 要发送的消息对象
*/
WxCpMessageSendResult messageSend(WxCpMessage message) throws WxErrorException;
/**
* 小程序登录凭证校验
*
* @param jsCode 登录时获取的 code
* @return the wx cp ma js code 2 session result
* @throws WxErrorException the wx error exception
*/
WxCpMaJsCode2SessionResult jsCode2Session(String jsCode) throws WxErrorException;
@@ -134,6 +142,7 @@ public interface WxCpService {
* </pre>
*
* @return { "ip_list": ["101.226.103.*", "101.226.62.*"] }
* @throws WxErrorException the wx error exception
*/
String[] getCallbackIp() throws WxErrorException;
@@ -147,12 +156,7 @@ public interface WxCpService {
*
* @param corpId 服务商的corpid
* @param providerSecret 服务商的secret在服务商管理后台可见
* @return {
* "errcode":0 ,
* "errmsg":"ok" ,
* "provider_access_token":"enLSZ5xxxxxxJRL",
* "expires_in":7200
* }
* @return { "errcode":0 , "errmsg":"ok" , "provider_access_token":"enLSZ5xxxxxxJRL", "expires_in":7200 }
* @throws WxErrorException .
*/
WxCpProviderToken getProviderToken(String corpId, String providerSecret) throws WxErrorException;
@@ -162,6 +166,8 @@ public interface WxCpService {
*
* @param url 接口地址
* @param queryParam 请求参数
* @return the string
* @throws WxErrorException the wx error exception
*/
String get(String url, String queryParam) throws WxErrorException;
@@ -170,6 +176,8 @@ public interface WxCpService {
*
* @param url 接口地址
* @param postData 请求body字符串
* @return the string
* @throws WxErrorException the wx error exception
*/
String post(String url, String postData) throws WxErrorException;
@@ -178,6 +186,8 @@ public interface WxCpService {
*
* @param url 接口地址
* @param postData 请求body字符串
* @return the string
* @throws WxErrorException the wx error exception
*/
String postWithoutToken(String url, String postData) throws WxErrorException;
@@ -188,11 +198,13 @@ public interface WxCpService {
* 可以参考,{@link MediaUploadRequestExecutor}的实现方法
* </pre>
*
* @param <T> 请求值类型
* @param <E> 返回值类型
* @param executor 执行器
* @param uri 请求地址
* @param data 参数
* @param <T> 请求值类型
* @param <E> 返回值类型
* @return the t
* @throws WxErrorException the wx error exception
*/
<T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException;
@@ -220,6 +232,7 @@ public interface WxCpService {
* 获取某个sessionId对应的session,如果sessionId没有对应的session则新建一个并返回。
*
* @param id id可以为任意字符串建议使用FromUserName作为id
* @return the session
*/
WxSession getSession(String id);
@@ -228,13 +241,14 @@ public interface WxCpService {
*
* @param id id可以为任意字符串建议使用FromUserName作为id
* @param create 是否新建
* @return the session
*/
WxSession getSession(String id, boolean create);
/**
* 获取WxSessionManager 对象
*
* @return WxSessionManager
* @return WxSessionManager session manager
*/
WxSessionManager getSessionManager();
@@ -252,6 +266,8 @@ public interface WxCpService {
* 上传部门列表覆盖企业号上的部门信息
*
* @param mediaId 媒体id
* @return the string
* @throws WxErrorException the wx error exception
*/
String replaceParty(String mediaId) throws WxErrorException;
@@ -259,11 +275,17 @@ public interface WxCpService {
* 上传用户列表覆盖企业号上的用户信息
*
* @param mediaId 媒体id
* @return the string
* @throws WxErrorException the wx error exception
*/
String replaceUser(String mediaId) throws WxErrorException;
/**
* 获取异步任务结果
*
* @param joinId the join id
* @return the task result
* @throws WxErrorException the wx error exception
*/
String getTaskResult(String joinId) throws WxErrorException;
@@ -275,7 +297,7 @@ public interface WxCpService {
/**
* 获取WxMpConfigStorage 对象
*
* @return WxMpConfigStorage
* @return WxMpConfigStorage wx cp config storage
*/
WxCpConfigStorage getWxCpConfigStorage();
@@ -288,75 +310,141 @@ public interface WxCpService {
/**
* 获取部门相关接口的服务类对象
*
* @return the department service
*/
WxCpDepartmentService getDepartmentService();
/**
* 获取媒体相关接口的服务类对象
*
* @return the media service
*/
WxCpMediaService getMediaService();
/**
* 获取菜单相关接口的服务类对象
*
* @return the menu service
*/
WxCpMenuService getMenuService();
/**
* 获取Oauth2相关接口的服务类对象
*
* @return the oauth 2 service
*/
WxCpOAuth2Service getOauth2Service();
/**
* 获取标签相关接口的服务类对象
*
* @return the tag service
*/
WxCpTagService getTagService();
/**
* 获取用户相关接口的服务类对象
*
* @return the user service
*/
WxCpUserService getUserService();
/**
* Gets external contact service.
*
* @return the external contact service
*/
WxCpExternalContactService getExternalContactService();
/**
* 获取群聊服务
*
* @return 群聊服务
* @return 群聊服务 chat service
*/
WxCpChatService getChatService();
/**
* 获取任务卡片服务
*
* @return 任务卡片服务
* @return 任务卡片服务 task card service
*/
WxCpTaskCardService getTaskCardService();
/**
* Gets agent service.
*
* @return the agent service
*/
WxCpAgentService getAgentService();
/**
* Gets message service.
*
* @return the message service
*/
WxCpMessageService getMessageService();
/**
* Gets oa service.
*
* @return the oa service
*/
WxCpOaService getOAService();
/**
* 获取群机器人消息推送服务
*
* @return 群机器人消息推送服务
* @return 群机器人消息推送服务 group robot service
*/
WxCpGroupRobotService getGroupRobotService();
/**
* http请求对象
*
* @return the request http
*/
RequestHttp<?, ?> getRequestHttp();
/**
* Sets user service.
*
* @param userService the user service
*/
void setUserService(WxCpUserService userService);
/**
* Sets department service.
*
* @param departmentService the department service
*/
void setDepartmentService(WxCpDepartmentService departmentService);
/**
* Sets media service.
*
* @param mediaService the media service
*/
void setMediaService(WxCpMediaService mediaService);
/**
* Sets menu service.
*
* @param menuService the menu service
*/
void setMenuService(WxCpMenuService menuService);
/**
* Sets oauth 2 service.
*
* @param oauth2Service the oauth 2 service
*/
void setOauth2Service(WxCpOAuth2Service oauth2Service);
/**
* Sets tag service.
*
* @param tagService the tag service
*/
void setTagService(WxCpTagService tagService);
}

View File

@@ -22,8 +22,6 @@ import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.cp.api.*;
import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
import me.chanjar.weixin.cp.bean.WxCpProviderToken;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
@@ -53,6 +51,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
private WxCpTaskCardService taskCardService = new WxCpTaskCardServiceImpl(this);
private WxCpExternalContactService externalContactService = new WxCpExternalContactServiceImpl(this);
private WxCpGroupRobotService groupRobotService = new WxCpGroupRobotServiceImpl(this);
private WxCpMessageService messageService = new WxCpMessageServiceImpl(this);
/**
* 全局的是否正在刷新access token的锁.
@@ -169,16 +168,6 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
return jsapiSignature;
}
@Override
public WxCpMessageSendResult messageSend(WxCpMessage message) throws WxErrorException {
Integer agentId = message.getAgentId();
if (null == agentId) {
message.setAgentId(this.getWxCpConfigStorage().getAgentId());
}
return WxCpMessageSendResult.fromJson(this.post(this.configStorage.getApiUrl(MESSAGE_SEND), message.toJson()));
}
@Override
public WxCpMaJsCode2SessionResult jsCode2Session(String jsCode) throws WxErrorException {
Map<String, String> params = new HashMap<>(2);
@@ -486,6 +475,11 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
return agentService;
}
@Override
public WxCpMessageService getMessageService() {
return this.messageService;
}
public void setAgentService(WxCpAgentService agentService) {
this.agentService = agentService;
}

View File

@@ -0,0 +1,31 @@
package me.chanjar.weixin.cp.api.impl;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpMessageService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
import me.chanjar.weixin.cp.constant.WxCpApiPathConsts;
/**
* 消息推送接口实现类.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @date 2020-08-30
*/
@RequiredArgsConstructor
public class WxCpMessageServiceImpl implements WxCpMessageService {
private final WxCpService cpService;
@Override
public WxCpMessageSendResult messageSend(WxCpMessage message) throws WxErrorException {
Integer agentId = message.getAgentId();
if (null == agentId) {
message.setAgentId(this.cpService.getWxCpConfigStorage().getAgentId());
}
return WxCpMessageSendResult.fromJson(this.cpService.post(this.cpService.getWxCpConfigStorage()
.getApiUrl(WxCpApiPathConsts.Message.MESSAGE_SEND), message.toJson()));
}
}

View File

@@ -14,7 +14,6 @@ public final class WxCpApiPathConsts {
public static final String GET_JSAPI_TICKET = "/cgi-bin/get_jsapi_ticket";
public static final String GET_AGENT_CONFIG_TICKET = "/cgi-bin/ticket/get?&type=agent_config";
public static final String MESSAGE_SEND = "/cgi-bin/message/send";
public static final String GET_CALLBACK_IP = "/cgi-bin/getcallbackip";
public static final String BATCH_REPLACE_PARTY = "/cgi-bin/batch/replaceparty";
public static final String BATCH_REPLACE_USER = "/cgi-bin/batch/replaceuser";
@@ -23,6 +22,22 @@ public final class WxCpApiPathConsts {
public static final String GET_TOKEN = "/cgi-bin/gettoken?corpid=%s&corpsecret=%s";
public static final String WEBHOOK_SEND = "/cgi-bin/webhook/send?key=";
/**
* 消息推送相关接口
* https://work.weixin.qq.com/api/doc/90000/90135/90235
*/
public static class Message {
/**
* 发送应用消息
*/
public static final String MESSAGE_SEND = "/cgi-bin/message/send";
/**
* 互联企业发送应用消息
*/
public static final String LINKEDCORP_MESSAGE_SEND = "/cgi-bin/linkedcorp/message/send";
}
public static class Agent {
public static final String AGENT_GET = "/cgi-bin/agent/get?agentid=%d";
public static final String AGENT_SET = "/cgi-bin/agent/set";