|
@@ -71,8 +71,17 @@ public class WeChatService {
|
|
|
text.put("content", content);
|
|
text.put("content", content);
|
|
|
message.put("text", text);
|
|
message.put("text", text);
|
|
|
|
|
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
- String response = restTemplate.postForObject(url, message.toJSONString(), String.class);
|
|
|
|
|
|
|
+ // 设置请求头,确保使用UTF-8编码
|
|
|
|
|
+ org.springframework.http.HttpHeaders headers = new org.springframework.http.HttpHeaders();
|
|
|
|
|
+ headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
|
|
|
|
|
+ headers.set("Accept-Charset", "UTF-8");
|
|
|
|
|
+
|
|
|
|
|
+ org.springframework.http.HttpEntity<String> entity =
|
|
|
|
|
+ new org.springframework.http.HttpEntity<>(message.toJSONString(), headers);
|
|
|
|
|
+
|
|
|
|
|
+ String response = restTemplate.postForObject(url, entity, String.class);
|
|
|
JSONObject jsonResponse = JSONObject.parseObject(response);
|
|
JSONObject jsonResponse = JSONObject.parseObject(response);
|
|
|
|
|
|
|
|
int errcode = jsonResponse.getIntValue("errcode");
|
|
int errcode = jsonResponse.getIntValue("errcode");
|
|
@@ -103,4 +112,70 @@ public class WeChatService {
|
|
|
}
|
|
}
|
|
|
return sendCustomMessage(openId, content);
|
|
return sendCustomMessage(openId, content);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送模板消息给指定用户
|
|
|
|
|
+ * 模板格式: 你好,{{name.DATA}},{{content.DATA}}
|
|
|
|
|
+ * @param openId 接收消息的用户OpenID
|
|
|
|
|
+ * @param name 用户名称
|
|
|
|
|
+ * @param content 消息内容
|
|
|
|
|
+ * @return 是否发送成功
|
|
|
|
|
+ */
|
|
|
|
|
+ public boolean sendTemplateMessage(String openId, String name, String content) {
|
|
|
|
|
+ String accessToken = AccessTokenUtil.getAccessToken();
|
|
|
|
|
+ if (accessToken == null || accessToken.isEmpty()) {
|
|
|
|
|
+ log.error("access_token为空,无法发送模板消息");
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 从配置中获取templateId (需要注入WeChatProperties)
|
|
|
|
|
+ String url = String.format("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s", accessToken);
|
|
|
|
|
+
|
|
|
|
|
+ // 构建模板消息JSON
|
|
|
|
|
+ JSONObject message = new JSONObject();
|
|
|
|
|
+ message.put("touser", openId);
|
|
|
|
|
+ message.put("template_id", "cQevVsdm6hLYMGKcNv7KVr8OtYbYVA8nNYx4qQ2nWkE");
|
|
|
|
|
+ message.put("url", "https://www.baidu.com"); // 点击跳转到百度
|
|
|
|
|
+
|
|
|
|
|
+ // 构建模板数据
|
|
|
|
|
+ JSONObject data = new JSONObject();
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject nameData = new JSONObject();
|
|
|
|
|
+ nameData.put("value", name);
|
|
|
|
|
+ nameData.put("color", "#173177");
|
|
|
|
|
+ data.put("name", nameData);
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject contentData = new JSONObject();
|
|
|
|
|
+ contentData.put("value", content);
|
|
|
|
|
+ contentData.put("color", "#173177");
|
|
|
|
|
+ data.put("content", contentData);
|
|
|
|
|
+
|
|
|
|
|
+ message.put("data", data);
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 设置请求头,确保使用UTF-8编码
|
|
|
|
|
+ org.springframework.http.HttpHeaders headers = new org.springframework.http.HttpHeaders();
|
|
|
|
|
+ headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
|
|
|
|
|
+ headers.set("Accept-Charset", "UTF-8");
|
|
|
|
|
+
|
|
|
|
|
+ org.springframework.http.HttpEntity<String> entity =
|
|
|
|
|
+ new org.springframework.http.HttpEntity<>(message.toJSONString(), headers);
|
|
|
|
|
+
|
|
|
|
|
+ String response = restTemplate.postForObject(url, entity, String.class);
|
|
|
|
|
+ JSONObject jsonResponse = JSONObject.parseObject(response);
|
|
|
|
|
+
|
|
|
|
|
+ int errcode = jsonResponse.getIntValue("errcode");
|
|
|
|
|
+ if (errcode == 0) {
|
|
|
|
|
+ log.info("成功发送模板消息给用户 {}: name={}, content={}", openId, name, content);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ String errmsg = jsonResponse.getString("errmsg");
|
|
|
|
|
+ log.error("发送模板消息失败 - errcode: {}, errmsg: {}", errcode, errmsg);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("发送模板消息时发生异常: ", e);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|