ソースを参照

update:律动日报监控 V1

lvzhiqiang 2 年 前
コミット
daceb191c2

+ 6 - 1
src/main/java/top/lvzhiqiang/service/CoinService.java

@@ -2,6 +2,8 @@ package top.lvzhiqiang.service;
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.bean.article.NewArticle;
 
 /**
  * Coin Service
@@ -15,10 +17,12 @@ public interface CoinService {
 
     void monitorAlarm(String content, String jobAlarmMode);
 
-    void monitorAlarm4APP_TEXT_CARD(String content, JSONObject params);
+    void monitorAlarm4APP_TEXT_CARD(String content, JSONObject params, WxCpService wxCpService);
 
     void monitorAlarm4CHAT_BOT(String content, JSONObject params);
 
+    void monitorAlarm4NEWS(JSONObject params, WxCpService wxCpService,NewArticle... articles);
+
     /**
      * 主查询
      *
@@ -34,5 +38,6 @@ public interface CoinService {
     String orderDetail2(String orderId, String symbol);
 
     void syncData4TraderList();
+
     void syncData4TraderListSub(JSONArray dataList);
 }

+ 191 - 5
src/main/java/top/lvzhiqiang/service/impl/CoinServiceImpl.java

@@ -8,8 +8,11 @@ import lombok.extern.slf4j.Slf4j;
 import me.chanjar.weixin.common.error.WxErrorException;
 import me.chanjar.weixin.cp.api.WxCpGroupRobotService;
 import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
+import me.chanjar.weixin.cp.bean.article.NewArticle;
 import me.chanjar.weixin.cp.bean.message.WxCpMessage;
 import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
+import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
 import org.jsoup.Connection;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Async;
@@ -89,6 +92,8 @@ public class CoinServiceImpl implements CoinService {
     private final ForkJoinPool forkJoinPool5 = new ForkJoinPool(16);
     private static final DecimalFormat df1 = new DecimalFormat("#,##0.00");
 
+    private static final WxCpServiceImpl wxCpService4News;
+
     static {
         // API KEY作为一个字符串。
         basicHeaderMap.put("ACCESS-KEY", "bg_433d37306df0e8901c6d107c6d9e9111");
@@ -104,6 +109,14 @@ public class CoinServiceImpl implements CoinService {
         basicHeaderMap.put("locale", "zh-CN");
 
         df1.setRoundingMode(RoundingMode.HALF_UP);
+
+
+        WxCpDefaultConfigImpl wxCpDefaultConfig = new WxCpDefaultConfigImpl();
+        wxCpDefaultConfig.setCorpId("ww95a4adba56acb55f");
+        wxCpDefaultConfig.setAgentId(1000004);
+        wxCpDefaultConfig.setCorpSecret("hG50gbVZ8pXm3tSzY3BpwTzbrMmm6sTf8_bPfJG_6Yc");
+        wxCpService4News = new WxCpServiceImpl();
+        wxCpService4News.setWxCpConfigStorage(wxCpDefaultConfig);
     }
 
     @Override
@@ -342,7 +355,7 @@ public class CoinServiceImpl implements CoinService {
                         params.put("title", (order.getString("side").contains("open") ? "合约开单" : "合约平单") + "监控报警");
                         params.put("logUrl", "https://jav.lvzhiqiang.top/coin/orderDetail2/" + orderId + "/" + symbol);
                         params.put("btnTxt", "订单详情");
-                        SpringUtils.getBean(CoinServiceImpl.class).monitorAlarm4APP_TEXT_CARD(content, params);
+                        SpringUtils.getBean(CoinServiceImpl.class).monitorAlarm4APP_TEXT_CARD(content, params, null);
                     }
                 }
             } catch (Exception e) {
@@ -475,7 +488,7 @@ public class CoinServiceImpl implements CoinService {
                             params.put("title", "交易员开单监控报警");
                             params.put("logUrl", "https://jav.lvzhiqiang.top/coin/orderDetail/" + order.getString("trackingNo"));
                             params.put("btnTxt", "跟单详情");
-                            SpringUtils.getBean(CoinServiceImpl.class).monitorAlarm4APP_TEXT_CARD(content, params);
+                            SpringUtils.getBean(CoinServiceImpl.class).monitorAlarm4APP_TEXT_CARD(content, params, null);
                         }
                     }
                 } catch (Exception ex) {
@@ -483,12 +496,123 @@ public class CoinServiceImpl implements CoinService {
             })).join();
         }, 0, 3, TimeUnit.SECONDS);
 
+        // 星球日报新闻快讯监控报警
+        scheduler.scheduleWithFixedDelay(() -> {
+            try {
+                Connection.Response response = JsoupUtil.requestBody("https://www.odaily.news/v1/openapi/feeds", JsoupUtil.HTTP_GET, InitRunner.proxy, null, null);
+                JSONObject result = JSONObject.parseObject(response.body());
+                JSONArray newsList = result.getJSONObject("data").getJSONArray("arr_news");
+
+                LocalDateTime endTime = LocalDateTime.now();
+                for (int i = 0; i < newsList.size(); i++) {
+                    if (i == 5) {
+                        break;
+                    }
+
+                    JSONObject news = newsList.getJSONObject(i);
+                    String id = "Odaily" + news.getString("id");
+                    String publishedAt = news.getString("published_at");
+                    LocalDateTime publishedAtTime = DateUtils.stringToLocalDateTime(publishedAt);
+                    if (Duration.between(publishedAtTime, endTime).getSeconds() < 20 && !orderMap.containsKey(id)) {
+                        orderMap.put(id, "1");
+
+                        String type = news.getString("type");
+                        String title = news.getString("title");
+                        String link = news.getString("link");
+
+                        String content = "";
+                        JSONObject params = new JSONObject();
+                        params.put("title", "Odaily监控报警");
+                        params.put("btnTxt", "新闻详情");
+                        params.put("logUrl", link);
+                        params.put("user", "@all");
+                        params.put("agentId", 1000004);
+
+                        if ("newsflashes".equals(type)) {
+                            String newsUrl = news.getString("news_url");
+                            content = "<div class=\"highlight\">标题:" + title + "</div>" +
+                                    "<div>类型:" + "新闻快讯" + "</div>" +
+                                    "<div>发布时间:" + publishedAt + "</div>" +
+                                    "<div class=\"gray\">描述:" + news.getString("description").replace("\n", "&nbsp;&nbsp;&nbsp;&nbsp;") + "</div>";
+                        } else if ("posts".equals(type)) {
+                            content = "<div class=\"highlight\">标题:" + title + "</div>" +
+                                    "<div>类型:" + "帖子" + "</div>" +
+                                    "<div>发布时间:" + publishedAt + "</div>" +
+                                    "<div class=\"gray\">描述:" + news.getString("summary").replace("\n", "&nbsp;&nbsp;&nbsp;&nbsp;") + "</div>";
+                        }
+
+                        SpringUtils.getBean(CoinServiceImpl.class).monitorAlarm4APP_TEXT_CARD(content, params, wxCpService4News);
+                    }
+                }
+            } catch (Exception e) {
+            }
+        }, 0, 3, TimeUnit.SECONDS);
+
+        // 律动日报新闻快讯监控报警
+        scheduler.scheduleWithFixedDelay(() -> {
+            try {
+                Map<String, String> paramMap = new HashMap<>();
+                paramMap.put("size", "10");
+                paramMap.put("page", "1");
+                Connection.Response response = JsoupUtil.requestBody("https://api.theblockbeats.news/v1/open-api/open-flash", JsoupUtil.HTTP_GET, InitRunner.proxy, null, paramMap);
+                JSONObject result = JSONObject.parseObject(response.body());
+                JSONArray newsList = result.getJSONObject("data").getJSONArray("data");
+
+                LocalDateTime endTime = LocalDateTime.now();
+                for (int i = 0; i < newsList.size(); i++) {
+                    if (i == 5) {
+                        break;
+                    }
+
+                    JSONObject news = newsList.getJSONObject(i);
+                    String id = "BlockBeats" + news.getString("id");
+                    String createTime = news.getString("create_time");
+                    LocalDateTime createTimeTime = DateUtils.longToLocalDateTime_(Long.valueOf(createTime));
+                    createTime = DateUtils.localDateTimeToString(createTimeTime);
+                    if (Duration.between(createTimeTime, endTime).getSeconds() < 20 && !orderMap.containsKey(id)) {
+                        orderMap.put(id, "1");
+
+                        String title = news.getString("title");
+                        String content = news.getString("content");
+                        String pic = news.getString("pic");
+                        String link = news.getString("link");
+                        String url = news.getString("url");
+
+                        JSONObject params = new JSONObject();
+                        params.put("title", "BlockBeats监控报警");
+                        params.put("btnTxt", "新闻详情");
+                        params.put("logUrl", link);
+                        params.put("user", "@all");
+                        params.put("agentId", 1000004);
+                        if (StringUtils.isEmpty(pic)) {
+                            // 文本卡片
+                            String contentStr = "<div class=\"highlight\">标题:" + title + "</div>" +
+                                    "<div>类型:" + "新闻快讯" + "</div>" +
+                                    "<div>发布时间:" + createTime + "</div>" +
+                                    "<div class=\"gray\">描述:" + content.replace("\n", "&nbsp;&nbsp;&nbsp;&nbsp;") + "</div>";
+                            SpringUtils.getBean(CoinServiceImpl.class).monitorAlarm4APP_TEXT_CARD(contentStr, params, wxCpService4News);
+                        } else {
+                            // 图文
+                            NewArticle article1 = new NewArticle();
+                            article1.setUrl(link);
+                            article1.setPicUrl(pic);
+                            article1.setDescription(content);
+                            article1.setTitle(title);
+
+                            SpringUtils.getBean(CoinServiceImpl.class).monitorAlarm4NEWS(params, wxCpService4News, article1);
+                        }
+                    }
+                }
+            } catch (Exception e) {
+            }
+        }, 0, 3, TimeUnit.SECONDS);
+
         return null;
     }
 
     @Override
     @Async("coinTaskExecutor")
-    public void monitorAlarm4APP_TEXT_CARD(String content, JSONObject params) {
+    public void monitorAlarm4APP_TEXT_CARD(String content, JSONObject params, WxCpService wxCpServiceFinal) {
         // 文本卡片模式发消息
         String title = "监控告警明细";
         if (params.containsKey("title")) {
@@ -515,7 +639,16 @@ public class CoinServiceImpl implements CoinService {
             tag = params.getString("tag");
         }
 
-        WxCpMessage wxCpMessage = WxCpMessage.TEXTCARD().agentId(properties.getAgentId())
+        Integer agentId = properties.getAgentId();
+        if (params.containsKey("agentId")) {
+            agentId = params.getInteger("agentId");
+        }
+
+        if (wxCpServiceFinal == null) {
+            wxCpServiceFinal = wxCpService;
+        }
+
+        WxCpMessage wxCpMessage = WxCpMessage.TEXTCARD().agentId(agentId)
                 .toUser(user)
                 .toParty(party)
                 .toTag(tag)
@@ -524,7 +657,60 @@ public class CoinServiceImpl implements CoinService {
                 .build();
         try {
             log.info("企业微信推送消息,send message: {}", wxCpMessage);
-            WxCpMessageSendResult sendResult = wxCpService.getMessageService().send(wxCpMessage);
+            WxCpMessageSendResult sendResult = wxCpServiceFinal.getMessageService().send(wxCpMessage);
+            log.info("企业微信推送消息成功,send result: {}", sendResult);
+        } catch (WxErrorException e) {
+            log.error("企业微信推送消息失败!Detail: ", e);
+        }
+    }
+
+    @Override
+    @Async("coinTaskExecutor")
+    public void monitorAlarm4NEWS(JSONObject params, WxCpService wxCpServiceFinal, NewArticle... articles) {
+        // 图文消息
+        String title = "监控告警明细";
+        if (params.containsKey("title")) {
+            title = params.getString("title");
+        }
+        String logUrl = "https://lvzhiqiang.top";
+        if (params.containsKey("logUrl")) {
+            logUrl = params.getString("logUrl");
+        }
+        String btnTxt = "日志详情";
+        if (params.containsKey("btnTxt")) {
+            btnTxt = params.getString("btnTxt");
+        }
+        String user = "LvZhiQiang";
+        if (params.containsKey("user")) {
+            user = params.getString("user");
+        }
+        String party = "";
+        if (params.containsKey("party")) {
+            party = params.getString("party");
+        }
+        String tag = "";
+        if (params.containsKey("tag")) {
+            tag = params.getString("tag");
+        }
+
+        Integer agentId = properties.getAgentId();
+        if (params.containsKey("agentId")) {
+            agentId = params.getInteger("agentId");
+        }
+
+        if (wxCpServiceFinal == null) {
+            wxCpServiceFinal = wxCpService;
+        }
+
+        WxCpMessage wxCpMessage = WxCpMessage.NEWS().agentId(agentId)
+                .toUser(user)
+                .toParty(party)
+                .toTag(tag)
+                .addArticle(articles)
+                .build();
+        try {
+            log.info("企业微信推送消息,send message: {}", wxCpMessage);
+            WxCpMessageSendResult sendResult = wxCpServiceFinal.getMessageService().send(wxCpMessage);
             log.info("企业微信推送消息成功,send result: {}", sendResult);
         } catch (WxErrorException e) {
             log.error("企业微信推送消息失败!Detail: ", e);

+ 11 - 0
src/main/java/top/lvzhiqiang/util/DateUtils.java

@@ -362,9 +362,16 @@ public class DateUtils {
         return dateFormatter.format(longToLocalDateTime(time));
     }
 
+    public static String localDateTimeToString(LocalDateTime time) {
+        return dateTimeFormatter.format(time);
+    }
+
     public static LocalDateTime longToLocalDateTime(Long time) {
         return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault());
     }
+    public static LocalDateTime longToLocalDateTime_(Long time) {
+        return LocalDateTime.ofInstant(Instant.ofEpochSecond(time), ZoneId.systemDefault());
+    }
 
     public static LocalDateTime longToLocalDateTime2(Long time) {
         return new Timestamp(time).toLocalDateTime();
@@ -374,6 +381,10 @@ public class DateUtils {
         return Timestamp.valueOf(time).getTime();
     }
 
+    public static LocalDateTime stringToLocalDateTime(String time) {
+        return LocalDateTime.parse(time, dateTimeFormatter);
+    }
+
     public static void main(String[] args) {
         System.out.println(getFewDateStrsByMonth(getToday(), 7));
     }