diff --git a/.gitignore b/.gitignore index 1a93568..de81008 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ **/target **/*.iml **/.DS_Store - +/templates/app/ diff --git a/novel-common/src/main/java/com/java2nb/novel/core/enums/ResponseStatus.java b/novel-common/src/main/java/com/java2nb/novel/core/enums/ResponseStatus.java index 97ad943..1c90576 100644 --- a/novel-common/src/main/java/com/java2nb/novel/core/enums/ResponseStatus.java +++ b/novel-common/src/main/java/com/java2nb/novel/core/enums/ResponseStatus.java @@ -44,6 +44,8 @@ public enum ResponseStatus implements IResultCode { * 小说相关错误 */ BOOK_EXISTS(5001,"该小说已存在"), + INDEX_NOT_EXISTS(5002,"该章节不存在!"), + NEWS_NOT_EXISTS(5003,"该新闻不存在!"), /** * 书单相关错误 diff --git a/novel-front/src/main/java/com/java2nb/novel/controller/BookController.java b/novel-front/src/main/java/com/java2nb/novel/controller/BookController.java index d686663..ac8e56f 100644 --- a/novel-front/src/main/java/com/java2nb/novel/controller/BookController.java +++ b/novel-front/src/main/java/com/java2nb/novel/controller/BookController.java @@ -9,6 +9,7 @@ import com.java2nb.novel.service.BookParagraphCommentService; import com.java2nb.novel.service.BookService; import com.java2nb.novel.service.IpLocationService; import com.java2nb.novel.service.LikeService; +import com.java2nb.novel.service.UserService; import com.java2nb.novel.vo.*; import io.github.xxyopen.model.page.PageBean; import io.github.xxyopen.model.page.builder.pagehelper.PageBuilder; @@ -42,6 +43,8 @@ public class BookController extends BaseController { private final BookParagraphCommentService bookParagraphCommentService; + private final UserService userService; + /** * 查询首页小说设置列表数据 */ @@ -143,6 +146,53 @@ public class BookController extends BaseController { return RestResult.ok(bookService.listRecBookByCatId(catId)); } + /** + * 读者端查询章节正文(供跨端客户端使用) + */ + @GetMapping("queryBookIndexContent/{indexId}") + public RestResult queryBookIndexContent(@PathVariable("indexId") Long indexId, + HttpServletRequest request) { + BookIndex bookIndex; + try { + // queryBookIndex 对不存在的章节会抛异常,统一转为业务错误码 + bookIndex = bookService.queryBookIndex(indexId); + } catch (RuntimeException e) { + bookIndex = null; + } + if (bookIndex == null) { + return RestResult.fail(ResponseStatus.INDEX_NOT_EXISTS); + } + Long bookId = bookIndex.getBookId(); + // 判断是否需要购买(与页面端渲染逻辑保持一致) + boolean needBuy = false; + if (bookIndex.getIsVip() != null && bookIndex.getIsVip() == 1) { + UserDetails user = getUserDetails(request); + if (user == null) { + // 未登录,需要购买 + needBuy = true; + } else { + // 判断用户是否购买过该目录 + needBuy = !userService.queryIsBuyBookIndex(user.getId(), indexId); + } + } + Map data = new HashMap<>(10); + data.put("bookId", bookId); + data.put("indexId", indexId); + data.put("indexNum", bookIndex.getIndexNum()); + data.put("indexName", bookIndex.getIndexName()); + data.put("isVip", bookIndex.getIsVip()); + data.put("bookPrice", bookIndex.getBookPrice()); + data.put("preBookIndexId", bookService.queryPreBookIndexId(bookId, bookIndex.getIndexNum())); + data.put("nextBookIndexId", bookService.queryNextBookIndexId(bookId, bookIndex.getIndexNum())); + data.put("needBuy", needBuy); + if (!needBuy) { + BookContent bookContent = bookContentServiceMap.get(bookIndex.getStorageType()) + .queryBookContent(bookId, indexId); + data.put("content", bookContent == null ? null : bookContent.getContent()); + } + return RestResult.ok(data); + } + /** * 分页查询书籍评论列表 diff --git a/novel-front/src/main/java/com/java2nb/novel/controller/NewsController.java b/novel-front/src/main/java/com/java2nb/novel/controller/NewsController.java index 2e97b0a..504aae3 100644 --- a/novel-front/src/main/java/com/java2nb/novel/controller/NewsController.java +++ b/novel-front/src/main/java/com/java2nb/novel/controller/NewsController.java @@ -2,6 +2,7 @@ package com.java2nb.novel.controller; import com.github.pagehelper.PageInfo; import io.github.xxyopen.model.page.PageBean; +import com.java2nb.novel.core.enums.ResponseStatus; import com.java2nb.novel.entity.News; import com.java2nb.novel.service.NewsService; import io.github.xxyopen.model.resp.RestResult; @@ -38,6 +39,24 @@ public class NewsController { return RestResult.ok(newsService.listByPage(page,pageSize)); } + /** + * 查询新闻详情(含正文,供跨端客户端使用) + * */ + @GetMapping("detail/{newsId}") + public RestResult detail(@PathVariable("newsId") Long newsId){ + News news; + try { + // queryNewsInfo 对不存在的新闻会抛异常,统一转为业务错误码 + news = newsService.queryNewsInfo(newsId); + } catch (RuntimeException e) { + news = null; + } + if (news == null) { + return RestResult.fail(ResponseStatus.NEWS_NOT_EXISTS); + } + return RestResult.ok(news); + } + /** * 增加新闻阅读量 * */ diff --git a/novel-front/src/main/java/com/java2nb/novel/core/config/WebMvcConfig.java b/novel-front/src/main/java/com/java2nb/novel/core/config/WebMvcConfig.java index 2d36783..190665e 100644 --- a/novel-front/src/main/java/com/java2nb/novel/core/config/WebMvcConfig.java +++ b/novel-front/src/main/java/com/java2nb/novel/core/config/WebMvcConfig.java @@ -3,6 +3,7 @@ package com.java2nb.novel.core.config; import com.java2nb.novel.core.converter.DateConverter; import org.springframework.context.annotation.Configuration; import org.springframework.format.FormatterRegistry; +import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** @@ -14,4 +15,17 @@ public class WebMvcConfig implements WebMvcConfigurer { public void addFormatters(FormatterRegistry registry) { registry.addConverter(new DateConverter()); } + + /** + * 跨域配置:供 uni-app 跨端客户端(H5 开发调试/独立部署)直连 REST 接口 + */ + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOriginPatterns("*") + .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") + .allowedHeaders("*") + .allowCredentials(true) + .maxAge(3600); + } } diff --git a/novel-front/src/main/java/com/java2nb/novel/service/impl/NewsServiceImpl.java b/novel-front/src/main/java/com/java2nb/novel/service/impl/NewsServiceImpl.java index 8079c74..4c0c2e2 100644 --- a/novel-front/src/main/java/com/java2nb/novel/service/impl/NewsServiceImpl.java +++ b/novel-front/src/main/java/com/java2nb/novel/service/impl/NewsServiceImpl.java @@ -64,7 +64,7 @@ public class NewsServiceImpl implements NewsService { @Override public PageBean listByPage(int page, int pageSize) { PageHelper.startPage(page, pageSize); - SelectStatementProvider selectStatement = select(id, catName, catId, title, createTime) + SelectStatementProvider selectStatement = select(id, catName, catId, title, readCount, createTime) .from(news) .orderBy(createTime.descending()) .build() diff --git a/novel-front/src/main/resources/mybatis/mapping/FrontUserBookListMapper.xml b/novel-front/src/main/resources/mybatis/mapping/FrontUserBookListMapper.xml index 4a30c2f..911b5ac 100644 --- a/novel-front/src/main/resources/mybatis/mapping/FrontUserBookListMapper.xml +++ b/novel-front/src/main/resources/mybatis/mapping/FrontUserBookListMapper.xml @@ -32,7 +32,7 @@ select t1.id, t1.user_id, t1.list_name, t1.description, t1.book_count, t1.status, - t1.create_time, t1.update_time, t2.username create_user_name, t2.user_photo create_user_photo + t1.create_time, t1.update_time, IFNULL(NULLIF(t2.nick_name, ''), t2.username) create_user_name, t2.user_photo create_user_photo from user_book_list t1 inner join user t2 on t1.user_id = t2.id where t1.user_id = #{userId} @@ -61,7 +61,7 @@