Compare commits

...

24 Commits

Author SHA1 Message Date
Leo
316cc7ea48 1 2025-12-02 17:39:35 +08:00
Leo
33d70cf266 1 2025-12-02 01:44:27 +08:00
Leo
ed1f241d9a 1 2025-12-02 01:34:30 +08:00
Leo
2a77188468 1 2025-11-05 16:50:24 +08:00
Leo
fb72e5284d 1 2025-11-05 16:36:54 +08:00
雷欧(林平凡)
3cc419dc17 1 2025-09-04 18:03:42 +08:00
雷欧(林平凡)
c488e94534 1 2025-09-04 17:54:18 +08:00
雷欧(林平凡)
204dae5860 1 2025-09-04 17:38:27 +08:00
d606d4a9d3 1 2025-08-31 02:14:53 +08:00
0cfa0b9bbf 1 2025-08-31 02:08:17 +08:00
3960baa105 1 2025-08-31 00:50:01 +08:00
ec9416e390 1 2025-08-31 00:24:44 +08:00
07c2ee659b 1 2025-08-30 23:55:11 +08:00
雷欧(林平凡)
78e32fbbf1 1 2025-07-29 16:28:29 +08:00
雷欧(林平凡)
3edd22705e 1 2025-07-29 16:04:54 +08:00
雷欧(林平凡)
5a412dc22e 1 2025-02-11 16:01:56 +08:00
雷欧(林平凡)
fd399c53b5 1 2025-02-05 09:54:27 +08:00
雷欧(林平凡)
2e5f5fdf88 1 2025-01-22 10:06:14 +08:00
雷欧(林平凡)
8279b4d8b2 1 2025-01-22 09:54:59 +08:00
雷欧(林平凡)
eb1a18f7a3 1 2025-01-22 09:52:12 +08:00
雷欧(林平凡)
687adfe3f8 1 2025-01-22 09:49:42 +08:00
雷欧(林平凡)
351c37ee2f 1 2025-01-22 09:44:31 +08:00
雷欧(林平凡)
f549846e1e 1 2025-01-21 14:31:39 +08:00
雷欧(林平凡)
f95035345d 1 2025-01-21 14:09:34 +08:00
29 changed files with 1319 additions and 185 deletions

11
pom.xml
View File

@@ -40,14 +40,9 @@
-->
<dependency>
<groupId>javax.rmi</groupId>
<artifactId>javax.rmi-api</artifactId>
<version>1.0.2.Final</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
<groupId>com.sun.activation</groupId>
<artifactId>jakarta.activation</artifactId>
<version>1.2.1</version>
</dependency>
<!-- redis 缓存操作 -->
<dependency>

View File

@@ -3,7 +3,10 @@ package cn.van333.wxsend.aop.annotation;
import cn.van333.wxsend.constant.CacheConstants;
import cn.van333.wxsend.enums.LimitType;
import java.lang.annotation.*;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author Leo

View File

@@ -1,9 +1,5 @@
package cn.van333.wxsend.aop.aspectj;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
import cn.van333.wxsend.aop.annotation.RateLimiter;
import cn.van333.wxsend.enums.LimitType;
import cn.van333.wxsend.exception.ServiceException;
@@ -20,6 +16,10 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
/**
* 限流处理

View File

@@ -1,16 +1,13 @@
package cn.van333.wxsend.business.controller;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.van333.wxsend.aop.annotation.RateLimiter;
import cn.van333.wxsend.business.model.R;
import cn.van333.wxsend.business.service.LogService;
import cn.van333.wxsend.enums.WXMessageType;
import cn.van333.wxsend.util.SourceForQLUtil;
import cn.van333.wxsend.util.TokenUtil;
import cn.van333.wxsend.util.WxSendUtil;
import cn.van333.wxsend.util.request.MessageRequest;
import com.alibaba.fastjson2.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
@@ -18,8 +15,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
* @author Leo
* @version 1.0
@@ -43,7 +38,7 @@ public class DCController {
if (!StrUtil.isAllNotEmpty(message.getTitle(), message.getText())) {
return R.error("缺少标题和内容");
}
String result = WxSendUtil.sendNotify(message.getTitle(), message.getText(), message.getTouser(), WXMessageType.TY);
String result = WxSendUtil.sendNotifyForMpnews(message.getTitle(), message.getText(), message.getTouser(), WXMessageType.TY);
return R.ok(result);
}
}

View File

@@ -5,17 +5,19 @@ import cn.hutool.core.util.StrUtil;
import cn.van333.wxsend.aop.annotation.RateLimiter;
import cn.van333.wxsend.business.model.R;
import cn.van333.wxsend.business.service.LogService;
import cn.van333.wxsend.util.TokenUtil;
import cn.van333.wxsend.enums.WXMessageType;
import cn.van333.wxsend.util.SourceForQLUtil;
import cn.van333.wxsend.util.TokenUtil;
import cn.van333.wxsend.util.WxSendUtil;
import cn.van333.wxsend.util.request.MessageRequest;
import cn.van333.wxsend.util.SourceForQLUtil;
import com.alibaba.fastjson2.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.rmi.CORBA.Util;
import javax.servlet.http.HttpServletRequest;
/**
@@ -47,7 +49,7 @@ public class WXController {
if (ObjectUtil.isEmpty(WXMessageType.valueOf(message.getMessageType()))) {
return R.error("消息类型不存在");
}
String result = WxSendUtil.sendNotify(message.getTitle(), message.getText(), message.getTouser(), WXMessageType.valueOf(message.getMessageType()));
String result = WxSendUtil.sendNotifyForMpnews(message.getTitle(), message.getText(), message.getTouser(), WXMessageType.valueOf(message.getMessageType()));
return R.ok(result);
}
@@ -82,10 +84,11 @@ public class WXController {
logger.info("vanToken 打印---{}",vanToken);
logger.info("source 打印---{}",source);
String sourceForQL = SourceForQLUtil.transferSource(source);
String a = "";
if (!StrUtil.isAllNotEmpty(message.getTitle(), message.getText())) {
return R.error("缺少标题和内容");
}
String result = WxSendUtil.sendNotify("("+sourceForQL+") "+message.getTitle(), message.getText(), message.getTouser(), WXMessageType.QL);
String result = WxSendUtil.sendNotifyForMpnews("("+sourceForQL+") "+message.getTitle(), message.getText(), message.getTouser(), WXMessageType.QL);
logger.info("result 打印---{}",result);
return R.ok(result);
@@ -103,7 +106,7 @@ public class WXController {
if (!StrUtil.isAllNotEmpty(message.getTitle(), message.getText())) {
return R.error("缺少标题和内容");
}
String result = WxSendUtil.sendNotify(message.getTitle(), message.getText(), message.getTouser(), WXMessageType.QH);
String result = WxSendUtil.sendNotifyForMpnews(message.getTitle(), message.getText(), message.getTouser(), WXMessageType.QH);
return R.ok(result);
}
@@ -119,25 +122,11 @@ public class WXController {
if (!StrUtil.isAllNotEmpty(message.getTitle(), message.getText())) {
return R.error("缺少标题和内容");
}
String result = WxSendUtil.sendNotify(message.getTitle(), message.getText(), message.getTouser(), WXMessageType.MT);
String result = WxSendUtil.sendNotifyForMpnews(message.getTitle(), message.getText(), message.getTouser(), WXMessageType.MT);
return R.ok(result);
}
@RequestMapping(value = "/send/imt")
@ResponseBody
@RateLimiter(time = 1, count = 60)
public R sendToIMT(@RequestBody MessageRequest message) throws Exception {
logger.info(message.toString());
if (!TokenUtil.checkToken(message.getVanToken())) {
return R.error("vanToken无效");
}
if (!StrUtil.isAllNotEmpty(message.getTitle(), message.getText())) {
return R.error("缺少标题和内容");
}
String result = WxSendUtil.sendNotify(message.getTitle(), message.getText(), message.getTouser(), WXMessageType.IMT);
return R.ok(result);
}
@RequestMapping(value = "/send/ty")
@ResponseBody
@@ -151,11 +140,11 @@ public class WXController {
if (!StrUtil.isAllNotEmpty(message.getTitle(), message.getText())) {
return R.error("缺少标题和内容");
}
String result = WxSendUtil.sendNotify(message.getTitle(), message.getText(), message.getTouser(), WXMessageType.TY);
String result = WxSendUtil.sendNotifyForMpnews(message.getTitle(), message.getText(), message.getTouser(), WXMessageType.TY);
return R.ok(result);
}
@RequestMapping(value = "/send/lf")
@RequestMapping(value = "/send/jenkins")
@ResponseBody
@RateLimiter(time = 5, count = 60)
public R sendToLF(HttpServletRequest request, @RequestBody MessageRequest message) throws Exception {
@@ -180,7 +169,7 @@ public class WXController {
if (!StrUtil.isAllNotEmpty(message.getTitle(), message.getText())) {
return R.error("缺少标题和内容");
}
String result = WxSendUtil.sendNotify("("+sourceForQL+") "+message.getTitle(), message.getText(), message.getTouser(), WXMessageType.LF);
String result = WxSendUtil.sendNotifyForMpnews("("+sourceForQL+") "+message.getTitle(), message.getText(), message.getTouser(), WXMessageType.JENKINS);
logger.info("result 打印---{}",result);
return R.ok(result);
@@ -188,7 +177,7 @@ public class WXController {
@RequestMapping(value = "/send/jd")
@ResponseBody
@RateLimiter(time = 5, count = 60)
@RateLimiter(time = 2, count = 1)
public R sendToJD(HttpServletRequest request, @RequestBody MessageRequest message) throws Exception {
logger.info("message 打印---{}",JSON.toJSONString(message));
String vanToken = request.getHeader("vanToken");
@@ -198,20 +187,24 @@ public class WXController {
if (!TokenUtil.checkToken(vanToken)) {
return R.error("vanToken无效");
}
String source = request.getHeader("source");
if (StrUtil.isEmpty(source)) {
return R.error("来源为空");
String result = WxSendUtil.sendNotifyForText(message.getText(), message.getTouser(), WXMessageType.JD);
logger.info("result 打印---{}",result);
return R.ok(result);
}
@RequestMapping(value = "/send/pdd")
@ResponseBody
@RateLimiter(time = 2, count = 1)
public R sendToPDD(HttpServletRequest request, @RequestBody MessageRequest message) throws Exception {
logger.info("message 打印---{}",JSON.toJSONString(message));
String vanToken = request.getHeader("vanToken");
if (StrUtil.isEmpty(vanToken)) {
return R.error("vanToken为空");
}
if (!StrUtil.isAllNotEmpty(message.getTitle(), message.getText())) {
return R.error("缺少标题和内容");
if (!TokenUtil.checkToken(vanToken)) {
return R.error("vanToken无效");
}
logger.info("vanToken 打印---{}",vanToken);
logger.info("source 打印---{}",source);
String sourceForQL = SourceForQLUtil.transferSource(source);
if (!StrUtil.isAllNotEmpty(message.getTitle(), message.getText())) {
return R.error("缺少标题和内容");
}
String result = WxSendUtil.sendNotify("("+sourceForQL+") "+message.getTitle(), message.getText(), message.getTouser(), WXMessageType.JD);
String result = WxSendUtil.sendNotifyForText(message.getText(), message.getTouser(), WXMessageType.PDD);
logger.info("result 打印---{}",result);
return R.ok(result);

View File

@@ -0,0 +1,170 @@
package cn.van333.wxsend.business.controller;
import cn.hutool.core.util.StrUtil;
import cn.van333.wxsend.business.model.R;
import cn.van333.wxsend.business.service.LogService;
import cn.van333.wxsend.util.QywxWebhookUtil;
import cn.van333.wxsend.util.TokenUtil;
import cn.van333.wxsend.util.request.WebhookMessageRequest;
import cn.van333.wxsend.util.response.SendRespones;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/wx/webhook")
public class WXWebhookController {
private static final Logger logger = LoggerFactory.getLogger(LogService.class);
@Value("${qywx.webhook.key:}")
private String defaultKey;
@Value("${qywx.webhook.secret:}")
private String defaultSecret;
@RequestMapping("/send")
@ResponseBody
public R send(@RequestBody WebhookMessageRequest req) {
logger.info("webhook send req: {}", req);
if (!TokenUtil.checkToken(req.getVanToken())) {
return R.error("vanToken无效");
}
String key = StrUtil.isNotBlank(req.getKey()) ? req.getKey() : defaultKey;
String secret = StrUtil.isNotBlank(req.getSecret()) ? req.getSecret() : defaultSecret;
if (StrUtil.isBlank(key)) {
return R.error("缺少webhook key");
}
if (StrUtil.isBlank(req.getMsgtype())) {
return R.error("缺少msgtype");
}
SendRespones resp;
switch (req.getMsgtype().toLowerCase()) {
case "text":
resp = QywxWebhookUtil.sendText(key, secret, req.getContent(), req.getMentionedList(), req.getMentionedMobileList());
break;
case "markdown":
resp = QywxWebhookUtil.sendMarkdown(key, secret, req.getContent());
break;
case "image":
if (StrUtil.isNotBlank(req.getImageBase64()) && StrUtil.isNotBlank(req.getImageMd5())) {
resp = QywxWebhookUtil.sendImageByBase64(key, secret, req.getImageBase64(), req.getImageMd5());
} else if (StrUtil.isNotBlank(req.getImageUrl())) {
resp = QywxWebhookUtil.sendImageByUrl(key, secret, req.getImageUrl());
} else {
return R.error("image需要imageBase64+imageMd5或imageUrl");
}
break;
case "news":
if (req.getNewsArticles() == null || req.getNewsArticles().isEmpty()) {
return R.error("news需要articles");
}
java.util.List<java.util.Map<String, String>> arts = new java.util.ArrayList<>();
for (cn.van333.wxsend.util.request.NewsArticle a : req.getNewsArticles()) {
java.util.Map<String, String> m = new java.util.HashMap<>();
if (StrUtil.isNotBlank(a.getTitle())) m.put("title", a.getTitle());
if (StrUtil.isNotBlank(a.getDescription())) m.put("description", a.getDescription());
if (StrUtil.isNotBlank(a.getUrl())) m.put("url", a.getUrl());
if (StrUtil.isNotBlank(a.getPicurl())) m.put("picurl", a.getPicurl());
arts.add(m);
}
resp = QywxWebhookUtil.sendNews(key, secret, arts);
break;
case "file":
if (StrUtil.isNotBlank(req.getFileBase64()) && StrUtil.isNotBlank(req.getFileName())) {
byte[] bytes = java.util.Base64.getDecoder().decode(req.getFileBase64());
resp = QywxWebhookUtil.sendFile(key, secret, bytes, req.getFileName());
} else if (StrUtil.isNotBlank(req.getFileUrl())) {
try {
cn.hutool.http.HttpResponse r = cn.hutool.http.HttpRequest.get(req.getFileUrl()).execute();
String disp = r.header("Content-Disposition");
String name = req.getFileName();
if (StrUtil.isBlank(name)) {
name = "file";
}
resp = QywxWebhookUtil.sendFile(key, secret, r.bodyBytes(), name);
} catch (Exception e) {
return R.error("下载文件失败:" + e.getMessage());
}
} else {
return R.error("file需要fileBase64+fileName或fileUrl");
}
break;
case "template_card":
if (StrUtil.isBlank(req.getCardType())) {
return R.error("template_card需要cardType");
}
java.util.Map<String, Object> card = new java.util.HashMap<>();
card.put("card_type", req.getCardType());
if (StrUtil.isNotBlank(req.getCardSourceIconUrl()) || StrUtil.isNotBlank(req.getCardSourceDesc())) {
java.util.Map<String, Object> source = new java.util.HashMap<>();
if (StrUtil.isNotBlank(req.getCardSourceIconUrl())) source.put("icon_url", req.getCardSourceIconUrl());
if (StrUtil.isNotBlank(req.getCardSourceDesc())) source.put("desc", req.getCardSourceDesc());
card.put("source", source);
}
if (StrUtil.isNotBlank(req.getCardMainTitle()) || StrUtil.isNotBlank(req.getCardMainTitleDesc())) {
java.util.Map<String, Object> mainTitle = new java.util.HashMap<>();
if (StrUtil.isNotBlank(req.getCardMainTitle())) mainTitle.put("title", req.getCardMainTitle());
if (StrUtil.isNotBlank(req.getCardMainTitleDesc())) mainTitle.put("desc", req.getCardMainTitleDesc());
card.put("main_title", mainTitle);
}
if (StrUtil.isNotBlank(req.getCardEmphasisContentTitle()) || StrUtil.isNotBlank(req.getCardEmphasisContentDesc())) {
java.util.Map<String, Object> emphasis = new java.util.HashMap<>();
if (StrUtil.isNotBlank(req.getCardEmphasisContentTitle())) emphasis.put("title", req.getCardEmphasisContentTitle());
if (StrUtil.isNotBlank(req.getCardEmphasisContentDesc())) emphasis.put("desc", req.getCardEmphasisContentDesc());
card.put("emphasis_content", emphasis);
}
if (StrUtil.isNotBlank(req.getCardQuoteAreaType()) || StrUtil.isNotBlank(req.getCardQuoteAreaText())) {
java.util.Map<String, Object> quote = new java.util.HashMap<>();
if (StrUtil.isNotBlank(req.getCardQuoteAreaType())) quote.put("type", Integer.parseInt(req.getCardQuoteAreaType()));
if (StrUtil.isNotBlank(req.getCardQuoteAreaText())) quote.put("quote_text", req.getCardQuoteAreaText());
card.put("quote_area", quote);
}
if (StrUtil.isNotBlank(req.getCardJumpUrl())) {
card.put("card_action", new java.util.HashMap<String, Object>() {{ put("type", 1); put("url", req.getCardJumpUrl()); }});
}
if (req.getCardHorizontalContents() != null && !req.getCardHorizontalContents().isEmpty()) {
java.util.List<java.util.Map<String, Object>> list = new java.util.ArrayList<>();
for (cn.van333.wxsend.util.request.TemplateCardHorizontalContent hc : req.getCardHorizontalContents()) {
java.util.Map<String, Object> m = new java.util.HashMap<>();
if (StrUtil.isNotBlank(hc.getKeyname())) m.put("keyname", hc.getKeyname());
java.util.Map<String, Object> val = new java.util.HashMap<>();
val.put("type", 0);
if (StrUtil.isNotBlank(hc.getValue())) val.put("content", hc.getValue());
if (StrUtil.isNotBlank(hc.getUrl())) val.put("url", hc.getUrl());
m.put("value", val);
list.add(m);
}
card.put("horizontal_content_list", list);
}
if (req.getCardJumps() != null && !req.getCardJumps().isEmpty()) {
java.util.List<java.util.Map<String, Object>> list = new java.util.ArrayList<>();
for (cn.van333.wxsend.util.request.TemplateCardJump j : req.getCardJumps()) {
java.util.Map<String, Object> m = new java.util.HashMap<>();
if (StrUtil.isNotBlank(j.getType())) m.put("type", Integer.parseInt(j.getType()));
if (StrUtil.isNotBlank(j.getUrl())) m.put("url", j.getUrl());
if (StrUtil.isNotBlank(j.getTitle())) m.put("title", j.getTitle());
list.add(m);
}
card.put("jump_list", list);
}
resp = QywxWebhookUtil.sendTemplateCard(key, secret, card);
break;
default:
return R.error("不支持的msgtype");
}
if (resp.getErrcode() != null && resp.getErrcode().equals(0)) {
return R.ok("Webhook消息发送成功");
}
return R.error(resp.getErrmsg() == null ? "发送失败" : resp.getErrmsg());
}
}

View File

@@ -0,0 +1,91 @@
package cn.van333.wxsend.business.controller;
import cn.hutool.core.util.StrUtil;
import cn.van333.wxsend.business.model.R;
import cn.van333.wxsend.util.WeComCallbackCrypto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/wecom/callback")
public class WeComCallbackController {
private static final Logger logger = LoggerFactory.getLogger(WeComCallbackController.class);
@Value("${qywx.app.corpId:}")
private String corpId;
@Value("${qywx.app.token:}")
private String token;
@Value("${qywx.app.encodingAESKey:}")
private String encodingAESKey;
@GetMapping(produces = MediaType.TEXT_PLAIN_VALUE)
public String verify(@RequestParam("msg_signature") String msgSignature,
@RequestParam("timestamp") String timestamp,
@RequestParam("nonce") String nonce,
@RequestParam("echostr") String echostr) {
logger.info("WeCom callback verify: ts={}, nonce={}", timestamp, nonce);
WeComCallbackCrypto crypto = new WeComCallbackCrypto(token, encodingAESKey, corpId);
String plain = crypto.verifyURL(msgSignature, timestamp, nonce, echostr);
return plain;
}
@PostMapping(consumes = MediaType.TEXT_XML_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)
public String receiveXml(@RequestParam("msg_signature") String msgSignature,
@RequestParam("timestamp") String timestamp,
@RequestParam("nonce") String nonce,
HttpServletRequest request) throws IOException {
String xml = readBody(request);
logger.info("WeCom callback received xml: {}", xml);
String encrypt = parseEncrypt(xml);
WeComCallbackCrypto crypto = new WeComCallbackCrypto(token, encodingAESKey, corpId);
String plainXml = crypto.decryptMsg(msgSignature, timestamp, nonce, encrypt);
logger.info("WeCom callback plain xml: {}", plainXml);
// TODO: 在此解析 plainXml 的 MsgType/Event/Content 等,进行业务处理
return "success";
}
private static String readBody(HttpServletRequest request) throws IOException {
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = request.getReader()) {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
}
return sb.toString();
}
private static String parseEncrypt(String xml) {
// 简单提取 <Encrypt><![CDATA[...]]></Encrypt>
String start = "<Encrypt><![CDATA[";
String end = "]]></Encrypt>";
int i = xml.indexOf(start);
int j = xml.indexOf(end);
if (i >= 0 && j > i) {
return xml.substring(i + start.length(), j);
}
// 兼容无 CDATA 的情况
start = "<Encrypt>";
end = "</Encrypt>";
i = xml.indexOf(start);
j = xml.indexOf(end);
if (i >= 0 && j > i) {
return xml.substring(i + start.length(), j);
}
throw new RuntimeException("Encrypt not found");
}
}

View File

@@ -21,6 +21,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
http.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN") // 设置admin/**路径需要ADMIN角色
.antMatchers("/wx/**").permitAll()
.antMatchers("/wecom/**").permitAll()
.anyRequest().authenticated() // 其他请求需要认证
.and()
.formLogin() // 启用默认登录页

View File

@@ -14,12 +14,12 @@ public enum WXMessageType {
QH("QH","1000003","ww929e7d6493c6336e","lHW1JT3tLB6WZXO_4ww0SdjhMoXtSPX7LBl_zqvY46g"),
// 美团
MT("MT","1000004","ww929e7d6493c6336e","kdViRoqUFJZcGQ2dAoJZwPTktQ-fovQPeTnloAbn7bg"),
// 爱茅台
IMT("IMT","1000005","ww929e7d6493c6336e","SpYfWAA4lougQECoQ3WNvZnJ31Po77NU-XSnuC8syGs"),
// 拼多多
PDD("PDD","1000005","ww929e7d6493c6336e","SpYfWAA4lougQECoQ3WNvZnJ31Po77NU-XSnuC8syGs"),
// TY
TY("TY","1000006","ww929e7d6493c6336e","sGrNc8uBd_Wp6hmme2oP_Rh37scmgGulPuCHiQ9PYcc"),
JD("JD","1000008","ww929e7d6493c6336e","neXT6KJ8FYVLR-LN455MAcFaeUYkVaTsAkIOqXx3wLA"),
LF("LF","1000009","ww929e7d6493c6336e","msWg3ugSSRFIXeWZuoGKN2MI2Kg9AJVAG6pCWFEeH5E"),;
JENKINS("JENKINS","1000009","ww929e7d6493c6336e","msWg3ugSSRFIXeWZuoGKN2MI2Kg9AJVAG6pCWFEeH5E"),;

View File

@@ -1,8 +1,6 @@
package cn.van333.wxsend.exception;
import cn.van333.wxsend.business.model.R;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

View File

@@ -1,61 +1,61 @@
package cn.van333.wxsend.util;
import com.alibaba.fastjson2.JSON;
import com.jd.open.api.sdk.DefaultJdClient;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.domain.kplunion.OrderService.request.query.OrderRowReq;
import com.jd.open.api.sdk.domain.kplunion.OrderService.response.query.OrderRowResp;
import com.jd.open.api.sdk.request.JdRequest;
import com.jd.open.api.sdk.request.kplunion.UnionOpenOrderRowQueryRequest;
import com.jd.open.api.sdk.response.AbstractResponse;
import com.jd.open.api.sdk.response.kplunion.UnionOpenOrderRowQueryResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author Leo
* @version 1.0
* @create 2024/11/5 17:40
* @description
*/
public class JDUtils {
private static final String SERVER_URL =
"https://api.jd.com/routerjson";
private static final String APP_KEY =
"98e21c89ae5610240ec3f5f575f86a59";
private static final String SECRET_KEY =
"3dcb6b23a1104639ac433fd07adb6dfb";
public static void main(String[] args) throws Exception {
String accessToken = "";
JdClient client = new DefaultJdClient(SERVER_URL, accessToken, APP_KEY, SECRET_KEY);
UnionOpenOrderRowQueryRequest request = new UnionOpenOrderRowQueryRequest();
OrderRowReq orderReq = new OrderRowReq();
orderReq.setPageIndex(1);
orderReq.setPageSize(20);
orderReq.setStartTime("2024-11-01 00:00:00");
orderReq.setEndTime("2024-11-05 23:59:59");
orderReq.setType(1);
request.setOrderReq(orderReq);
request.setVersion("1.0");
request.setSignmethod("md5");
// 时间戳格式为yyyy-MM-dd HH:mm:ss时区为GMT+8。API服务端允许客户端请求最大时间误差为10分钟
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
request.setTimestamp(simpleDateFormat.format(date));
UnionOpenOrderRowQueryResponse response = client.execute(request);
System.out.println("响应码:" + response.getQueryResult().getCode());
System.out.println(JSON.toJSONString(response));
OrderRowResp[] orderRowResps = response.getQueryResult().getData();
//for (OrderRowResp orderRowResp : orderRowResps) {
// System.out.println(orderRowResp.getOrderId());
//}
}
}
//package cn.van333.wxsend.util;
//
//import com.alibaba.fastjson2.JSON;
//import com.jd.open.api.sdk.DefaultJdClient;
//import com.jd.open.api.sdk.JdClient;
//import com.jd.open.api.sdk.domain.kplunion.OrderService.request.query.OrderRowReq;
//import com.jd.open.api.sdk.domain.kplunion.OrderService.response.query.OrderRowResp;
//import com.jd.open.api.sdk.request.JdRequest;
//import com.jd.open.api.sdk.request.kplunion.UnionOpenOrderRowQueryRequest;
//import com.jd.open.api.sdk.response.AbstractResponse;
//import com.jd.open.api.sdk.response.kplunion.UnionOpenOrderRowQueryResponse;
//
//import java.text.SimpleDateFormat;
//import java.util.Date;
//
///**
// * @author Leo
// * @version 1.0
// * @create 2024/11/5 17:40
// * @description
// */
//public class JDUtils {
// private static final String SERVER_URL =
// "https://api.jd.com/routerjson";
// private static final String APP_KEY =
// "98e21c89ae5610240ec3f5f575f86a59";
// private static final String SECRET_KEY =
// "3dcb6b23a1104639ac433fd07adb6dfb";
//
//
// public static void main(String[] args) throws Exception {
// String accessToken = "";
// JdClient client = new DefaultJdClient(SERVER_URL, accessToken, APP_KEY, SECRET_KEY);
// UnionOpenOrderRowQueryRequest request = new UnionOpenOrderRowQueryRequest();
// OrderRowReq orderReq = new OrderRowReq();
// orderReq.setPageIndex(1);
// orderReq.setPageSize(20);
// orderReq.setStartTime("2024-11-01 00:00:00");
// orderReq.setEndTime("2024-11-05 23:59:59");
// orderReq.setType(1);
//
//
// request.setOrderReq(orderReq);
// request.setVersion("1.0");
// request.setSignmethod("md5");
// // 时间戳格式为yyyy-MM-dd HH:mm:ss时区为GMT+8。API服务端允许客户端请求最大时间误差为10分钟
// Date date = new Date();
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// request.setTimestamp(simpleDateFormat.format(date));
//
//
// UnionOpenOrderRowQueryResponse response = client.execute(request);
// System.out.println("响应码:" + response.getQueryResult().getCode());
// System.out.println(JSON.toJSONString(response));
// OrderRowResp[] orderRowResps = response.getQueryResult().getData();
// //for (OrderRowResp orderRowResp : orderRowResps) {
// // System.out.println(orderRowResp.getOrderId());
// //}
// }
//
//}

View File

@@ -1,16 +1,9 @@
package cn.van333.wxsend.util;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.engine.velocity.VelocityEngine;
import org.apache.ibatis.jdbc.SQL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.util.HtmlUtils;
import javax.activation.DataSource;
@@ -55,7 +48,7 @@ public class QCUtil {
* @param msg
*/
public static final void consume(long start, long end, String msg) {
System.err.println(String.format("%s consume times: %s=%s-%s", msg, (end - start), end, start));
System.err.printf("%s consume times: %s=%s-%s%n", msg, (end - start), end, start);
}
/**

View File

@@ -0,0 +1,183 @@
package cn.van333.wxsend.util;
import cn.van333.wxsend.business.service.LogService;
import cn.van333.wxsend.util.response.SendRespones;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson2.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static cn.hutool.http.HttpRequest.post;
/**
* 企业微信群机器人 Webhook 工具
* 参考文档https://developer.work.weixin.qq.com/document/path/99110
*/
public class QywxWebhookUtil {
private static final Logger logger = LoggerFactory.getLogger(LogService.class);
private static final String QYWX_ORIGIN = "https://qyapi.weixin.qq.com";
private static final String WEBHOOK_SEND = QYWX_ORIGIN + "/cgi-bin/webhook/send";
public static String buildUrl(String key) {
return WEBHOOK_SEND + "?key=" + key;
}
public static String buildSignedUrl(String key, String secret) {
if (secret == null || secret.isEmpty()) {
return buildUrl(key);
}
long timestamp = System.currentTimeMillis() / 1000;
String stringToSign = timestamp + "\n" + secret;
String sign = hmacSha256Base64(secret, stringToSign);
String encodedSign = URLEncoder.encode(sign, StandardCharsets.UTF_8);
return buildUrl(key) + "&timestamp=" + timestamp + "&sign=" + encodedSign;
}
public static SendRespones sendText(String key, String secret, String content,
List<String> mentionedList, List<String> mentionedMobileList) {
Map<String, Object> body = new HashMap<>();
body.put("msgtype", "text");
Map<String, Object> text = new HashMap<>();
text.put("content", content);
if (mentionedList != null && !mentionedList.isEmpty()) {
text.put("mentioned_list", mentionedList);
}
if (mentionedMobileList != null && !mentionedMobileList.isEmpty()) {
text.put("mentioned_mobile_list", mentionedMobileList);
}
body.put("text", text);
return doSend(buildSignedUrl(key, secret), body);
}
public static SendRespones sendMarkdown(String key, String secret, String content) {
Map<String, Object> body = new HashMap<>();
body.put("msgtype", "markdown");
Map<String, Object> markdown = new HashMap<>();
markdown.put("content", content);
body.put("markdown", markdown);
return doSend(buildSignedUrl(key, secret), body);
}
public static SendRespones sendImageByBase64(String key, String secret, String base64, String md5) {
Map<String, Object> body = new HashMap<>();
body.put("msgtype", "image");
Map<String, Object> image = new HashMap<>();
image.put("base64", base64);
image.put("md5", md5);
body.put("image", image);
return doSend(buildSignedUrl(key, secret), body);
}
public static SendRespones sendImageByUrl(String key, String secret, String imageUrl) {
try {
HttpResponse resp = HttpRequest.get(imageUrl).execute();
byte[] bytes = resp.bodyBytes();
String base64 = java.util.Base64.getEncoder().encodeToString(bytes);
String md5 = DigestUtil.md5Hex(bytes);
return sendImageByBase64(key, secret, base64, md5);
} catch (Exception e) {
return new SendRespones(500, e.getMessage());
}
}
public static SendRespones sendNews(String key, String secret, java.util.List<Map<String, String>> articles) {
Map<String, Object> body = new HashMap<>();
body.put("msgtype", "news");
Map<String, Object> news = new HashMap<>();
news.put("articles", articles);
body.put("news", news);
return doSend(buildSignedUrl(key, secret), body);
}
public static SendRespones sendFile(String key, String secret, byte[] fileBytes, String fileName) {
try {
String uploadUrl = QYWX_ORIGIN + "/cgi-bin/webhook/upload_media?key=" + key + "&type=file";
String boundary = "----WebKitFormBoundary" + System.currentTimeMillis();
String contentType = "multipart/form-data; boundary=" + boundary;
StringBuilder sb = new StringBuilder();
sb.append("--").append(boundary).append("\r\n");
sb.append("Content-Disposition: form-data; name=\"media\"; filename=\"")
.append(fileName).append("\"\r\n");
sb.append("Content-Type: application/octet-stream\r\n\r\n");
byte[] prefix = sb.toString().getBytes(StandardCharsets.UTF_8);
byte[] suffix = ("\r\n--" + boundary + "--\r\n").getBytes(StandardCharsets.UTF_8);
byte[] body = new byte[prefix.length + fileBytes.length + suffix.length];
System.arraycopy(prefix, 0, body, 0, prefix.length);
System.arraycopy(fileBytes, 0, body, prefix.length, fileBytes.length);
System.arraycopy(suffix, 0, body, prefix.length + fileBytes.length, suffix.length);
String responseStr = HttpRequest.post(uploadUrl)
.header("Content-Type", contentType)
.body(body)
.execute().body();
logger.info("文件上传响应: {}", responseStr);
com.alibaba.fastjson2.JSONObject obj = JSON.parseObject(responseStr);
String mediaId = obj.getString("media_id");
if (mediaId == null) {
return new SendRespones(500, obj.getString("errmsg"));
}
Map<String, Object> sendBody = new HashMap<>();
sendBody.put("msgtype", "file");
Map<String, Object> file = new HashMap<>();
file.put("media_id", mediaId);
sendBody.put("file", file);
return doSend(buildSignedUrl(key, secret), sendBody);
} catch (Exception e) {
return new SendRespones(500, e.getMessage());
}
}
public static SendRespones sendTemplateCard(String key, String secret, Map<String, Object> card) {
Map<String, Object> body = new HashMap<>();
body.put("msgtype", "template_card");
body.put("template_card", card);
return doSend(buildSignedUrl(key, secret), body);
}
private static SendRespones doSend(String url, Map<String, Object> body) {
String json = JSON.toJSONString(body);
logger.info("企业微信 Webhook 请求: url={}, body={}", url, json);
String responseStr = post(url).body(json).execute().body();
logger.info("企业微信 Webhook 响应: {}", responseStr);
try {
SendRespones resp = JSON.parseObject(responseStr, SendRespones.class);
if (resp == null) {
return new SendRespones(500, "empty response");
}
return resp;
} catch (Exception e) {
return new SendRespones(500, e.getMessage());
}
}
private static String hmacSha256Base64(String secret, String data) {
try {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
byte[] signData = mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
return java.util.Base64.getEncoder().encodeToString(signData);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -13,6 +13,8 @@ public class SourceForQLUtil {
private static final String QH_UBUNTU = "QH_UBUNTU";
public static final String XZJ_UBUNTU = "XZJ_UBUNTU";
public static final String PVE_UBUNTU = "PVE_UBUNTU";
public static String transferSource(String source){
@@ -23,6 +25,8 @@ public class SourceForQLUtil {
return "群晖-Ubuntu";
case XZJ_UBUNTU:
return "小主机-Ubuntu";
case PVE_UBUNTU:
return "PVE-Ubuntu";
default:
return "未知";
}

View File

@@ -0,0 +1,164 @@
package cn.van333.wxsend.util;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Base64;
import java.util.Comparator;
/**
* 企业微信回调加解密工具(等价于官方 WXBizMsgCrypt 的精简实现)。
* 参考https://developer.work.weixin.qq.com/document/path/90238
*/
public class WeComCallbackCrypto {
private static final String AES_ALGORITHM = "AES/CBC/NoPadding";
private final String token;
private final byte[] aesKey; // 32字节
private final String corpId;
private final byte[] iv;
public WeComCallbackCrypto(String token, String encodingAESKey, String corpId) {
this.token = token;
this.aesKey = Base64.getDecoder().decode(encodingAESKey + "=");
this.corpId = corpId;
this.iv = Arrays.copyOfRange(aesKey, 0, 16);
}
public String verifyURL(String msgSignature, String timestamp, String nonce, String echoStr) {
String sign = sha1(token, timestamp, nonce, echoStr);
if (!sign.equals(msgSignature)) {
throw new RuntimeException("signature not match");
}
String plain = decrypt(echoStr);
return plain;
}
public String decryptMsg(String msgSignature, String timestamp, String nonce, String encrypt) {
String sign = sha1(token, timestamp, nonce, encrypt);
if (!sign.equals(msgSignature)) {
throw new RuntimeException("signature not match");
}
return decrypt(encrypt);
}
public String encryptMsg(String reply, String timestamp, String nonce) {
String encrypt = encrypt(reply);
String sign = sha1(token, timestamp, nonce, encrypt);
// 返回 XML与官方结构一致
return "<xml>" +
"<Encrypt><![CDATA[" + encrypt + "]]></Encrypt>" +
"<MsgSignature><![CDATA[" + sign + "]]></MsgSignature>" +
"<TimeStamp>" + timestamp + "</TimeStamp>" +
"<Nonce><![CDATA[" + nonce + "]]></Nonce>" +
"</xml>";
}
private String decrypt(String base64CipherText) {
try {
byte[] cipherData = Base64.getDecoder().decode(base64CipherText);
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
SecretKeySpec keySpec = new SecretKeySpec(aesKey, "AES");
cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv));
byte[] plainPadded = cipher.doFinal(cipherData);
byte[] plain = PKCS7Encoder.decode(plainPadded);
// 结构16字节随机 + 4字节网络序正文长度 + 正文 + corpId
byte[] networkOrder = Arrays.copyOfRange(plain, 16, 20);
int xmlLength = ByteBuffer.wrap(networkOrder).order(ByteOrder.BIG_ENDIAN).getInt();
byte[] xmlBytes = Arrays.copyOfRange(plain, 20, 20 + xmlLength);
String fromCorpId = new String(Arrays.copyOfRange(plain, 20 + xmlLength, plain.length), StandardCharsets.UTF_8);
if (!fromCorpId.equals(corpId)) {
throw new RuntimeException("corpId mismatch");
}
return new String(xmlBytes, StandardCharsets.UTF_8);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private String encrypt(String plainText) {
try {
byte[] random16 = randomBytes(16);
byte[] xmlBytes = plainText.getBytes(StandardCharsets.UTF_8);
ByteBuffer lenBuf = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(xmlBytes.length);
byte[] corpBytes = corpId.getBytes(StandardCharsets.UTF_8);
byte[] raw = concat(random16, lenBuf.array(), xmlBytes, corpBytes);
byte[] padded = PKCS7Encoder.encode(raw);
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
SecretKeySpec keySpec = new SecretKeySpec(aesKey, "AES");
cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv));
byte[] cipherData = cipher.doFinal(padded);
return Base64.getEncoder().encodeToString(cipherData);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static String sha1(String token, String timestamp, String nonce, String encrypt) {
try {
String[] arr = new String[]{token, timestamp, nonce, encrypt};
Arrays.sort(arr, Comparator.naturalOrder());
String joined = String.join("", arr);
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] digest = md.digest(joined.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
String hex = Integer.toHexString(b & 0xff);
if (hex.length() < 2) sb.append('0');
sb.append(hex);
}
return sb.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static byte[] randomBytes(int n) {
byte[] b = new byte[n];
// 简易随机即可满足要求
new java.security.SecureRandom().nextBytes(b);
return b;
}
private static byte[] concat(byte[]... parts) {
int len = 0;
for (byte[] p : parts) len += p.length;
byte[] all = new byte[len];
int pos = 0;
for (byte[] p : parts) {
System.arraycopy(p, 0, all, pos, p.length);
pos += p.length;
}
return all;
}
private static class PKCS7Encoder {
private static final int BLOCK_SIZE = 32;
static byte[] encode(byte[] src) {
int amountToPad = BLOCK_SIZE - (src.length % BLOCK_SIZE);
if (amountToPad == 0) amountToPad = BLOCK_SIZE;
byte padChr = (byte) (amountToPad & 0xFF);
byte[] padding = new byte[amountToPad];
Arrays.fill(padding, padChr);
return concat(src, padding);
}
static byte[] decode(byte[] decrypted) {
int pad = decrypted[decrypted.length - 1] & 0xFF;
if (pad < 1 || pad > BLOCK_SIZE) pad = 0;
return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad);
}
}
}

View File

@@ -40,7 +40,7 @@ public class WxSendUtil {
public static String getToken(String corpid, String corpsecret) {
String token = redisCache.getCacheObject(WX_ACCESS_TOKEN+corpsecret);
String token = redisCache.getCacheObject(WX_ACCESS_TOKEN + corpsecret);
if (StrUtil.isNotEmpty(token)) {
return token;
} else {
@@ -56,7 +56,7 @@ public class WxSendUtil {
if (ObjectUtil.isNotEmpty(responseStr)) {
GetTokenResponse response = JSON.parseObject(responseStr, GetTokenResponse.class);
if (response.getErrcode() == 0) {
redisCache.setCacheObject(WX_ACCESS_TOKEN+corpsecret, response.getAccess_token(), 7200, TimeUnit.SECONDS);
redisCache.setCacheObject(WX_ACCESS_TOKEN + corpsecret, response.getAccess_token(), 7200, TimeUnit.SECONDS);
return response.getAccess_token();
}
}
@@ -66,7 +66,7 @@ public class WxSendUtil {
}
}
public static String sendNotify(String title, String text, String touser, WXMessageType wxMessageType) throws Exception {
public static String sendNotifyForMpnews(String title, String text, String touser, WXMessageType wxMessageType) throws Exception {
if (!StrUtil.isAllNotEmpty(title, text)) {
return "title,text不能为空";
}
@@ -75,10 +75,10 @@ public class WxSendUtil {
String[] split = touser.split(",");
touserList.addAll(Arrays.asList(split));
}
return sendNotify(title, text, touserList, wxMessageType);
return sendNotifyForMpnews(title, text, touserList, wxMessageType);
}
public static String sendNotify(String title, String text, List<String> touser, WXMessageType wxMessageType) throws Exception {
public static String sendNotifyForMpnews(String title, String text, List<String> touser, WXMessageType wxMessageType) throws Exception {
if (!StrUtil.isAllNotEmpty(title, text)) {
return "title,text不能为空";
@@ -86,14 +86,16 @@ public class WxSendUtil {
HashMap<String, Object> jsonMap = new HashMap<>();
StringBuilder touserStringBuilder = new StringBuilder();
String touserStr = "@all";
String touserStr = "LinPingFan";
if (ObjectUtil.isNotEmpty(touser)) {
if (touser.size() > 1) {
touser.forEach(t ->
touserStringBuilder.append(t).append("|")
);
touserStr = touserStringBuilder.substring(0, touser.size() - 1);
// 修复bug: 应该使用 touserStringBuilder.length() - 1而不是 touser.size() - 1
// 因为要删除最后一个 "|" 字符,应该基于字符串长度而不是列表大小
touserStr = touserStringBuilder.substring(0, touserStringBuilder.length() - 1);
} else {
touserStr = touserStringBuilder.append(touser.get(0)).toString();
}
@@ -111,17 +113,87 @@ public class WxSendUtil {
// 李星云
articles.put("thumb_media_id", "2ES5cuBiuNKcbFp7RKsjebNM3joCIloIr1QWYwGS86SQzgG_7uxGrJpFlmuHXZl75");
String content = text.replaceAll("\\\\n", "<br/>");
if (content.contains("\n")){
if (content.contains("\n")) {
content = text.replaceAll("\\n", "<br/>");
}
articles.put("content",content);
articles.put("content", content);
//articles.put("content",text.replaceAll("\n", System.getProperty("line.separator")));
articlesList.add(articles);
HashMap<Object, Object> mpnews = new HashMap<>();
mpnews.put("articles", articlesList);
jsonMap.put("mpnews", mpnews);
// 表示是否开启id转译0表示否1表示是默认0
jsonMap.put("enable id trans",1);
jsonMap.put("enable id trans", 1);
String finalSendStr = JSON.toJSONString(jsonMap);
//finalSendStr = finalSendStr.replaceAll("\\\\n", "<p></p>");
logger.info("发送的消息内容: \n" + finalSendStr);
String token = getToken(wxMessageType.getCorpid(), wxMessageType.getCorpsecret());
//logger.info("获取的token"+token);
if (StrUtil.isEmptyIfStr(token)) {
throw new Exception();
}
String responseStr = HttpRequest.post(SEND + getToken(wxMessageType.getCorpid(), wxMessageType.getCorpsecret()))
.body(finalSendStr)//头信息,多个头信息多次调用此方法即可
.execute().body();
logger.info("发送消息的响应: \n" + responseStr);
SendRespones sendRespones = JSON.parseObject(responseStr, SendRespones.class);
if (sendRespones.getErrcode().equals(0)) {
return "企业微信应用消息发送通知消息成功\uD83C\uDF89。";
} else {
return sendRespones.getErrmsg();
}
}
public static String sendNotifyForText(String content, String touser, WXMessageType wxMessageType) throws Exception {
ArrayList<String> touserList = new ArrayList<>();
if (StringUtils.isNotEmpty(touser)) {
String[] split = touser.split(",");
touserList.addAll(Arrays.asList(split));
}
return sendNotifyForText(content, touserList, wxMessageType);
}
public static String sendNotifyForText(String content, List<String> touser, WXMessageType wxMessageType) throws Exception {
HashMap<String, Object> jsonMap = new HashMap<>();
StringBuilder touserStringBuilder = new StringBuilder();
String touserStr = "LinPingFan";
if (ObjectUtil.isNotEmpty(touser)) {
if (touser.size() > 1) {
touser.forEach(t ->
touserStringBuilder.append(t).append("|")
);
// 修复bug: 应该使用 touserStringBuilder.length() - 1而不是 touser.size() - 1
// 因为要删除最后一个 "|" 字符,应该基于字符串长度而不是列表大小
touserStr = touserStringBuilder.substring(0, touserStringBuilder.length() - 1);
} else {
touserStr = touserStringBuilder.append(touser.get(0)).toString();
}
}
jsonMap.put("touser", touserStr);
jsonMap.put("agentid", wxMessageType.getAgentid());
jsonMap.put("safe", "0");
jsonMap.put("msgtype", "text");
HashMap<String, String> text = new HashMap<>();
// 刘亦菲
//articles.put("thumb_media_id", "258F4sbTUwwHLRtKDDr4yqH2PzfYPlHPbOLCazHou_3JCgq7Dh1f9PMvrIaIv2oHk");
// 李星云
//articles.put("thumb_media_id", "2ES5cuBiuNKcbFp7RKsjebNM3joCIloIr1QWYwGS86SQzgG_7uxGrJpFlmuHXZl75");
text.put("content", content);
jsonMap.put("text", text);
// 表示是否开启id转译0表示否1表示是默认0
jsonMap.put("enable id trans", 1);
String finalSendStr = JSON.toJSONString(jsonMap);
//finalSendStr = finalSendStr.replaceAll("\\\\n", "<p></p>");

View File

@@ -0,0 +1,42 @@
package cn.van333.wxsend.util.request;
public class NewsArticle {
private String title;
private String description;
private String url;
private String picurl;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getPicurl() {
return picurl;
}
public void setPicurl(String picurl) {
this.picurl = picurl;
}
}

View File

@@ -0,0 +1,33 @@
package cn.van333.wxsend.util.request;
public class TemplateCardHorizontalContent {
private String keyname;
private String value;
private String url;
public String getKeyname() {
return keyname;
}
public void setKeyname(String keyname) {
this.keyname = keyname;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}

View File

@@ -0,0 +1,33 @@
package cn.van333.wxsend.util.request;
public class TemplateCardJump {
private String type; // 0: URL 跳转
private String url;
private String title;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}

View File

@@ -0,0 +1,251 @@
package cn.van333.wxsend.util.request;
import java.util.List;
public class WebhookMessageRequest {
private String vanToken;
private String key;
private String secret; // 可选加签 secret
private String msgtype; // text 或 markdown
private String content; // 文本/markdown 内容
private List<String> mentionedList;
private List<String> mentionedMobileList;
// image 专用字段(任选其一):
private String imageBase64; // 图片base64不含 data: 前缀)
private String imageMd5; // 图片md532位小写十六进制
private String imageUrl; // 图片直链若提供则后端下载计算base64+md5
// file 专用字段(任选其一):
private String fileUrl; // 文件直链
private String fileBase64; // 文件base64
private String fileName; // 文件名当使用base64时需要
// news 专用字段:
private List<NewsArticle> newsArticles;
// template_card 专用字段(简化版,涵盖常用字段)
private String cardType; // text_notice 或 news_notice
private String cardSourceIconUrl;
private String cardSourceDesc;
private String cardMainTitle;
private String cardMainTitleDesc;
private String cardEmphasisContentTitle;
private String cardEmphasisContentDesc;
private String cardQuoteAreaType;
private String cardQuoteAreaText;
private String cardJumpUrl; // 整卡跳转
private List<TemplateCardHorizontalContent> cardHorizontalContents;
private List<TemplateCardJump> cardJumps;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getSecret() {
return secret;
}
public void setSecret(String secret) {
this.secret = secret;
}
public String getMsgtype() {
return msgtype;
}
public void setMsgtype(String msgtype) {
this.msgtype = msgtype;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public List<String> getMentionedList() {
return mentionedList;
}
public void setMentionedList(List<String> mentionedList) {
this.mentionedList = mentionedList;
}
public List<String> getMentionedMobileList() {
return mentionedMobileList;
}
public void setMentionedMobileList(List<String> mentionedMobileList) {
this.mentionedMobileList = mentionedMobileList;
}
public String getVanToken() {
return vanToken;
}
public void setVanToken(String vanToken) {
this.vanToken = vanToken;
}
public String getImageBase64() {
return imageBase64;
}
public void setImageBase64(String imageBase64) {
this.imageBase64 = imageBase64;
}
public String getImageMd5() {
return imageMd5;
}
public void setImageMd5(String imageMd5) {
this.imageMd5 = imageMd5;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getFileUrl() {
return fileUrl;
}
public void setFileUrl(String fileUrl) {
this.fileUrl = fileUrl;
}
public String getFileBase64() {
return fileBase64;
}
public void setFileBase64(String fileBase64) {
this.fileBase64 = fileBase64;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public List<NewsArticle> getNewsArticles() {
return newsArticles;
}
public void setNewsArticles(List<NewsArticle> newsArticles) {
this.newsArticles = newsArticles;
}
public String getCardType() {
return cardType;
}
public void setCardType(String cardType) {
this.cardType = cardType;
}
public String getCardSourceIconUrl() {
return cardSourceIconUrl;
}
public void setCardSourceIconUrl(String cardSourceIconUrl) {
this.cardSourceIconUrl = cardSourceIconUrl;
}
public String getCardSourceDesc() {
return cardSourceDesc;
}
public void setCardSourceDesc(String cardSourceDesc) {
this.cardSourceDesc = cardSourceDesc;
}
public String getCardMainTitle() {
return cardMainTitle;
}
public void setCardMainTitle(String cardMainTitle) {
this.cardMainTitle = cardMainTitle;
}
public String getCardMainTitleDesc() {
return cardMainTitleDesc;
}
public void setCardMainTitleDesc(String cardMainTitleDesc) {
this.cardMainTitleDesc = cardMainTitleDesc;
}
public String getCardEmphasisContentTitle() {
return cardEmphasisContentTitle;
}
public void setCardEmphasisContentTitle(String cardEmphasisContentTitle) {
this.cardEmphasisContentTitle = cardEmphasisContentTitle;
}
public String getCardEmphasisContentDesc() {
return cardEmphasisContentDesc;
}
public void setCardEmphasisContentDesc(String cardEmphasisContentDesc) {
this.cardEmphasisContentDesc = cardEmphasisContentDesc;
}
public String getCardQuoteAreaType() {
return cardQuoteAreaType;
}
public void setCardQuoteAreaType(String cardQuoteAreaType) {
this.cardQuoteAreaType = cardQuoteAreaType;
}
public String getCardQuoteAreaText() {
return cardQuoteAreaText;
}
public void setCardQuoteAreaText(String cardQuoteAreaText) {
this.cardQuoteAreaText = cardQuoteAreaText;
}
public String getCardJumpUrl() {
return cardJumpUrl;
}
public void setCardJumpUrl(String cardJumpUrl) {
this.cardJumpUrl = cardJumpUrl;
}
public List<TemplateCardHorizontalContent> getCardHorizontalContents() {
return cardHorizontalContents;
}
public void setCardHorizontalContents(List<TemplateCardHorizontalContent> cardHorizontalContents) {
this.cardHorizontalContents = cardHorizontalContents;
}
public List<TemplateCardJump> getCardJumps() {
return cardJumps;
}
public void setCardJumps(List<TemplateCardJump> cardJumps) {
this.cardJumps = cardJumps;
}
}

View File

@@ -1,7 +1,5 @@
package cn.van333.wxsend.util.str;
import cn.van333.wxsend.util.str.StringUtils;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

View File

@@ -0,0 +1,65 @@
server:
port: 36699
spring:
application:
name: wxSend
#数据源配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://134.175.126.60:33306/wxts?characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
username: root
password: mysql_7sjTXH
#redis配置
redis:
host: 134.175.126.60
port: 36379
database: 7
timeout: 1800000
lettuce:
pool:
max-active: 20
#最大阻塞等待时间(负数表示没限制)
max-wait: -1
max-idle: 5
min-idle: 0
password: redis_6PZ52S # 文件上传
servlet:
multipart:
# 单个文件大小
max-file-size: 20MB
# 设置总上传的文件大小
max-request-size: 20MB
#MyWebMvcConfig中开启@EnableWebMvc则失效
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
# # 对象字段为null不显示
# default-property-inclusion: non_null
# 资源信息
messages:
# 国际化资源文件路径
basename: i18n/messages
# token配置
token:
# 令牌自定义标识
header: Authorization
# 令牌密钥
secret: 5c6649a39f184678af3580795a7307d9
# 令牌有效期(单位分钟)
expireTime: 1440
# 用户配置
user:
password:
# 密码最大错误次数
maxRetryCount: 5
# 密码锁定时间默认10分钟
lockTime: 10
# 日志配置
logging:
level:
cn.van333: debug
org.springframework: warn

View File

@@ -0,0 +1,65 @@
server:
port: 36699
spring:
application:
name: wxSend
#数据源配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://134.175.126.60:33306/wxts?characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
username: root
password: mysql_7sjTXH
#redis配置
redis:
host: 134.175.126.60
port: 36379
database: 7
timeout: 1800000
lettuce:
pool:
max-active: 20
#最大阻塞等待时间(负数表示没限制)
max-wait: -1
max-idle: 5
min-idle: 0
password: redis_6PZ52S # 文件上传
servlet:
multipart:
# 单个文件大小
max-file-size: 20MB
# 设置总上传的文件大小
max-request-size: 20MB
#MyWebMvcConfig中开启@EnableWebMvc则失效
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
# # 对象字段为null不显示
# default-property-inclusion: non_null
# 资源信息
messages:
# 国际化资源文件路径
basename: i18n/messages
# token配置
token:
# 令牌自定义标识
header: Authorization
# 令牌密钥
secret: 5c6649a39f184678af3580795a7307d9
# 令牌有效期(单位分钟)
expireTime: 1440
# 用户配置
user:
password:
# 密码最大错误次数
maxRetryCount: 5
# 密码锁定时间默认10分钟
lockTime: 10
# 日志配置
logging:
level:
cn.van333: debug
org.springframework: warn

View File

@@ -1,7 +1,5 @@
server:
port: 36699
spring:
application:
name: wxSend
@@ -10,12 +8,12 @@ spring:
#数据源配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://134.175.126.60:33306/flarum_tsayij?characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
url: jdbc:mysql://134.175.126.60:33306/wxts?characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
username: root
password: LK.807878712
#redis配置
redis:
host: 134.175.126.60
host:
port: 36379
database: 7
timeout: 1800000
@@ -26,7 +24,7 @@ spring:
max-wait: -1
max-idle: 5
min-idle: 0
password: LK.807878712 # 文件上传
password: redis_6PZ52S # 文件上传
servlet:
multipart:
# 单个文件大小
@@ -67,3 +65,15 @@ logging:
level:
cn.van333: debug
org.springframework: warn
qywx:
webhook:
# 默认 webhook key可在请求体中显式传入key覆盖https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=ea1737ff-f906-426d-b39c-2cdace31c3af
key: "ea1737ff-f906-426d-b39c-2cdace31c3af"
# 机器人安全设置中的加签secret可选。若不开启加签可留空
secret: ""
app:
corpId: "ww4f2e72baba7d07ea"
agentId: "1000002"
token: "7UV4cedJT5gx2kCQXmz7PH5"
encodingAESKey: "nHxBmJYFn9dAjwltyIdLi9YvxmHDGNRsX1MgBIPsog9"

View File

@@ -1,24 +0,0 @@
package cn.van333.wxsend;
import cn.van333.wxsend.business.service.PCService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* @author Leo
* @version 1.0
* @create 2024/4/29 下午5:06
* @description
*/
@SpringBootTest
public class Test001 {
@Autowired
PCService pcService;
@Test
public void test001() throws InterruptedException {
System.out.println("test001");
pcService.getData();
}
}

View File

@@ -1,13 +0,0 @@
package cn.van333.wxsend;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class WxSendApplicationTests {
@Test
void contextLoads() {
}
}

0
~/logs/sys-error.log Normal file
View File

12
~/logs/sys-info.log Normal file
View File

@@ -0,0 +1,12 @@
14:56:51.623 [main] INFO c.v.w.WxSendApplication - [logStarting,55] - Starting WxSendApplication using Java 1.8.0_381 on Van with PID 307388 (D:\Code\wxSend\target\classes started by 80787 in D:\Code\wxSend)
14:56:51.625 [main] INFO c.v.w.WxSendApplication - [logStartupProfileInfo,645] - No active profile set, falling back to 1 default profile: "default"
14:56:52.168 [main] INFO o.s.b.w.e.t.TomcatWebServer - [initialize,108] - Tomcat initialized with port(s): 8080 (http)
14:56:52.173 [main] INFO o.a.c.h.Http11NioProtocol - [log,173] - Initializing ProtocolHandler ["http-nio-8080"]
14:56:52.174 [main] INFO o.a.c.c.StandardService - [log,173] - Starting service [Tomcat]
14:56:52.174 [main] INFO o.a.c.c.StandardEngine - [log,173] - Starting Servlet engine: [Apache Tomcat/9.0.68]
14:56:52.272 [main] INFO o.a.c.c.C.[.[.[/] - [log,173] - Initializing Spring embedded WebApplicationContext
14:56:52.272 [main] INFO o.s.b.w.s.c.ServletWebServerApplicationContext - [prepareWebApplicationContext,290] - Root WebApplicationContext: initialization completed in 618 ms
14:56:52.523 [main] INFO o.s.s.w.DefaultSecurityFilterChain - [<init>,55] - Will secure any request with [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@74abbb, org.springframework.security.web.context.SecurityContextPersistenceFilter@b10a26d, org.springframework.security.web.header.HeaderWriterFilter@252f626c, org.springframework.security.web.csrf.CsrfFilter@6972c30a, org.springframework.security.web.authentication.logout.LogoutFilter@15ac59c2, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@b965857, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@6d9fb2d1, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@64a4dd8d, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@4cb0a000, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@7e4d2287, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@31464a43, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@2dddc1b9, org.springframework.security.web.session.SessionManagementFilter@48284d0e, org.springframework.security.web.access.ExceptionTranslationFilter@7cf283e1, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@4ba380c7]
14:56:52.555 [main] INFO o.a.c.h.Http11NioProtocol - [log,173] - Starting ProtocolHandler ["http-nio-8080"]
14:56:52.564 [main] INFO o.s.b.w.e.t.TomcatWebServer - [start,220] - Tomcat started on port(s): 8080 (http) with context path ''
14:56:52.572 [main] INFO c.v.w.WxSendApplication - [logStarted,61] - Started WxSendApplication in 1.283 seconds (JVM running for 1.809)

0
~/logs/sys-user.log Normal file
View File