【已解决】 管理中心文章文章条件查询返回数据不一致?
管理中心文章条件查询 返回到后端的数据不一致
有存在文章分类/关键字/状态时 返回为 data.content

在没有分类/关键字/状态时返回为 contents

contents和content不一样时 总有部分功能无法实现,而在康师傅的课件里时可以都实现的。
疑问1:在后端使其格式统一?
疑问2:在前端实现某种提取?


/**
* 管理中心 获取文章
*
* @param page 页码
* @param size 每一页数量
* @param state 状态:已删除 草稿 已发布 置顶
* @param keyword 挑剔关键字 搜索
* @param categoryId 分类ID
* @return
*/
@Override
public ResponseResult listArticles(int page, int size,
String keyword, String categoryId, String state) {
// 处理一下size page
page = checkPage(page);
size = checkSize(size);
// 第一页内容做缓存
String articleListJson = (String) redisUtils.get(Constants.Article.KEY_ARTICLE_LIST_FIRST_PAGE);
boolean isSearch = !TextUtils.isEmpty(keyword) || !TextUtils.isEmpty(categoryId) || !TextUtils.isEmpty(state);
if (!TextUtils.isEmpty(articleListJson) && page == 1 && !isSearch) {
PageList<ArticleNoContent> result = gson.fromJson(articleListJson, new TypeToken<PageList<ArticleNoContent>>() {
}.getType());
log.info("article list first page from redis");
return ResponseResult.SUCCESS("获取文章列表成功").setData(result);
}
// 创建分页和排序
Sort sort = new Sort(Sort.Direction.DESC, "createTime");
Pageable pageable = PageRequest.of(page - 1, size, sort);
// 开始查询
Page<ArticleNoContent> all = articleNoContentDao.findAll(new Specification<ArticleNoContent>() {
@Override
public Predicate toPredicate(Root<ArticleNoContent> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicates = new ArrayList<>();
if (!TextUtils.isEmpty(state)) {
Predicate statePre = criteriaBuilder.equal(root.get("state").as(String.class), state);
predicates.add(statePre);
}
if (!TextUtils.isEmpty(categoryId)) {
Predicate categoryPre = criteriaBuilder.equal(root.get("categoryId").as(String.class), categoryId);
predicates.add(categoryPre);
}
if (!TextUtils.isEmpty(keyword)) {
Predicate titlePre = criteriaBuilder.like(root.get("title").as(String.class), "%" + keyword + "%");
predicates.add(titlePre);
}
Predicate[] preArray = new Predicate[predicates.size()];
predicates.toArray(preArray);
return criteriaBuilder.and(preArray);
}
}, pageable);
// 处理查询条件
PageList<ArticleNoContent> result = new PageList<>();
// 解析page
result.parsePage(all);
// 保存到redis
if (page == 1 && !isSearch) {
redisUtils.set(Constants.Article.KEY_ARTICLE_LIST_FIRST_PAGE, gson.toJson(result), Constants.TimeValueInSecond.MIN_15);
}
return ResponseResult.SUCCESS("获取列表成功").setData(all);
}
改成这样子
一个是contents,一个是content.
contents是page的结果
content应该是我们自定义的一个返回结果。
你把这个接口对应的service代码,补充到问题里吧。如果看不懂的话。