package com.ruoyi.wechat;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.infoLecturer.domain.InfoLecturer;
import com.ruoyi.infoLecturer.service.IInfoLecturerService;
import com.ruoyi.infoMeeting.domain.InfoMeeting;
import com.ruoyi.infoMeeting.service.IInfoMeetingService;
import com.ruoyi.infoOrder.domain.InfoOrder;
import com.ruoyi.infoOrder.mapper.InfoOrderMapper;
import com.ruoyi.infoOrder.service.IInfoOrderService;
import com.ruoyi.wechatMessage.domain.UserWechatMessage;
import com.ruoyi.wechatMessage.mapper.UserWechatMessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.*;
@RestController
@RequestMapping("/release")
@Component("/release/ryTask")
public class SendMessageController {
@Autowired
private IInfoOrderService infoOrderService;
@Autowired
private InfoOrderMapper infoOrderMapper;
@Autowired
private IInfoMeetingService infoMeetingService;
@Autowired
private IInfoLecturerService infoLecturerService;
@Autowired
private UserWechatMessageMapper messageMapper;
@GetMapping("/sendMessage")
public void sendMsg() throws ParseException, JsonProcessingException {
InfoOrder infoOrder = new InfoOrder();
infoOrder.setOrderStatus("1");
infoOrder.setBusinessType("0");
List<InfoOrder> infoOrders = infoOrderMapper.selectInfoOrderOpenId(infoOrder);
for (InfoOrder order : infoOrders) {
//获取会议时间
Long businessId = order.getBusinessId();
InfoMeeting infoMeeting = infoMeetingService.selectInfoMeetingById(businessId);
Date meetingStartTime = infoMeeting.getMeetingStartTime();
//把时间转换为LocalDateTime格式
Instant instant = meetingStartTime.toInstant();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
// 将开始时间设为今天的 startTime
LocalDateTime nextRunTime = LocalDateTime.of(LocalDate.now(), LocalTime.from(localDateTime));//11.00
LocalDateTime currentTime = LocalDateTime.now();
//进行比较
boolean isBeforeHalfHour = currentTime.isBefore(nextRunTime) && currentTime.isAfter(nextRunTime.minusMinutes(30));
if (isBeforeHalfHour) {
// 如果是,则执行定时任务
System.out.println("开始执行定时任务");
//获取openId
String openId = order.getOpenId();
//1:获取token(接口调用凭证)
String token = queryToken();
//2:发送订阅消息
send(token, openId);
System.out.println("结束执行定时任务");
}else {
System.out.println("未到会议开始时间");
continue;
}
}
}
// 1: 获取 access_token (2h过期)
public String queryToken() {
String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
tokenUrl = tokenUrl + "&appid=" + appId + "&secret=" + secret;
String result = HttpUtil.get(tokenUrl);
JSONObject jsonObject = JSONUtil.parseObj(result);
String token = jsonObject.get("access_token").toString();
return token;
}
/**
* 发送订阅消息
* @param token
* @param openId
* @throws ParseException
* @throws JsonProcessingException
*/
public void send(String token, String openId) throws ParseException, JsonProcessingException {
String msgUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send";
msgUrl = msgUrl + "?access_token=" + token;
//获取openid
if (openId != "") {
// 设置模板参数
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("touser", openId); // 接收方的openId
paramMap.put("template_id", templateId); // 模板id
paramMap.put("page", "pages/home/index"); // 消息中要跳转的页面
// 设置data 模板内容
InfoOrder infoOrder = new InfoOrder();
infoOrder.setOpenId(openId);
infoOrder.setBusinessType("0");
List<InfoOrder> infoOrders = infoOrderService.selectInfoOrderList(infoOrder);
HashMap<String, Object> data = new HashMap<>();
for (InfoOrder order : infoOrders) {
Long id = order.getBusinessId();
if ("0".equals(order.getBusinessType())) {
InfoMeeting infoMeeting = infoMeetingService.selectInfoMeetingById(id);
Date meetingStartTime = infoMeeting.getMeetingStartTime();
Date meetingEndTime = infoMeeting.getMeetingEndTime();
//转换时间格式2022年02月02日
SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss", Locale.CHINA);
Date startDate = inputFormat.parse(String.valueOf(meetingStartTime));
Date endDate = inputFormat.parse(String.valueOf(meetingEndTime));
String formattedStartTime = outputFormat.format(startDate);
String formattedEndTime = outputFormat.format(endDate);
String time = formattedStartTime + "~" + formattedEndTime;
String meetingNumber = infoMeeting.getMeetingNumber();
String formattedPhoneNumber = meetingNumber.replace("-", "");
InfoLecturer infoLecturer = infoLecturerService.selectInfoLecturerById(infoMeeting.getTeacherId());
String lecturerName = infoLecturer.getLecturerName();
Map<String, String> thing48Map = new HashMap<>();
thing48Map.put("value", order.getOrderTitle());//标题
data.put("thing48", thing48Map);
Map<String, String> thing49Map = new HashMap<>();
thing49Map.put("value", time);//会议时间2023年05月02日
data.put("time49", thing49Map);
Map<String, String> phrase50 = new HashMap<>();
phrase50.put("value", "腾讯会议");//观看方式5个字
data.put("phrase50", phrase50);
Map<String, Object> number20 = new HashMap<>();
number20.put("value", formattedPhoneNumber);//会议号
data.put("number20", number20);
Map<String, String> name10 = new HashMap<>();
name10.put("value", lecturerName);//预约老师
data.put("name10", name10);
}
//这个data 中的数据 按模板 添加
paramMap.put("data", data);
// 转json字符串
String jsonObject = JSONUtil.toJsonStr(paramMap);
String result = HttpUtil.post(msgUrl, jsonObject);
//解析 是否发送成功
//记录发送成功或失败日志留存
ObjectMapper objectMapper = new ObjectMapper();
UserWechatMessage message = new UserWechatMessage();
JsonNode jsonNode = objectMapper.readTree(result);
String errcode = jsonNode.get("errcode").asText();
String errmsg = jsonNode.get("errmsg").asText();
if (errcode.equals("0")) {
message.setErrcode(errcode);
message.setErrmsg(errmsg);
message.setRid(result);
String msgid = jsonNode.get("msgid").asText();
message.setMsgid(msgid);
message.setOpenid(openId);
} else {
message.setErrcode(errcode);
message.setErrmsg(errmsg);
message.setRid(result);
message.setOpenid(openId);
}
messageMapper.insertUserWechatMessage(message);
}
}else {
return;
}
}
}
小程序消息推送部分代码
于 2023-08-07 15:49:07 首次发布