This commit is contained in:
van
2026-05-09 23:46:27 +08:00
parent 5d8cff2029
commit a48570da44
3 changed files with 52 additions and 4 deletions

View File

@@ -17,6 +17,8 @@ import javax.annotation.Resource;
* <p> * <p>
* Header: X-WxSend-WeCom-Push-Secret 与 {@code jarvis.wecom.push-secret} 一致 * Header: X-WxSend-WeCom-Push-Secret 与 {@code jarvis.wecom.push-secret} 一致
* <p> * <p>
* 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})。 * 闲鱼通知请使用 {@code WXController} 的 {@code POST /wx/send/goofish}(与 {@code /send/pdd} 相同 Header vanToken + 请求体 {@code MessageRequest})。
*/ */
@RestController @RestController
@@ -47,7 +49,10 @@ public class WeComActivePushController {
return R.error(400, "content 不能为空"); return R.error(400, "content 不能为空");
} }
try { try {
weComApplicationTextPushService.sendTextToUser(body.getToUser().trim(), body.getContent()); weComApplicationTextPushService.sendTextToUser(
body.getToUser().trim(),
body.getContent(),
body.getAgentId());
return R.ok("sent"); return R.ok("sent");
} catch (Exception e) { } catch (Exception e) {
log.warn("企微主动推送失败 toUser={} err={}", body.getToUser(), e.toString()); log.warn("企微主动推送失败 toUser={} err={}", body.getToUser(), e.toString());

View File

@@ -7,6 +7,8 @@ public class WeComActivePushRequest {
private String toUser; private String toUser;
private String content; private String content;
/** 可选:与回调 XML 中 AgentId 一致时传入,用于选用与被动同一自建应用的 secret不传则用 qywx.app.agentId */
private String agentId;
public String getToUser() { public String getToUser() {
return toUser; return toUser;
@@ -23,4 +25,12 @@ public class WeComActivePushRequest {
public void setContent(String content) { public void setContent(String content) {
this.content = content; this.content = content;
} }
public String getAgentId() {
return agentId;
}
public void setAgentId(String agentId) {
this.agentId = agentId;
}
} }

View File

@@ -52,10 +52,43 @@ public class WeComApplicationTextPushService {
private String goofishSecret; private String goofishSecret;
public void sendTextToUser(String toUser, String content) throws Exception { public void sendTextToUser(String toUser, String content) throws Exception {
if (StrUtil.isBlank(corpId) || StrUtil.isBlank(corpSecret) || StrUtil.isBlank(agentId)) { sendTextToUser(toUser, content, null);
throw new IllegalStateException("未配置 qywx.app.corpId / agentId / secret无法主动发应用消息"); }
/**
* @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无法主动发应用消息");
}
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); 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 均不一致;请补配置或传空使用默认应用");
} }
/** /**