diff --git a/src/main/java/cn/van333/wxsend/business/controller/WeComActivePushController.java b/src/main/java/cn/van333/wxsend/business/controller/WeComActivePushController.java index 5d03fbb..0d21511 100644 --- a/src/main/java/cn/van333/wxsend/business/controller/WeComActivePushController.java +++ b/src/main/java/cn/van333/wxsend/business/controller/WeComActivePushController.java @@ -17,6 +17,8 @@ import javax.annotation.Resource; *

* Header: X-WxSend-WeCom-Push-Secret 与 {@code jarvis.wecom.push-secret} 一致 *

+ * JSON 体:{@code toUser}(企微成员 UserID,多个用 {@code |} 分隔)、{@code content}、可选 {@code agentId} + * (与回调明文 XML 中 AgentId 一致时强烈建议传入,以便选用与被动消息同一自建应用的 secret)。 * 闲鱼通知请使用 {@code WXController} 的 {@code POST /wx/send/goofish}(与 {@code /send/pdd} 相同 Header vanToken + 请求体 {@code MessageRequest})。 */ @RestController @@ -47,7 +49,10 @@ public class WeComActivePushController { return R.error(400, "content 不能为空"); } try { - weComApplicationTextPushService.sendTextToUser(body.getToUser().trim(), body.getContent()); + weComApplicationTextPushService.sendTextToUser( + body.getToUser().trim(), + body.getContent(), + body.getAgentId()); return R.ok("sent"); } catch (Exception e) { log.warn("企微主动推送失败 toUser={} err={}", body.getToUser(), e.toString()); diff --git a/src/main/java/cn/van333/wxsend/business/dto/WeComActivePushRequest.java b/src/main/java/cn/van333/wxsend/business/dto/WeComActivePushRequest.java index d050daa..298c043 100644 --- a/src/main/java/cn/van333/wxsend/business/dto/WeComActivePushRequest.java +++ b/src/main/java/cn/van333/wxsend/business/dto/WeComActivePushRequest.java @@ -7,6 +7,8 @@ public class WeComActivePushRequest { private String toUser; private String content; + /** 可选:与回调 XML 中 AgentId 一致时传入,用于选用与被动同一自建应用的 secret;不传则用 qywx.app.agentId */ + private String agentId; public String getToUser() { return toUser; @@ -23,4 +25,12 @@ public class WeComActivePushRequest { public void setContent(String content) { this.content = content; } + + public String getAgentId() { + return agentId; + } + + public void setAgentId(String agentId) { + this.agentId = agentId; + } } diff --git a/src/main/java/cn/van333/wxsend/business/service/WeComApplicationTextPushService.java b/src/main/java/cn/van333/wxsend/business/service/WeComApplicationTextPushService.java index 53487b2..7cf86eb 100644 --- a/src/main/java/cn/van333/wxsend/business/service/WeComApplicationTextPushService.java +++ b/src/main/java/cn/van333/wxsend/business/service/WeComApplicationTextPushService.java @@ -52,10 +52,43 @@ public class WeComApplicationTextPushService { private String goofishSecret; public void sendTextToUser(String toUser, String content) throws Exception { - if (StrUtil.isBlank(corpId) || StrUtil.isBlank(corpSecret) || StrUtil.isBlank(agentId)) { - throw new IllegalStateException("未配置 qywx.app.corpId / agentId / secret,无法主动发应用消息"); + sendTextToUser(toUser, content, null); + } + + /** + * @param requestAgentId 与回调 XML 中 AgentId 一致的字符串;为空则使用 {@code qywx.app.agentId}; + * 若与 {@link #goofishAgentId} 一致则使用闲鱼应用 secret。 + */ + public void sendTextToUser(String toUser, String content, String requestAgentId) throws Exception { + if (StrUtil.isBlank(corpId)) { + throw new IllegalStateException("未配置 qywx.app.corpId,无法主动发应用消息"); } - sendTextWithAgent(toUser, content, agentId, corpSecret); + String rid = StrUtil.trimToEmpty(requestAgentId); + if (StrUtil.isBlank(rid)) { + if (StrUtil.isBlank(corpSecret) || StrUtil.isBlank(agentId)) { + throw new IllegalStateException("未配置 qywx.app.agentId / secret,无法主动发应用消息"); + } + sendTextWithAgent(toUser, content, agentId, corpSecret); + return; + } + String defAid = StrUtil.trimToEmpty(agentId); + String fishAid = StrUtil.trimToEmpty(goofishAgentId); + if (StrUtil.isNotBlank(defAid) && rid.equals(defAid)) { + if (StrUtil.isBlank(corpSecret)) { + throw new IllegalStateException("未配置 qywx.app.secret(与 agentId 对应)"); + } + sendTextWithAgent(toUser, content, defAid, corpSecret); + return; + } + if (StrUtil.isNotBlank(fishAid) && rid.equals(fishAid)) { + if (StrUtil.isBlank(goofishSecret)) { + throw new IllegalStateException("未配置 qywx.app.goofishSecret(与 goofishAgentId 对应)"); + } + sendTextWithAgent(toUser, content, fishAid, goofishSecret); + return; + } + throw new IllegalStateException( + "active-push 未识别的 agentId=" + rid + ",与 qywx.app.agentId / goofishAgentId 均不一致;请补配置或传空使用默认应用"); } /**