Browse Source

提交修复

master
主管理员 4 months ago
parent
commit
2a57cb0939
4 changed files with 154 additions and 1 deletions
  1. +8
    -0
      jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/service/YaoDuNoticeService.java
  2. +126
    -0
      jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/service/impl/YaoDuNoticeServiceImpl.java
  3. +19
    -0
      jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/yaoduapi/YaoDuNoticeController.java
  4. +1
    -1
      jeecg-boot-module-system/src/main/resources/application-dev.yml

+ 8
- 0
jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/service/YaoDuNoticeService.java View File

@ -0,0 +1,8 @@
package org.jeecg.modules.api.service;
public interface YaoDuNoticeService {
}

+ 126
- 0
jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/service/impl/YaoDuNoticeServiceImpl.java View File

@ -0,0 +1,126 @@
package org.jeecg.modules.api.service.impl;
import com.fasterxml.jackson.databind.ObjectMapper;
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.jeecg.modules.api.service.YaoDuNoticeService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
@Service
public class YaoDuNoticeServiceImpl implements YaoDuNoticeService {
private static final String APP_ID = "wxa4d29e67e8a58d38";
private static final String APP_SECRET = "866e4ba72bd86a4c79403b6b1341461b";
private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APP_ID + "&secret=" + APP_SECRET;
private static final String TEMPLATE_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=";
private static String getAccessToken() throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(ACCESS_TOKEN_URL);
CloseableHttpResponse response = httpClient.execute(httpPost);
String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
Map<String, String> responseMap = new ObjectMapper().readValue(responseString, Map.class);
return responseMap.get("access_token");
}
//
// private static void sendTemplateMessage(String accessToken, String templateId, String openId, Map<String, Object> data) throws IOException {
// CloseableHttpClient httpClient = HttpClients.createDefault();
// HttpPost httpPost = new HttpPost(TEMPLATE_MESSAGE_URL + accessToken);
//
// Map<String, Object> payload = new HashMap<>();
// payload.put("touser", openId);
// payload.put("template_id", templateId);
// payload.put("data", data);
//
// StringEntity entity = new StringEntity(new ObjectMapper().writeValueAsString(payload), "UTF-8");
// httpPost.setEntity(entity);
// httpPost.setHeader("Content-type", "application/json");
//
// CloseableHttpResponse response = httpClient.execute(httpPost);
// String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
// System.out.println("Response: " + responseString);
// }
// private static final String ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"; // 替换为你的access_token
private static final String TEMPLATE_ID = "uXZnHWrjtcX9JHlnMpdlWmzgJp71sKxCRiMn3TrE-EE"; // 替换为你的template_id
private static final String OPENID = "oFzrW4tqZUvN9T0RQzWPdCT1St68"; // 替换为用户的openid
public static void main(String[] args) {
try {
sendSubscribeMessage();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void sendSubscribeMessage() throws IOException {
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getAccessToken();
// 构建请求数据
Map<String, Object> data = new HashMap<>();
Map<String, String> thing01Data = new HashMap<>();
thing01Data.put("value", "今天星期天");
data.put("thing1", thing01Data);
Map<String, String> thing02Data = new HashMap<>();
thing02Data.put("value", "一个可爱的评论者");
data.put("thing2", thing02Data);
Map<String, String> thing03Data = new HashMap<>();
thing03Data.put("value", "收到一条评论");
data.put("thing3", thing03Data);
Map<String, String> date04Data = new HashMap<>();
date04Data.put("value", "2018-01-01");
data.put("time4", date04Data);
Map<String, Object> requestPayload = new HashMap<>();
requestPayload.put("touser", OPENID); // 确保OPENID是有效的
requestPayload.put("template_id", TEMPLATE_ID); // 确保TEMPLATE_ID是有效的且与模板消息配置相匹配
requestPayload.put("page", "/pages/index/index"); // 用户点击消息后跳转的小程序页面路径
requestPayload.put("data", data); // 这里应该是包含所有模板数据的映射
requestPayload.put("miniprogram_state", "trial"); // 如果是测试阶段使用trial正式阶段使用formal
requestPayload.put("lang", "zh_CN");
// 将请求数据转换为JSON字符串
ObjectMapper objectMapper = new ObjectMapper();
String jsonRequest = objectMapper.writeValueAsString(requestPayload);
// 创建HttpClient实例
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new StringEntity(jsonRequest, StandardCharsets.UTF_8));
httpPost.setHeader("Content-Type", "application/json");
// 发送请求并获取响应
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
String jsonResponse = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
System.out.println("Response: " + jsonResponse);
// 解析响应数据这里只是简单打印实际应用中应根据需要处理
Map<String, Object> responseMap = objectMapper.readValue(jsonResponse, Map.class);
Integer errcode = (Integer) responseMap.get("errcode");
String errmsg = (String) responseMap.get("errmsg");
if (errcode == 0) {
System.out.println("Message sent successfully!");
} else {
System.out.println("Error sending message: " + errmsg);
}
}
}
}
}

+ 19
- 0
jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/yaoduapi/YaoDuNoticeController.java View File

@ -0,0 +1,19 @@
package org.jeecg.modules.api.yaoduapi;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(tags="消息通知类接口")
@RestController
@RequestMapping("/notice")
@Slf4j
public class YaoDuNoticeController {
}

+ 1
- 1
jeecg-boot-module-system/src/main/resources/application-dev.yml View File

@ -1,5 +1,5 @@
server:
port: 8000
port: 8001
tomcat:
max-swallow-size: -1
error:


Loading…
Cancel
Save