mirror of
https://github.com/201206030/novel-plus.git
synced 2025-07-02 23:46:38 +00:00
小说内容页性能优化
This commit is contained in:
@ -19,7 +19,10 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
@ -37,6 +40,8 @@ public class PageController extends BaseController {
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
private final ThreadPoolExecutor threadPoolExecutor;
|
||||
|
||||
|
||||
@RequestMapping("{url}.html")
|
||||
public String module(@PathVariable("url") String url) {
|
||||
@ -44,9 +49,9 @@ public class PageController extends BaseController {
|
||||
}
|
||||
|
||||
@RequestMapping("{module}/{url}.html")
|
||||
public String module2(@PathVariable("module") String module, @PathVariable("url") String url,HttpServletRequest request) {
|
||||
public String module2(@PathVariable("module") String module, @PathVariable("url") String url, HttpServletRequest request) {
|
||||
|
||||
if(request.getRequestURI().startsWith("/author")) {
|
||||
if (request.getRequestURI().startsWith("/author")) {
|
||||
//访问作者专区
|
||||
UserDetails user = getUserDetails(request);
|
||||
if (user == null) {
|
||||
@ -56,7 +61,7 @@ public class PageController extends BaseController {
|
||||
|
||||
boolean isAuthor = authorService.isAuthor(user.getId());
|
||||
if (!isAuthor) {
|
||||
return "redirect:/author/register.html" ;
|
||||
return "redirect:/author/register.html";
|
||||
}
|
||||
}
|
||||
|
||||
@ -70,15 +75,15 @@ public class PageController extends BaseController {
|
||||
|
||||
/**
|
||||
* 首页
|
||||
* */
|
||||
*/
|
||||
@RequestMapping(path = {"/", "/index", "/index.html"})
|
||||
public String index() {
|
||||
return ThreadLocalUtil.getTemplateDir()+"index";
|
||||
return ThreadLocalUtil.getTemplateDir() + "index";
|
||||
}
|
||||
|
||||
/**
|
||||
* 作品页
|
||||
* */
|
||||
*/
|
||||
@RequestMapping("book/bookclass.html")
|
||||
public String bookClass() {
|
||||
return "book/bookclass";
|
||||
@ -86,129 +91,170 @@ public class PageController extends BaseController {
|
||||
|
||||
/**
|
||||
* 排行页
|
||||
* */
|
||||
*/
|
||||
@RequestMapping("book/book_ranking.html")
|
||||
public String bookRank() {
|
||||
|
||||
return ThreadLocalUtil.getTemplateDir()+"book/book_ranking";
|
||||
return ThreadLocalUtil.getTemplateDir() + "book/book_ranking";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 详情页
|
||||
* */
|
||||
*/
|
||||
@SneakyThrows
|
||||
@RequestMapping("/book/{bookId}.html")
|
||||
public String bookDetail(@PathVariable("bookId") Long bookId, Model model) {
|
||||
Book book = bookService.queryBookDetail(bookId);
|
||||
model.addAttribute("book",book);
|
||||
if(book.getLastIndexId() != null) {
|
||||
model.addAttribute("book", book);
|
||||
if (book.getLastIndexId() != null) {
|
||||
//查询首章目录ID
|
||||
Long firstBookIndexId = bookService.queryFirstBookIndexId(bookId);
|
||||
model.addAttribute("firstBookIndexId", firstBookIndexId);
|
||||
}
|
||||
return ThreadLocalUtil.getTemplateDir()+"book/book_detail";
|
||||
return ThreadLocalUtil.getTemplateDir() + "book/book_detail";
|
||||
}
|
||||
|
||||
/**
|
||||
* 目录页
|
||||
* */
|
||||
*/
|
||||
@SneakyThrows
|
||||
@RequestMapping("/book/indexList-{bookId}.html")
|
||||
public String indexList(@PathVariable("bookId") Long bookId, Model model) {
|
||||
Book book = bookService.queryBookDetail(bookId);
|
||||
model.addAttribute("book",book);
|
||||
List<BookIndex> bookIndexList = bookService.queryIndexList(bookId,null,1,null);
|
||||
model.addAttribute("bookIndexList",bookIndexList);
|
||||
model.addAttribute("bookIndexCount",bookIndexList.size());
|
||||
return ThreadLocalUtil.getTemplateDir()+"book/book_index";
|
||||
model.addAttribute("book", book);
|
||||
List<BookIndex> bookIndexList = bookService.queryIndexList(bookId, null, 1, null);
|
||||
model.addAttribute("bookIndexList", bookIndexList);
|
||||
model.addAttribute("bookIndexCount", bookIndexList.size());
|
||||
return ThreadLocalUtil.getTemplateDir() + "book/book_index";
|
||||
}
|
||||
|
||||
/**
|
||||
* 内容页
|
||||
* */
|
||||
*/
|
||||
@SneakyThrows
|
||||
@RequestMapping("/book/{bookId}/{bookIndexId}.html")
|
||||
public String indexList(@PathVariable("bookId") Long bookId,@PathVariable("bookIndexId") Long bookIndexId, HttpServletRequest request, Model model) {
|
||||
//查询书籍
|
||||
Book book = bookService.queryBookDetail(bookId);
|
||||
//查询目录
|
||||
BookIndex bookIndex = bookService.queryBookIndex(bookIndexId);
|
||||
model.addAttribute("book",book);
|
||||
model.addAttribute("bookIndex",bookIndex);
|
||||
//查询上一章节目录ID
|
||||
Long preBookIndexId = bookService.queryPreBookIndexId(bookId,bookIndex.getIndexNum());
|
||||
model.addAttribute("preBookIndexId",preBookIndexId);
|
||||
//查询下一章目录ID
|
||||
Long nextBookIndexId = bookService.queryNextBookIndexId(bookId,bookIndex.getIndexNum());
|
||||
model.addAttribute("nextBookIndexId",nextBookIndexId);
|
||||
//查询内容
|
||||
BookContent bookContent = bookService.queryBookContent(bookIndex.getId());
|
||||
model.addAttribute("bookContent",bookContent);
|
||||
public String indexList(@PathVariable("bookId") Long bookId, @PathVariable("bookIndexId") Long bookIndexId, HttpServletRequest request, Model model) {
|
||||
|
||||
boolean needBuy = false;
|
||||
//判断该目录是否收费
|
||||
if(bookIndex.getIsVip()!=null && bookIndex.getIsVip() == 1 ){
|
||||
UserDetails user = getUserDetails(request);
|
||||
if(user == null){
|
||||
//未登录
|
||||
return "redirect:/user/login.html?originUrl="+request.getRequestURI();
|
||||
|
||||
//加载小说基本信息线程
|
||||
CompletableFuture<Book> bookCompletableFuture = CompletableFuture.supplyAsync(() -> {
|
||||
//查询书籍
|
||||
Book book = bookService.queryBookDetail(bookId);
|
||||
log.debug("加载小说基本信息线程结束");
|
||||
return book;
|
||||
}, threadPoolExecutor);
|
||||
|
||||
//加载小说章节信息线程
|
||||
CompletableFuture<BookIndex> bookIndexCompletableFuture = CompletableFuture.supplyAsync(() -> {
|
||||
//查询目录
|
||||
BookIndex bookIndex = bookService.queryBookIndex(bookIndexId);
|
||||
log.debug("加载小说章节信息线程结束");
|
||||
return bookIndex;
|
||||
}, threadPoolExecutor);
|
||||
|
||||
//加载小说上一章节信息线程,该线程在加载小说章节信息线程执行完毕后才执行
|
||||
CompletableFuture<Long> preBookIndexIdCompletableFuture = bookIndexCompletableFuture.thenApplyAsync((bookIndex) -> {
|
||||
//查询上一章节目录ID
|
||||
Long preBookIndexId = bookService.queryPreBookIndexId(bookId, bookIndex.getIndexNum());
|
||||
log.debug("加载小说上一章节信息线程结束");
|
||||
return preBookIndexId;
|
||||
}, threadPoolExecutor);
|
||||
|
||||
//加载小说下一章节信息线程,该线程在加载小说章节信息线程执行完毕后才执行
|
||||
CompletableFuture<Long> nextBookIndexIdCompletableFuture = bookIndexCompletableFuture.thenApplyAsync((bookIndex) -> {
|
||||
//查询下一章目录ID
|
||||
Long nextBookIndexId = bookService.queryNextBookIndexId(bookId, bookIndex.getIndexNum());
|
||||
log.debug("加载小说下一章节信息线程结束");
|
||||
return nextBookIndexId;
|
||||
}, threadPoolExecutor);
|
||||
|
||||
//加载小说内容信息线程
|
||||
CompletableFuture<BookContent> bookContentCompletableFuture = CompletableFuture.supplyAsync(() -> {
|
||||
//查询内容
|
||||
BookContent bookContent = bookService.queryBookContent(bookIndexId);
|
||||
log.debug("加载小说内容信息线程结束");
|
||||
return bookContent;
|
||||
}, threadPoolExecutor);
|
||||
|
||||
|
||||
//判断用户是否需要购买线程,该线程在加载小说章节信息线程执行完毕后才执行
|
||||
CompletableFuture<Boolean> needBuyCompletableFuture = bookIndexCompletableFuture.thenApplyAsync((bookIndex) -> {
|
||||
//判断该目录是否收费
|
||||
if (bookIndex.getIsVip() != null && bookIndex.getIsVip() == 1) {
|
||||
//收费
|
||||
UserDetails user = getUserDetails(request);
|
||||
if (user == null) {
|
||||
//未登录,需要购买
|
||||
return true;
|
||||
}
|
||||
//判断用户是否购买过该目录
|
||||
boolean isBuy = userService.queryIsBuyBookIndex(user.getId(), bookIndexId);
|
||||
if (!isBuy) {
|
||||
//没有购买过,需要购买
|
||||
return true;
|
||||
}
|
||||
}
|
||||
//收费,判断用户是否购买过该目录
|
||||
boolean isBuy = userService.queryIsBuyBookIndex(user.getId(),bookIndexId);
|
||||
if(!isBuy){
|
||||
//没有购买过,需要购买
|
||||
bookContent.setContent(null);
|
||||
needBuy = true;
|
||||
}
|
||||
}
|
||||
model.addAttribute("needBuy",needBuy);
|
||||
return ThreadLocalUtil.getTemplateDir()+"book/book_content";
|
||||
|
||||
log.debug("判断用户是否需要购买线程结束");
|
||||
return false;
|
||||
|
||||
}, threadPoolExecutor);
|
||||
|
||||
|
||||
model.addAttribute("book", bookCompletableFuture.get());
|
||||
model.addAttribute("bookIndex", bookIndexCompletableFuture.get());
|
||||
model.addAttribute("preBookIndexId", preBookIndexIdCompletableFuture.get());
|
||||
model.addAttribute("nextBookIndexId", nextBookIndexIdCompletableFuture.get());
|
||||
model.addAttribute("bookContent", bookContentCompletableFuture.get());
|
||||
model.addAttribute("needBuy", needBuyCompletableFuture.get());
|
||||
|
||||
return ThreadLocalUtil.getTemplateDir() + "book/book_content";
|
||||
}
|
||||
|
||||
/**
|
||||
* 评论页面
|
||||
* */
|
||||
*/
|
||||
@RequestMapping("/book/comment-{bookId}.html")
|
||||
public String commentList(@PathVariable("bookId") Long bookId, Model model) {
|
||||
//查询书籍
|
||||
Book book = bookService.queryBookDetail(bookId);
|
||||
model.addAttribute("book",book);
|
||||
model.addAttribute("book", book);
|
||||
return "book/book_comment";
|
||||
}
|
||||
|
||||
/**
|
||||
* 新闻内容页面
|
||||
* */
|
||||
*/
|
||||
@RequestMapping("/about/newsInfo-{newsId}.html")
|
||||
public String newsInfo(@PathVariable("newsId") Long newsId, Model model) {
|
||||
//查询新闻
|
||||
News news = newsService.queryNewsInfo(newsId);
|
||||
model.addAttribute("news",news);
|
||||
model.addAttribute("news", news);
|
||||
return "about/news_info";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 作者注册页面
|
||||
* */
|
||||
*/
|
||||
@RequestMapping("author/register.html")
|
||||
public String authorRegister(Author author, HttpServletRequest request, Model model){
|
||||
public String authorRegister(Author author, HttpServletRequest request, Model model) {
|
||||
UserDetails user = getUserDetails(request);
|
||||
if(user == null){
|
||||
if (user == null) {
|
||||
//未登录
|
||||
return "redirect:/user/login.html?originUrl=/author/register.html";
|
||||
}
|
||||
|
||||
if(StringUtils.isNotBlank(author.getInviteCode())) {
|
||||
if (StringUtils.isNotBlank(author.getInviteCode())) {
|
||||
//提交作者注册信息
|
||||
String errorInfo = authorService.register(user.getId(), author);
|
||||
if(StringUtils.isBlank(errorInfo)){
|
||||
if (StringUtils.isBlank(errorInfo)) {
|
||||
//注册成功
|
||||
return "redirect:/author/index.html";
|
||||
}
|
||||
model.addAttribute("LabErr",errorInfo);
|
||||
model.addAttribute("author",author);
|
||||
model.addAttribute("LabErr", errorInfo);
|
||||
model.addAttribute("author", author);
|
||||
}
|
||||
return "author/register";
|
||||
}
|
||||
|
Reference in New Issue
Block a user