首页最新文章-ajax缓存分页设计

最后发布时间:2023-07-16 17:52:30 浏览量:

生信小木屋

生信小木屋

ComponentsServiceImpl

else if(components.getDataName().startsWith(CmsConst.ARTICLE_DATA_SORT_SIZE)){
                map = getModelPageSize(components,0,5,"DESC");
                return map;
}
    @Override
    public Map<String ,Object> getModelPageSize(Components components, Integer page, Integer size,String order) {
        TemplateUtil.deleteFile("html/components/"+components.getId());
        Map<String,Object> map = new HashMap<>();
        String args = components.getDataName().substring(CmsConst.ARTICLE_DATA_SORT_SIZE.length());
        if(args==null||"".equals(args)){
            throw new ObjectException("数据参数不能为空!!");
        }
        String[] argsArray = args.split(",");
        List<String> argLists = Arrays.asList(argsArray);
//        int size=5;
        Set<String> sortStr = new HashSet<>();
        for (String arg : argLists){
            if(arg.startsWith("size_")){
                size = Integer.parseInt(arg.replace("size_", ""));
            }else  if(arg.startsWith("order_")){
                order = arg.replace("order_", "");

            }else if(arg.startsWith("sort_")){
                String sort_ = arg.replace("sort_", "");
                sortStr.add(sort_);
            }
        }
//        String orderSort = sortStr.stream()
//                .collect(Collectors.joining(","))+","+direction.name();



        ArticlePageCondition articlePageCondition= articleService.pagePublishBy(components.getId(),sortStr,order,page, size);
        Page<Article> articles =articlePageCondition.getArticles();
        Page<ArticleVO> articleVOS = articleService.convertToPageVo(articles);
//                Map<String,Object> map = new HashMap<>();
        map.put("view",articleVOS);
        map.put("info",articlePageCondition);
//        map.put("showUrl","/articleList?sort="+orderSort); //likes,DESC
        map.put("name",components.getName());
        map.put("componentIds",components.getId());
        map.put("url","component_"+components.getId()+",category_"+
                Joiner.on(",").join(articlePageCondition.getIds())+
                ",sort_"+Joiner.on(",").join(articlePageCondition.getSortStr())+
                ",order_"+articlePageCondition.getOrder()+
                ",page_"+(articlePageCondition.getPage()+ 1)+
                ",size_"+articlePageCondition.getSize());

        return map;
    }

生信小木屋

  function loadList(path) {
            // console.log(path)
            id = path.split(",")[0]
            // console.log()
            var url = location.hostname;
            var protocol = window.location.protocol;
            var port = window.location.port
            let address = protocol + "//" + url + ":" + port + "/" + path
            $.ajax({
                url: address,
                headers: {
                    'Content-Type': 'text/html;charset=utf8',
                    // 'Accept': 'application/text'
                },
                type: 'GET',
                success: function (data) {
                    html = $(data).find(".list-group").children()



                    $("#" + id + '-list').append(html)

                    loadBtn = $(data).find("#" + id + '-btn').children()
                    $("#" + id + '-btn').html(loadBtn)
                    flag = true
                    // console.log(loadBtn)
                }, error: function (data) {
                    res = JSON.parse(data.responseText)
                    // console.log(res.message)
                    $("#" + id + '-btn').html(res.message)
                }
            });
        }


        $(window).scroll(function () {
            var _target = $(document).height() - $(window).height() - $(window).scrollTop()
            // console.log(_target)
            if (_target < 400 && flag) {
                flag = false
                url = $(".tab-pane.fade.active.show .load-id").data("url")
                console.log(url)
                $("img.lazy").lazyload({ effect: "fadeIn", threshold: 500 });
                loadList(url)
            }

        });

WebController

    @GetMapping(value = "/component_{id},category_{ids},sort_{sort},order_{order},page_{page},size_{size}",produces={"text/html;charset=UTF-8;","application/json;"})
    @Anonymous
    @ResponseBody
    public String articlePageCondition(@PathVariable("id") Integer componentId,
                                    @PathVariable("ids") String categoryIds,
                                    @PathVariable("sort") String sort,
                                    @PathVariable("order") String order,
                                    @PathVariable("page") Integer page,
                                    @PathVariable("size") Integer size){
//        response.setContentType("");
        Set<Integer> ids= new HashSet<>();
        String[] idsSplit = categoryIds.split(",");
        for(String i : idsSplit){
            ids.add(Integer.parseInt(i));
        }
        String[] sortSplit = sort.split(",");
        Set<String>  sortStr= new HashSet<>();
        sortStr.addAll(Arrays.asList(sortSplit));

        String html = htmlService.articlePageCondition(componentId, ids, sortStr, order, page, size);
//        TemplateUtil.saveFile(path,viewName,html);
//        Map<String,String> map = new HashMap<>();
//        map.put("html",html);
//        map.put("url",html);

        return html;

    }
  public String articlePageCondition(Integer componentId, Set<Integer> ids, Set<String> sortStr, String order, Integer page, Integer size){
        String url = "component_"+componentId+",category_"+Joiner.on(",").join(ids)+",sort_"+Joiner.on(",").join(sortStr)+",order_"+order+",page_"+(page+1)+",size_"+size;
        if(TemplateUtil.checkFileExist("html/components/"+componentId,url)){
            return TemplateUtil.openFile("html/components/"+componentId,url);
        }

        Components components = componentsService.findById(componentId);
        Map<String,Object> map = new HashMap<>();
        ArticlePageCondition articlePageCondition = articleService.pagePublishBy(ids, sortStr, order, page, size);
        Page<Article> articles =articlePageCondition.getArticles();
        Page<ArticleVO> articleVOS = articleService.convertToPageVo(articles);
        if(articleVOS.getContent().size()==0){
            throw new ObjectException("没有数据!!");
        }
//                Map<String,Object> map = new HashMap<>();

        map.put("view",articleVOS);
        map.put("info",articlePageCondition);

        map.put("url",url);
//        map.put("showUrl","/articleList?sort="+orderSort); //likes,DESC
//        TemplateUtil.convertHtmlAndSave(category.getPath()+CMSUtils.getArticleRecommendPath(),category.getViewName(),map,template);
        Context context = new Context();
        context.setVariables(map);
        String html = TemplateUtil.getHtml(components.getTemplateValue(),context);
        TemplateUtil.saveFile("html/components/"+componentId,url,html);
        return html;
    }