This commit is contained in:
van
2026-05-09 23:46:33 +08:00
parent 217ea3afdc
commit 83dc41f3a5
7 changed files with 100 additions and 25 deletions

View File

@@ -34,12 +34,13 @@ public class WxSendWeComPushClient {
private String pushSecret;
/**
* 与 {@link #scheduleActivePushes(String, List)} 相同顺序与延迟,但在当前线程同步执行,并返回是否全部成功。
* 供「开/慢开」异步 TG 查询结束后立刻知晓推送结果并打错误日志
* 与 {@link #scheduleActivePushes(String, String, List)} 相同顺序与延迟,但在当前线程同步执行,并返回是否全部成功。
* {@code wecomCallbackAgentId} 与回调 XML 中 AgentId 一致时须传入,便于 wxSend 选用对应应用 secret 调用 message/send
*/
public boolean pushAfterPassiveDelaySync(String toUser, List<String> contents) {
public boolean pushAfterPassiveDelaySync(String toUser, String wecomCallbackAgentId, List<String> contents) {
final String userId = toUser != null ? toUser.trim() : "";
final List<String> list = contents != null ? new ArrayList<>(contents) : Collections.emptyList();
final String agentOpt = StringUtils.hasText(wecomCallbackAgentId) ? wecomCallbackAgentId.trim() : null;
if (!StringUtils.hasText(wxsendBaseUrl)) {
log.error("企微主动推送未执行:未配置 jarvis.wecom.wxsend-base-url用户会话中收不到查询结果或报错");
@@ -69,7 +70,7 @@ public class WxSendWeComPushClient {
continue;
}
anySent = true;
if (!postJson(url, userId, c.trim())) {
if (!postJson(url, userId, c.trim(), agentOpt)) {
allOk = false;
}
Thread.sleep(120);
@@ -95,11 +96,13 @@ public class WxSendWeComPushClient {
/**
* 在被动回复返回后再发,保证企微侧先出现首条被动消息。
*/
public void scheduleActivePushes(String toUser, List<String> contents) {
public void scheduleActivePushes(String toUser, String wecomCallbackAgentId, List<String> contents) {
final String userId = toUser != null ? toUser.trim() : "";
final String agentPass = wecomCallbackAgentId != null ? wecomCallbackAgentId.trim() : "";
final List<String> list = contents != null ? new ArrayList<>(contents) : Collections.emptyList();
CompletableFuture.runAsync(() -> {
boolean ok = pushAfterPassiveDelaySync(userId, list);
boolean ok = pushAfterPassiveDelaySync(
userId, StringUtils.hasText(agentPass) ? agentPass : null, list);
if (!ok) {
log.error(
"scheduleActivePushes 未完全成功 userId={}(用户可能未收到会话内的后续分段)",
@@ -117,10 +120,13 @@ public class WxSendWeComPushClient {
}
/** @return HTTP 2xx 且无异常时为 true */
private boolean postJson(String url, String toUser, String content) {
private boolean postJson(String url, String toUser, String content, String agentIdOpt) {
JSONObject body = new JSONObject();
body.put("toUser", toUser);
body.put("content", content);
if (StringUtils.hasText(agentIdOpt)) {
body.put("agentId", agentIdOpt.trim());
}
byte[] bytes = body.toJSONString().getBytes(StandardCharsets.UTF_8);
HttpURLConnection conn = null;
try {