微信公众号开发学习

申请测试号

地址

  • 通过F12抓取体验接口权限表的HTML
    微信公众号开发学习_第1张图片

解析HTML

引入pom

		<dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>

        <dependency>
            <groupId>cn.hutoolgroupId>
            <artifactId>hutool-extraartifactId>
            <version>5.8.21version>
        dependency>
        <dependency>
            <groupId>cn.hutoolgroupId>
            <artifactId>hutool-coreartifactId>
            <version>5.8.21version>
        dependency>

        <dependency>
            <groupId>io.github.biezhigroupId>
            <artifactId>TinyPinyinartifactId>
            <version>2.0.3.RELEASEversion>
        dependency>
   <dependency>
            <groupId>org.jsoupgroupId>
            <artifactId>jsoupartifactId>
            <version>1.16.1version>
	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


<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.5.15version>
        <relativePath/>
    parent>
    <groupId>com.binarywanggroupId>
    <artifactId>wx-java-mp-demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>wx mp demoname>
    <description>Demo project for wx mp with spring bootdescription>

    <properties>
        <java.version>1.8java.version>
        <swagger.version>3.0.0swagger.version>
    properties>

    <dependencies>

        <dependency>
            <groupId>org.apache.velocitygroupId>
            <artifactId>velocity-engine-coreartifactId>
            <version>2.3version>
        dependency>
        <dependency>
            <groupId>cn.hutoolgroupId>
            <artifactId>hutool-coreartifactId>
            <version>5.8.21version>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>

        <dependency>
            <groupId>cn.hutoolgroupId>
            <artifactId>hutool-extraartifactId>
            <version>5.8.21version>
        dependency>
        <dependency>
            <groupId>cn.hutoolgroupId>
            <artifactId>hutool-coreartifactId>
            <version>5.8.21version>
        dependency>

        <dependency>
            <groupId>io.github.biezhigroupId>
            <artifactId>TinyPinyinartifactId>
            <version>2.0.3.RELEASEversion>
        dependency>


        
        <dependency>
            <groupId>org.jsoupgroupId>
            <artifactId>jsoupartifactId>
            <version>1.16.1version>
        dependency>


        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>com.github.binarywanggroupId>
            <artifactId>wx-java-mp-spring-boot-starterartifactId>
            <version>4.5.3.Bversion>
        dependency>


        <dependency>
            <groupId>com.github.jedis-lockgroupId>
            <artifactId>jedis-lockartifactId>
            <version>1.0.0version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>

        
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-boot-starterartifactId>
            <version>${swagger.version}version>
            <exclusions>
                <exclusion>
                    <groupId>io.swaggergroupId>
                    <artifactId>swagger-modelsartifactId>
                exclusion>
            exclusions>
        dependency>

        
        <dependency>
            <groupId>io.swaggergroupId>
            <artifactId>swagger-modelsartifactId>
            <version>1.6.2version>
        dependency>

    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            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("  ");
    }

你可能感兴趣的:(Java项目,微信,学习)