This commit is contained in:
van
2026-05-09 23:38:10 +08:00
parent 7582d22d2a
commit 217ea3afdc
4 changed files with 119 additions and 31 deletions

View File

@@ -12,7 +12,7 @@ public class PhoneForwardActivePushImpl implements IPhoneForwardActivePush {
private WxSendWeComPushClient wxSendWeComPushClient;
@Override
public void schedulePushChunks(String toUser, List<String> chunks) {
wxSendWeComPushClient.scheduleActivePushes(toUser, chunks);
public boolean schedulePushChunks(String toUser, List<String> chunks) {
return wxSendWeComPushClient.pushAfterPassiveDelaySync(toUser, chunks);
}
}

View File

@@ -13,6 +13,7 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@@ -33,29 +34,76 @@ public class WxSendWeComPushClient {
private String pushSecret;
/**
* 在被动回复返回后延迟再发,保证企微侧先出现首条被动消息
* 与 {@link #scheduleActivePushes(String, List)} 相同顺序与延迟,但在当前线程同步执行,并返回是否全部成功
* 供「开/慢开」异步 TG 查询结束后立刻知晓推送结果并打错误日志。
*/
public boolean pushAfterPassiveDelaySync(String toUser, List<String> contents) {
final String userId = toUser != null ? toUser.trim() : "";
final List<String> list = contents != null ? new ArrayList<>(contents) : Collections.emptyList();
if (!StringUtils.hasText(wxsendBaseUrl)) {
log.error("企微主动推送未执行:未配置 jarvis.wecom.wxsend-base-url用户会话中收不到查询结果或报错");
return false;
}
if (!StringUtils.hasText(pushSecret)) {
log.error("企微主动推送未执行:未配置 jarvis.wecom.push-secret");
return false;
}
if (!StringUtils.hasText(userId)) {
log.error("企微主动推送未执行:目标成员 UserID 为空");
return false;
}
if (list.isEmpty()) {
log.warn("企微主动推送未执行:推送内容为空 userId={}", userId);
return false;
}
try {
Thread.sleep(450);
String base = normalizeBase(wxsendBaseUrl);
String url = base + "/wecom/active-push";
boolean anySent = false;
boolean allOk = true;
for (String c : list) {
if (!StringUtils.hasText(c)) {
continue;
}
anySent = true;
if (!postJson(url, userId, c.trim())) {
allOk = false;
}
Thread.sleep(120);
}
if (!anySent) {
log.error("企微主动推送:无有效正文段 userId={}", userId);
return false;
}
if (!allOk) {
log.error("企微主动推送部分失败 userId={}(请检查 wxSend /wecom/active-push 与密钥)", userId);
}
return allOk;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
log.error("企微主动推送被中断 userId={}", userId, e);
return false;
} catch (Exception e) {
log.error("企微主动推送异常 userId={}", userId, e);
return false;
}
}
/**
* 在被动回复返回后再发,保证企微侧先出现首条被动消息。
*/
public void scheduleActivePushes(String toUser, List<String> contents) {
if (!StringUtils.hasText(wxsendBaseUrl) || !StringUtils.hasText(pushSecret)
|| !StringUtils.hasText(toUser) || contents == null || contents.isEmpty()) {
return;
}
final String userId = toUser.trim();
final List<String> list = new ArrayList<>(contents);
final String userId = toUser != null ? toUser.trim() : "";
final List<String> list = contents != null ? new ArrayList<>(contents) : Collections.emptyList();
CompletableFuture.runAsync(() -> {
try {
Thread.sleep(450);
String base = normalizeBase(wxsendBaseUrl);
String url = base + "/wecom/active-push";
for (String c : list) {
if (!StringUtils.hasText(c)) {
continue;
}
postJson(url, userId, c.trim());
Thread.sleep(120);
}
} catch (Exception e) {
log.warn("企微主动推送任务异常 userId={} msg={}", userId, e.toString());
boolean ok = pushAfterPassiveDelaySync(userId, list);
if (!ok) {
log.error(
"scheduleActivePushes 未完全成功 userId={}(用户可能未收到会话内的后续分段)",
userId);
}
});
}
@@ -68,7 +116,8 @@ public class WxSendWeComPushClient {
return b;
}
private void postJson(String url, String toUser, String content) {
/** @return HTTP 2xx 且无异常时为 true */
private boolean postJson(String url, String toUser, String content) {
JSONObject body = new JSONObject();
body.put("toUser", toUser);
body.put("content", content);
@@ -89,12 +138,14 @@ public class WxSendWeComPushClient {
InputStream is = code >= 200 && code < 300 ? conn.getInputStream() : conn.getErrorStream();
String resp = readAll(is);
if (code < 200 || code >= 300) {
log.warn("wxSend active-push HTTP {} body={}", code, resp);
} else {
log.debug("wxSend active-push OK http={} resp={}", code, resp);
log.error("wxSend active-push HTTP {} url={} body={}", code, url, resp);
return false;
}
log.debug("wxSend active-push OK http={} resp={}", code, resp);
return true;
} catch (Exception e) {
log.warn("wxSend active-push 请求失败 url={} err={}", url, e.toString());
log.error("wxSend active-push 请求失败 url={} err={}", url, e.toString(), e);
return false;
} finally {
if (conn != null) {
conn.disconnect();