|
|
|
|
@@ -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
|
|
|
|
|
@@ -143,11 +148,11 @@ public class PushDailyController {
|
|
|
|
|
// 获取微信访问令牌并发送消息
|
|
|
|
|
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)))
|
|
|
|
|
.log();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("处理晚安推送数据时发生错误", e);
|
|
|
|
|
return Mono.error(new PushException("处理晚安推送数据时发生错误", e));
|
|
|
|
|
@@ -217,14 +222,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,
|
|
|
|
|
@@ -253,11 +258,11 @@ public class PushDailyController {
|
|
|
|
|
// 获取微信访问令牌并发送消息
|
|
|
|
|
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)))
|
|
|
|
|
.log();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("处理早安推送数据时发生错误", e);
|
|
|
|
|
return Mono.error(new PushException("处理早安推送数据时发生错误", e));
|
|
|
|
|
@@ -272,8 +277,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);
|
|
|
|
|
}
|
|
|
|
|
|