Browse Source

校检 事项链接 可不可访问

master
PoffyZhang 9 months ago
parent
commit
9d4629d260
4 changed files with 182 additions and 1 deletions
  1. +30
    -0
      kqapi/src/main/java/com/ningdatech/kqapi/common/model/entity/Item.java
  2. +11
    -0
      kqapi/src/main/java/com/ningdatech/kqapi/scheduler/controller/TaskController.java
  3. +1
    -1
      kqapi/src/main/java/com/ningdatech/kqapi/scheduler/model/CommonLog.java
  4. +140
    -0
      kqapi/src/main/java/com/ningdatech/kqapi/scheduler/task/CheckMattersUrlTask.java

+ 30
- 0
kqapi/src/main/java/com/ningdatech/kqapi/common/model/entity/Item.java View File

@@ -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;
}
}

+ 11
- 0
kqapi/src/main/java/com/ningdatech/kqapi/scheduler/controller/TaskController.java View File

@@ -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 "操作成功";
}
}

kqapi/src/main/java/com/ningdatech/kqapi/scheduler/task/model/CommonLog.java → kqapi/src/main/java/com/ningdatech/kqapi/scheduler/model/CommonLog.java View File

@@ -1,4 +1,4 @@
package com.ningdatech.kqapi.scheduler.task.model;
package com.ningdatech.kqapi.scheduler.model;

import lombok.Data;


+ 140
- 0
kqapi/src/main/java/com/ningdatech/kqapi/scheduler/task/CheckMattersUrlTask.java View File

@@ -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<NdKqZzsfwMenu> menus = menuService.list(Wrappers.lambdaQuery(NdKqZzsfwMenu.class)
.eq(NdKqZzsfwMenu::getHasUrl, 1)
.isNotNull(NdKqZzsfwMenu::getWebapplyurl));
Integer errNum = 0;
List<String> 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<String, String> map = new LinkedMultiValueMap<>();

String webapplyurl = menu.getWebapplyurl();
webapplyurl = webapplyurl.replace("/#","");
Map<String, String> 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<MultiValueMap<String, String>> formEntity = new HttpEntity(map, headers);
ResponseEntity<Item> 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<String, String> queryParams = getQueryParams(new URL(url));
System.out.println(queryParams.get("localInnerCode"));
}

public static Map<String, String> getQueryParams(URL url) {
Map<String, String> 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;
}
}

Loading…
Cancel
Save