mirror of
https://github.com/201206030/novel-plus.git
synced 2025-06-24 04:46:37 +00:00
上传代码
This commit is contained in:
@ -0,0 +1,36 @@
|
||||
package com.java2nb.novel;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableTransactionManagement
|
||||
@EnableScheduling
|
||||
@EnableCaching
|
||||
@MapperScan(basePackages = {"com.java2nb.novel.mapper"})
|
||||
public class FrontNovelApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(FrontNovelApplication.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解决同一时间只能一个定时任务执行的问题
|
||||
* */
|
||||
@Bean
|
||||
public TaskScheduler taskScheduler() {
|
||||
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
|
||||
taskScheduler.setPoolSize(5);
|
||||
return taskScheduler;
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.java2nb.novel.controller;
|
||||
|
||||
import com.java2nb.novel.core.bean.UserDetails;
|
||||
import com.java2nb.novel.core.utils.JwtTokenUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
public class BaseController {
|
||||
|
||||
protected JwtTokenUtil jwtTokenUtil;
|
||||
|
||||
|
||||
protected String getToken(HttpServletRequest request){
|
||||
Cookie[] cookies = request.getCookies();
|
||||
if(cookies != null) {
|
||||
for (Cookie cookie : cookies) {
|
||||
if (cookie.getName().equals("Authorization")) {
|
||||
return cookie.getValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
return request.getHeader("Authorization");
|
||||
}
|
||||
|
||||
protected UserDetails getUserDetails(HttpServletRequest request) {
|
||||
String token = getToken(request);
|
||||
if(StringUtils.isBlank(token)){
|
||||
return null;
|
||||
}else{
|
||||
return jwtTokenUtil.getUserDetailsFromToken(token);
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setJwtTokenUtil(JwtTokenUtil jwtTokenUtil) {
|
||||
this.jwtTokenUtil = jwtTokenUtil;
|
||||
}
|
||||
}
|
@ -0,0 +1,164 @@
|
||||
package com.java2nb.novel.controller;
|
||||
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.java2nb.novel.core.bean.ResultBean;
|
||||
import com.java2nb.novel.core.bean.UserDetails;
|
||||
import com.java2nb.novel.core.enums.ResponseStatus;
|
||||
import com.java2nb.novel.entity.Book;
|
||||
import com.java2nb.novel.entity.BookComment;
|
||||
import com.java2nb.novel.entity.BookIndex;
|
||||
import com.java2nb.novel.search.BookSP;
|
||||
import com.java2nb.novel.service.BookService;
|
||||
import com.java2nb.novel.vo.BookVO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@RequestMapping("book")
|
||||
@RestController
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class BookController extends BaseController{
|
||||
|
||||
private final BookService bookService;
|
||||
|
||||
|
||||
/**
|
||||
* 查询首页小说设置列表数据
|
||||
* */
|
||||
@PostMapping("listBookSetting")
|
||||
public ResultBean listBookSetting(){
|
||||
return ResultBean.ok(bookService.listBookSettingVO());
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询首页点击榜单数据
|
||||
* */
|
||||
@PostMapping("listClickRank")
|
||||
public ResultBean listClickRank(){
|
||||
return ResultBean.ok(bookService.listClickRank());
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询首页新书榜单数据
|
||||
* */
|
||||
@PostMapping("listNewRank")
|
||||
public ResultBean listNewRank(){
|
||||
return ResultBean.ok(bookService.listNewRank());
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询首页更新榜单数据
|
||||
* */
|
||||
@PostMapping("listUpdateRank")
|
||||
public ResultBean listUpdateRank(){
|
||||
return ResultBean.ok(bookService.listUpdateRank());
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询小说分类列表
|
||||
* */
|
||||
@PostMapping("listBookCategory")
|
||||
public ResultBean listBookCategory(){
|
||||
return ResultBean.ok(bookService.listBookCategory());
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页搜索
|
||||
* */
|
||||
@PostMapping("searchByPage")
|
||||
public ResultBean searchByPage(BookSP bookSP, @RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "20") int pageSize){
|
||||
List<BookVO> books = bookService.searchByPage(bookSP,page,pageSize);
|
||||
return ResultBean.ok(new PageInfo<>(books));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询小说详情信息
|
||||
* */
|
||||
@PostMapping("queryBookDetail")
|
||||
public ResultBean queryBookDetail(Long id){
|
||||
return ResultBean.ok(bookService.queryBookDetail(id));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查询小说排行信息
|
||||
* */
|
||||
@PostMapping("listRank")
|
||||
public ResultBean listRank(@RequestParam(value = "type",defaultValue = "0") Byte type,@RequestParam(value = "limit",defaultValue = "30") Integer limit){
|
||||
return ResultBean.ok(bookService.listRank(type,limit));
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加点击次数
|
||||
* */
|
||||
@PostMapping("addVisitCount")
|
||||
public ResultBean addVisitCount(Long bookId){
|
||||
bookService.addVisitCount(bookId);
|
||||
return ResultBean.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询章节相关信息
|
||||
* */
|
||||
@PostMapping("queryBookIndexAbout")
|
||||
public ResultBean queryBookIndexAbout(Long bookId,Long lastBookIndexId) {
|
||||
Map<String,Object> data = new HashMap<>(2);
|
||||
data.put("bookIndexCount",bookService.queryIndexCount(bookId));
|
||||
data.put("lastBookContent",bookService.queryBookContent(lastBookIndexId).getContent().substring(0,42));
|
||||
return ResultBean.ok(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据分类id查询同类推荐书籍
|
||||
* */
|
||||
@PostMapping("listRecBookByCatId")
|
||||
public ResultBean listRecBookByCatId(Integer catId) {
|
||||
return ResultBean.ok(bookService.listRecBookByCatId(catId));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*分页查询书籍评论列表
|
||||
* */
|
||||
@PostMapping("listCommentByPage")
|
||||
public ResultBean listCommentByPage(@RequestParam("bookId") Long bookId,@RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "5") int pageSize) {
|
||||
return ResultBean.ok(new PageInfo<>(bookService.listCommentByPage(null,bookId,page,pageSize)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增评价
|
||||
* */
|
||||
@PostMapping("addBookComment")
|
||||
public ResultBean addBookComment(BookComment comment, HttpServletRequest request) {
|
||||
UserDetails userDetails = getUserDetails(request);
|
||||
if (userDetails == null) {
|
||||
return ResultBean.fail(ResponseStatus.NO_LOGIN);
|
||||
}
|
||||
bookService.addBookComment(userDetails.getId(),comment);
|
||||
return ResultBean.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据小说ID查询小说前十条最新更新目录集合
|
||||
* */
|
||||
@PostMapping("queryNewIndexList")
|
||||
public ResultBean queryNewIndexList(Long bookId){
|
||||
return ResultBean.ok(bookService.queryIndexList(bookId,"index_num desc",10));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
package com.java2nb.novel.controller;
|
||||
|
||||
import com.java2nb.novel.core.bean.ResultBean;
|
||||
import com.java2nb.novel.core.cache.CacheKey;
|
||||
import com.java2nb.novel.core.cache.CacheService;
|
||||
import com.java2nb.novel.core.enums.ResponseStatus;
|
||||
import com.java2nb.novel.service.BookService;
|
||||
import com.java2nb.novel.service.FriendLinkService;
|
||||
import com.java2nb.novel.service.NewsService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@RequestMapping("cache")
|
||||
@RestController
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class CacheController {
|
||||
|
||||
@Value("${cache.manager.password}")
|
||||
private String cacheManagerPass;
|
||||
|
||||
private final CacheService cacheService;
|
||||
|
||||
private final BookService bookService;
|
||||
|
||||
private final NewsService newsService;
|
||||
|
||||
private final FriendLinkService friendLinkService;
|
||||
|
||||
/**
|
||||
* 刷新缓存
|
||||
* @param type 缓存类型,1:首页书籍推荐,2:首页新闻,3:首页友情链接
|
||||
* */
|
||||
@GetMapping("refresh/{pass}/{type}")
|
||||
public ResultBean refreshCache(@PathVariable("type") Byte type, @PathVariable("pass") String pass){
|
||||
if(!cacheManagerPass.equals(pass)){
|
||||
return ResultBean.fail(ResponseStatus.PASSWORD_ERROR);
|
||||
}
|
||||
switch (type){
|
||||
case 1:{
|
||||
//刷新首页推荐书籍缓存
|
||||
cacheService.del(CacheKey.INDEX_BOOK_SETTINGS_KEY);
|
||||
bookService.listBookSettingVO();
|
||||
break;
|
||||
}
|
||||
case 2:{
|
||||
//刷新首页新闻缓存
|
||||
cacheService.del(CacheKey.INDEX_NEWS_KEY);
|
||||
newsService.listIndexNews();
|
||||
break;
|
||||
}
|
||||
case 3:{
|
||||
//刷新首页友情链接
|
||||
cacheService.del(CacheKey.INDEX_LINK_KEY);
|
||||
friendLinkService.listIndexLink();
|
||||
break;
|
||||
}
|
||||
default:{
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return ResultBean.ok();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.java2nb.novel.controller;
|
||||
|
||||
import com.java2nb.novel.core.cache.CacheService;
|
||||
import com.java2nb.novel.core.utils.RandomValidateCodeUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping("file")
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class FileController {
|
||||
|
||||
private final CacheService cacheService;
|
||||
|
||||
/**
|
||||
* 生成验证码
|
||||
*/
|
||||
@GetMapping(value = "getVerify")
|
||||
public void getVerify(HttpServletRequest request, HttpServletResponse response) {
|
||||
try {
|
||||
//设置相应类型,告诉浏览器输出的内容为图片
|
||||
response.setContentType("image/jpeg");
|
||||
//设置响应头信息,告诉浏览器不要缓存此内容
|
||||
response.setHeader("Pragma", "No-cache");
|
||||
response.setHeader("Cache-Control", "no-cache");
|
||||
response.setDateHeader("Expire", 0);
|
||||
RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
|
||||
//输出验证码图片方法
|
||||
randomValidateCode.getRandcode(cacheService, response);
|
||||
} catch (Exception e) {
|
||||
log.error("获取验证码失败>>>> ", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.java2nb.novel.controller;
|
||||
|
||||
import com.java2nb.novel.core.bean.ResultBean;
|
||||
import com.java2nb.novel.service.FriendLinkService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@RequestMapping("friendLink")
|
||||
@RestController
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class FriendLinkController {
|
||||
|
||||
private final FriendLinkService friendLinkService;
|
||||
|
||||
/**
|
||||
* 查询首页友情链接
|
||||
* */
|
||||
@PostMapping("listIndexLink")
|
||||
public ResultBean listIndexLink(){
|
||||
return ResultBean.ok(friendLinkService.listIndexLink());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.java2nb.novel.controller;
|
||||
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.java2nb.novel.core.bean.ResultBean;
|
||||
import com.java2nb.novel.service.NewsService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@RequestMapping("news")
|
||||
@RestController
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class NewsController {
|
||||
|
||||
private final NewsService newsService;
|
||||
|
||||
/**
|
||||
* 查询首页新闻
|
||||
* */
|
||||
@PostMapping("listIndexNews")
|
||||
public ResultBean listIndexNews(){
|
||||
return ResultBean.ok(newsService.listIndexNews());
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询新闻列表
|
||||
* */
|
||||
@PostMapping("listByPage")
|
||||
public ResultBean listByPage(@RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "5") int pageSize){
|
||||
return ResultBean.ok(new PageInfo<>(newsService.listByPage(page,pageSize)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,144 @@
|
||||
package com.java2nb.novel.controller;
|
||||
|
||||
import com.java2nb.novel.core.utils.TemplateUtil;
|
||||
import com.java2nb.novel.entity.Book;
|
||||
import com.java2nb.novel.entity.BookContent;
|
||||
import com.java2nb.novel.entity.BookIndex;
|
||||
import com.java2nb.novel.entity.News;
|
||||
import com.java2nb.novel.service.BookService;
|
||||
import com.java2nb.novel.service.NewsService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
@Controller
|
||||
public class PageController{
|
||||
|
||||
private final BookService bookService;
|
||||
|
||||
private final NewsService newsService;
|
||||
|
||||
|
||||
@RequestMapping("{url}.html")
|
||||
public String module(@PathVariable("url") String url) {
|
||||
return url;
|
||||
}
|
||||
|
||||
@RequestMapping("{module}/{url}.html")
|
||||
public String module2(@PathVariable("module") String module, @PathVariable("url") String url) {
|
||||
return module + "/" + url;
|
||||
}
|
||||
|
||||
@RequestMapping("{module}/{classify}/{url}.html")
|
||||
public String module3(@PathVariable("module") String module, @PathVariable("classify") String classify, @PathVariable("url") String url) {
|
||||
return module + "/" + classify + "/" + url;
|
||||
}
|
||||
|
||||
/**
|
||||
* 首页
|
||||
* */
|
||||
@RequestMapping(path = {"/", "/index", "/index.html"})
|
||||
public String index() {
|
||||
return TemplateUtil.getTemplateDir()+"index";
|
||||
}
|
||||
|
||||
/**
|
||||
* 作品页
|
||||
* */
|
||||
@RequestMapping("book/bookclass.html")
|
||||
public String bookClass() {
|
||||
return "book/bookclass";
|
||||
}
|
||||
|
||||
/**
|
||||
* 排行页
|
||||
* */
|
||||
@RequestMapping("book/book_ranking.html")
|
||||
public String bookRank() {
|
||||
|
||||
return TemplateUtil.getTemplateDir()+"book/book_ranking";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 详情页
|
||||
* */
|
||||
@RequestMapping("/book/{bookId}.html")
|
||||
public String bookDetail(@PathVariable("bookId") Long bookId, Model model) {
|
||||
Book book = bookService.queryBookDetail(bookId);
|
||||
model.addAttribute("book",book);
|
||||
//查询首章目录ID
|
||||
Long firstBookIndexId = bookService.queryFirstBookIndexId(bookId);
|
||||
model.addAttribute("firstBookIndexId",firstBookIndexId);
|
||||
return TemplateUtil.getTemplateDir()+"book/book_detail";
|
||||
}
|
||||
|
||||
/**
|
||||
* 目录页
|
||||
* */
|
||||
@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,null);
|
||||
model.addAttribute("bookIndexList",bookIndexList);
|
||||
model.addAttribute("bookIndexCount",bookIndexList.size());
|
||||
return TemplateUtil.getTemplateDir()+"book/book_index";
|
||||
}
|
||||
|
||||
/**
|
||||
* 内容页
|
||||
* */
|
||||
@RequestMapping("/book/{bookId}/{bookIndexId}.html")
|
||||
public String indexList(@PathVariable("bookId") Long bookId,@PathVariable("bookIndexId") Long bookIndexId, Model model) {
|
||||
//查询书籍
|
||||
Book book = bookService.queryBookDetail(bookId);
|
||||
model.addAttribute("book",book);
|
||||
//查询目录
|
||||
BookIndex bookIndex = bookService.queryBookIndex(bookIndexId);
|
||||
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);
|
||||
return TemplateUtil.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);
|
||||
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);
|
||||
return "about/news_info";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,269 @@
|
||||
package com.java2nb.novel.controller;
|
||||
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.java2nb.novel.core.bean.ResultBean;
|
||||
import com.java2nb.novel.core.bean.UserDetails;
|
||||
import com.java2nb.novel.core.cache.CacheService;
|
||||
import com.java2nb.novel.core.enums.ResponseStatus;
|
||||
import com.java2nb.novel.core.utils.RandomValidateCodeUtil;
|
||||
import com.java2nb.novel.entity.User;
|
||||
import com.java2nb.novel.form.UserForm;
|
||||
import com.java2nb.novel.service.BookService;
|
||||
import com.java2nb.novel.service.UserService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.validation.Valid;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("user")
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class UserController extends BaseController {
|
||||
|
||||
|
||||
private final CacheService cacheService;
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
private final BookService bookService;
|
||||
|
||||
/**
|
||||
* 登陆
|
||||
*/
|
||||
@PostMapping("login")
|
||||
public ResultBean login(@Valid UserForm user, BindingResult result) {
|
||||
//判断参数是否合法
|
||||
if (result.hasErrors()) {
|
||||
log.info(result.getAllErrors().toString());
|
||||
return ResultBean.fail(ResponseStatus.PARAM_ERROR);
|
||||
}
|
||||
|
||||
//登陆
|
||||
UserDetails userDetails = userService.login(user);
|
||||
|
||||
Map<String, Object> data = new HashMap<>(1);
|
||||
data.put("token", jwtTokenUtil.generateToken(userDetails));
|
||||
|
||||
return ResultBean.ok(data);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册
|
||||
*/
|
||||
@PostMapping("register")
|
||||
public ResultBean register(@Valid UserForm user, @RequestParam(value = "velCode", defaultValue = "") String velCode, BindingResult result) {
|
||||
|
||||
//判断参数是否合法
|
||||
if (result.hasErrors()) {
|
||||
log.info(result.getAllErrors().toString());
|
||||
return ResultBean.fail(ResponseStatus.PARAM_ERROR);
|
||||
}
|
||||
|
||||
//判断验证码是否正确
|
||||
if (!velCode.equals(cacheService.get(RandomValidateCodeUtil.RANDOM_CODE_KEY))) {
|
||||
return ResultBean.fail(ResponseStatus.VEL_CODE_ERROR);
|
||||
}
|
||||
|
||||
//注册
|
||||
UserDetails userDetails = userService.register(user);
|
||||
Map<String, Object> data = new HashMap<>(1);
|
||||
data.put("token", jwtTokenUtil.generateToken(userDetails));
|
||||
|
||||
return ResultBean.ok(data);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 刷新token
|
||||
*/
|
||||
@PostMapping("refreshToken")
|
||||
public ResultBean refreshToken(HttpServletRequest request) {
|
||||
String token = getToken(request);
|
||||
if (jwtTokenUtil.canRefresh(token)) {
|
||||
token = jwtTokenUtil.refreshToken(token);
|
||||
Map<String, Object> data = new HashMap<>(2);
|
||||
data.put("token", token);
|
||||
data.put("username", jwtTokenUtil.getUserDetailsFromToken(token).getUsername());
|
||||
return ResultBean.ok(data);
|
||||
|
||||
} else {
|
||||
return ResultBean.fail(ResponseStatus.NO_LOGIN);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询小说是否已加入书架
|
||||
*/
|
||||
@PostMapping("queryIsInShelf")
|
||||
public ResultBean queryIsInShelf(Long bookId, HttpServletRequest request) {
|
||||
UserDetails userDetails = getUserDetails(request);
|
||||
if (userDetails == null) {
|
||||
return ResultBean.fail(ResponseStatus.NO_LOGIN);
|
||||
}
|
||||
return ResultBean.ok(userService.queryIsInShelf(userDetails.getId(), bookId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 加入书架
|
||||
* */
|
||||
@PostMapping("addToBookShelf")
|
||||
public ResultBean addToBookShelf(Long bookId,Long preContentId, HttpServletRequest request) {
|
||||
UserDetails userDetails = getUserDetails(request);
|
||||
if (userDetails == null) {
|
||||
return ResultBean.fail(ResponseStatus.NO_LOGIN);
|
||||
}
|
||||
userService.addToBookShelf(userDetails.getId(),bookId,preContentId);
|
||||
return ResultBean.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 移出书架
|
||||
* */
|
||||
@PostMapping("removeFromBookShelf")
|
||||
public ResultBean removeFromBookShelf(Long bookId, HttpServletRequest request) {
|
||||
UserDetails userDetails = getUserDetails(request);
|
||||
if (userDetails == null) {
|
||||
return ResultBean.fail(ResponseStatus.NO_LOGIN);
|
||||
}
|
||||
userService.removeFromBookShelf(userDetails.getId(),bookId);
|
||||
return ResultBean.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询书架
|
||||
* */
|
||||
@PostMapping("listBookShelfByPage")
|
||||
public ResultBean listBookShelfByPage(@RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "10") int pageSize,HttpServletRequest request) {
|
||||
UserDetails userDetails = getUserDetails(request);
|
||||
if (userDetails == null) {
|
||||
return ResultBean.fail(ResponseStatus.NO_LOGIN);
|
||||
}
|
||||
return ResultBean.ok(new PageInfo<>(userService.listBookShelfByPage(userDetails.getId(),page,pageSize)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询阅读记录
|
||||
* */
|
||||
@PostMapping("listReadHistoryByPage")
|
||||
public ResultBean listReadHistoryByPage(@RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "10") int pageSize,HttpServletRequest request) {
|
||||
UserDetails userDetails = getUserDetails(request);
|
||||
if (userDetails == null) {
|
||||
return ResultBean.fail(ResponseStatus.NO_LOGIN);
|
||||
}
|
||||
return ResultBean.ok(new PageInfo<>(userService.listReadHistoryByPage(userDetails.getId(),page,pageSize)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加阅读记录
|
||||
* */
|
||||
@PostMapping("addReadHistory")
|
||||
public ResultBean addReadHistory(Long bookId,Long preContentId, HttpServletRequest request) {
|
||||
UserDetails userDetails = getUserDetails(request);
|
||||
if (userDetails == null) {
|
||||
return ResultBean.fail(ResponseStatus.NO_LOGIN);
|
||||
}
|
||||
userService.addReadHistory(userDetails.getId(),bookId,preContentId);
|
||||
return ResultBean.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加反馈
|
||||
* */
|
||||
@PostMapping("addFeedBack")
|
||||
public ResultBean addFeedBack(String content, HttpServletRequest request) {
|
||||
UserDetails userDetails = getUserDetails(request);
|
||||
if (userDetails == null) {
|
||||
return ResultBean.fail(ResponseStatus.NO_LOGIN);
|
||||
}
|
||||
userService.addFeedBack(userDetails.getId(),content);
|
||||
return ResultBean.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询我的反馈列表
|
||||
* */
|
||||
@PostMapping("listUserFeedBackByPage")
|
||||
public ResultBean listUserFeedBackByPage(@RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "5") int pageSize, HttpServletRequest request){
|
||||
UserDetails userDetails = getUserDetails(request);
|
||||
if (userDetails == null) {
|
||||
return ResultBean.fail(ResponseStatus.NO_LOGIN);
|
||||
}
|
||||
return ResultBean.ok(new PageInfo<>(userService.listUserFeedBackByPage(userDetails.getId(),page,pageSize)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询个人信息
|
||||
* */
|
||||
@PostMapping("userInfo")
|
||||
public ResultBean userInfo(HttpServletRequest request) {
|
||||
UserDetails userDetails = getUserDetails(request);
|
||||
if (userDetails == null) {
|
||||
return ResultBean.fail(ResponseStatus.NO_LOGIN);
|
||||
}
|
||||
return ResultBean.ok(userService.userInfo(userDetails.getId()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新个人信息
|
||||
* */
|
||||
@PostMapping("updateUserInfo")
|
||||
public ResultBean updateUserInfo(User user,HttpServletRequest request) {
|
||||
UserDetails userDetails = getUserDetails(request);
|
||||
if (userDetails == null) {
|
||||
return ResultBean.fail(ResponseStatus.NO_LOGIN);
|
||||
}
|
||||
userService.updateUserInfo(userDetails.getId(),user);
|
||||
return ResultBean.ok();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 更新密码
|
||||
* */
|
||||
@PostMapping("updatePassword")
|
||||
public ResultBean updatePassword(String oldPassword,String newPassword1,String newPassword2,HttpServletRequest request) {
|
||||
UserDetails userDetails = getUserDetails(request);
|
||||
if (userDetails == null) {
|
||||
return ResultBean.fail(ResponseStatus.NO_LOGIN);
|
||||
}
|
||||
if(!(StringUtils.isNotBlank(newPassword1) && newPassword1.equals(newPassword2))){
|
||||
ResultBean.fail(ResponseStatus.TWO_PASSWORD_DIFF);
|
||||
}
|
||||
userService.updatePassword(userDetails.getId(),oldPassword,newPassword1);
|
||||
return ResultBean.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询用户书评
|
||||
* */
|
||||
@PostMapping("listCommentByPage")
|
||||
public ResultBean listCommentByPage(@RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "5") int pageSize,HttpServletRequest request) {
|
||||
UserDetails userDetails = getUserDetails(request);
|
||||
if (userDetails == null) {
|
||||
return ResultBean.fail(ResponseStatus.NO_LOGIN);
|
||||
}
|
||||
return ResultBean.ok(new PageInfo<>(bookService.listCommentByPage(userDetails.getId(),null,page,pageSize)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.java2nb.novel.core.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@Data
|
||||
public class UserDetails {
|
||||
|
||||
private Long id;
|
||||
|
||||
private String username;
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.java2nb.novel.core.config;
|
||||
|
||||
import com.java2nb.novel.core.filter.NovelFilter;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 过滤器配置类
|
||||
* @author Administrator
|
||||
*/
|
||||
@Configuration
|
||||
public class FilterConfig{
|
||||
|
||||
@Value("${pic.save.path}")
|
||||
private String picSavePath;
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean<NovelFilter> filterRegist() {
|
||||
FilterRegistrationBean<NovelFilter> frBean = new FilterRegistrationBean<>();
|
||||
frBean.setFilter(new NovelFilter());
|
||||
frBean.addUrlPatterns("/*");
|
||||
frBean.addInitParameter("picSavePath",picSavePath);
|
||||
return frBean;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package com.java2nb.novel.core.filter;
|
||||
|
||||
import com.java2nb.novel.core.cache.CacheKey;
|
||||
import com.java2nb.novel.core.cache.CacheService;
|
||||
import com.java2nb.novel.core.utils.BrowserUtil;
|
||||
import com.java2nb.novel.core.utils.Constants;
|
||||
import com.java2nb.novel.core.utils.SpringUtil;
|
||||
import com.java2nb.novel.core.utils.TemplateUtil;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* 项目核心过滤器
|
||||
* @author Administrator
|
||||
*/
|
||||
public class NovelFilter implements Filter {
|
||||
|
||||
/**
|
||||
* 本地图片保存路径
|
||||
* */
|
||||
private String picSavePath;
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig){
|
||||
picSavePath = filterConfig.getInitParameter("picSavePath");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
||||
HttpServletRequest req = (HttpServletRequest) servletRequest;
|
||||
HttpServletResponse resp = (HttpServletResponse) servletResponse;
|
||||
String requestUri = req.getRequestURI();
|
||||
|
||||
//本地图片访问处理
|
||||
if (requestUri.contains(Constants.LOCAL_PIC_PREFIX)) {
|
||||
//缓存10天
|
||||
resp.setDateHeader("expires", System.currentTimeMillis()+60*60*24*10*1000);
|
||||
OutputStream out = resp.getOutputStream();
|
||||
InputStream input = new FileInputStream(new File(picSavePath + requestUri));
|
||||
byte[] b = new byte[4096];
|
||||
for (int n; (n = input.read(b)) != -1; ) {
|
||||
out.write(b, 0, n);
|
||||
}
|
||||
input.close();
|
||||
out.close();
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
//根据浏览器类型选择前端模板
|
||||
String to = req.getParameter("to");
|
||||
CacheService cacheService = SpringUtil.getBean(CacheService.class);
|
||||
if("pc".equals(to)){
|
||||
//直接进PC站
|
||||
cacheService.set(CacheKey.TEMPLATE_DIR_KEY,"",60*60*24);
|
||||
}else if("mobile".equals(to)){
|
||||
//直接进手机站
|
||||
cacheService.set(CacheKey.TEMPLATE_DIR_KEY,"mobile/",60*60*24);
|
||||
}else{
|
||||
//自动识别是PC站还是手机站
|
||||
if(BrowserUtil.isMobile(req)){
|
||||
//手机端访问
|
||||
TemplateUtil.setTemplateDir("mobile/");
|
||||
}else{
|
||||
//PC端访问
|
||||
TemplateUtil.setTemplateDir("");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
filterChain.doFilter(servletRequest,servletResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package com.java2nb.novel.core.schedule;
|
||||
|
||||
import com.java2nb.novel.entity.Book;
|
||||
import com.java2nb.novel.service.BookService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 网络图片转存本地任务
|
||||
* @author Administrator
|
||||
*/
|
||||
@ConditionalOnProperty(prefix = "pic.save",name = "type",havingValue = "2")
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class Network2LocalPicSchedule {
|
||||
|
||||
private final BookService bookService;
|
||||
|
||||
@Value("${pic.save.type}")
|
||||
private Integer picSaveType;
|
||||
|
||||
@Value("${pic.save.path}")
|
||||
private String picSavePath;
|
||||
|
||||
/**
|
||||
* 10分钟转一次
|
||||
*/
|
||||
@Scheduled(fixedRate = 1000 * 60 * 10)
|
||||
public void trans() {
|
||||
|
||||
log.info("Network2LocalPicSchedule。。。。。。。。。。。。");
|
||||
|
||||
|
||||
Integer offset = 0, limit = 100;
|
||||
List<Book> networkPicBooks;
|
||||
do {
|
||||
networkPicBooks = bookService.queryNetworkPicBooks(limit, offset);
|
||||
for (Book book : networkPicBooks) {
|
||||
bookService.updateBookPicToLocal(book.getPicUrl(), book.getId());
|
||||
}
|
||||
offset += limit;
|
||||
} while (networkPicBooks.size() > 0);
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package com.java2nb.novel.core.utils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class BrowserUtil {
|
||||
|
||||
// 浏览器类型
|
||||
public static final String[] mobileAgents = { "iphone", "android", "phone", "mobile", "wap", "netfront", "java",
|
||||
"opera mobi", "opera mini", "ucweb", "windows ce", "symbian", "series", "webos", "sony", "blackberry",
|
||||
"dopod", "nokia", "samsung", "palmsource", "xda", "pieplus", "meizu", "midp", "cldc", "motorola", "foma",
|
||||
"docomo", "up.browser", "up.link", "blazer", "helio", "hosin", "huawei", "novarra", "coolpad", "webos",
|
||||
"techfaith", "palmsource", "alcatel", "amoi", "ktouch", "nexian", "ericsson", "philips", "sagem", "wellcom",
|
||||
"bunjalloo", "maui", "smartphone", "iemobile", "spice", "bird", "zte-", "longcos", "pantech", "gionee",
|
||||
"portalmmm", "jig browser", "hiptop", "benq", "haier", "^lct", "320x320", "240x320", "176x220", "w3c ",
|
||||
"acs-", "alav", "alca", "amoi", "audi", "avan", "benq", "bird", "blac", "blaz", "brew", "cell", "cldc",
|
||||
"cmd-", "dang", "doco", "eric", "hipt", "inno", "ipaq", "java", "jigs", "kddi", "keji", "leno", "lg-c",
|
||||
"lg-d", "lg-g", "lge-", "maui", "maxo", "midp", "mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-",
|
||||
"newt", "noki", "oper", "palm", "pana", "pant", "phil", "play", "port", "prox", "qwap", "sage", "sams",
|
||||
"sany", "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-", "siem", "smal", "smar", "sony", "sph-",
|
||||
"symb", "t-mo", "teli", "tim-", "tosh", "tsm-", "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi",
|
||||
"wapp", "wapr", "webc", "winw", "winw", "xda", "xda-", "Googlebot-Mobile" };
|
||||
|
||||
/**
|
||||
*
|
||||
* @Title: JudgelsMobile @Description: TODO(判断是否是手机浏览器) @param @param
|
||||
* request @param @return 设定文件 @return boolean 返回类型 @throws
|
||||
*/
|
||||
public static boolean isMobile(HttpServletRequest request) {
|
||||
boolean isMobile = false;
|
||||
if (request.getHeader("User-Agent") != null) {
|
||||
for (String mobileAgent : mobileAgents) {
|
||||
if (request.getHeader("User-Agent").toLowerCase().indexOf(mobileAgent) > 0) {
|
||||
isMobile = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return isMobile;
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.java2nb.novel.core.utils;
|
||||
|
||||
/**
|
||||
* 常量类
|
||||
* @author Administrator
|
||||
*/
|
||||
public class Constants {
|
||||
|
||||
/**
|
||||
* 模板路径前缀保存key
|
||||
* */
|
||||
public static final String TEMPLATE_PATH_PREFIX_KEY = "templatePathPrefixKey";
|
||||
|
||||
/**
|
||||
* 本地图片保存前缀
|
||||
* */
|
||||
public static final String LOCAL_PIC_PREFIX = "/localPic/";
|
||||
}
|
@ -0,0 +1,128 @@
|
||||
package com.java2nb.novel.core.utils;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.java2nb.novel.core.bean.UserDetails;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.SignatureAlgorithm;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class JwtTokenUtil {
|
||||
|
||||
private static final String CLAIM_KEY_USERNAME = "sub";
|
||||
private static final String CLAIM_KEY_CREATED = "created";
|
||||
@Value("${jwt.secret}")
|
||||
private String secret;
|
||||
@Value("${jwt.expiration}")
|
||||
private Long expiration;
|
||||
|
||||
/**
|
||||
* 根据负责生成JWT的token
|
||||
*/
|
||||
private String generateToken(Map<String, Object> claims) {
|
||||
return Jwts.builder()
|
||||
.setClaims(claims)
|
||||
.setExpiration(generateExpirationDate())
|
||||
.signWith(SignatureAlgorithm.HS512, secret)
|
||||
.compact();
|
||||
}
|
||||
|
||||
/**
|
||||
* 从token中获取JWT中的负载
|
||||
*/
|
||||
private Claims getClaimsFromToken(String token) {
|
||||
Claims claims = null;
|
||||
try {
|
||||
claims = Jwts.parser()
|
||||
.setSigningKey(secret)
|
||||
.parseClaimsJws(token)
|
||||
.getBody();
|
||||
} catch (Exception e) {
|
||||
log.info("JWT格式验证失败:{}",token);
|
||||
}
|
||||
return claims;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成token的过期时间
|
||||
*/
|
||||
private Date generateExpirationDate() {
|
||||
return new Date(System.currentTimeMillis() + expiration * 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从token中获取用户信息
|
||||
*/
|
||||
public UserDetails getUserDetailsFromToken(String token) {
|
||||
if(isTokenExpired(token)){
|
||||
return null;
|
||||
}
|
||||
UserDetails userDetail;
|
||||
try {
|
||||
Claims claims = getClaimsFromToken(token);
|
||||
userDetail = new ObjectMapper().readValue(claims.getSubject(),UserDetails.class);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(),e);
|
||||
userDetail = null;
|
||||
}
|
||||
return userDetail;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 判断token是否已经失效
|
||||
*/
|
||||
private boolean isTokenExpired(String token) {
|
||||
Date expiredDate = getExpiredDateFromToken(token);
|
||||
return expiredDate.before(new Date());
|
||||
}
|
||||
|
||||
/**
|
||||
* 从token中获取过期时间
|
||||
*/
|
||||
private Date getExpiredDateFromToken(String token) {
|
||||
Claims claims = getClaimsFromToken(token);
|
||||
return claims.getExpiration();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户信息生成token
|
||||
*/
|
||||
@SneakyThrows
|
||||
public String generateToken(UserDetails userDetails) {
|
||||
Map<String, Object> claims = new HashMap<>(2);
|
||||
claims.put(CLAIM_KEY_USERNAME, new ObjectMapper().writeValueAsString(userDetails));
|
||||
claims.put(CLAIM_KEY_CREATED, new Date());
|
||||
return generateToken(claims);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断token是否可以被刷新
|
||||
*/
|
||||
public boolean canRefresh(String token) {
|
||||
return !isTokenExpired(token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新token
|
||||
*/
|
||||
public String refreshToken(String token) {
|
||||
Claims claims = getClaimsFromToken(token);
|
||||
claims.put(CLAIM_KEY_CREATED, new Date());
|
||||
return generateToken(claims);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.java2nb.novel.core.utils;
|
||||
|
||||
import com.java2nb.novel.core.cache.CacheKey;
|
||||
import com.java2nb.novel.core.cache.CacheService;
|
||||
|
||||
/**
|
||||
* 模板操作工具类
|
||||
* @author Administrator
|
||||
*/
|
||||
public class TemplateUtil {
|
||||
|
||||
/**
|
||||
* 存储当前线程访问的模板目录
|
||||
* */
|
||||
private static ThreadLocal<String> templateDir = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 设置当前应该访问的模板目录
|
||||
* */
|
||||
public static void setTemplateDir(String dir){
|
||||
templateDir.set(dir);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前应该访问的模板路径前缀
|
||||
* */
|
||||
public static String getTemplateDir(){
|
||||
CacheService cacheService = SpringUtil.getBean(CacheService.class);
|
||||
String prefix = cacheService.get(CacheKey.TEMPLATE_DIR_KEY);
|
||||
if(prefix != null){
|
||||
return prefix;
|
||||
}
|
||||
return templateDir.get();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.java2nb.novel.form;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.annotation.Generated;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
@Data
|
||||
public class UserForm {
|
||||
@NotBlank(message="手机号不能为空!")
|
||||
@Pattern(regexp="^1[3|4|5|6|7|8|9][0-9]{9}$",message="手机号格式不正确!")
|
||||
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
||||
private String username;
|
||||
|
||||
@NotBlank(message="密码不能为空!")
|
||||
@Generated("org.mybatis.generator.api.MyBatisGenerator")
|
||||
private String password;
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.java2nb.novel.mapper;
|
||||
|
||||
import com.java2nb.novel.vo.BookCommentVO;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
*/
|
||||
public interface FrontBookCommentMapper extends BookCommentMapper {
|
||||
|
||||
List<BookCommentVO> listCommentByPage(@Param("userId") Long userId, @Param("bookId") Long bookId);
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.java2nb.novel.mapper;
|
||||
|
||||
import com.java2nb.novel.entity.Book;
|
||||
import com.java2nb.novel.search.BookSP;
|
||||
import com.java2nb.novel.vo.BookVO;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
*/
|
||||
public interface FrontBookMapper extends BookMapper {
|
||||
|
||||
|
||||
List<BookVO> searchByPage(BookSP params);
|
||||
|
||||
void addVisitCount(@Param("bookId") Long bookId);
|
||||
|
||||
List<Book> listRecBookByCatId(@Param("catId") Integer catId);
|
||||
|
||||
void addCommentCount(@Param("bookId") Long bookId);
|
||||
|
||||
List<Book> queryNetworkPicBooks(@Param("limit") Integer limit,@Param("offset") Integer offset);
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.java2nb.novel.mapper;
|
||||
|
||||
import com.java2nb.novel.vo.BookSettingVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
*/
|
||||
public interface FrontBookSettingMapper extends BookSettingMapper {
|
||||
|
||||
List<BookSettingVO> listVO();
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.java2nb.novel.mapper;
|
||||
|
||||
import com.java2nb.novel.vo.BookShelfVO;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
*/
|
||||
public interface FrontUserBookshelfMapper extends UserBookshelfMapper {
|
||||
|
||||
List<BookShelfVO> listBookShelf(@Param("userId") Long userId);
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.java2nb.novel.mapper;
|
||||
|
||||
import com.java2nb.novel.vo.BookReadHistoryVO;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
*/
|
||||
public interface FrontUserReadHistoryMapper extends UserReadHistoryMapper {
|
||||
|
||||
List<BookReadHistoryVO> listReadHistory(@Param("userId") Long userId);
|
||||
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.java2nb.novel.search;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 小说搜索参数
|
||||
* @author 11797
|
||||
*/
|
||||
@Data
|
||||
public class BookSP {
|
||||
|
||||
private String keyword;
|
||||
|
||||
private Byte workDirection;
|
||||
|
||||
private Integer catId;
|
||||
|
||||
private Byte isVip;
|
||||
|
||||
private Byte bookStatus;
|
||||
|
||||
private Integer wordCountMin;
|
||||
|
||||
private Integer wordCountMax;
|
||||
|
||||
private Date updateTimeMin;
|
||||
|
||||
private Long updatePeriod;
|
||||
|
||||
private String sort;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,194 @@
|
||||
package com.java2nb.novel.service;
|
||||
|
||||
|
||||
import com.java2nb.novel.search.BookSP;
|
||||
import com.java2nb.novel.vo.BookCommentVO;
|
||||
import com.java2nb.novel.vo.BookSettingVO;
|
||||
import com.java2nb.novel.entity.*;
|
||||
import com.java2nb.novel.vo.BookVO;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
public interface BookService {
|
||||
|
||||
/**
|
||||
* 查询首页小说设置列表数据
|
||||
* @return
|
||||
* */
|
||||
Map<Byte, List<BookSettingVO>> listBookSettingVO();
|
||||
|
||||
/**
|
||||
* 查询首页点击榜单数据
|
||||
* @return
|
||||
* */
|
||||
List<Book> listClickRank();
|
||||
|
||||
/**
|
||||
* 查询首页新书榜单数据
|
||||
* @return
|
||||
* */
|
||||
List<Book> listNewRank();
|
||||
|
||||
/**
|
||||
* 查询首页更新榜单数据
|
||||
* @return
|
||||
* */
|
||||
List<BookVO> listUpdateRank();
|
||||
|
||||
/**
|
||||
* 分页搜索
|
||||
* @param params 搜索参数
|
||||
* @param page 页码
|
||||
* @param pageSize 分页大小
|
||||
* @return 小说集合
|
||||
* */
|
||||
List<BookVO> searchByPage(BookSP params, int page, int pageSize);
|
||||
|
||||
/**
|
||||
* 查询小说分类列表
|
||||
* @return 分类集合
|
||||
* */
|
||||
List<BookCategory> listBookCategory();
|
||||
|
||||
/**
|
||||
* 查询小说详情信息
|
||||
* @return 书籍信息
|
||||
* @param id 书籍ID*/
|
||||
Book queryBookDetail(Long id);
|
||||
|
||||
/**
|
||||
* 查询目录列表
|
||||
* @param bookId 书籍ID
|
||||
* @param orderBy 排序
|
||||
*@param limit 查询条数
|
||||
*@return 目录集合
|
||||
* */
|
||||
List<BookIndex> queryIndexList(Long bookId, String orderBy, Integer limit);
|
||||
|
||||
/**
|
||||
* 查询目录
|
||||
* @param bookIndexId 目录ID
|
||||
* @return 目录信息
|
||||
* */
|
||||
BookIndex queryBookIndex(Long bookIndexId);
|
||||
|
||||
/**
|
||||
* 查询上一章节目录ID
|
||||
* @param bookId 书籍ID
|
||||
* @param indexNum 目录号
|
||||
* @return 上一章节目录ID,没有则返回0
|
||||
* */
|
||||
Long queryPreBookIndexId(Long bookId, Integer indexNum);
|
||||
|
||||
/**
|
||||
* 查询下一章目录ID
|
||||
* @param bookId 书籍ID
|
||||
* @param indexNum 目录号
|
||||
* @return 下一章节目录ID,没有则返回0
|
||||
* */
|
||||
Long queryNextBookIndexId(Long bookId, Integer indexNum);
|
||||
|
||||
/**
|
||||
* 查询章节内容
|
||||
* @param bookIndexId 目录ID
|
||||
* @return 书籍内容
|
||||
* */
|
||||
BookContent queryBookContent(Long bookIndexId);
|
||||
|
||||
/**
|
||||
* 查询小说排行信息
|
||||
* @param type 排行类型,0点击排行,1新书排行,2更新排行
|
||||
* @param limit 查询条数
|
||||
* @return 小说集合
|
||||
* */
|
||||
List<Book> listRank(Byte type, Integer limit);
|
||||
|
||||
/**
|
||||
* 增加点击次数
|
||||
* @param bookId 书籍ID
|
||||
* */
|
||||
void addVisitCount(Long bookId);
|
||||
|
||||
/**
|
||||
* 查询章节数
|
||||
* @param bookId 书籍ID
|
||||
* @return 章节数量
|
||||
* */
|
||||
long queryIndexCount(Long bookId);
|
||||
|
||||
/**
|
||||
* 根据分类id查询同类推荐书籍
|
||||
* @param catId 分类id
|
||||
* @return 书籍集合
|
||||
* */
|
||||
List<Book> listRecBookByCatId(Integer catId);
|
||||
|
||||
/**
|
||||
* 查询首章目录ID
|
||||
* @param bookId 书籍ID
|
||||
* @return 首章目录ID
|
||||
* */
|
||||
Long queryFirstBookIndexId(Long bookId);
|
||||
|
||||
/**
|
||||
*分页查询书籍评论列表
|
||||
* @param userId 用户ID
|
||||
* @param bookId 书籍ID
|
||||
* @param page 页码
|
||||
* @param pageSize 分页大小
|
||||
* @return 评论集合
|
||||
* */
|
||||
List<BookCommentVO> listCommentByPage(Long userId, Long bookId, int page, int pageSize);
|
||||
|
||||
/**
|
||||
* 新增评价
|
||||
* @param userId 用户ID
|
||||
* @param comment 评论内容
|
||||
* */
|
||||
void addBookComment(Long userId, BookComment comment);
|
||||
|
||||
/**
|
||||
* 通过作者名获取或创建作者Id
|
||||
* @param authorName 作者名
|
||||
* @param workDirection 作品方向
|
||||
* @return 作者ID
|
||||
* */
|
||||
Long getOrCreateAuthorIdByName(String authorName, Byte workDirection);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 查询小说ID
|
||||
* @param bookName 书名
|
||||
* @param author 作者名
|
||||
* @return 小说ID
|
||||
* */
|
||||
Long queryIdByNameAndAuthor(String bookName, String author);
|
||||
|
||||
/**
|
||||
* 根据小说ID查询目录号集合
|
||||
* @param bookId 小说ID
|
||||
* @return 目录号集合
|
||||
* */
|
||||
List<Integer> queryIndexNumByBookId(Long bookId);
|
||||
|
||||
/**
|
||||
* 查询网络图片的小说
|
||||
* @param limit 查询条数
|
||||
* @param offset 开始行数
|
||||
* @return 返回小说集合
|
||||
* */
|
||||
List<Book> queryNetworkPicBooks(Integer limit, Integer offset);
|
||||
|
||||
|
||||
/**
|
||||
* 更新小说网络图片到本地
|
||||
* @param picUrl 网络图片路径
|
||||
* @param bookId 小说ID
|
||||
*/
|
||||
void updateBookPicToLocal(String picUrl, Long bookId);
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.java2nb.novel.service;
|
||||
|
||||
|
||||
import com.java2nb.novel.entity.FriendLink;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
public interface FriendLinkService {
|
||||
|
||||
/**
|
||||
* 查询首页友情链接
|
||||
* @return 集合
|
||||
* */
|
||||
List<FriendLink> listIndexLink();
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.java2nb.novel.service;
|
||||
|
||||
|
||||
import com.java2nb.novel.entity.News;
|
||||
import com.java2nb.novel.vo.NewsVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
public interface NewsService {
|
||||
|
||||
/**
|
||||
* 查询首页新闻
|
||||
* @return
|
||||
* */
|
||||
List<News> listIndexNews();
|
||||
|
||||
/**
|
||||
* 查询新闻
|
||||
* @param newsId 新闻id
|
||||
* @return 新闻
|
||||
* */
|
||||
News queryNewsInfo(Long newsId);
|
||||
|
||||
/**
|
||||
* 分页查询新闻列表
|
||||
* @param page 页码
|
||||
* @param pageSize 分页大小
|
||||
* @return 新闻集合
|
||||
* */
|
||||
List<NewsVO> listByPage(int page, int pageSize);
|
||||
}
|
@ -0,0 +1,120 @@
|
||||
package com.java2nb.novel.service;
|
||||
|
||||
|
||||
import com.java2nb.novel.core.bean.UserDetails;
|
||||
import com.java2nb.novel.form.UserForm;
|
||||
import com.java2nb.novel.vo.BookReadHistoryVO;
|
||||
import com.java2nb.novel.vo.BookShelfVO;
|
||||
import com.java2nb.novel.entity.User;
|
||||
import com.java2nb.novel.vo.UserFeedbackVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
public interface UserService {
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
* @param form 用户注册提交信息类
|
||||
* @return jwt载体信息类
|
||||
* */
|
||||
UserDetails register(UserForm form);
|
||||
|
||||
/**
|
||||
* 用户登陆
|
||||
* @param form 用户登陆提交信息类
|
||||
* @return jwt载体信息类
|
||||
* */
|
||||
UserDetails login(UserForm form);
|
||||
|
||||
/**
|
||||
* 查询小说是否已加入书架
|
||||
* @param userId 用户ID
|
||||
* @param bookId 小说ID
|
||||
* @return true:已加入书架,未加入书架
|
||||
* */
|
||||
Boolean queryIsInShelf(Long userId, Long bookId);
|
||||
|
||||
/**
|
||||
* 加入书架
|
||||
* @param userId 用户ID
|
||||
* @param bookId 小说ID
|
||||
* @param preContentId 阅读的内容ID
|
||||
* */
|
||||
void addToBookShelf(Long userId, Long bookId, Long preContentId);
|
||||
|
||||
/**
|
||||
* 移出书架
|
||||
* @param userId 用户ID
|
||||
* @param bookId 小说ID
|
||||
* */
|
||||
void removeFromBookShelf(Long userId, Long bookId);
|
||||
|
||||
/**
|
||||
* 查询书架
|
||||
* @param userId 用户ID
|
||||
* @param page
|
||||
* @param pageSize
|
||||
* @return 书架集合
|
||||
* */
|
||||
List<BookShelfVO> listBookShelfByPage(Long userId, int page, int pageSize);
|
||||
|
||||
/**
|
||||
* 添加阅读记录
|
||||
* @param userId 用户id
|
||||
* @param bookId 书籍id
|
||||
* @param preContentId 阅读的目录id
|
||||
* */
|
||||
void addReadHistory(Long userId, Long bookId, Long preContentId);
|
||||
|
||||
/**
|
||||
* 添加反馈
|
||||
* @param userId 用户id
|
||||
* @param content 反馈内容
|
||||
* */
|
||||
void addFeedBack(Long userId, String content);
|
||||
|
||||
/**
|
||||
* 分页查询我的反馈列表
|
||||
* @param userId 用户ID
|
||||
* @param page 页码
|
||||
* @param pageSize 分页大小
|
||||
* @return 反馈集合
|
||||
* */
|
||||
List<UserFeedbackVO> listUserFeedBackByPage(Long userId, int page, int pageSize);
|
||||
|
||||
/**
|
||||
* 查询个人信息
|
||||
* @param userId 用户id
|
||||
* @return 用户信息
|
||||
* */
|
||||
User userInfo(Long userId);
|
||||
|
||||
/**
|
||||
* 分页查询阅读记录
|
||||
* @param userId 用户id
|
||||
* @param page 页码
|
||||
* @param pageSize 分页大小
|
||||
* @return
|
||||
* */
|
||||
List<BookReadHistoryVO> listReadHistoryByPage(Long userId, int page, int pageSize);
|
||||
|
||||
/**
|
||||
* 更新个人信息
|
||||
* @param userId 用户id
|
||||
* @param user 需要更新的信息
|
||||
* */
|
||||
void updateUserInfo(Long userId, User user);
|
||||
|
||||
/**
|
||||
* 更新密码
|
||||
* @param userId 用户id
|
||||
* @param oldPassword 旧密码
|
||||
* @param newPassword 新密码
|
||||
* */
|
||||
void updatePassword(Long userId, String oldPassword, String newPassword);
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,403 @@
|
||||
package com.java2nb.novel.service.impl;
|
||||
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.java2nb.novel.core.cache.CacheKey;
|
||||
import com.java2nb.novel.core.cache.CacheService;
|
||||
import com.java2nb.novel.core.enums.ResponseStatus;
|
||||
import com.java2nb.novel.core.exception.BusinessException;
|
||||
import com.java2nb.novel.core.utils.BeanUtil;
|
||||
import com.java2nb.novel.core.utils.Constants;
|
||||
import com.java2nb.novel.core.utils.FileUtil;
|
||||
import com.java2nb.novel.core.utils.IdWorker;
|
||||
import com.java2nb.novel.entity.*;
|
||||
import com.java2nb.novel.entity.Book;
|
||||
import com.java2nb.novel.mapper.*;
|
||||
import com.java2nb.novel.search.BookSP;
|
||||
import com.java2nb.novel.service.BookService;
|
||||
import com.java2nb.novel.vo.BookCommentVO;
|
||||
import com.java2nb.novel.vo.BookSettingVO;
|
||||
import com.java2nb.novel.vo.BookVO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.mybatis.dynamic.sql.SortSpecification;
|
||||
import org.mybatis.dynamic.sql.render.RenderingStrategies;
|
||||
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import tk.mybatis.orderbyhelper.OrderByHelper;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.java2nb.novel.mapper.BookCategoryDynamicSqlSupport.bookCategory;
|
||||
import static com.java2nb.novel.mapper.BookCommentDynamicSqlSupport.bookComment;
|
||||
import static com.java2nb.novel.mapper.BookContentDynamicSqlSupport.bookContent;
|
||||
import static com.java2nb.novel.mapper.BookDynamicSqlSupport.*;
|
||||
import static com.java2nb.novel.mapper.BookIndexDynamicSqlSupport.bookIndex;
|
||||
import static org.mybatis.dynamic.sql.SqlBuilder.*;
|
||||
import static org.mybatis.dynamic.sql.select.SelectDSL.select;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class BookServiceImpl implements BookService {
|
||||
|
||||
/**
|
||||
* 本地图片保存路径
|
||||
* */
|
||||
@Value("${pic.save.path}")
|
||||
private String picSavePath;
|
||||
|
||||
private final FrontBookSettingMapper bookSettingMapper;
|
||||
|
||||
private final FrontBookMapper bookMapper;
|
||||
|
||||
private final BookCategoryMapper bookCategoryMapper;
|
||||
|
||||
private final BookIndexMapper bookIndexMapper;
|
||||
|
||||
private final BookContentMapper bookContentMapper;
|
||||
|
||||
private final FrontBookCommentMapper bookCommentMapper;
|
||||
|
||||
private final BookAuthorMapper bookAuthorMapper;
|
||||
|
||||
private final CacheService cacheService;
|
||||
|
||||
|
||||
@Override
|
||||
public Map<Byte, List<BookSettingVO>> listBookSettingVO() {
|
||||
Map<Byte, List<BookSettingVO>> result = (Map<Byte, List<BookSettingVO>>) cacheService.getObject(CacheKey.INDEX_BOOK_SETTINGS_KEY);
|
||||
if (result == null || result.size() == 0) {
|
||||
List<BookSettingVO> list = bookSettingMapper.listVO();
|
||||
result = list.stream().collect(Collectors.groupingBy(BookSettingVO::getType));
|
||||
cacheService.setObject(CacheKey.INDEX_BOOK_SETTINGS_KEY, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Book> listClickRank() {
|
||||
List<Book> result = (List<Book>) cacheService.getObject(CacheKey.INDEX_CLICK_BANK_BOOK_KEY);
|
||||
if (result == null || result.size() == 0) {
|
||||
result = listRank((byte) 0, 10);
|
||||
cacheService.setObject(CacheKey.INDEX_CLICK_BANK_BOOK_KEY, result, 5000);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Book> listNewRank() {
|
||||
List<Book> result = (List<Book>) cacheService.getObject(CacheKey.INDEX_NEW_BOOK_KEY);
|
||||
if (result == null || result.size() == 0) {
|
||||
result = listRank((byte) 1, 10);
|
||||
cacheService.setObject(CacheKey.INDEX_NEW_BOOK_KEY, result, 3600);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BookVO> listUpdateRank() {
|
||||
List<BookVO> result = (List<BookVO>) cacheService.getObject(CacheKey.INDEX_UPDATE_BOOK_KEY);
|
||||
if (result == null || result.size() == 0) {
|
||||
List<Book> bookPOList = listRank((byte) 2, 23);
|
||||
result = BeanUtil.copyList(bookPOList,BookVO.class);
|
||||
cacheService.setObject(CacheKey.INDEX_UPDATE_BOOK_KEY, result, 60 * 10);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BookVO> searchByPage(BookSP params, int page, int pageSize) {
|
||||
PageHelper.startPage(page, pageSize);
|
||||
if (params.getUpdatePeriod() != null) {
|
||||
long cur = System.currentTimeMillis();
|
||||
long period = params.getUpdatePeriod() * 24 * 3600 * 1000;
|
||||
long time = cur - period;
|
||||
params.setUpdateTimeMin(new Date(time));
|
||||
}
|
||||
if (StringUtils.isNotBlank(params.getSort())) {
|
||||
OrderByHelper.orderBy(params.getSort() + " desc");
|
||||
}
|
||||
return bookMapper.searchByPage(params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BookCategory> listBookCategory() {
|
||||
SelectStatementProvider selectStatementProvider = select(BookCategoryDynamicSqlSupport.id, BookCategoryDynamicSqlSupport.name, BookCategoryDynamicSqlSupport.workDirection)
|
||||
.from(bookCategory)
|
||||
.orderBy(BookCategoryDynamicSqlSupport.sort)
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
return bookCategoryMapper.selectMany(selectStatementProvider);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Book queryBookDetail(Long bookId) {
|
||||
SelectStatementProvider selectStatement = select(id, catName, catId, picUrl, bookName, authorId, authorName, bookDesc, bookStatus, visitCount, wordCount, lastIndexId, lastIndexName, lastIndexUpdateTime,score)
|
||||
.from(book)
|
||||
.where(id, isEqualTo(bookId))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
return bookMapper.selectMany(selectStatement).get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BookIndex> queryIndexList(Long bookId,String orderBy, Integer limit) {
|
||||
if(StringUtils.isNotBlank(orderBy)){
|
||||
OrderByHelper.orderBy(orderBy);
|
||||
}
|
||||
if(limit != null){
|
||||
PageHelper.startPage(1,limit);
|
||||
}
|
||||
|
||||
SelectStatementProvider selectStatement = select(BookIndexDynamicSqlSupport.id, BookIndexDynamicSqlSupport.bookId, BookIndexDynamicSqlSupport.indexNum, BookIndexDynamicSqlSupport.indexName, BookIndexDynamicSqlSupport.updateTime)
|
||||
.from(bookIndex)
|
||||
.where(BookIndexDynamicSqlSupport.bookId, isEqualTo(bookId))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
|
||||
return bookIndexMapper.selectMany(selectStatement);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BookIndex queryBookIndex(Long bookIndexId) {
|
||||
SelectStatementProvider selectStatement = select(BookIndexDynamicSqlSupport.id, BookIndexDynamicSqlSupport.bookId, BookIndexDynamicSqlSupport.indexNum, BookIndexDynamicSqlSupport.indexName, BookIndexDynamicSqlSupport.wordCount, BookIndexDynamicSqlSupport.updateTime)
|
||||
.from(bookIndex)
|
||||
.where(BookIndexDynamicSqlSupport.id, isEqualTo(bookIndexId))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
return bookIndexMapper.selectMany(selectStatement).get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long queryPreBookIndexId(Long bookId, Integer indexNum) {
|
||||
SelectStatementProvider selectStatement = select(BookIndexDynamicSqlSupport.id)
|
||||
.from(bookIndex)
|
||||
.where(BookIndexDynamicSqlSupport.bookId, isEqualTo(bookId))
|
||||
.and(BookIndexDynamicSqlSupport.indexNum, isLessThan(indexNum))
|
||||
.orderBy(BookIndexDynamicSqlSupport.indexNum.descending())
|
||||
.limit(1)
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
List<BookIndex> list = bookIndexMapper.selectMany(selectStatement);
|
||||
if (list.size() == 0) {
|
||||
return 0L;
|
||||
} else {
|
||||
return list.get(0).getId();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long queryNextBookIndexId(Long bookId, Integer indexNum) {
|
||||
SelectStatementProvider selectStatement = select(BookIndexDynamicSqlSupport.id)
|
||||
.from(bookIndex)
|
||||
.where(BookIndexDynamicSqlSupport.bookId, isEqualTo(bookId))
|
||||
.and(BookIndexDynamicSqlSupport.indexNum, isGreaterThan(indexNum))
|
||||
.orderBy(BookIndexDynamicSqlSupport.indexNum)
|
||||
.limit(1)
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
List<BookIndex> list = bookIndexMapper.selectMany(selectStatement);
|
||||
if (list.size() == 0) {
|
||||
return 0L;
|
||||
} else {
|
||||
return list.get(0).getId();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public BookContent queryBookContent(Long bookIndexId) {
|
||||
SelectStatementProvider selectStatement = select(BookContentDynamicSqlSupport.id,BookContentDynamicSqlSupport.content)
|
||||
.from(bookContent)
|
||||
.where(BookContentDynamicSqlSupport.indexId, isEqualTo(bookIndexId))
|
||||
.limit(1)
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
return bookContentMapper.selectMany(selectStatement).get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Book> listRank(Byte type, Integer limit) {
|
||||
SortSpecification sortSpecification = visitCount.descending();
|
||||
switch (type) {
|
||||
case 1: {
|
||||
//最新入库排序
|
||||
sortSpecification = createTime.descending();
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
//最新更新时间排序
|
||||
sortSpecification = lastIndexUpdateTime.descending();
|
||||
break;
|
||||
}
|
||||
case 3: {
|
||||
//评论数量排序
|
||||
sortSpecification = commentCount.descending();
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
SelectStatementProvider selectStatement = select(id, catId, catName, bookName, lastIndexId, lastIndexName, authorId, authorName, picUrl, bookDesc, wordCount, lastIndexUpdateTime)
|
||||
.from(book)
|
||||
.orderBy(sortSpecification)
|
||||
.limit(limit)
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
return bookMapper.selectMany(selectStatement);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addVisitCount(Long bookId) {
|
||||
bookMapper.addVisitCount(bookId);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public long queryIndexCount(Long bookId) {
|
||||
SelectStatementProvider selectStatement = select(count(BookIndexDynamicSqlSupport.id))
|
||||
.from(bookIndex)
|
||||
.where(BookIndexDynamicSqlSupport.bookId, isEqualTo(bookId))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
|
||||
return bookIndexMapper.count(selectStatement);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Book> listRecBookByCatId(Integer catId) {
|
||||
return bookMapper.listRecBookByCatId(catId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long queryFirstBookIndexId(Long bookId) {
|
||||
SelectStatementProvider selectStatement = select(BookIndexDynamicSqlSupport.id)
|
||||
.from(bookIndex)
|
||||
.where(BookIndexDynamicSqlSupport.bookId, isEqualTo(bookId))
|
||||
.orderBy(BookIndexDynamicSqlSupport.indexNum)
|
||||
.limit(1)
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
return bookIndexMapper.selectMany(selectStatement).get(0).getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BookCommentVO> listCommentByPage(Long userId,Long bookId, int page, int pageSize) {
|
||||
PageHelper.startPage(page, pageSize);
|
||||
OrderByHelper.orderBy("t1.create_time desc");
|
||||
return bookCommentMapper.listCommentByPage(userId,bookId);
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void addBookComment(Long userId, BookComment comment) {
|
||||
//判断该用户是否已评论过该书籍
|
||||
SelectStatementProvider selectStatement = select(count(BookCommentDynamicSqlSupport.id))
|
||||
.from(bookComment)
|
||||
.where(BookCommentDynamicSqlSupport.createUserId,isEqualTo(userId))
|
||||
.and(BookCommentDynamicSqlSupport.bookId,isEqualTo(comment.getBookId()))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
if(bookCommentMapper.count(selectStatement)>0){
|
||||
throw new BusinessException(ResponseStatus.HAS_COMMENTS);
|
||||
}
|
||||
//增加评论
|
||||
comment.setCreateUserId(userId);
|
||||
comment.setCreateTime(new Date());
|
||||
bookCommentMapper.insertSelective(comment);
|
||||
//增加书籍评论数
|
||||
bookMapper.addCommentCount(comment.getBookId());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getOrCreateAuthorIdByName(String authorName, Byte workDirection) {
|
||||
Long authorId;
|
||||
SelectStatementProvider selectStatement = select(BookAuthorDynamicSqlSupport.id)
|
||||
.from(BookAuthorDynamicSqlSupport.bookAuthor)
|
||||
.where(BookAuthorDynamicSqlSupport.penName,isEqualTo(authorName))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
List<BookAuthor> bookAuthors = bookAuthorMapper.selectMany(selectStatement);
|
||||
if(bookAuthors.size()>0){
|
||||
//作者存在
|
||||
authorId = bookAuthors.get(0).getId();
|
||||
}else{
|
||||
//作者不存在,先创建作者
|
||||
Date currentDate = new Date();
|
||||
authorId = new IdWorker().nextId();
|
||||
BookAuthor bookAuthor = new BookAuthor();
|
||||
bookAuthor.setId(authorId);
|
||||
bookAuthor.setPenName(authorName);
|
||||
bookAuthor.setWorkDirection(workDirection);
|
||||
bookAuthor.setStatus((byte) 1);
|
||||
bookAuthor.setCreateTime(currentDate);
|
||||
bookAuthor.setUpdateTime(currentDate);
|
||||
bookAuthorMapper.insertSelective(bookAuthor);
|
||||
|
||||
|
||||
}
|
||||
|
||||
return authorId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Long queryIdByNameAndAuthor(String bookName, String author) {
|
||||
//查询小说ID
|
||||
SelectStatementProvider selectStatement = select(id)
|
||||
.from(book)
|
||||
.where(BookDynamicSqlSupport.bookName,isEqualTo(bookName))
|
||||
.and(BookDynamicSqlSupport.authorName,isEqualTo(authorName))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
List<Book> books = bookMapper.selectMany(selectStatement);
|
||||
if(books.size()>0){
|
||||
return books.get(0).getId();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> queryIndexNumByBookId(Long bookId) {
|
||||
SelectStatementProvider selectStatement = select(BookIndexDynamicSqlSupport.indexNum)
|
||||
.from(BookIndexDynamicSqlSupport.bookIndex)
|
||||
.where(BookIndexDynamicSqlSupport.bookId,isEqualTo(bookId))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
|
||||
return bookIndexMapper.selectMany(selectStatement).stream().map(BookIndex::getIndexNum).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Book> queryNetworkPicBooks(Integer limit, Integer offset) {
|
||||
return bookMapper.queryNetworkPicBooks(limit,offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateBookPicToLocal(String picUrl, Long bookId) {
|
||||
|
||||
picUrl = FileUtil.network2Local(picUrl,picSavePath, Constants.LOCAL_PIC_PREFIX);
|
||||
|
||||
bookMapper.update(update(book)
|
||||
.set(BookDynamicSqlSupport.picUrl)
|
||||
.equalTo(picUrl)
|
||||
.set(updateTime)
|
||||
.equalTo(new Date())
|
||||
.where(id,isEqualTo(bookId))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3));
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package com.java2nb.novel.service.impl;
|
||||
|
||||
import com.java2nb.novel.core.utils.BeanUtil;
|
||||
import com.java2nb.novel.service.FriendLinkService;
|
||||
import com.java2nb.novel.core.cache.CacheKey;
|
||||
import com.java2nb.novel.core.cache.CacheService;
|
||||
import com.java2nb.novel.entity.FriendLink;
|
||||
import com.java2nb.novel.mapper.FriendLinkMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.mybatis.dynamic.sql.render.RenderingStrategies;
|
||||
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.java2nb.novel.mapper.FriendLinkDynamicSqlSupport.*;
|
||||
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
|
||||
import static org.mybatis.dynamic.sql.select.SelectDSL.select;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class FriendLinkServiceImpl implements FriendLinkService {
|
||||
|
||||
private final FriendLinkMapper friendLinkMapper;
|
||||
|
||||
private final CacheService cacheService;
|
||||
|
||||
|
||||
@Override
|
||||
public List<FriendLink> listIndexLink() {
|
||||
List<FriendLink> result = (List<FriendLink>) cacheService.getObject(CacheKey.INDEX_LINK_KEY);
|
||||
if(result == null || result.size() == 0) {
|
||||
SelectStatementProvider selectStatement = select(linkName,linkUrl)
|
||||
.from(friendLink)
|
||||
.where(isOpen,isEqualTo((byte)1))
|
||||
.orderBy(sort)
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
result = friendLinkMapper.selectMany(selectStatement);
|
||||
cacheService.setObject(CacheKey.INDEX_LINK_KEY,result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package com.java2nb.novel.service.impl;
|
||||
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.java2nb.novel.core.utils.BeanUtil;
|
||||
import com.java2nb.novel.service.NewsService;
|
||||
import com.java2nb.novel.core.cache.CacheKey;
|
||||
import com.java2nb.novel.core.cache.CacheService;
|
||||
import com.java2nb.novel.entity.News;
|
||||
import com.java2nb.novel.mapper.NewsMapper;
|
||||
import com.java2nb.novel.vo.NewsVO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.mybatis.dynamic.sql.render.RenderingStrategies;
|
||||
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.java2nb.novel.mapper.NewsDynamicSqlSupport.*;
|
||||
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
|
||||
import static org.mybatis.dynamic.sql.select.SelectDSL.select;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class NewsServiceImpl implements NewsService {
|
||||
|
||||
private final NewsMapper newsMapper;
|
||||
|
||||
private final CacheService cacheService;
|
||||
|
||||
|
||||
@Override
|
||||
public List<News> listIndexNews() {
|
||||
List<News> result = (List<News>) cacheService.getObject(CacheKey.INDEX_NEWS_KEY);
|
||||
if(result == null || result.size() == 0) {
|
||||
SelectStatementProvider selectStatement = select(id, catName, catId, title)
|
||||
.from(news)
|
||||
.orderBy(createTime.descending())
|
||||
.limit(2)
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
result = newsMapper.selectMany(selectStatement);
|
||||
cacheService.setObject(CacheKey.INDEX_NEWS_KEY,result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public News queryNewsInfo(Long newsId) {
|
||||
SelectStatementProvider selectStatement = select(news.allColumns())
|
||||
.from(news)
|
||||
.where(id,isEqualTo(newsId))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
return newsMapper.selectMany(selectStatement).get(0);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<NewsVO> listByPage(int page, int pageSize) {
|
||||
PageHelper.startPage(page,pageSize);
|
||||
SelectStatementProvider selectStatement = select(id, catName, catId, title,createTime)
|
||||
.from(news)
|
||||
.orderBy(createTime.descending())
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
|
||||
return BeanUtil.copyList(newsMapper.selectMany(selectStatement),NewsVO.class);
|
||||
}
|
||||
}
|
@ -0,0 +1,261 @@
|
||||
package com.java2nb.novel.service.impl;
|
||||
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.java2nb.novel.core.bean.UserDetails;
|
||||
import com.java2nb.novel.core.utils.BeanUtil;
|
||||
import com.java2nb.novel.form.UserForm;
|
||||
import com.java2nb.novel.service.UserService;
|
||||
import com.java2nb.novel.core.enums.ResponseStatus;
|
||||
import com.java2nb.novel.core.exception.BusinessException;
|
||||
import com.java2nb.novel.entity.User;
|
||||
import com.java2nb.novel.entity.UserBookshelf;
|
||||
import com.java2nb.novel.entity.UserFeedback;
|
||||
import com.java2nb.novel.entity.UserReadHistory;
|
||||
import com.java2nb.novel.mapper.*;
|
||||
import com.java2nb.novel.vo.BookReadHistoryVO;
|
||||
import com.java2nb.novel.vo.BookShelfVO;
|
||||
import com.java2nb.novel.core.utils.IdWorker;
|
||||
import com.java2nb.novel.core.utils.MD5Util;
|
||||
import com.java2nb.novel.vo.UserFeedbackVO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.codec.Charsets;
|
||||
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
|
||||
import org.mybatis.dynamic.sql.render.RenderingStrategies;
|
||||
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
|
||||
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import static com.java2nb.novel.mapper.UserBookshelfDynamicSqlSupport.userBookshelf;
|
||||
import static com.java2nb.novel.mapper.UserDynamicSqlSupport.*;
|
||||
import static com.java2nb.novel.mapper.UserFeedbackDynamicSqlSupport.userFeedback;
|
||||
import static com.java2nb.novel.mapper.UserReadHistoryDynamicSqlSupport.userReadHistory;
|
||||
import static org.mybatis.dynamic.sql.SqlBuilder.*;
|
||||
import static org.mybatis.dynamic.sql.select.SelectDSL.select;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
private final UserMapper userMapper;
|
||||
|
||||
private final FrontUserBookshelfMapper userBookshelfMapper;
|
||||
|
||||
private final FrontUserReadHistoryMapper userReadHistoryMapper;
|
||||
|
||||
private final UserFeedbackMapper userFeedbackMapper;
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public UserDetails register(UserForm form) {
|
||||
//查询用户名是否已注册
|
||||
SelectStatementProvider selectStatement = select(count(id))
|
||||
.from(user)
|
||||
.where(username, isEqualTo(form.getUsername()))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
long count = userMapper.count(selectStatement);
|
||||
if (count > 0) {
|
||||
//用户名已注册
|
||||
throw new BusinessException(ResponseStatus.USERNAME_EXIST);
|
||||
}
|
||||
User entity = new User();
|
||||
BeanUtils.copyProperties(form,entity);
|
||||
//数据库生成注册记录
|
||||
Long id = new IdWorker().nextId();
|
||||
entity.setId(id);
|
||||
entity.setNickName(entity.getUsername());
|
||||
Date currentDate = new Date();
|
||||
entity.setCreateTime(currentDate);
|
||||
entity.setUpdateTime(currentDate);
|
||||
entity.setPassword(MD5Util.MD5Encode(entity.getPassword(), Charsets.UTF_8.name()));
|
||||
userMapper.insertSelective(entity);
|
||||
//生成UserDetail对象并返回
|
||||
UserDetails userDetails = new UserDetails();
|
||||
userDetails.setId(id);
|
||||
userDetails.setUsername(entity.getUsername());
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserDetails login(UserForm form) {
|
||||
//根据用户名密码查询记录
|
||||
SelectStatementProvider selectStatement = select(id, username)
|
||||
.from(user)
|
||||
.where(username, isEqualTo(form.getUsername()))
|
||||
.and(password, isEqualTo(MD5Util.MD5Encode(form.getPassword(), Charsets.UTF_8.name())))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
List<User> users = userMapper.selectMany(selectStatement);
|
||||
if (users.size() == 0) {
|
||||
throw new BusinessException(ResponseStatus.USERNAME_PASS_ERROR);
|
||||
}
|
||||
//生成UserDetail对象并返回
|
||||
UserDetails userDetails = new UserDetails();
|
||||
userDetails.setId(users.get(0).getId());
|
||||
userDetails.setUsername(form.getUsername());
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean queryIsInShelf(Long userId, Long bookId) {
|
||||
SelectStatementProvider selectStatement = select(count(UserBookshelfDynamicSqlSupport.id))
|
||||
.from(userBookshelf)
|
||||
.where(UserBookshelfDynamicSqlSupport.userId, isEqualTo(userId))
|
||||
.and(UserBookshelfDynamicSqlSupport.bookId, isEqualTo(bookId))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
|
||||
return userBookshelfMapper.count(selectStatement) > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addToBookShelf(Long userId, Long bookId, Long preContentId) {
|
||||
if (!queryIsInShelf(userId, bookId)) {
|
||||
UserBookshelf shelf = new UserBookshelf();
|
||||
shelf.setUserId(userId);
|
||||
shelf.setBookId(bookId);
|
||||
shelf.setPreContentId(preContentId);
|
||||
shelf.setCreateTime(new Date());
|
||||
userBookshelfMapper.insert(shelf);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeFromBookShelf(Long userId, Long bookId) {
|
||||
DeleteStatementProvider deleteStatement = deleteFrom(userBookshelf)
|
||||
.where(UserBookshelfDynamicSqlSupport.userId, isEqualTo(userId))
|
||||
.and(UserBookshelfDynamicSqlSupport.bookId, isEqualTo(bookId))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
userBookshelfMapper.delete(deleteStatement);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BookShelfVO> listBookShelfByPage(Long userId, int page, int pageSize) {
|
||||
PageHelper.startPage(page, pageSize);
|
||||
return userBookshelfMapper.listBookShelf(userId);
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void addReadHistory(Long userId, Long bookId, Long preContentId) {
|
||||
|
||||
Date currentDate = new Date();
|
||||
//删除该书以前的历史记录
|
||||
DeleteStatementProvider deleteStatement = deleteFrom(userReadHistory)
|
||||
.where(UserReadHistoryDynamicSqlSupport.bookId, isEqualTo(bookId))
|
||||
.and(UserReadHistoryDynamicSqlSupport.userId, isEqualTo(userId))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
userReadHistoryMapper.delete(deleteStatement);
|
||||
|
||||
//插入该书新的历史记录
|
||||
UserReadHistory userReadHistory = new UserReadHistory();
|
||||
userReadHistory.setBookId(bookId);
|
||||
userReadHistory.setUserId(userId);
|
||||
userReadHistory.setPreContentId(preContentId);
|
||||
userReadHistory.setCreateTime(currentDate);
|
||||
userReadHistory.setUpdateTime(currentDate);
|
||||
userReadHistoryMapper.insertSelective(userReadHistory);
|
||||
|
||||
|
||||
//更新书架的阅读历史
|
||||
UpdateStatementProvider updateStatement = update(userBookshelf)
|
||||
.set(UserBookshelfDynamicSqlSupport.preContentId)
|
||||
.equalTo(preContentId)
|
||||
.set(UserBookshelfDynamicSqlSupport.updateTime)
|
||||
.equalTo(currentDate)
|
||||
.where(UserBookshelfDynamicSqlSupport.userId, isEqualTo(userId))
|
||||
.and(UserBookshelfDynamicSqlSupport.bookId, isEqualTo(bookId))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
|
||||
userBookshelfMapper.update(updateStatement);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addFeedBack(Long userId, String content) {
|
||||
UserFeedback feedback = new UserFeedback();
|
||||
feedback.setUserId(userId);
|
||||
feedback.setContent(content);
|
||||
feedback.setCreateTime(new Date());
|
||||
userFeedbackMapper.insertSelective(feedback);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserFeedbackVO> listUserFeedBackByPage(Long userId, int page, int pageSize) {
|
||||
PageHelper.startPage(page, pageSize);
|
||||
SelectStatementProvider selectStatement = select(UserFeedbackDynamicSqlSupport.content, UserFeedbackDynamicSqlSupport.createTime)
|
||||
.from(userFeedback)
|
||||
.where(UserFeedbackDynamicSqlSupport.userId, isEqualTo(userId))
|
||||
.orderBy(UserFeedbackDynamicSqlSupport.id.descending())
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
return BeanUtil.copyList(userFeedbackMapper.selectMany(selectStatement),UserFeedbackVO.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User userInfo(Long userId) {
|
||||
SelectStatementProvider selectStatement = select(username, nickName, userPhoto,userSex)
|
||||
.from(user)
|
||||
.where(id, isEqualTo(userId))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
return userMapper.selectMany(selectStatement).get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BookReadHistoryVO> listReadHistoryByPage(Long userId, int page, int pageSize) {
|
||||
PageHelper.startPage(page, pageSize);
|
||||
return userReadHistoryMapper.listReadHistory(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateUserInfo(Long userId, User user) {
|
||||
User updateUser = new User();
|
||||
updateUser.setId(userId);
|
||||
updateUser.setNickName(user.getNickName());
|
||||
updateUser.setUserSex(user.getUserSex());
|
||||
updateUser.setUpdateTime(new Date());
|
||||
userMapper.updateByPrimaryKeySelective(updateUser);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updatePassword(Long userId, String oldPassword, String newPassword) {
|
||||
SelectStatementProvider selectStatement = select(password)
|
||||
.from(user)
|
||||
.where(id,isEqualTo(userId))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
if(!userMapper.selectMany(selectStatement).get(0).getPassword().equals(MD5Util.MD5Encode(oldPassword, Charsets.UTF_8.name()))){
|
||||
throw new BusinessException(ResponseStatus.OLD_PASSWORD_ERROR);
|
||||
}
|
||||
UpdateStatementProvider updateStatement = update(user)
|
||||
.set(password)
|
||||
.equalTo(MD5Util.MD5Encode(newPassword, Charsets.UTF_8.name()))
|
||||
.where(id,isEqualTo(userId))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3);
|
||||
userMapper.update(updateStatement);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.java2nb.novel.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.java2nb.novel.entity.BookComment;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.annotation.Generated;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@Data
|
||||
public class BookCommentVO extends BookComment {
|
||||
|
||||
private String createUserName;
|
||||
|
||||
private String createUserPhoto;
|
||||
|
||||
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date createTime;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.java2nb.novel.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.java2nb.novel.entity.UserReadHistory;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@Data
|
||||
public class BookReadHistoryVO extends UserReadHistory {
|
||||
|
||||
private Integer catId;
|
||||
private String catName;
|
||||
private Long lastIndexId;
|
||||
|
||||
private String lastIndexName;
|
||||
private String bookName;
|
||||
|
||||
@JsonFormat(timezone = "GMT+8", pattern = "MM/dd HH:mm:ss")
|
||||
private Date lastIndexUpdateTime;
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.java2nb.novel.vo;
|
||||
|
||||
import com.java2nb.novel.entity.BookSetting;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@Data
|
||||
public class BookSettingVO extends BookSetting implements Serializable {
|
||||
|
||||
private String bookName;
|
||||
|
||||
private String picUrl;
|
||||
|
||||
private String authorName;
|
||||
|
||||
private String bookDesc;
|
||||
|
||||
private Float score;
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.java2nb.novel.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.java2nb.novel.entity.UserBookshelf;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author 11797
|
||||
*/
|
||||
@Data
|
||||
public class BookShelfVO extends UserBookshelf {
|
||||
|
||||
private Integer catId;
|
||||
private String catName;
|
||||
private Long lastIndexId;
|
||||
|
||||
private String lastIndexName;
|
||||
private String bookName;
|
||||
|
||||
@JsonFormat(timezone = "GMT+8", pattern = "MM/dd HH:mm:ss")
|
||||
private Date lastIndexUpdateTime;
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
}
|
20
novel-front/src/main/java/com/java2nb/novel/vo/BookVO.java
Normal file
20
novel-front/src/main/java/com/java2nb/novel/vo/BookVO.java
Normal file
@ -0,0 +1,20 @@
|
||||
package com.java2nb.novel.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.java2nb.novel.entity.Book;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
*/
|
||||
@Data
|
||||
public class BookVO extends Book implements Serializable {
|
||||
|
||||
@JsonFormat(timezone = "GMT+8", pattern = "MM/dd HH:mm")
|
||||
private Date lastIndexUpdateTime;
|
||||
|
||||
|
||||
}
|
17
novel-front/src/main/java/com/java2nb/novel/vo/NewsVO.java
Normal file
17
novel-front/src/main/java/com/java2nb/novel/vo/NewsVO.java
Normal file
@ -0,0 +1,17 @@
|
||||
package com.java2nb.novel.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.java2nb.novel.entity.News;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
*/
|
||||
@Data
|
||||
public class NewsVO extends News {
|
||||
|
||||
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
|
||||
private Date createTime;
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.java2nb.novel.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.java2nb.novel.entity.UserFeedback;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
*/
|
||||
@Data
|
||||
public class UserFeedbackVO extends UserFeedback {
|
||||
|
||||
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date createTime;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user