diff --git a/novel-front/src/main/java/xyz/zinglizingli/books/enums/PicSaveType.java b/novel-front/src/main/java/xyz/zinglizingli/books/enums/PicSaveType.java new file mode 100644 index 0000000..99fc1d0 --- /dev/null +++ b/novel-front/src/main/java/xyz/zinglizingli/books/enums/PicSaveType.java @@ -0,0 +1,27 @@ +package xyz.zinglizingli.books.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +/** + * 图片保存类型 + * @author 11797 + */ + +@Getter +@AllArgsConstructor +@NoArgsConstructor +public enum PicSaveType { + /** + * 使用网络图片,不保存 + * */ + NETWORK(1), + + /** + * 本地保存 + * */ + LOCAL(2); + + private int value; +} diff --git a/novel-front/src/main/java/xyz/zinglizingli/books/service/BookService.java b/novel-front/src/main/java/xyz/zinglizingli/books/service/BookService.java index d5902a4..db2097b 100644 --- a/novel-front/src/main/java/xyz/zinglizingli/books/service/BookService.java +++ b/novel-front/src/main/java/xyz/zinglizingli/books/service/BookService.java @@ -16,6 +16,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import tk.mybatis.orderbyhelper.OrderByHelper; import xyz.zinglizingli.books.constant.CacheKeyConstans; +import xyz.zinglizingli.books.enums.PicSaveType; import xyz.zinglizingli.books.mapper.*; import xyz.zinglizingli.books.po.*; import xyz.zinglizingli.books.util.UUIDUtils; @@ -27,6 +28,7 @@ import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.*; +import java.util.stream.Collectors; /** * @author XXY @@ -49,7 +51,7 @@ public class BookService { private final CommonCacheUtil cacheUtil; @Value("${pic.save.type}") - private Byte picSaveType; + private Integer picSaveType; @Value("${pic.save.path}") private String picSavePath; @@ -71,45 +73,15 @@ public class BookService { if (books.size() > 0) { //更新 bookId = books.get(0).getId(); - book.setId(bookId); - String picSrc = book.getPicUrl(); - if(picSaveType == 2 && StringUtils.isNotBlank(picSrc)){ - try { - HttpHeaders headers = new HttpHeaders(); - HttpEntity requestEntity = new HttpEntity<>(null, headers); - ResponseEntity resEntity = RestTemplateUtil.getInstance(Charsets.ISO_8859_1).exchange(picSrc, HttpMethod.GET, requestEntity, Resource.class); - InputStream input = Objects.requireNonNull(resEntity.getBody()).getInputStream(); - Date currentDate = new Date(); - picSrc = "/localPic/" + DateUtils.formatDate(currentDate, "yyyy") + "/" + DateUtils.formatDate(currentDate, "MM") + "/" + DateUtils.formatDate(currentDate, "dd") - + UUIDUtils.getUUID32() - + picSrc.substring(picSrc.lastIndexOf(".")); - File picFile = new File(picSavePath + picSrc); - File parentFile = picFile.getParentFile(); - if (!parentFile.exists()) { - parentFile.mkdirs(); - } - OutputStream out = new FileOutputStream(picFile); - byte[] b = new byte[4096]; - for (int n; (n = input.read(b)) != -1; ) { - out.write(b, 0, n); - } - out.close(); - input.close(); - book.setPicUrl(picSrc); - }catch (Exception e){ - log.error(e.getMessage(),e); - } - - } - bookMapper.updateByPrimaryKeySelective(book); + updateBook(book, bookId); isUpdate = true; } else { + //插入 if (book.getVisitCount() == null) { - Long visitCount = generateVisiteCount(book.getScore()); + Long visitCount = generateVisitCount(book.getScore()); book.setVisitCount(visitCount); } - //插入 int rows = bookMapper.insertSelective(book); if (rows > 0) { bookId = book.getId(); @@ -159,6 +131,44 @@ public class BookService { } + /** + * 更新书籍 + * */ + private void updateBook(Book book, Long bookId) { + book.setId(bookId); + String picSrc = book.getPicUrl(); + if(picSaveType == PicSaveType.LOCAL.getValue() && StringUtils.isNotBlank(picSrc)){ + try { + //本地图片保存 + HttpHeaders headers = new HttpHeaders(); + HttpEntity requestEntity = new HttpEntity<>(null, headers); + ResponseEntity resEntity = RestTemplateUtil.getInstance(Charsets.ISO_8859_1).exchange(picSrc, HttpMethod.GET, requestEntity, Resource.class); + InputStream input = Objects.requireNonNull(resEntity.getBody()).getInputStream(); + Date currentDate = new Date(); + picSrc = "/localPic/" + DateUtils.formatDate(currentDate, "yyyy") + "/" + DateUtils.formatDate(currentDate, "MM") + "/" + DateUtils.formatDate(currentDate, "dd") + + UUIDUtils.getUUID32() + + picSrc.substring(picSrc.lastIndexOf(".")); + File picFile = new File(picSavePath + picSrc); + File parentFile = picFile.getParentFile(); + if (!parentFile.exists()) { + parentFile.mkdirs(); + } + OutputStream out = new FileOutputStream(picFile); + byte[] b = new byte[4096]; + for (int n; (n = input.read(b)) != -1; ) { + out.write(b, 0, n); + } + out.close(); + input.close(); + book.setPicUrl(picSrc); + }catch (Exception e){ + log.error(e.getMessage(),e); + } + + } + bookMapper.updateByPrimaryKeySelective(book); + } + /** * 批量插入章节目录表和章节内容表 * */ @@ -172,7 +182,7 @@ public class BookService { /** * 生成随机访问次数 * */ - private Long generateVisiteCount(Float score) { + private Long generateVisitCount(Float score) { int baseNum = (int)(score * 100); return Long.parseLong(baseNum + new Random().nextInt(1000) + ""); } @@ -340,10 +350,9 @@ public class BookService { } /** - * 查询该书籍目录数量 + * 查询该书籍已存在目录号 */ - public List queryIndexCountByBookNameAndAuthor(String bookName, String author) { - List result = new ArrayList<>(); + public List queryIndexNumByBookNameAndAuthor(String bookName, String author) { BookExample example = new BookExample(); example.createCriteria().andBookNameEqualTo(bookName).andAuthorEqualTo(author); List books = bookMapper.selectByExample(example); @@ -353,15 +362,13 @@ public class BookService { BookIndexExample bookIndexExample = new BookIndexExample(); bookIndexExample.createCriteria().andBookIdEqualTo(bookId); List bookIndices = bookIndexMapper.selectByExample(bookIndexExample); - if (bookIndices != null && bookIndices.size() > 0) { - for (BookIndex bookIndex : bookIndices) { - result.add(bookIndex.getIndexNum()); - } + if(bookIndices.size()>0) { + return bookIndices.stream().map(BookIndex::getIndexNum).collect(Collectors.toList()); } } - return result; + return new ArrayList<>(0); } @@ -494,15 +501,6 @@ public class BookService { } - /** - * 查询完本书籍 - * */ - public List queryEndBookIdList() { - return bookMapper.queryEndBookIdList(); - } - - - /** diff --git a/novel-front/src/main/java/xyz/zinglizingli/books/web/BookController.java b/novel-front/src/main/java/xyz/zinglizingli/books/web/BookController.java index e1d08f9..2abdcb2 100644 --- a/novel-front/src/main/java/xyz/zinglizingli/books/web/BookController.java +++ b/novel-front/src/main/java/xyz/zinglizingli/books/web/BookController.java @@ -20,8 +20,8 @@ import xyz.zinglizingli.books.service.BookService; import xyz.zinglizingli.books.service.UserService; import xyz.zinglizingli.books.vo.BookVO; import xyz.zinglizingli.common.cache.CommonCacheUtil; +import xyz.zinglizingli.common.utils.Constants; -import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.OutputStream; @@ -31,6 +31,7 @@ import java.util.*; /** * 小说Controller + * * @author 11797 */ @Controller @@ -46,66 +47,63 @@ public class BookController { private final CommonCacheUtil commonCacheUtil; - /** * 精品小说搜索页 - * */ + */ @RequestMapping("search") public String search(@RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "20") int pageSize, @RequestParam(value = "keyword", required = false) String keyword, @RequestParam(value = "catId", required = false) Integer catId, @RequestParam(value = "historyBookIds", required = false) String ids, @RequestParam(value = "bookStatus", required = false) String bookStatus, @RequestParam(value = "token", required = false) String token, - @RequestParam(value = "sortBy", defaultValue = "update_time") String sortBy, @RequestParam(value = "sort", defaultValue = "DESC") String sort, - HttpServletRequest req, HttpServletResponse resp, ModelMap modelMap) { + @RequestParam(value = "sortBy", defaultValue = "update_time") String sortBy, + @RequestParam(value = "sort", defaultValue = "DESC") String sort, ModelMap modelMap) { String userId = null; String titleType = "最近更新"; if (catId != null) { titleType = bookService.getCatNameById(catId) + "分类频道"; - ; - } else if ("score".equals(sortBy)) { + } else if (Constants.NOVEL_TOP_FIELD.equals(sortBy)) { titleType = "小说排行"; } else if (ids != null) { titleType = "阅读记录"; } else if (token != null) { userId = commonCacheUtil.get(token); titleType = "我的书架"; - } else if (bookStatus != null && bookStatus.contains("完成")) { + } else if (bookStatus != null && bookStatus.contains(Constants.NOVEL_END_TAG)) { titleType = "完本小说"; } else if (keyword != null) { titleType = "搜索"; } modelMap.put("titleType", titleType); List books; - List bookVOList; + List bookVoList; if (StringUtils.isEmpty(ids) || !StringUtils.isEmpty(keyword)) { books = bookService.search(page, pageSize, userId, ids, keyword, bookStatus, catId, null, null, sortBy, sort); - bookVOList = new ArrayList<>(); + bookVoList = new ArrayList<>(); for (Book book : books) { BookVO bookvo = new BookVO(); BeanUtils.copyProperties(book, bookvo); bookvo.setCateName(bookService.getCatNameById(bookvo.getCatid())); - bookVOList.add(bookvo); + bookVoList.add(bookvo); } } else { - if (!ids.contains("-")) { - books = bookService.search(page, 50, userId, ids, keyword, null, catId, null, null, sortBy, sort); + if (!ids.contains(Constants.BOOK_ID_SEPARATOR)) { + books = bookService.search(page, 50, null, ids, keyword, null, catId, null, null, sortBy, sort); List idsArr = Arrays.asList(ids.split(",")); - int length = idsArr.size(); - BookVO[] bookVOArr = new BookVO[books.size()]; + BookVO[] bookVoArr = new BookVO[books.size()]; for (Book book : books) { int index = idsArr.indexOf(book.getId() + ""); BookVO bookvo = new BookVO(); BeanUtils.copyProperties(book, bookvo); bookvo.setCateName(bookService.getCatNameById(bookvo.getCatid())); - bookVOArr[books.size() - index - 1] = bookvo; + bookVoArr[books.size() - index - 1] = bookvo; } - bookVOList = Arrays.asList(bookVOArr); + bookVoList = Arrays.asList(bookVoArr); } else { books = new ArrayList<>(); - bookVOList = new ArrayList<>(); + bookVoList = new ArrayList<>(); } } @@ -114,7 +112,7 @@ public class BookController { modelMap.put("limit", bookPageInfo.getPageSize()); modelMap.put("curr", bookPageInfo.getPageNum()); modelMap.put("total", bookPageInfo.getTotal()); - modelMap.put("books", bookVOList); + modelMap.put("books", bookVoList); modelMap.put("ids", ids); modelMap.put("token", token); modelMap.put("bookStatus", bookStatus); @@ -127,26 +125,31 @@ public class BookController { /** - * 轻小说搜索页 - * */ + * 轻小说或漫画搜索页 + */ @RequestMapping("searchSoftBook.html") public String searchSoftBook(@RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "20") int pageSize, @RequestParam(value = "keyword", required = false) String keyword, @RequestParam(value = "catId", defaultValue = "8") Integer catId, @RequestParam(value = "softCat", required = false) Integer softCat, @RequestParam(value = "bookStatus", required = false) String bookStatus, @RequestParam(value = "softTag", required = false) String softTag, - @RequestParam(value = "sortBy", defaultValue = "update_time") String sortBy, @RequestParam(value = "sort", defaultValue = "DESC") String sort, - HttpServletRequest req, HttpServletResponse resp, ModelMap modelMap) { + @RequestParam(value = "sortBy", defaultValue = "update_time") String sortBy, + @RequestParam(value = "sort", defaultValue = "DESC") String sort, ModelMap modelMap) { - String userId = null; - List books = bookService.search(page, pageSize, userId, null, keyword, bookStatus, catId, softCat, softTag, sortBy, sort); - List bookVOList; - bookVOList = new ArrayList<>(); + List books = bookService.search(page, pageSize, null, null, keyword, bookStatus, catId, softCat, softTag, sortBy, sort); + List bookVoList; + bookVoList = new ArrayList<>(); for (Book book : books) { BookVO bookvo = new BookVO(); BeanUtils.copyProperties(book, bookvo); - bookvo.setCateName(bookService.getSoftCatNameById(bookvo.getSoftCat())); - bookVOList.add(bookvo); + if(catId == Constants.SOFT_NOVEL_CAT) { + //轻小说 + bookvo.setCateName(bookService.getSoftCatNameById(bookvo.getSoftCat())); + }else if(catId == Constants.MH_NOVEL_CAT){ + //漫画 + bookvo.setCateName(bookService.getMhCatNameById(bookvo.getSoftCat())); + } + bookVoList.add(bookvo); } @@ -154,64 +157,34 @@ public class BookController { modelMap.put("limit", bookPageInfo.getPageSize()); modelMap.put("curr", bookPageInfo.getPageNum()); modelMap.put("total", bookPageInfo.getTotal()); - modelMap.put("books", bookVOList); + modelMap.put("books", bookVoList); modelMap.put("keyword", keyword); modelMap.put("bookStatus", bookStatus); modelMap.put("softCat", softCat); modelMap.put("softTag", softTag); modelMap.put("sortBy", sortBy); modelMap.put("sort", sort); - return "books/soft_book_search"; - } - - /** - * 漫画搜索页 - * */ - @RequestMapping("searchMhBook.html") - public String searchMhBook(@RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "20") int pageSize, - @RequestParam(value = "keyword", required = false) String keyword, @RequestParam(value = "catId", defaultValue = "9") Integer catId, - @RequestParam(value = "softCat", required = false) Integer softCat, - @RequestParam(value = "bookStatus", required = false) String bookStatus, - @RequestParam(value = "softTag", required = false) String softTag, - @RequestParam(value = "sortBy", defaultValue = "update_time") String sortBy, @RequestParam(value = "sort", defaultValue = "DESC") String sort, - HttpServletRequest req, HttpServletResponse resp, ModelMap modelMap) { - - String userId = null; - List books = bookService.search(page, pageSize, userId, null, keyword, bookStatus, catId, softCat, softTag, sortBy, sort); - List bookVOList; - bookVOList = new ArrayList<>(); - for (Book book : books) { - BookVO bookvo = new BookVO(); - BeanUtils.copyProperties(book, bookvo); - bookvo.setCateName(bookService.getMhCatNameById(bookvo.getSoftCat())); - bookVOList.add(bookvo); + if(catId == Constants.SOFT_NOVEL_CAT){ + //轻小说 + return "books/soft_book_search"; + }else if(catId == Constants.MH_NOVEL_CAT){ + //漫画 + return "books/mh_book_search"; } - - - PageInfo bookPageInfo = new PageInfo<>(books); - modelMap.put("limit", bookPageInfo.getPageSize()); - modelMap.put("curr", bookPageInfo.getPageNum()); - modelMap.put("total", bookPageInfo.getTotal()); - modelMap.put("books", bookVOList); - modelMap.put("keyword", keyword); - modelMap.put("bookStatus", bookStatus); - modelMap.put("softCat", softCat); - modelMap.put("softTag", softTag); - modelMap.put("sortBy", sortBy); - modelMap.put("sort", sort); - return "books/mh_book_search"; + return null; } + /** * 书籍详情页 - * */ + */ @RequestMapping("{bookId}.html") - public String detail(@PathVariable("bookId") Long bookId, @RequestParam(value = "token",required = false)String token, ModelMap modelMap) { + public String detail(@PathVariable("bookId") Long bookId, @RequestParam(value = "token", required = false) String token, ModelMap modelMap) { String userId = commonCacheUtil.get(token); - if(org.apache.commons.lang3.StringUtils.isNotBlank(userId)){ - Integer indexNumber = userService.queryBookIndexNumber(userId,bookId); - if(indexNumber!=null){ - return "redirect:/book/"+bookId+"/"+indexNumber+".html"; + if (org.apache.commons.lang3.StringUtils.isNotBlank(userId)) { + Integer indexNumber = userService.queryBookIndexNumber(userId, bookId); + if (indexNumber != null) { + return "redirect:/book/" + bookId + "/" + indexNumber + ".html"; } } @@ -246,7 +219,7 @@ public class BookController { /** * 书籍目录页 - * */ + */ @RequestMapping("{bookId}/index.html") public String bookIndex(@PathVariable("bookId") Long bookId, ModelMap modelMap) { List indexList = bookService.queryAllIndexList(bookId); @@ -260,7 +233,7 @@ public class BookController { /** * 书籍内容页 - * */ + */ @RequestMapping("{bookId}/{indexNum}.html") public String bookContent(@PathVariable("bookId") Long bookId, @PathVariable("indexNum") Integer indexNum, ModelMap modelMap) { BookContent bookContent = bookService.queryBookContent(bookId, indexNum); @@ -278,11 +251,11 @@ public class BookController { List preAndNextIndexNum = bookService.queryPreAndNextIndexNum(bookId, indexNum); modelMap.put("nextIndexNum", preAndNextIndexNum.get(0)); modelMap.put("preIndexNum", preAndNextIndexNum.get(1)); - bookContent.setContent(bookContent.getContent().replaceAll("]+app\\.html[^>]+>\\s*]+>\\s*]+>[^<]+\\s*]+>[^<]+]+>>>[^<]+<<\\s*\\s*\\s*","")); + bookContent.setContent(bookContent.getContent().replaceAll("]+app\\.html[^>]+>\\s*]+>\\s*]+>[^<]+\\s*]+>[^<]+]+>>>[^<]+<<\\s*\\s*\\s*", "")); modelMap.put("bookContent", bookContent); modelMap.put("indexName", indexName); Book basicBook = bookService.queryBaseInfo(bookId); - if(basicBook.getCatid() < 8) { + if (basicBook.getCatid() <= Constants.MAX_NOVEL_CAT) { bookContent.setContent(StringEscapeUtils.unescapeHtml4(bookContent.getContent())); } String bookName = basicBook.getBookName(); @@ -295,24 +268,24 @@ public class BookController { /** * 增加访问次数 - * */ + */ @RequestMapping("addVisit") @ResponseBody - public String addVisit(@RequestParam("bookId") Long bookId,@RequestParam(value = "indexNum",defaultValue = "0") Integer indexNum,@RequestParam(value = "token",defaultValue = "") String token) { + public String addVisit(@RequestParam("bookId") Long bookId, @RequestParam(value = "indexNum", defaultValue = "0") Integer indexNum, @RequestParam(value = "token", defaultValue = "") String token) { String userId = commonCacheUtil.get(token); - bookService.addVisitCount(bookId,userId,indexNum); + bookService.addVisitCount(bookId, userId, indexNum); return "ok"; } /** * 发送弹幕 - * */ + */ @RequestMapping("sendBullet") @ResponseBody public Map sendBullet(@RequestParam("contentId") Long contentId, @RequestParam("bullet") String bullet) { - Map result = new HashMap<>(); + Map result = new HashMap<>(2); bookService.sendBullet(contentId, bullet); result.put("code", 1); result.put("desc", "ok"); @@ -321,12 +294,12 @@ public class BookController { /** * 查询是否正在下载 - * */ + */ @RequestMapping("queryIsDownloading") @ResponseBody public Map queryIsDownloading(HttpSession session) { - Map result = new HashMap<>(); - if (session.getAttribute("isDownloading") != null) { + Map result = new HashMap<>(1); + if (session.getAttribute(Constants.NOVEL_IS_DOWNLOADING_KEY) != null) { result.put("code", 1); } else { result.put("code", 0); @@ -337,7 +310,7 @@ public class BookController { /** * 查询弹幕 - * */ + */ @RequestMapping("queryBullet") @ResponseBody public List queryBullet(@RequestParam("contentId") Long contentId) { @@ -352,7 +325,7 @@ public class BookController { @RequestMapping(value = "/download") public void download(@RequestParam("bookId") Long bookId, @RequestParam("bookName") String bookName, HttpServletResponse resp, HttpSession session) { try { - session.setAttribute("isDownloading", 1); + session.setAttribute(Constants.NOVEL_IS_DOWNLOADING_KEY, 1); int count = bookService.countIndex(bookId); @@ -365,19 +338,19 @@ public class BookController { if (count > 0) { for (int i = 0; i < count; i++) { String index = bookService.queryIndexNameByBookIdAndIndexNum(bookId, i); - if(index != null) { + if (index != null) { String content = bookService.queryContentList(bookId, i); out.write(index.getBytes(StandardCharsets.UTF_8)); out.write("\n".getBytes(StandardCharsets.UTF_8)); - content = content.replaceAll("", "\r\n"); - content = content.replaceAll(" ", " "); - content = content.replaceAll("]*>", ""); - content = content.replaceAll("", ""); - content = content.replaceAll("]*>", ""); - content = content.replaceAll("", ""); - content = content.replaceAll("]*>[^<]*]*>[^<]*\\s*

", ""); - content = content.replaceAll("]*>", ""); - content = content.replaceAll("

", "\r\n"); + content = content.replaceAll("", "\r\n") + .replaceAll(" ", " ") + .replaceAll("]*>", "") + .replaceAll("", "") + .replaceAll("]*>", "") + .replaceAll("", "") + .replaceAll("]*>[^<]*]*>[^<]*\\s*

", "") + .replaceAll("]*>", "") + .replaceAll("

", "\r\n"); out.write(content.getBytes(StandardCharsets.UTF_8)); out.write("\r\n".getBytes(StandardCharsets.UTF_8)); out.write("\r\n".getBytes(StandardCharsets.UTF_8)); @@ -393,7 +366,7 @@ public class BookController { } catch (Exception e) { e.printStackTrace(); } finally { - session.removeAttribute("isDownloading"); + session.removeAttribute(Constants.NOVEL_IS_DOWNLOADING_KEY); } } diff --git a/novel-front/src/main/java/xyz/zinglizingli/common/cache/impl/EHCacheUtil.java b/novel-front/src/main/java/xyz/zinglizingli/common/cache/impl/EhCacheUtil.java similarity index 77% rename from novel-front/src/main/java/xyz/zinglizingli/common/cache/impl/EHCacheUtil.java rename to novel-front/src/main/java/xyz/zinglizingli/common/cache/impl/EhCacheUtil.java index 1b56da1..001412e 100644 --- a/novel-front/src/main/java/xyz/zinglizingli/common/cache/impl/EHCacheUtil.java +++ b/novel-front/src/main/java/xyz/zinglizingli/common/cache/impl/EhCacheUtil.java @@ -7,44 +7,30 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import xyz.zinglizingli.common.cache.CommonCacheUtil; +/** + * @author 11797 + */ @Service -public class EHCacheUtil implements CommonCacheUtil { +public class EhCacheUtil implements CommonCacheUtil { @Autowired private CacheManager cacheManager ; - private static final String CACHE_NAME = "utilCache"; + private static final String CACHE_NAME = "util_cache"; /** * 获得一个Cache,没有则创建一个。 - * @param cacheName * @return */ private Cache getCache(){ - /*Cache cache = cacheManager.getCache(cacheName); - if (cache == null){ - cacheManager.addCache(cacheName); - cache = cacheManager.getCache(cacheName); - CacheConfiguration config = cache.getCacheConfiguration(); - config.setEternal(false); - config.internalSetTimeToIdle(0); - config.internalSetTimeToIdle(0); - }*/ - Cache cache = cacheManager.getCache("util_cache"); + Cache cache = cacheManager.getCache(CACHE_NAME); return cache; } - - public CacheManager getCacheManager() { - return cacheManager; - } + - - - - @Override public String get(String key) { Element element = getCache().get(key); @@ -55,7 +41,8 @@ public class EHCacheUtil implements CommonCacheUtil { public void set(String key, String value) { Element element = new Element(key, value); Cache cache = getCache(); - cache.getCacheConfiguration().setEternal(true);//不过期 + //不过期 + cache.getCacheConfiguration().setEternal(true); cache.put(element); } @@ -106,13 +93,13 @@ public class EHCacheUtil implements CommonCacheUtil { /** * 设置Object类型的缓存 - * @param */ @Override public void setObject(String key, Object value) { Element element = new Element(key, value); Cache cache = getCache(); - cache.getCacheConfiguration().setEternal(true);//不过期 + //不过期 + cache.getCacheConfiguration().setEternal(true); cache.put(element); } @@ -131,6 +118,9 @@ public class EHCacheUtil implements CommonCacheUtil { } + /** + * 刷新过期时间 + * */ @Override public void refresh(String key) { Element element = getCache().get(key); diff --git a/novel-front/src/main/java/xyz/zinglizingli/common/schedule/CrawlBooksSchedule.java b/novel-front/src/main/java/xyz/zinglizingli/common/schedule/CrawlBooksSchedule.java index 4f64c01..e1bbb46 100644 --- a/novel-front/src/main/java/xyz/zinglizingli/common/schedule/CrawlBooksSchedule.java +++ b/novel-front/src/main/java/xyz/zinglizingli/common/schedule/CrawlBooksSchedule.java @@ -46,10 +46,6 @@ public class CrawlBooksSchedule { @Value("${crawl.website.type}") private Byte websiteType; - - @Value("${pic.save.type}") - private Byte picSaveType; - @Value("${pic.save.path}") private String picSavePath; @@ -96,7 +92,7 @@ public class CrawlBooksSchedule { if (isFind) { //解析第一页书籍的数据 Pattern bookPatten = compile("href=\"/(\\d+_\\d+)/\""); - parseBiquTaBook(bookPatten, forObject, bookClass, baseUrl); + parseBiquTaBook(bookPatten, forObject, baseUrl); } } } @@ -104,7 +100,7 @@ public class CrawlBooksSchedule { /** * 解析笔趣塔数据 */ - private void parseBiquTaBook(Pattern bookPatten, String forObject, int catNum, String baseUrl) { + private void parseBiquTaBook(Pattern bookPatten, String forObject, String baseUrl) { Matcher bookMatcher = bookPatten.matcher(forObject); boolean isFind = bookMatcher.find(); @@ -142,108 +138,112 @@ public class CrawlBooksSchedule { if (statusMatch.find()) { String status = statusMatch.group(1); - Pattern updateTimePatten = compile("更新:(\\d+-\\d+-\\d+\\s\\d+:\\d+:\\d+)"); - Matcher updateTimeMatch = updateTimePatten.matcher(body); - if (updateTimeMatch.find()) { - String updateTimeStr = updateTimeMatch.group(1); - SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); - Date updateTime = format.parse(updateTimeStr); - Pattern picPatten = compile("]+)\"\\s+onerror=\"this.src="); - Matcher picMather = picPatten.matcher(body); - if (picMather.find()) { - String picSrc = picMather.group(1); + Pattern catPatten = compile("类别:([^/]+)"); + Matcher catMatch = catPatten.matcher(body); + if (catMatch.find()) { + String catName = catMatch.group(1); + int catNum = getCatNum(catName); - Pattern descPatten = compile("class=\"review\">([^<]+)

"); - Matcher descMatch = descPatten.matcher(body); - if (descMatch.find()) { - String desc = descMatch.group(1); + Pattern updateTimePatten = compile("更新:(\\d+-\\d+-\\d+\\s\\d+:\\d+:\\d+)"); + Matcher updateTimeMatch = updateTimePatten.matcher(body); + if (updateTimeMatch.find()) { + String updateTimeStr = updateTimeMatch.group(1); + SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); + Date updateTime = format.parse(updateTimeStr); + Pattern picPatten = compile("]+)\"\\s+onerror=\"this.src="); + Matcher picMather = picPatten.matcher(body); + if (picMather.find()) { + String picSrc = picMather.group(1); - Book book = new Book(); - book.setAuthor(author); - book.setCatid(catNum); - book.setBookDesc(desc); - book.setBookName(bookName); - book.setScore(score > 10 ? 8.0f : score); - book.setPicUrl(picSrc); - book.setBookStatus(status); - book.setUpdateTime(updateTime); - - List indexList = new ArrayList<>(); - List contentList = new ArrayList<>(); - - //读取目录 - Pattern indexPatten = compile("查看完整目录"); - Matcher indexMatch = indexPatten.matcher(body); - if (indexMatch.find()) { - String indexUrl = baseUrl + indexMatch.group(1); - String body2 = getByRestTemplate(indexUrl); - if (body2 != null) { - Pattern indexListPatten = compile("([^/]+)"); - Matcher indexListMatch = indexListPatten.matcher(body2); - - boolean isFindIndex = indexListMatch.find(); - - int indexNum = 0; - - //查询该书籍已存在目录号 - List hasIndexNum = bookService.queryIndexCountByBookNameAndAuthor(bookName, author); - //更新和插入分别开,插入只在凌晨做一次 - if (hasIndexNum.size() > 0) { - while (isFindIndex) { - if (!hasIndexNum.contains(indexNum)) { - - String contentUrl = baseUrl + indexListMatch.group(1); - String indexName = indexListMatch.group(2); + Pattern descPatten = compile("class=\"review\">([^<]+)

"); + Matcher descMatch = descPatten.matcher(body); + if (descMatch.find()) { + String desc = descMatch.group(1); - //查询章节内容 - String body3 = getByRestTemplate(contentUrl); - if (body3 != null) { - String start = "『章节错误,点此举报』"; - String end = "『加入书签,方便阅读』"; - String content = body3.substring(body3.indexOf(start) + start.length(), body3.indexOf(end)); - //TODO插入章节目录和章节内容 - BookIndex bookIndex = new BookIndex(); - bookIndex.setIndexName(indexName); - bookIndex.setIndexNum(indexNum); - indexList.add(bookIndex); - BookContent bookContent = new BookContent(); - bookContent.setContent(content); - bookContent.setIndexNum(indexNum); - contentList.add(bookContent); + Book book = new Book(); + book.setAuthor(author); + book.setCatid(catNum); + book.setBookDesc(desc); + book.setBookName(bookName); + book.setScore(score > 10 ? 8.0f : score); + book.setPicUrl(picSrc); + book.setBookStatus(status); + book.setUpdateTime(updateTime); + + List indexList = new ArrayList<>(); + List contentList = new ArrayList<>(); + + //读取目录 + Pattern indexPatten = compile("查看完整目录"); + Matcher indexMatch = indexPatten.matcher(body); + if (indexMatch.find()) { + String indexUrl = baseUrl + indexMatch.group(1); + String body2 = getByRestTemplate(indexUrl); + if (body2 != null) { + Pattern indexListPatten = compile("([^/]+)"); + Matcher indexListMatch = indexListPatten.matcher(body2); + + boolean isFindIndex = indexListMatch.find(); + + int indexNum = 0; + + //查询该书籍已存在目录号 + List hasIndexNum = bookService.queryIndexNumByBookNameAndAuthor(bookName, author); + //更新和插入分别开,插入只在凌晨做一次 + if (hasIndexNum.size() > 0) { + while (isFindIndex) { + if (!hasIndexNum.contains(indexNum)) { + + String contentUrl = baseUrl + indexListMatch.group(1); + String indexName = indexListMatch.group(2); + + + //查询章节内容 + String body3 = getByRestTemplate(contentUrl); + if (body3 != null) { + String start = "『章节错误,点此举报』"; + String end = "『加入书签,方便阅读』"; + String content = body3.substring(body3.indexOf(start) + start.length(), body3.indexOf(end)); + //TODO插入章节目录和章节内容 + BookIndex bookIndex = new BookIndex(); + bookIndex.setIndexName(indexName); + bookIndex.setIndexNum(indexNum); + indexList.add(bookIndex); + BookContent bookContent = new BookContent(); + bookContent.setContent(content); + bookContent.setIndexNum(indexNum); + contentList.add(bookContent); + + + } else { + break; + } - } else { - break; } + indexNum++; + isFindIndex = indexListMatch.find(); + } + if (indexList.size() == contentList.size() && indexList.size() > 0) { + ExcutorUtils.excuteFixedTask(() -> + bookService.saveBookAndIndexAndContent(book, indexList, contentList) + ); } - indexNum++; - isFindIndex = indexListMatch.find(); - } - - if (indexList.size() == contentList.size() && indexList.size() > 0) { - ExcutorUtils.excuteFixedTask(new Runnable() { - @Override - public void run() { - bookService.saveBookAndIndexAndContent(book, indexList, contentList); - } - }); - } } + + } } - } - - } } } @@ -340,37 +340,7 @@ public class CrawlBooksSchedule { Matcher catMatch = catPatten.matcher(body); if (catMatch.find()) { String catName = catMatch.group(1); - int catNum; - switch (catName) { - case "武侠仙侠": { - catNum = 2; - break; - } - case "都市言情": { - catNum = 3; - break; - } - case "历史军事": { - catNum = 4; - break; - } - case "科幻灵异": { - catNum = 5; - break; - } - case "网游竞技": { - catNum = 6; - break; - } - case "女生频道": { - catNum = 7; - break; - } - default: { - catNum = 1; - break; - } - } + int catNum = getCatNum(catName); Pattern updateTimePatten = compile("更新:(\\d+-\\d+-\\d+\\s\\d+:\\d+:\\d+)"); Matcher updateTimeMatch = updateTimePatten.matcher(body); if (updateTimeMatch.find()) { @@ -417,8 +387,8 @@ public class CrawlBooksSchedule { int indexNum = 0; //查询该书籍已存在目录号 - List hasIndexNum = bookService.queryIndexCountByBookNameAndAuthor(bookName, author); - //更新和插入分别开,插入只在凌晨做一次 + List hasIndexNum = bookService.queryIndexNumByBookNameAndAuthor(bookName, author); + //只更新已存在的书籍 if (hasIndexNum.size() > 0) { while (isFindIndex) { if (!hasIndexNum.contains(indexNum)) { @@ -495,6 +465,41 @@ public class CrawlBooksSchedule { } + private int getCatNum(String catName) { + int catNum; + switch (catName) { + case "武侠仙侠": { + catNum = 2; + break; + } + case "都市言情": { + catNum = 3; + break; + } + case "历史军事": { + catNum = 4; + break; + } + case "科幻灵异": { + catNum = 5; + break; + } + case "网游竞技": { + catNum = 6; + break; + } + case "女生频道": { + catNum = 7; + break; + } + default: { + catNum = 1; + break; + } + } + return catNum; + } + private String getByRestTemplate(String url) { try { ResponseEntity forEntity = utf8RestTemplate.getForEntity(url, String.class); diff --git a/novel-front/src/main/java/xyz/zinglizingli/common/utils/Constants.java b/novel-front/src/main/java/xyz/zinglizingli/common/utils/Constants.java index 62e7474..ac4ed0a 100644 --- a/novel-front/src/main/java/xyz/zinglizingli/common/utils/Constants.java +++ b/novel-front/src/main/java/xyz/zinglizingli/common/utils/Constants.java @@ -51,4 +51,38 @@ public class Constants { public static final String CRAWL_CARTOON_STATIC_URL_PREFIX = "https://static.dmzj.com/"; + /** + * 最大的小说分类ID + * */ + public static final Integer MAX_NOVEL_CAT = 7; + + /** + * 小说是否正在下载的key + * */ + public static final String NOVEL_IS_DOWNLOADING_KEY = "isDownloading"; + + /** + * 轻小说分类ID + * */ + public static final int SOFT_NOVEL_CAT = 8; + + /** + * 漫画分类ID + * */ + public static final int MH_NOVEL_CAT = 9; + + /** + * 小说排行字段名 + * */ + public static final String NOVEL_TOP_FIELD = "score"; + + /** + * 完本小说标识名 + * */ + public static final String NOVEL_END_TAG = "完成"; + + /** + * 多本书籍ID分隔符 + * */ + public static final String BOOK_ID_SEPARATOR = "-"; } diff --git a/novel-front/src/main/resources/application.yml b/novel-front/src/main/resources/application.yml index dd5fe56..46026bd 100644 --- a/novel-front/src/main/resources/application.yml +++ b/novel-front/src/main/resources/application.yml @@ -3,9 +3,9 @@ server: spring: datasource: - url: jdbc:mysql://127.0.0.1:3306/books?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai + url: jdbc:mysql://47.106.243.172:3306/books?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai username: books - password: books + password: books!8888 # url: jdbc:mysql://127.0.0.1:3306/books?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai # username: root # password: test123456 diff --git a/novel-front/src/main/resources/templates/books/book_detail.html b/novel-front/src/main/resources/templates/books/book_detail.html index f56df16..cec9853 100644 --- a/novel-front/src/main/resources/templates/books/book_detail.html +++ b/novel-front/src/main/resources/templates/books/book_detail.html @@ -313,7 +313,7 @@ " "); }else{ innerHtml += ("
  • \n" + - " " + tag + "\n" + + " " + tag + "\n" + "
  • "); } } diff --git a/novel-front/src/main/resources/templates/books/mh_book_search.html b/novel-front/src/main/resources/templates/books/mh_book_search.html index f0b555d..a887b0a 100644 --- a/novel-front/src/main/resources/templates/books/mh_book_search.html +++ b/novel-front/src/main/resources/templates/books/mh_book_search.html @@ -89,21 +89,21 @@