|
|
|
|
@@ -3,7 +3,10 @@ package com.bamanker.dailylove.controller;
|
|
|
|
|
import cn.hutool.core.date.ChineseDate;
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
|
import com.bamanker.dailylove.config.DailyLoveConfigure;
|
|
|
|
|
import com.bamanker.dailylove.domain.*;
|
|
|
|
|
import com.bamanker.dailylove.domain.DataItem;
|
|
|
|
|
import com.bamanker.dailylove.domain.ResultVo;
|
|
|
|
|
import com.bamanker.dailylove.domain.TianXinReqParam;
|
|
|
|
|
import com.bamanker.dailylove.domain.Weather;
|
|
|
|
|
import com.bamanker.dailylove.exception.PushException;
|
|
|
|
|
import com.bamanker.dailylove.service.DataRemoteService;
|
|
|
|
|
import com.bamanker.dailylove.service.WechatRequestService;
|
|
|
|
|
@@ -13,12 +16,14 @@ import org.springframework.aot.hint.annotation.RegisterReflectionForBinding;
|
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
import reactor.util.function.Tuple4;
|
|
|
|
|
import tools.jackson.databind.JsonNode;
|
|
|
|
|
import tools.jackson.databind.json.JsonMapper;
|
|
|
|
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.time.LocalDate;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author bamanker
|
|
|
|
|
@@ -63,11 +68,11 @@ public class PushDailyController {
|
|
|
|
|
String term = chineseDate.getTerm();
|
|
|
|
|
|
|
|
|
|
// 计算各种日期
|
|
|
|
|
int girlBirthdays = DataUtils.getBirthdays(DailyLoveConfigure.Girl_Birthday);
|
|
|
|
|
int boyBirthdays = DataUtils.getChineseBirthdays(DailyLoveConfigure.Boy_Birthday);
|
|
|
|
|
int catBirthdays = DataUtils.getBirthdays(DailyLoveConfigure.Cat_Birthday);
|
|
|
|
|
int loveDays = DataUtils.getDayDiff(DailyLoveConfigure.Love_Day);
|
|
|
|
|
int weddingDays = DataUtils.getDayDiff(DailyLoveConfigure.Wedding_Day);
|
|
|
|
|
int girlBirthdays = DataUtils.getBirthdays(DailyLoveConfigure.Girl_Birthday) - 1;
|
|
|
|
|
int boyBirthdays = DataUtils.getChineseBirthdays(DailyLoveConfigure.Boy_Birthday) - 1;
|
|
|
|
|
int catBirthdays = DataUtils.getBirthdays(DailyLoveConfigure.Cat_Birthday) - 1;
|
|
|
|
|
int loveDays = DataUtils.getDayDiff(DailyLoveConfigure.Love_Day) + 1;
|
|
|
|
|
int weddingDays = DataUtils.getDayDiff(DailyLoveConfigure.Wedding_Day) + 1;
|
|
|
|
|
|
|
|
|
|
log.info("gbir_days:{}, bbir_days:{}, cbir_days:{}, love_days:{}, wedding_days:{}",
|
|
|
|
|
girlBirthdays, boyBirthdays, catBirthdays, loveDays, weddingDays);
|
|
|
|
|
@@ -133,21 +138,23 @@ public class PushDailyController {
|
|
|
|
|
resultVo.setAttribute("rainbow", new DataItem(rainbowContent, DailyLoveConfigure.Color_dailyCn));
|
|
|
|
|
resultVo.setAttribute("lunar", new DataItem(chineseDate.toString(), DailyLoveConfigure.Color_chineseDate));
|
|
|
|
|
resultVo.setAttribute("festival", new DataItem(festival + " " + term, DailyLoveConfigure.Color_festival));
|
|
|
|
|
resultVo.setAttribute("gbir_day", new DataItem((girlBirthdays - 1) + "", DailyLoveConfigure.Color_gbir));
|
|
|
|
|
resultVo.setAttribute("bbir_day", new DataItem((boyBirthdays - 1) + "", DailyLoveConfigure.Color_bbir));
|
|
|
|
|
resultVo.setAttribute("cbir_day", new DataItem((catBirthdays - 1) + "", DailyLoveConfigure.Color_cbir));
|
|
|
|
|
resultVo.setAttribute("gbir_day", new DataItem(girlBirthdays + "", DailyLoveConfigure.Color_gbir));
|
|
|
|
|
resultVo.setAttribute("bbir_day", new DataItem(boyBirthdays + "", DailyLoveConfigure.Color_bbir));
|
|
|
|
|
resultVo.setAttribute("cbir_day", new DataItem(catBirthdays + "", DailyLoveConfigure.Color_cbir));
|
|
|
|
|
resultVo.setAttribute("words", new DataItem(words, DailyLoveConfigure.Color_remark));
|
|
|
|
|
resultVo.setAttribute("love_day", new DataItem((loveDays + 1) + "", DailyLoveConfigure.Color_loveDay));
|
|
|
|
|
resultVo.setAttribute("wedding_day", new DataItem((weddingDays + 1) + "", DailyLoveConfigure.Color_weddingDay));
|
|
|
|
|
resultVo.setAttribute("love_day", new DataItem(loveDays + "", DailyLoveConfigure.Color_loveDay));
|
|
|
|
|
resultVo.setAttribute("wedding_day", new DataItem(weddingDays + "", DailyLoveConfigure.Color_weddingDay));
|
|
|
|
|
|
|
|
|
|
log.info("推送消息: {}", resultVo);
|
|
|
|
|
|
|
|
|
|
// 获取微信访问令牌并发送消息
|
|
|
|
|
return wechatRequestService.getAccessToken("client_credential", DailyLoveConfigure.App_ID, DailyLoveConfigure.App_Secret)
|
|
|
|
|
.onErrorMap(throwable -> new PushException("获取微信访问令牌失败: " + throwable.getMessage(), throwable))
|
|
|
|
|
.map(tokenResp -> {
|
|
|
|
|
return mapper.readTree(tokenResp).get("access_token").asString();
|
|
|
|
|
})
|
|
|
|
|
.map(tokenResp -> mapper.readTree(tokenResp).get("access_token").asString())
|
|
|
|
|
.flatMap(accessToken -> wechatRequestService.sendMsg(accessToken, resultVo)
|
|
|
|
|
.onErrorMap(throwable -> new PushException("发送微信消息失败: " + throwable.getMessage(), throwable)));
|
|
|
|
|
.doOnNext(this::printPushLog)
|
|
|
|
|
.onErrorMap(throwable -> new PushException("发送微信消息失败: " + throwable.getMessage(), throwable)))
|
|
|
|
|
.doOnSuccess(response -> log.info("推送结果: {}", response));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("处理晚安推送数据时发生错误", e);
|
|
|
|
|
return Mono.error(new PushException("处理晚安推送数据时发生错误", e));
|
|
|
|
|
@@ -217,14 +224,14 @@ public class PushDailyController {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 解析天气数据
|
|
|
|
|
String city = mapper.readTree(weatherResp).get("result").get("area").asText();
|
|
|
|
|
String city = mapper.readTree(weatherResp).get("result").get("area").asString();
|
|
|
|
|
var weatherData = mapper.readTree(weatherResp).get("result").get("list").get(0);
|
|
|
|
|
Weather weather = mapper.treeToValue(weatherData, Weather.class);
|
|
|
|
|
|
|
|
|
|
// 解析其他数据
|
|
|
|
|
String tips = mapper.readTree(tipsResp).get("result").get("content").asText();
|
|
|
|
|
String morningContent = mapper.readTree(morningResp).get("result").get("content").asText();
|
|
|
|
|
String rainbowContent = mapper.readTree(rainbowResp).get("result").get("content").asText();
|
|
|
|
|
String tips = mapper.readTree(tipsResp).get("result").get("content").asString();
|
|
|
|
|
String morningContent = mapper.readTree(morningResp).get("result").get("content").asString();
|
|
|
|
|
String rainbowContent = mapper.readTree(rainbowResp).get("result").get("content").asString();
|
|
|
|
|
|
|
|
|
|
// 创建结果对象
|
|
|
|
|
ResultVo resultVo = ResultVo.initializeResultVo(DailyLoveConfigure.Open_ID,
|
|
|
|
|
@@ -250,14 +257,15 @@ public class PushDailyController {
|
|
|
|
|
resultVo.setAttribute("love_day", new DataItem(loveDays + "", DailyLoveConfigure.Color_loveDay));
|
|
|
|
|
resultVo.setAttribute("wedding_day", new DataItem(weddingDays + "", DailyLoveConfigure.Color_weddingDay));
|
|
|
|
|
|
|
|
|
|
log.info("推送消息: {}", resultVo);
|
|
|
|
|
// 获取微信访问令牌并发送消息
|
|
|
|
|
return wechatRequestService.getAccessToken("client_credential", DailyLoveConfigure.App_ID, DailyLoveConfigure.App_Secret)
|
|
|
|
|
.onErrorMap(throwable -> new PushException("获取微信访问令牌失败: " + throwable.getMessage(), throwable))
|
|
|
|
|
.map(tokenResp -> {
|
|
|
|
|
return mapper.readTree(tokenResp).get("access_token").asString();
|
|
|
|
|
})
|
|
|
|
|
.map(tokenResp -> mapper.readTree(tokenResp).get("access_token").asString())
|
|
|
|
|
.flatMap(accessToken -> wechatRequestService.sendMsg(accessToken, resultVo)
|
|
|
|
|
.onErrorMap(throwable -> new PushException("发送微信消息失败: " + throwable.getMessage(), throwable)));
|
|
|
|
|
.doOnNext(PushDailyController.this::printPushLog)
|
|
|
|
|
.onErrorMap(throwable -> new PushException("发送微信消息失败: " + throwable.getMessage(), throwable)))
|
|
|
|
|
.doOnSuccess(response -> log.info("推送结果: {}", response));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("处理早安推送数据时发生错误", e);
|
|
|
|
|
return Mono.error(new PushException("处理早安推送数据时发生错误", e));
|
|
|
|
|
@@ -272,8 +280,8 @@ public class PushDailyController {
|
|
|
|
|
*/
|
|
|
|
|
private void printPushLog(String responseStr) {
|
|
|
|
|
JsonNode jsonNode = mapper.readTree(responseStr);
|
|
|
|
|
String msgCode = jsonNode.get("errcode").asText();
|
|
|
|
|
String msgContent = jsonNode.get("errmsg").asText();
|
|
|
|
|
String msgCode = jsonNode.get("errcode").asString();
|
|
|
|
|
String msgContent = jsonNode.get("errmsg").asString();
|
|
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
System.out.println("[ " + dateFormat.format(new Date()) + " ] : messageCode=" + msgCode + ",messageContent=" + msgContent);
|
|
|
|
|
}
|
|
|
|
|
|