前端-胡立永 3 weeks ago
parent
commit
20a244cddb
3 changed files with 133 additions and 3 deletions
  1. +27
    -2
      jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/service/impl/YaoDuApiServiceImpl.java
  2. +105
    -0
      jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/utils/WxTemplateUtil.java
  3. +1
    -1
      jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/yaoduapi/YaoDuApiController.java

+ 27
- 2
jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/service/impl/YaoDuApiServiceImpl.java View File

@ -28,6 +28,7 @@ import org.jeecg.modules.api.bean.HttpClientUtil;
import org.jeecg.modules.api.bean.PageBean;
import org.jeecg.modules.api.bean.WxQrCodeVo;
import org.jeecg.modules.api.service.YaoDuApiService;
import org.jeecg.modules.api.utils.WxTemplateUtil;
import org.jeecg.modules.citiyClass.entity.CitiyClass;
import org.jeecg.modules.citiyClass.service.ICitiyClassService;
import org.jeecg.modules.citiyShopAuthentication.entity.CitiyShopAuthentication;
@ -182,6 +183,9 @@ public class YaoDuApiServiceImpl implements YaoDuApiService {
private ICitySignLogService citySignLogService;
@Resource
private ICityMoneyLogService cityMoneyLogService;
@Resource
private WxTemplateUtil wxTemplateUtil;
@Resource
private ICityGiftService cityGiftService;
@ -1099,6 +1103,28 @@ public class YaoDuApiServiceImpl implements YaoDuApiService {
return Result.error("内容含有敏感词");
}
if (StringUtils.isNotBlank(cityComment.getPid())){
// 子评论通知给用户
CityComment comment = cityCommentService.getById(cityComment.getPid());
HanHaiMember member = hanHaiMemberService.getById(comment.getUserId());
if (member != null){
String content = cityComment.getUserValue();
if (StringUtils.isNotBlank(cityComment.getUserImage())){
for (String s : cityComment.getUserImage().split(",")) {
content += "[图片]";
}
}
wxTemplateUtil.sendCommentMessage(
member.getAppletOpenid(),
hanHaiMember.getNickName(),
content,
""
);
}
}
cityComment.setUserId(hanHaiMember.getId());
cityComment.setUserName(hanHaiMember.getNickName());
@ -1326,7 +1352,7 @@ public class YaoDuApiServiceImpl implements YaoDuApiService {
String accessToken = getAccessToken2();
if (accessToken != null && !accessToken.isEmpty()) {
Map<String, Object> templateData = createTemplateData();
this.sendTemplateMessage2(accessToken, openid, templateId,templateData );
this.sendTemplateMessage2(accessToken, openid, templateId, templateData);
return Result.OK("发送成功", templateData);
} else {
return Result.error("无法获取Access Token");
@ -1337,7 +1363,6 @@ public class YaoDuApiServiceImpl implements YaoDuApiService {
}
}
private Map<String, Object> createTemplateData() {
Map<String, Object> data = new HashMap<>();
data.put("thing1", new HashMap<String, String>() {{


+ 105
- 0
jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/utils/WxTemplateUtil.java View File

@ -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);
}
}
}

+ 1
- 1
jeecg-boot-module-system/src/main/java/org/jeecg/modules/api/yaoduapi/YaoDuApiController.java View File

@ -231,7 +231,7 @@ public class YaoDuApiController {
@GetMapping(value = "/sendTemplateMessage")
public Result<?> sendTemplateMessage() {
String templateId = "uXZnHWrjtcX9JHlnMpdlWmzgJp71sKxCRiMn3TrE-EE";
String touser = "oFzrW4tqZUvN9T0RQzWPdCT1St68";
String touser = "oFzrW4uyDIvH_9_NXOs__z6qKn1E";
return yaoDuApiService.sendTemplateMessage(templateId,touser);
}


Loading…
Cancel
Save