申请测试号
地址
- 通过F12抓取体验接口权限表的HTML
解析HTML
引入pom
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-extra</artifactId>
<version>5.8.21</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.8.21</version>
</dependency>
<dependency>
<groupId>io.github.biezhi</groupId>
<artifactId>TinyPinyin</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.16.1</version>
</dependency>
代码
CellDTO
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class CellDTO {
private String namePY;
private String nameZW;
private String url;
}
HeadDTO
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class HeadDTO {
private String namePY;
private String nameZW;
}
核心逻辑
private static CellDTO[][] gen(String html){
Document document = Jsoup.parse(html);
List<HeadDTO> headDTOList =new ArrayList<>();
for (Element theadELe : document.select("thead th")) {
List<String> resultFindAll = ReUtil.findAll("[\\u4e00-\\u9fa5]+", theadELe.html(), 0, new ArrayList<>());
String zw=String.join("",resultFindAll);
String pinyin= PinyinUtil.getFirstLetter(zw,"");
headDTOList.add(HeadDTO.builder().namePY(pinyin).nameZW(zw).build());
}
int columnSize = headDTOList.size();
Map<Integer,Integer> tdCount =new HashMap<>();
Elements trs = document.select(".table_special > tbody > tr");
int trTotal = trs.size();
CellDTO[][] table = new CellDTO[trTotal][columnSize-2];
int tdNowFixNum =0;
int tdTotalFixNum =0;
for (int trIndex = 0; trIndex < trs.size(); trIndex++) {
Elements tds = trs.get(trIndex).select("> td ");
int tdSize = tds.size();
int nowTrIndex=trIndex;
for (int tdIndex = 0; tdIndex < tdSize-2; tdIndex++) {
if(0==tdIndex){
for (Map.Entry<Integer,Integer> entry : tdCount.entrySet()) {
Integer key=entry.getKey(); //获取键
Integer value=entry.getValue();
value=value-1;
if(0==value){
tdTotalFixNum--;
}
tdCount.put(key,value);
}
tdNowFixNum=tdTotalFixNum;
}
Element td =tds.get(tdIndex);
int nowTdIndex = columnSize==tdSize?tdIndex:(tdIndex+tdTotalFixNum );
String tdValue;
Elements a = td.select("a");
String url="";
if(!a.isEmpty()){
tdValue=a.html();
url=a.attr("href");
} else {
tdValue= td.html();
}
List<String> resultFindAll = ReUtil.findAll("[\\u4e00-\\u9fa5]+", tdValue, 0, new ArrayList<>());
String zw=String.join("",resultFindAll);
String pinyin= PinyinUtil.getFirstLetter(zw,"");
CellDTO cell = CellDTO.builder().url(url).nameZW(zw).namePY(PinyinUtil.getFirstLetter(pinyin, "")).build();
// System.out.println("> "+(trIndex+1)+" "+ (tdIndex+1)+" || "+nowTrIndex+" "+ nowTdIndex +" > "+tdNowFixNum +" : "+tdTotalFixNum + " >> "+tdValue);
if (td.hasAttr("rowspan")) {
Integer trAddNum = Integer.valueOf(td.attr("rowspan"));
for (int fillTrNum = nowTrIndex; fillTrNum < nowTrIndex+trAddNum; fillTrNum++) {
table[fillTrNum][nowTdIndex]=cell;
}
tdCount.put(nowTdIndex,trAddNum);
tdNowFixNum++;
}else {
table[nowTrIndex][nowTdIndex]=cell;
}
}
tdTotalFixNum =tdNowFixNum;
}
return table;
}
vm模板代码生成
wx-java-mp-spring-boot-starter
引入pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.15</version>
<relativePath/>
</parent>
<groupId>com.binarywang</groupId>
<artifactId>wx-java-mp-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>wx mp demo</name>
<description>Demo project for wx mp with spring boot</description>
<properties>
<java.version>1.8</java.version>
<swagger.version>3.0.0</swagger.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.8.21</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-extra</artifactId>
<version>5.8.21</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.8.21</version>
</dependency>
<dependency>
<groupId>io.github.biezhi</groupId>
<artifactId>TinyPinyin</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.16.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>wx-java-mp-spring-boot-starter</artifactId>
<version>4.5.3.B</version>
</dependency>
<dependency>
<groupId>com.github.jedis-lock</groupId>
<artifactId>jedis-lock</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- Swagger3依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>${swagger.version}</version>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 防止进入swagger页面报类型转换错误,排除3.0.0中的引用,手动增加1.6.2版本 -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置模板
文件路径:src/main/resources/templates/Controller.java.vm
package com.binarywang.demo.wx.mp.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/${requestMapping}")
@Api(value = "${apiValue}", tags = {"${apiValue}"})
public class ${requestMappingUF}Controller {
@Autowired
private WxMpService wxMpService;
#foreach ($data in $datas)
@ApiOperation("${data.nameZW}")
@GetMapping("/${data.namePY}")
public Object ${data.namePY}ListFn() throws WxErrorException {
return null;
}
#end
}
核心代码
html 是F12抓取的页面代码 , 需要替换中文 文件目录
CellDTO[][] gen = HtmlUtils.gen(html);
String key =null;
List<CellDTO> data =null;
TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("templates", TemplateConfig.ResourceMode.CLASSPATH));
// TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("路径\\templates", TemplateConfig.ResourceMode.FILE));
Template template = engine.getTemplate("Controller.java.vm");
for (int i = 0; i <gen.length ; i++) {
CellDTO cell= gen[i][1];
if (key == null) {
key=cell.getNamePY();
data =new ArrayList<>();
}
if(key.equals(cell.getNamePY() ) ){
data.add( gen[i][2]);
}else {
CODE_WRITE(gen,i,data,template);
key=cell.getNamePY();
data =new ArrayList<>();
data.add( gen[i][2]);
}
}
CODE_WRITE(gen,gen.length,data,template);
}
public static void CODE_WRITE(CellDTO[][] gen, int i , List<CellDTO> data, Template template){
String py= gen[i-1][1].getNamePY();
String apiValue =gen[i-1][0].getNameZW()+" - "+gen[i-1][1].getNameZW();
Map<String,Object> map = new HashMap<>();
map.put("datas",data);
map.put("requestMapping",py);
String requestMappingUF=StrUtil.upperFirst(py);
map.put("requestMappingUF",requestMappingUF );
map.put("apiValue",apiValue);
String fileName= requestMappingUF+"Controller.java";
String result = template.render(map);
String filePath="文件目录\\main\\java\\com\\binarywang\\demo\\wx\\mp\\controller\\"+fileName;
FileUtil.writeUtf8String(result,new File(filePath));
System.out.println(result);
System.out.println(" ");
}