Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yd-notice
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
xingmin
yd-notice
Commits
5ed42264
Commit
5ed42264
authored
Apr 07, 2026
by
zhangxingmin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
push
parent
da721aef
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
5 deletions
+27
-5
yd-notice-service/src/main/java/com/yd/notice/service/send/WecomMessageSender.java
+27
-5
No files found.
yd-notice-service/src/main/java/com/yd/notice/service/send/WecomMessageSender.java
View file @
5ed42264
...
...
@@ -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
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment