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