Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
邬吉朋
ops
Commits
bd088915
Commit
bd088915
authored
Oct 16, 2025
by
wujipeng
Browse files
企业微信@指定人消息
parent
459a6ae7
Changes
3
Show whitespace changes
Inline
Side-by-side
pom.xml
View file @
bd088915
...
@@ -128,6 +128,11 @@
...
@@ -128,6 +128,11 @@
<artifactId>
poi-ooxml
</artifactId>
<artifactId>
poi-ooxml
</artifactId>
<version>
5.2.5
</version>
<version>
5.2.5
</version>
</dependency>
</dependency>
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpclient
</artifactId>
<version>
4.5.13
</version>
</dependency>
</dependencies>
</dependencies>
<repositories>
<repositories>
...
...
src/main/java/com/ruzhoukj/sms/common/WeChatRobotSender.java
0 → 100644
View file @
bd088915
package
com.ruzhoukj.sms.common
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.entity.StringEntity
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
import
org.apache.http.util.EntityUtils
;
import
java.util.Arrays
;
public
class
WeChatRobotSender
{
private
final
String
webhookUrl
;
public
WeChatRobotSender
(
String
webhookUrl
)
{
this
.
webhookUrl
=
webhookUrl
;
}
public
void
sendTextMessage
(
String
content
,
String
[]
mentionedList
,
String
[]
mentionedMobileList
)
throws
Exception
{
try
(
CloseableHttpClient
httpClient
=
HttpClients
.
createDefault
())
{
HttpPost
httpPost
=
new
HttpPost
(
this
.
webhookUrl
);
httpPost
.
setHeader
(
"Content-Type"
,
"application/json; charset=utf-8"
);
// 构建JSON请求体
String
jsonMsg
=
buildTextMessageJson
(
content
,
mentionedList
,
mentionedMobileList
);
System
.
out
.
println
(
"请求体: "
+
jsonMsg
);
StringEntity
entity
=
new
StringEntity
(
jsonMsg
,
"UTF-8"
);
httpPost
.
setEntity
(
entity
);
// 发送请求并获取响应
try
(
CloseableHttpResponse
response
=
httpClient
.
execute
(
httpPost
))
{
String
responseBody
=
EntityUtils
.
toString
(
response
.
getEntity
());
System
.
out
.
println
(
"响应结果: "
+
responseBody
);
}
}
}
private
String
buildTextMessageJson
(
String
content
,
String
[]
mentionedList
,
String
[]
mentionedMobileList
)
{
// 使用字符串拼接或Json库(如Jackson、Gson)构建JSON更稳妥,此处为示例
StringBuilder
json
=
new
StringBuilder
();
json
.
append
(
"{"
);
json
.
append
(
"\"msgtype\": \"text\","
);
json
.
append
(
"\"text\": {"
);
json
.
append
(
"\"content\": \""
).
append
(
content
).
append
(
"\""
);
if
(
mentionedList
!=
null
&&
mentionedList
.
length
>
0
)
{
json
.
append
(
", \"mentioned_list\": "
).
append
(
Arrays
.
toString
(
mentionedList
).
replace
(
"["
,
"[\""
).
replace
(
"]"
,
"\"]"
).
replace
(
", "
,
"\", \""
));
}
if
(
mentionedMobileList
!=
null
&&
mentionedMobileList
.
length
>
0
)
{
json
.
append
(
", \"mentioned_mobile_list\": "
).
append
(
Arrays
.
toString
(
mentionedMobileList
).
replace
(
"["
,
"[\""
).
replace
(
"]"
,
"\"]"
).
replace
(
", "
,
"\", \""
));
}
json
.
append
(
"}"
);
json
.
append
(
"}"
);
return
json
.
toString
();
}
public
static
void
main
(
String
[]
args
)
{
String
webhookUrl
=
"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=04c5f353-33a7-4cb0-8d00-798dee5df953"
;
WeChatRobotSender
sender
=
new
WeChatRobotSender
(
webhookUrl
);
try
{
// 发送文本消息,并@指定手机号的成员和所有人
// sender.sendTextMessage("这个月的运维工作记得做", null, new String[]{"19825040761", "@all"});
// sender.sendTextMessage("这个月的运维工作记得做", null, new String[]{"19825040761"});
// sender.sendTextMessage("这个月的运维工作记得做", null, new String[]{"19825040761"});
}
catch
(
Exception
ignored
)
{
}
}
}
\ No newline at end of file
src/main/java/com/ruzhoukj/sms/controller/CertificateController.java
View file @
bd088915
...
@@ -2,6 +2,7 @@ package com.ruzhoukj.sms.controller;
...
@@ -2,6 +2,7 @@ package com.ruzhoukj.sms.controller;
import
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
;
import
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
;
import
com.platform.model.resp.Result
;
import
com.platform.model.resp.Result
;
import
com.ruzhoukj.sms.common.WeChatRobotSender
;
import
com.ruzhoukj.sms.entity.CertificateEntity
;
import
com.ruzhoukj.sms.entity.CertificateEntity
;
import
com.ruzhoukj.sms.entity.kzReminderEntity
;
import
com.ruzhoukj.sms.entity.kzReminderEntity
;
import
com.ruzhoukj.sms.pojo.dto.CertificateDTO
;
import
com.ruzhoukj.sms.pojo.dto.CertificateDTO
;
...
@@ -136,4 +137,17 @@ public class CertificateController {
...
@@ -136,4 +137,17 @@ public class CertificateController {
return
Result
.
data
(
null
);
return
Result
.
data
(
null
);
}
}
@GetMapping
(
"/specified/send"
)
public
Result
<
Boolean
>
send
(
@RequestParam
(
"phone"
)
String
phone
,
@RequestParam
(
"content"
)
String
content
)
{
String
webhookUrl
=
"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=04c5f353-33a7-4cb0-8d00-798dee5df953"
;
WeChatRobotSender
sender
=
new
WeChatRobotSender
(
webhookUrl
);
try
{
// 发送文本消息,并@指定手机号的成员
sender
.
sendTextMessage
(
content
,
null
,
new
String
[]{
phone
});
return
Result
.
data
(
true
);
}
catch
(
Exception
ignored
)
{
return
Result
.
data
(
false
);
}
}
}
}
\ No newline at end of file
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