Commit 5ed42264 by zhangxingmin

push

parent da721aef
......@@ -10,9 +10,10 @@ import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.Arrays;
import java.util.List;
......@@ -32,6 +33,11 @@ public class WecomMessageSender implements MessageSender {
"40014" // 不合法的 access_token
);
/**
* 发送企业微信应用消息
* @param task 消息任务(含标题、内容、接收人、渠道ID等)
* @return
*/
@Override
public SendResult send(NotificationTask task) {
try {
......@@ -58,15 +64,31 @@ public class WecomMessageSender implements MessageSender {
WxCpService wxCpService = new WxCpServiceImpl();
wxCpService.setWxCpConfigStorage(wxConfig);
// 4. 构建消息并发送
// 4. 构建消息
String receiver = task.getReceiver(); // 支持 "user1|user2|user3" 多接收人格式
if (!StringUtils.hasText(receiver)) {
return SendResult.failNonRetryable("EMPTY_RECEIVER", "接收人为空");
}
WxCpMessage message = WxCpMessage.TEXT()
.agentId(agentId)
.toUser(task.getReceiver())
.toUser(receiver) // 企业微信原生支持用 "|" 分隔多个 UserID
.content(task.getContent())
.build();
wxCpService.getMessageService().send(message);
log.info("企业微信消息发送成功, taskBizId={}, receiver={}", task.getTaskBizId(), task.getReceiver());
// 5. 发送消息并获取详细结果
WxCpMessageSendResult result = wxCpService.getMessageService().send(message);
// 6. 处理发送结果中的无效用户
if (result != null && result.getInvalidUser() != null && !result.getInvalidUser().isEmpty()) {
String invalidUsers = String.join(",", result.getInvalidUser());
log.warn("企业微信部分用户发送失败, taskBizId={}, invalidUsers={}", task.getTaskBizId(), invalidUsers);
// 可根据业务需求决定返回部分成功还是失败,这里返回可重试的失败(让上层决定是否重新发送)
return SendResult.failRetryable("PARTIAL_FAILURE",
"以下用户发送失败: " + invalidUsers);
}
log.info("企业微信消息发送成功, taskBizId={}, receiver={}", task.getTaskBizId(), receiver);
return SendResult.success();
} catch (WxErrorException e) {
......@@ -87,4 +109,4 @@ public class WecomMessageSender implements MessageSender {
public String getSupportedChannelType() {
return "wecom";
}
}
\ No newline at end of file
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment