|
@@ -1,8 +1,12 @@
|
|
|
package com.fs.company.util;
|
|
package com.fs.company.util;
|
|
|
|
|
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.fs.common.core.redis.RedisCache;
|
|
import com.fs.common.core.redis.RedisCache;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
|
|
+import org.springframework.http.MediaType;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
|
@@ -58,13 +62,13 @@ public class WechatApi {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static void sendSubscribeMessage(String accessToken, String openId) {
|
|
public static void sendSubscribeMessage(String accessToken, String openId) {
|
|
|
- accessToken = "97_Pw_YjpbbakVd9Oy9opTVwiKz7vz-ZtglyFdipnion71g-PgRW9PKEN1IxxS-E3m23dU7GrmlktRN6ht3SUOtcKtU7eMeRv3fSMWmX5PBqMF9X6vhsMcUqjcPrsUCOAhACAXJV";
|
|
|
|
|
|
|
+ accessToken = "97_AWdZem-aCEBY8bt8vlQV0Hz1OmJnXGCRwWZiiUAuVxZmwYtIJbxP13ADoqZ5p8cFBef2JrF8lo3wMUjvGCUTsXuhA0hrlG-RYhPzuoRFHh8o4Iwxj0Ai4TDWvGcWEQhAFAEMY";
|
|
|
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/bizsend?access_token=" + accessToken;
|
|
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/bizsend?access_token=" + accessToken;
|
|
|
|
|
|
|
|
// 消息体
|
|
// 消息体
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
Map<String, Object> data = new HashMap<>();
|
|
|
data.put("touser", openId);
|
|
data.put("touser", openId);
|
|
|
- data.put("template_id", "CHyzPCokptj4Z9qTpo-UJQzd_OAN4cNa6zpZo9LZ_YY");
|
|
|
|
|
|
|
+ data.put("template_id", "CHyzPCokptj4Z9qTpo-UJSDR4NmFTFTnEs4IOl9Jjjw");
|
|
|
|
|
|
|
|
// 模板参数
|
|
// 模板参数
|
|
|
Map<String, Object> payloadData = new HashMap<>();
|
|
Map<String, Object> payloadData = new HashMap<>();
|
|
@@ -83,13 +87,85 @@ public class WechatApi {
|
|
|
System.out.println("微信返回结果:" + result);
|
|
System.out.println("微信返回结果:" + result);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public static void sendTemplateMessage(String accessToken, String openId) {
|
|
|
|
|
+ accessToken = "97_AWdZem-aCEBY8bt8vlQV0Hz1OmJnXGCRwWZiiUAuVxZmwYtIJbxP13ADoqZ5p8cFBef2JrF8lo3wMUjvGCUTsXuhA0hrlG-RYhPzuoRFHh8o4Iwxj0Ai4TDWvGcWEQhAFAEMY";
|
|
|
|
|
+ String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
|
|
|
|
|
+
|
|
|
|
|
+ // 消息体
|
|
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
|
|
+ data.put("touser", openId);
|
|
|
|
|
+ data.put("template_id", "phVk0BLSeFZOw9f3vNlWGGvqvzICkOvjZJiI7wgpNyg");
|
|
|
|
|
+
|
|
|
|
|
+ // 模板参数
|
|
|
|
|
+ Map<String, Object> payloadData = new HashMap<>();
|
|
|
|
|
+ payloadData.put("time3", createValue("2025年10月30日 14:00"));
|
|
|
|
|
+ payloadData.put("thing4", createValue("张三"));
|
|
|
|
|
+ payloadData.put("amount8", createValue("10.00"));
|
|
|
|
|
+ payloadData.put("character_string9", createValue("123456789"));
|
|
|
|
|
+
|
|
|
|
|
+ data.put("data", payloadData);
|
|
|
|
|
+
|
|
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> result = restTemplate.postForObject(url, data, Map.class);
|
|
|
|
|
+
|
|
|
|
|
+ System.out.println("微信返回结果:" + result);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private static Map<String, String> createValue(String value) {
|
|
private static Map<String, String> createValue(String value) {
|
|
|
Map<String, String> map = new HashMap<>();
|
|
Map<String, String> map = new HashMap<>();
|
|
|
map.put("value", value);
|
|
map.put("value", value);
|
|
|
return map;
|
|
return map;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public Map<String, Object> sendTemplateMessage(String openId, String templateId,
|
|
|
|
|
+ Map<String, Object> data, String url,
|
|
|
|
|
+ Map<String, Object> miniProgram) {
|
|
|
|
|
+ String apiUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + getAccessToken();
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> message = new HashMap<>();
|
|
|
|
|
+ message.put("touser", openId);
|
|
|
|
|
+ message.put("template_id", templateId);
|
|
|
|
|
+
|
|
|
|
|
+ if (url != null && !url.isEmpty()) {
|
|
|
|
|
+ message.put("url", url);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (miniProgram != null && !miniProgram.isEmpty()) {
|
|
|
|
|
+ message.put("miniprogram", miniProgram);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ message.put("data", data);
|
|
|
|
|
+
|
|
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
+ String jsonBody = mapper.writeValueAsString(message);
|
|
|
|
|
+ HttpEntity<String> request = new HttpEntity<>(jsonBody, headers);
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> result = restTemplate.postForObject(apiUrl, request, Map.class);
|
|
|
|
|
+ System.out.println("发送模板消息结果: " + result);
|
|
|
|
|
+ return result;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ throw new RuntimeException("发送模板消息失败", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 创建模板数据值(带颜色)
|
|
|
|
|
+ */
|
|
|
|
|
+ public static Map<String, String> createValue(String value, String color) {
|
|
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
|
|
+ map.put("value", value);
|
|
|
|
|
+ map.put("color", color);
|
|
|
|
|
+ return map;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
|
- sendSubscribeMessage("","ooWg-6u1yGLDT9xlmV4lvwjLZV5o");
|
|
|
|
|
|
|
+ sendTemplateMessage("","ooWg-6u1yGLDT9xlmV4lvwjLZV5o");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|