| @ -0,0 +1,105 @@ | |||
| package org.jeecg.modules.api.utils; | |||
| import com.alibaba.fastjson.JSON; | |||
| import com.baomidou.mybatisplus.core.toolkit.StringUtils; | |||
| import com.fasterxml.jackson.databind.ObjectMapper; | |||
| import lombok.extern.log4j.Log4j2; | |||
| 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 org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Component; | |||
| import java.io.IOException; | |||
| import java.time.LocalDateTime; | |||
| import java.time.format.DateTimeFormatter; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| @Log4j2 | |||
| @Component | |||
| public class WxTemplateUtil { | |||
| @Autowired | |||
| private WxHttpUtils wxHttpUtils; | |||
| private final String TEMPLATE_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="; | |||
| private final String commentTemplateId = "uXZnHWrjtcX9JHlnMpdlWmzgJp71sKxCRiMn3TrE-EE"; | |||
| /** | |||
| * 发送评论信息通知 | |||
| * @param toUser 接收者openid | |||
| * @param discussant 评论人 | |||
| * @param content 评论内容 | |||
| * @param params 页面参数 | |||
| */ | |||
| public void sendCommentMessage(String toUser, String discussant, String content, String params){ | |||
| Map<String, Object> data = new HashMap<>(); | |||
| data.put("thing1", new HashMap<String, String>() {{ | |||
| put("value", "评论通知"); | |||
| put("color", "#173177"); | |||
| }}); | |||
| data.put("thing2", new HashMap<String, String>() {{ | |||
| put("value", discussant); | |||
| put("color", "#173177"); | |||
| }}); | |||
| data.put("thing3", new HashMap<String, String>() {{ | |||
| put("value", content); | |||
| put("color", "#173177"); | |||
| }}); | |||
| data.put("time4", new HashMap<String, String>() {{ | |||
| put("value", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); | |||
| put("color", "#173177"); | |||
| }}); | |||
| String url = "/pages/index/index"; | |||
| if (StringUtils.isNotBlank(params)){ | |||
| url += "?" + params; | |||
| } | |||
| try { | |||
| sendTemplateMessage(toUser, commentTemplateId, | |||
| url, | |||
| data | |||
| ); | |||
| } catch (IOException e) { | |||
| log.error("发送评论信息通知失败:{}", e.getMessage()); | |||
| } | |||
| } | |||
| private void sendTemplateMessage(String toUser, String templateId, String page, Map<String, Object> data) throws IOException { | |||
| String url = TEMPLATE_MESSAGE_URL + wxHttpUtils.getAccessToken(); | |||
| Map<String, Object> requestBody = new HashMap<>(); | |||
| requestBody.put("touser", toUser); | |||
| requestBody.put("template_id", templateId); | |||
| requestBody.put("data", data); | |||
| requestBody.put("miniprogram", new HashMap<String, String>() {{ | |||
| put("appid", wxHttpUtils.getAppid()); | |||
| put("page", page); | |||
| }}); | |||
| //输出请求体 | |||
| System.out.println("Request Body: " + JSON.toJSONString(requestBody)); | |||
| ObjectMapper objectMapper = new ObjectMapper(); | |||
| String jsonRequest = objectMapper.writeValueAsString(requestBody); | |||
| try (CloseableHttpClient httpClient = HttpClients.createDefault()) { | |||
| HttpPost httpPost = new HttpPost(url); | |||
| httpPost.setEntity(new StringEntity(jsonRequest, "UTF-8")); | |||
| httpPost.setHeader("Content-Type", "application/json"); | |||
| CloseableHttpResponse response = httpClient.execute(httpPost); | |||
| String responseString = EntityUtils.toString(response.getEntity()); | |||
| System.out.println("Response: " + responseString); | |||
| } | |||
| } | |||
| } | |||