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

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

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 {

View File

@@ -5,6 +5,7 @@ import com.ruoyi.jarvis.domain.dto.WeComInboundRequest;
import com.ruoyi.jarvis.domain.dto.WeComInboundResult;
import com.ruoyi.jarvis.service.IWeComInboundService;
import com.ruoyi.jarvis.service.IWeComInboundTraceService;
import com.ruoyi.jarvis.service.SuperAdminService;
import com.ruoyi.jarvis.wecom.WxSendWeComPushClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
@@ -32,6 +33,8 @@ public class WeComInboundController {
private IWeComInboundTraceService weComInboundTraceService;
@Resource
private WxSendWeComPushClient wxSendWeComPushClient;
@Resource
private SuperAdminService superAdminService;
@PostMapping("/inbound")
public AjaxResult inbound(
@@ -46,7 +49,12 @@ public class WeComInboundController {
Map<String, Object> data = new HashMap<>(4);
data.put("reply", result.getPassiveReply());
data.put("activePushCount", result.getActivePushContents().size());
wxSendWeComPushClient.scheduleActivePushes(req.getFromUserName(), result.getActivePushContents());
String pipeTouser = superAdminService.resolveTouserPipeForActivePush(req.getFromUserName());
if (!StringUtils.hasText(pipeTouser) && StringUtils.hasText(req.getFromUserName())) {
pipeTouser = req.getFromUserName().trim();
}
String callbackAgentId = StringUtils.hasText(req.getAgentId()) ? req.getAgentId().trim() : null;
wxSendWeComPushClient.scheduleActivePushes(pipeTouser, callbackAgentId, result.getActivePushContents());
return AjaxResult.success(data);
}
}