From 9d4629d260ee14088dd05c61ce923090013c3089 Mon Sep 17 00:00:00 2001 From: PoffyZhang <99775271@qq.com> Date: Fri, 22 Mar 2024 16:36:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=A1=E6=A3=80=20=E4=BA=8B=E9=A1=B9?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=20=E5=8F=AF=E4=B8=8D=E5=8F=AF=E8=AE=BF?= =?UTF-8?q?=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ningdatech/kqapi/common/model/entity/Item.java | 30 +++++ .../kqapi/scheduler/controller/TaskController.java | 11 ++ .../kqapi/scheduler/model/CommonLog.java | 43 +++++++ .../kqapi/scheduler/task/CheckMattersUrlTask.java | 140 +++++++++++++++++++++ .../kqapi/scheduler/task/model/CommonLog.java | 43 ------- 5 files changed, 224 insertions(+), 43 deletions(-) create mode 100644 kqapi/src/main/java/com/ningdatech/kqapi/common/model/entity/Item.java create mode 100644 kqapi/src/main/java/com/ningdatech/kqapi/scheduler/model/CommonLog.java create mode 100644 kqapi/src/main/java/com/ningdatech/kqapi/scheduler/task/CheckMattersUrlTask.java delete mode 100644 kqapi/src/main/java/com/ningdatech/kqapi/scheduler/task/model/CommonLog.java diff --git a/kqapi/src/main/java/com/ningdatech/kqapi/common/model/entity/Item.java b/kqapi/src/main/java/com/ningdatech/kqapi/common/model/entity/Item.java new file mode 100644 index 0000000..62739be --- /dev/null +++ b/kqapi/src/main/java/com/ningdatech/kqapi/common/model/entity/Item.java @@ -0,0 +1,30 @@ +package com.ningdatech.kqapi.common.model.entity; + +import lombok.Data; + +import javax.swing.*; +import java.io.Serializable; + +/** + * @Classname Item + * @Description + * @Date 2024/3/22 15:34 + * @Author PoffyZhang + */ +@Data +public class Item implements Serializable { + + private String roles; + private String permissions; + + private Boolean success; + + private String code; + + private ItemData data; + + @Data + public static class ItemData { + private Object item; + } +} diff --git a/kqapi/src/main/java/com/ningdatech/kqapi/scheduler/controller/TaskController.java b/kqapi/src/main/java/com/ningdatech/kqapi/scheduler/controller/TaskController.java index baf2c52..7b6b5c0 100644 --- a/kqapi/src/main/java/com/ningdatech/kqapi/scheduler/controller/TaskController.java +++ b/kqapi/src/main/java/com/ningdatech/kqapi/scheduler/controller/TaskController.java @@ -2,6 +2,7 @@ package com.ningdatech.kqapi.scheduler.controller; import com.ningdatech.kqapi.scheduler.manage.SynManage; +import com.ningdatech.kqapi.scheduler.task.CheckMattersUrlTask; import com.ningdatech.kqapi.scheduler.task.RemoveMattersTask; import com.ningdatech.kqapi.scheduler.task.SynTask; import com.ningdatech.kqapi.zzsfw.entity.vo.MatterTopVO; @@ -17,6 +18,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.net.MalformedURLException; import java.net.UnknownHostException; import java.util.Arrays; import java.util.List; @@ -38,6 +40,8 @@ import java.util.List; public class TaskController { private final RemoveMattersTask removeMattersTask; + private final CheckMattersUrlTask checkMattersUrlTask; + private final SynTask synTask; private final SynManage synManage; @@ -80,4 +84,11 @@ public class TaskController { synTask.synMenu(); return "同步成功"; } + + @ApiOperation(value = "check链接", notes = "check链接") + @GetMapping("/check-url") + public String checkUrl() throws UnknownHostException, MalformedURLException { + checkMattersUrlTask.doTask(); + return "操作成功"; + } } diff --git a/kqapi/src/main/java/com/ningdatech/kqapi/scheduler/model/CommonLog.java b/kqapi/src/main/java/com/ningdatech/kqapi/scheduler/model/CommonLog.java new file mode 100644 index 0000000..3853ce8 --- /dev/null +++ b/kqapi/src/main/java/com/ningdatech/kqapi/scheduler/model/CommonLog.java @@ -0,0 +1,43 @@ +package com.ningdatech.kqapi.scheduler.model; + +import lombok.Data; + +@Data +public class CommonLog { + /** + * 用户id + */ + private String userId; + /** + * 用户角色(群众、企业、政府工作人员、第三方) + */ + private String userRole; + /** + * 地区编码 + */ + private String areaCode; + /** + * 操作类型(1-登录 2-离开 3-办事开始 4-办事结束) + */ + private Integer actionType; + /** + * 操作标识 + */ + private String actionId; + /** + * 操作时间 + */ + private String actionTime; + /** + * 操作时长 + */ + private Long actionDuration; + /** + * 操作状态(0-成功 1-失败) + */ + private Integer actionStatus; + /** + * 应用编码 + */ + private String appCode; +} diff --git a/kqapi/src/main/java/com/ningdatech/kqapi/scheduler/task/CheckMattersUrlTask.java b/kqapi/src/main/java/com/ningdatech/kqapi/scheduler/task/CheckMattersUrlTask.java new file mode 100644 index 0000000..1bdbb1b --- /dev/null +++ b/kqapi/src/main/java/com/ningdatech/kqapi/scheduler/task/CheckMattersUrlTask.java @@ -0,0 +1,140 @@ +package com.ningdatech.kqapi.scheduler.task; + +import cn.hutool.core.date.StopWatch; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.google.common.collect.Lists; +import com.ningdatech.basic.model.ApiResponse; +import com.ningdatech.kqapi.common.model.entity.Item; +import com.ningdatech.kqapi.common.util.HttpUtil; +import com.ningdatech.kqapi.zzsfw.entity.entity.NdKqZzsfwMenu; +import com.ningdatech.kqapi.zzsfw.service.INdKqZzsfwMenuService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.*; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.CompletableFuture; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @author ZPF + * @since 2023/08/31 18:16 + */ +@Component +@Slf4j +@RequiredArgsConstructor +public class CheckMattersUrlTask { + + @Value("${hostname}") + public String HOST; + + @Value("${spring.profiles.active}") + public String active; + + @Autowired + private INdKqZzsfwMenuService menuService; + + /** + * 每天9点 开始校检 链接 是不是可行 + */ + @Scheduled(cron = "0 0 9 * * ?") + public void doTask() throws UnknownHostException, MalformedURLException { +// if (!HOST.equals(InetAddress.getLocalHost().getHostName())) { +// log.info("定时器没开启或者host不对! {}:{}", +// HOST,InetAddress.getLocalHost().getHostName()); +// return; +// } + + StopWatch stopWatch = new StopWatch(); + stopWatch.start(); + + log.info("==========开始校检事项表里的链接 会不会404 如果404就隐藏"); + + List menus = menuService.list(Wrappers.lambdaQuery(NdKqZzsfwMenu.class) + .eq(NdKqZzsfwMenu::getHasUrl, 1) + .isNotNull(NdKqZzsfwMenu::getWebapplyurl)); + Integer errNum = 0; + List errUrls = Lists.newArrayList(); + + for (NdKqZzsfwMenu menu : menus) { + RestTemplate restTemplate = new RestTemplate(HttpUtil.generateHttpRequestFactory()); + String url = "https://www.zjzwfw.gov.cn/jpaas-zjservice-server/open-api/item/getItemDetail"; + MultiValueMap map = new LinkedMultiValueMap<>(); + + String webapplyurl = menu.getWebapplyurl(); + webapplyurl = webapplyurl.replace("/#",""); + Map queryParams = getQueryParams(new URL(webapplyurl)); + String localInnerCode = queryParams.get("localInnerCode"); + log.info("localInnerCode :{}",localInnerCode); + map.add("localInnerCode", localInnerCode); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + HttpEntity> formEntity = new HttpEntity(map, headers); + ResponseEntity response = restTemplate.postForEntity(url, formEntity, Item.class); + System.out.println(response.getBody()); + Item body = response.getBody(); + Item.ItemData itemData = body.getData(); + //如果是空的 + if(Objects.isNull(itemData.getItem())){ + //隐藏 + menuService.update(Wrappers.lambdaUpdate(NdKqZzsfwMenu.class) + .eq(NdKqZzsfwMenu::getId,menu.getId()) + .set(NdKqZzsfwMenu::getHasUrl,0) + .set(NdKqZzsfwMenu::getWebapplyurl,null)); + errNum += 1; + errUrls.add(webapplyurl); + } + } + + log.info("不可用的链接有 :{}条 ,详情:{}",errNum,errUrls); + + stopWatch.stop(); + log.info("校检事项表里的链接结束====={}s",stopWatch.getTotalTimeSeconds()); + } + + public static void main(String[] args) throws Exception { + String url = "https://www.zjzwfw.gov.cn/zjservice-fe/#/workguide?localInnerCode=3bd735e2-ddfe-4377-86b3-beec85252f21&siteCode=330000000000"; + url = url.replace("/#",""); + Map queryParams = getQueryParams(new URL(url)); + System.out.println(queryParams.get("localInnerCode")); + } + + public static Map getQueryParams(URL url) { + Map queryPairs = new HashMap<>(); + String query = url.getQuery(); + String[] pairs = query.split("&"); + for (String pair : pairs) { + int idx = pair.indexOf("="); + try { + if (idx > 0) { + String key = pair.substring(0, idx); + String value = pair.substring(idx + 1); + queryPairs.put(key, value); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + return queryPairs; + } +} diff --git a/kqapi/src/main/java/com/ningdatech/kqapi/scheduler/task/model/CommonLog.java b/kqapi/src/main/java/com/ningdatech/kqapi/scheduler/task/model/CommonLog.java deleted file mode 100644 index 3418056..0000000 --- a/kqapi/src/main/java/com/ningdatech/kqapi/scheduler/task/model/CommonLog.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.ningdatech.kqapi.scheduler.task.model; - -import lombok.Data; - -@Data -public class CommonLog { - /** - * 用户id - */ - private String userId; - /** - * 用户角色(群众、企业、政府工作人员、第三方) - */ - private String userRole; - /** - * 地区编码 - */ - private String areaCode; - /** - * 操作类型(1-登录 2-离开 3-办事开始 4-办事结束) - */ - private Integer actionType; - /** - * 操作标识 - */ - private String actionId; - /** - * 操作时间 - */ - private String actionTime; - /** - * 操作时长 - */ - private Long actionDuration; - /** - * 操作状态(0-成功 1-失败) - */ - private Integer actionStatus; - /** - * 应用编码 - */ - private String appCode; -}