|
|
@@ -10,11 +10,13 @@ import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.client.RestClientException;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.net.URLEncoder;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
import java.util.Collections;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
@@ -93,30 +95,51 @@ public class WechatApi {
|
|
|
}
|
|
|
|
|
|
//发送模板消息
|
|
|
- @Async
|
|
|
+ //@Async
|
|
|
public void sendTemplateMessage(String accessToken, String openId, Date payTime, String userName, BigDecimal payPrice,String orderCode) {
|
|
|
- //accessToken = "97_AWdZem-aCEBY8bt8vlQV0Hz1OmJnXGCRwWZiiUAuVxZmwYtIJbxP13ADoqZ5p8cFBef2JrF8lo3wMUjvGCUTsXuhA0hrlG-RYhPzuoRFHh8o4Iwxj0Ai4TDWvGcWEQhAFAEMY";
|
|
|
- String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
|
|
|
+ // accessToken = "97_suqOMLamU1WOJpore5R1QABi9uqjZQZ8MwzOl_RwVLnIbyROkuSGNVx8M-qgiCnMqh9Ee0oROPAt7pxJROxxYcE2zm35fCD46mjKZPp7ClWwN5S9_AXgzjI21b0FELgAAARQX";
|
|
|
+ log.info("进入 sendTemplateMessage 方法");
|
|
|
+ try {
|
|
|
+ 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");
|
|
|
+ // 格式化时间
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
|
|
|
+ String payTimeStr = sdf.format(payTime);
|
|
|
|
|
|
- // 模板参数
|
|
|
- Map<String, Object> payloadData = new HashMap<>();
|
|
|
- payloadData.put("time3", payTime);
|
|
|
- payloadData.put("thing4", userName);
|
|
|
- payloadData.put("amount8", payPrice);
|
|
|
- payloadData.put("character_string9", orderCode);
|
|
|
+ // 构造模板参数(Java 8 写法)
|
|
|
+ Map<String, Object> time3 = new HashMap<>();
|
|
|
+ time3.put("value", payTimeStr);
|
|
|
|
|
|
- data.put("data", payloadData);
|
|
|
+ Map<String, Object> thing4 = new HashMap<>();
|
|
|
+ thing4.put("value", userName);
|
|
|
|
|
|
- RestTemplate restTemplate = new RestTemplate();
|
|
|
+ Map<String, Object> amount8 = new HashMap<>();
|
|
|
+ amount8.put("value", payPrice.toPlainString() + "元");
|
|
|
|
|
|
- Map<String, Object> result = restTemplate.postForObject(url, data, Map.class);
|
|
|
+ Map<String, Object> character_string9 = new HashMap<>();
|
|
|
+ character_string9.put("value", orderCode);
|
|
|
+
|
|
|
+ // 组合模板字段
|
|
|
+ Map<String, Object> payloadData = new HashMap<>();
|
|
|
+ payloadData.put("time3", time3);
|
|
|
+ payloadData.put("thing4", thing4);
|
|
|
+ payloadData.put("amount8", amount8);
|
|
|
+ payloadData.put("character_string9", character_string9);
|
|
|
+
|
|
|
+ // 组合整体消息体
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("touser", openId);
|
|
|
+ data.put("template_id", "phVk0BLSeFZOw9f3vNlWGGvqvzICkOvjZJiI7wgpNyg");
|
|
|
+ data.put("data", payloadData);
|
|
|
|
|
|
- log.info("微信返回结果:{}" , result);
|
|
|
+ // 发送请求
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ Map<String, Object> result = restTemplate.postForObject(url, data, Map.class);
|
|
|
+
|
|
|
+ log.info("微信返回结果:{}" , result);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("发送模板消息失败", e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private static Map<String, String> createValue(String value) {
|
|
|
@@ -173,6 +196,6 @@ public class WechatApi {
|
|
|
}
|
|
|
|
|
|
// public static void main(String[] args) {
|
|
|
-// sendTemplateMessage("","ooWg-6u1yGLDT9xlmV4lvwjLZV5o");
|
|
|
+// sendTemplateMessage("","ooWg-6u1yGLDT9xlmV4lvwjLZV5o",new Date(),"fcj",new BigDecimal(100),"2025123456");
|
|
|
// }
|
|
|
}
|