mirror of https://github.com/halo-dev/halo
👽 将IndexController的各个部分提取成单独的Controller,方便后期维护。
👽 将IndexController的各个部分提取成单独的Controller,方便后期维护。
pull/1/head
parent
d7d0d991db
commit
d0d7e90a16
|
@ -6,7 +6,7 @@ import cc.ryanc.halo.model.dto.Theme;
|
|||
import cc.ryanc.halo.service.AttachmentService;
|
||||
import cc.ryanc.halo.service.OptionsService;
|
||||
import cc.ryanc.halo.util.HaloUtil;
|
||||
import cc.ryanc.halo.web.controller.BaseController;
|
||||
import cc.ryanc.halo.web.controller.core.BaseController;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
|
|
|
@ -2,6 +2,7 @@ package cc.ryanc.halo.repository;
|
|||
|
||||
import cc.ryanc.halo.model.domain.Category;
|
||||
import cc.ryanc.halo.model.domain.Post;
|
||||
import cc.ryanc.halo.model.domain.Tag;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
@ -135,4 +136,13 @@ public interface PostRepository extends JpaRepository<Post,Long>{
|
|||
Page<Post> findPostByYearAndMonth(@Param("year") String year,@Param("month") String month,Pageable pageable);
|
||||
|
||||
List<Post> findPostByCategories(Category category);
|
||||
|
||||
/**
|
||||
* 根据标签查询文章
|
||||
*
|
||||
* @param tag tag
|
||||
* @param pageable pageable
|
||||
* @return page
|
||||
*/
|
||||
Page<Post> findPostsByTags(Tag tag,Pageable pageable);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package cc.ryanc.halo.service;
|
||||
|
||||
import cc.ryanc.halo.model.domain.Post;
|
||||
import cc.ryanc.halo.model.domain.Tag;
|
||||
import cc.ryanc.halo.model.dto.Archive;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
@ -179,6 +180,15 @@ public interface PostService {
|
|||
*/
|
||||
List<Post> findPostByYear(String year);
|
||||
|
||||
/**
|
||||
* 根据标签查询文章
|
||||
*
|
||||
* @param tag tag
|
||||
* @param pageable pageable
|
||||
* @return page
|
||||
*/
|
||||
Page<Post> findPostsByTags(Tag tag,Pageable pageable);
|
||||
|
||||
/**
|
||||
* 生成rss
|
||||
*
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package cc.ryanc.halo.service.impl;
|
||||
|
||||
import cc.ryanc.halo.model.domain.Post;
|
||||
import cc.ryanc.halo.model.domain.Tag;
|
||||
import cc.ryanc.halo.model.dto.Archive;
|
||||
import cc.ryanc.halo.repository.PostRepository;
|
||||
import cc.ryanc.halo.service.PostService;
|
||||
|
@ -280,6 +281,18 @@ public class PostServiceImpl implements PostService {
|
|||
return postRepository.findPostByYearAndMonth(year,month,pageable);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据标签查询文章
|
||||
*
|
||||
* @param tag tag
|
||||
* @param pageable pageable
|
||||
* @return page
|
||||
*/
|
||||
@Override
|
||||
public Page<Post> findPostsByTags(Tag tag, Pageable pageable) {
|
||||
return postRepository.findPostsByTags(tag,pageable);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成rss
|
||||
*
|
||||
|
|
|
@ -1,533 +0,0 @@
|
|||
package cc.ryanc.halo.web.controller;
|
||||
|
||||
import cc.ryanc.halo.model.domain.*;
|
||||
import cc.ryanc.halo.model.dto.Archive;
|
||||
import cc.ryanc.halo.model.dto.HaloConst;
|
||||
import cc.ryanc.halo.service.*;
|
||||
import cc.ryanc.halo.util.HaloUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.websocket.server.PathParam;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
/**
|
||||
* @author : RYAN0UP
|
||||
* @date : 2017/11/23
|
||||
* @version : 1.0
|
||||
* description : 前台控制器
|
||||
*/
|
||||
@Slf4j
|
||||
@Controller
|
||||
@RequestMapping(value = {"/","index"})
|
||||
public class IndexController extends BaseController{
|
||||
|
||||
@Autowired
|
||||
private PostService postService;
|
||||
|
||||
@Autowired
|
||||
private LinkService linkService;
|
||||
|
||||
@Autowired
|
||||
private CategoryService categoryService;
|
||||
|
||||
@Autowired
|
||||
private TagService tagService;
|
||||
|
||||
@Autowired
|
||||
private CommentService commentService;
|
||||
|
||||
@Autowired
|
||||
private MenuService menuService;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private GalleryService galleryService;
|
||||
|
||||
@Autowired
|
||||
private MailService mailService;
|
||||
|
||||
/**
|
||||
* 请求首页
|
||||
*
|
||||
* @param model model
|
||||
* @return 模板路径
|
||||
*/
|
||||
@GetMapping
|
||||
public String index(Model model){
|
||||
//调用方法渲染首页
|
||||
return this.index(model,1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 首页分页
|
||||
*
|
||||
* @param model model
|
||||
* @param page 当前页码
|
||||
* @param size 每页数量
|
||||
* @return 模板路径/themes/{theme}/index
|
||||
*/
|
||||
@GetMapping(value = "page/{page}")
|
||||
public String index(Model model,
|
||||
@PathVariable(value = "page") Integer page){
|
||||
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
|
||||
//默认显示10条
|
||||
Integer size = 10;
|
||||
//尝试加载设置选项,用于设置显示条数
|
||||
if(!StringUtils.isBlank(HaloConst.OPTIONS.get("index_posts"))){
|
||||
size = Integer.parseInt(HaloConst.OPTIONS.get("index_posts"));
|
||||
}
|
||||
//所有文章数据,分页
|
||||
Pageable pageable = new PageRequest(page-1,size,sort);
|
||||
Page<Post> posts = postService.findPostByStatus(0,pageable);
|
||||
model.addAttribute("posts",posts);
|
||||
|
||||
//文章总数
|
||||
model.addAttribute("postsCount",postService.findAllPosts().size());
|
||||
|
||||
model.addAttribute("is_home",true);
|
||||
|
||||
//用户信息
|
||||
User user = userService.findUser();
|
||||
model.addAttribute("user",user);
|
||||
|
||||
//所有分类目录
|
||||
List<Category> categories = categoryService.findAllCategories();
|
||||
model.addAttribute("categories",categories);
|
||||
|
||||
//菜单列表
|
||||
List<Menu> menus = menuService.findAllMenus();
|
||||
model.addAttribute("menus",menus);
|
||||
|
||||
//归档数据,包含[year,month,count,List<Post>]
|
||||
List<Archive> archives = postService.findPostGroupByYearAndMonth();
|
||||
model.addAttribute("archives",archives);
|
||||
|
||||
//设置选项
|
||||
model.addAttribute("options",HaloConst.OPTIONS);
|
||||
return this.render("index");
|
||||
}
|
||||
|
||||
/**
|
||||
* ajax分页
|
||||
*
|
||||
* @param page page 当前页码
|
||||
* @return List<Post>集合</>
|
||||
*/
|
||||
@GetMapping(value = "next")
|
||||
@ResponseBody
|
||||
public List<Post> ajaxIndex(@PathParam(value = "page") Integer page){
|
||||
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
|
||||
//默认显示10条
|
||||
Integer size = 10;
|
||||
//尝试加载设置选项,用于设置显示条数
|
||||
if(!StringUtils.isBlank(HaloConst.OPTIONS.get("index_posts"))){
|
||||
size = Integer.parseInt(HaloConst.OPTIONS.get("index_posts"));
|
||||
}
|
||||
|
||||
//文章数据,只获取文章,没有分页
|
||||
Pageable pageable = new PageRequest(page-1,size,sort);
|
||||
List<Post> posts = postService.findPostByStatus(0,pageable).getContent();
|
||||
return posts;
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染文章详情
|
||||
*
|
||||
* @param postUrl 文章路径名
|
||||
* @param model model
|
||||
* @return 模板路径/themes/{theme}/post
|
||||
*/
|
||||
@GetMapping(value = "archives/{postUrl}")
|
||||
public String getPost(@PathVariable String postUrl, Model model){
|
||||
Post post = postService.findByPostUrl(postUrl);
|
||||
//获得当前文章的发布日期
|
||||
Date postDate = post.getPostDate();
|
||||
try {
|
||||
//查询当前文章日期之前的所有文章
|
||||
List<Post> beforePosts = postService.findByPostDateBefore(postDate);
|
||||
|
||||
//查询当前文章日期之后的所有文章
|
||||
List<Post> afterPosts = postService.findByPostDateAfter(postDate);
|
||||
|
||||
if(null!=beforePosts&&beforePosts.size()>0){
|
||||
model.addAttribute("beforePost",beforePosts.get(beforePosts.size()-1));
|
||||
}
|
||||
if(null!=afterPosts&&afterPosts.size()>0){
|
||||
model.addAttribute("afterPost",afterPosts.get(afterPosts.size()-1));
|
||||
}
|
||||
}catch (Exception e){
|
||||
log.error("未知错误:{0}",e.getMessage());
|
||||
}
|
||||
model.addAttribute("post",post);
|
||||
|
||||
//文章总数
|
||||
model.addAttribute("postsCount",postService.findAllPosts().size());
|
||||
|
||||
//用户信息
|
||||
User user = userService.findUser();
|
||||
model.addAttribute("user",user);
|
||||
|
||||
//所有分类目录
|
||||
List<Category> categories = categoryService.findAllCategories();
|
||||
model.addAttribute("categories",categories);
|
||||
|
||||
//归档数据,包含[year,month,count,List<Post>]
|
||||
List<Archive> archives = postService.findPostGroupByYearAndMonth();
|
||||
|
||||
//菜单列表
|
||||
List<Menu> menus = menuService.findAllMenus();
|
||||
model.addAttribute("menus",menus);
|
||||
|
||||
model.addAttribute("archives",archives);
|
||||
|
||||
//设置选项
|
||||
model.addAttribute("options",HaloConst.OPTIONS);
|
||||
return this.render("post");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文章的评论
|
||||
*
|
||||
* @param postId postId 文章编号
|
||||
* @return List<Comment>集合</>
|
||||
*/
|
||||
@GetMapping(value = "/getComment/{postId}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@ResponseBody
|
||||
public List<Comment> getComment(@PathVariable Long postId){
|
||||
Optional<Post> post = postService.findByPostId(postId);
|
||||
Sort sort = new Sort(Sort.Direction.DESC,"commentDate");
|
||||
Pageable pageable = new PageRequest(0,10,sort);
|
||||
List<Comment> comments = commentService.findCommentsByPostAndCommentStatus(post.get(),pageable,2).getContent();
|
||||
if(null==comments){
|
||||
return null;
|
||||
}
|
||||
return comments;
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染关于页面
|
||||
*
|
||||
* @param model model
|
||||
* @return 模板路径/themes/{theme}/about
|
||||
*/
|
||||
@GetMapping(value = "/about")
|
||||
public String about(Model model){
|
||||
model.addAttribute("about","709831589");
|
||||
List<Category> categories = categoryService.findAllCategories();
|
||||
model.addAttribute("categories",categories);
|
||||
|
||||
//设置选项
|
||||
model.addAttribute("options",HaloConst.OPTIONS);
|
||||
return this.render("about");
|
||||
}
|
||||
|
||||
/**
|
||||
* 跳转到图库页面
|
||||
*
|
||||
* @return 模板路径/themes/{theme}/gallery
|
||||
*/
|
||||
@GetMapping(value = "/gallery")
|
||||
public String gallery(Model model){
|
||||
List<Gallery> galleries = galleryService.findAllGalleries();
|
||||
model.addAttribute("galleries",galleries);
|
||||
|
||||
//用户信息
|
||||
User user = userService.findUser();
|
||||
model.addAttribute("user",user);
|
||||
|
||||
model.addAttribute("is_gallery",true);
|
||||
|
||||
//设置选项
|
||||
model.addAttribute("options",HaloConst.OPTIONS);
|
||||
return this.render("gallery");
|
||||
}
|
||||
|
||||
/**
|
||||
* 友情链接
|
||||
*
|
||||
* @param model model
|
||||
* @return 模板路径/themes/{theme}/links
|
||||
*/
|
||||
@GetMapping(value = "/links")
|
||||
public String links(Model model){
|
||||
|
||||
//所有友情链接
|
||||
List<Link> links = linkService.findAllLinks();
|
||||
model.addAttribute("links",links);
|
||||
|
||||
//用户信息
|
||||
User user = userService.findUser();
|
||||
model.addAttribute("user",user);
|
||||
|
||||
model.addAttribute("is_links",true);
|
||||
|
||||
//文章总数
|
||||
model.addAttribute("postsCount",postService.findAllPosts().size());
|
||||
|
||||
//菜单列表
|
||||
List<Menu> menus = menuService.findAllMenus();
|
||||
model.addAttribute("menus",menus);
|
||||
|
||||
//所有分类目录
|
||||
List<Category> categories = categoryService.findAllCategories();
|
||||
model.addAttribute("categories",categories);
|
||||
|
||||
//归档数据,包含[year,month,count,List<Post>]
|
||||
List<Archive> archives = postService.findPostGroupByYearAndMonth();
|
||||
model.addAttribute("archives",archives);
|
||||
|
||||
//设置选项
|
||||
model.addAttribute("options",HaloConst.OPTIONS);
|
||||
return this.render("links");
|
||||
}
|
||||
|
||||
/**
|
||||
* 标签
|
||||
*
|
||||
* @param model model
|
||||
* @return 模板路径/themes/{theme}/tags
|
||||
*/
|
||||
@GetMapping(value = "/tags")
|
||||
public String tags(Model model){
|
||||
//所有标签
|
||||
List<Tag> tags = tagService.findAllTags();
|
||||
model.addAttribute("tags",tags);
|
||||
|
||||
//所有分类目录
|
||||
List<Category> categories = categoryService.findAllCategories();
|
||||
model.addAttribute("categories",categories);
|
||||
|
||||
User user = userService.findUser();
|
||||
model.addAttribute("user",user);
|
||||
|
||||
//文章总数
|
||||
model.addAttribute("postsCount",postService.findAllPosts().size());
|
||||
|
||||
//菜单列表
|
||||
List<Menu> menus = menuService.findAllMenus();
|
||||
model.addAttribute("menus",menus);
|
||||
|
||||
//归档数据,包含[year,month,count,List<Post>]
|
||||
List<Archive> archives = postService.findPostGroupByYearAndMonth();
|
||||
model.addAttribute("archives",archives);
|
||||
|
||||
|
||||
//设置选项
|
||||
model.addAttribute("options",HaloConst.OPTIONS);
|
||||
return this.render("tags");
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据分类路径查询文章
|
||||
*
|
||||
* @param model model
|
||||
* @param cateUrl cateUrl
|
||||
* @return string
|
||||
*/
|
||||
@GetMapping(value = "categories/{cateUrl}")
|
||||
public String categories(Model model,
|
||||
@PathVariable("cateUrl") String cateUrl){
|
||||
List<Post> posts;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 文章归档
|
||||
*
|
||||
* @param model model
|
||||
* @return 模板路径
|
||||
*/
|
||||
@GetMapping(value = "/archives")
|
||||
public String archives(Model model){
|
||||
return this.archives(model,1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文章归档分页
|
||||
*
|
||||
* @param model model
|
||||
* @param page page 当前页码
|
||||
* @return 模板路径/themes/{theme}/archives
|
||||
*/
|
||||
@GetMapping(value = "/archives/page/{page}")
|
||||
public String archives(Model model,
|
||||
@PathVariable(value = "page") Integer page){
|
||||
|
||||
//所有文章数据,分页,material主题适用
|
||||
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
|
||||
Pageable pageable = new PageRequest(page-1,5,sort);
|
||||
Page<Post> posts = postService.findPostByStatus(0,pageable);
|
||||
model.addAttribute("posts",posts);
|
||||
|
||||
//文章总数
|
||||
model.addAttribute("postsCount",postService.findAllPosts().size());
|
||||
|
||||
model.addAttribute("is_archives",true);
|
||||
|
||||
//包含[List<Post>,year,month,count]
|
||||
List<Archive> archives = postService.findPostGroupByYearAndMonth();
|
||||
model.addAttribute("archives",archives);
|
||||
|
||||
//包含[List<Post>,year,count]
|
||||
List<Archive> archivesLess = postService.findPostGroupByYear();
|
||||
model.addAttribute("archivesLess",archivesLess);
|
||||
|
||||
//用户信息
|
||||
User user = userService.findUser();
|
||||
model.addAttribute("user",user);
|
||||
|
||||
//菜单列表
|
||||
List<Menu> menus = menuService.findAllMenus();
|
||||
model.addAttribute("menus",menus);
|
||||
|
||||
//所有分类目录
|
||||
List<Category> categories = categoryService.findAllCategories();
|
||||
model.addAttribute("categories",categories);
|
||||
|
||||
//是否是归档页,用于判断输出链接
|
||||
model.addAttribute("isArchives","true");
|
||||
|
||||
//设置选项
|
||||
model.addAttribute("options",HaloConst.OPTIONS);
|
||||
return this.render("archives");
|
||||
}
|
||||
|
||||
/**
|
||||
* 文章归档,根据年月
|
||||
*
|
||||
* @param model model
|
||||
* @param year year 年份
|
||||
* @param month month 月份
|
||||
* @return 模板路径/themes/{theme}/archives
|
||||
*/
|
||||
@GetMapping(value = "/archives/{year}/{month}")
|
||||
public String archives(Model model,
|
||||
@PathVariable(value = "year") String year,
|
||||
@PathVariable(value = "month") String month){
|
||||
log.info(year);
|
||||
log.info(month);
|
||||
|
||||
//根据年月查出的文章数据,分页
|
||||
Sort sort = new Sort(Sort.Direction.DESC,"post_date");
|
||||
Pageable pageable = new PageRequest(0,5,sort);
|
||||
Page<Post> posts = postService.findPostByYearAndMonth(year,month,pageable);
|
||||
model.addAttribute("posts",posts);
|
||||
|
||||
//文章总数
|
||||
model.addAttribute("postsCount",postService.findAllPosts().size());
|
||||
|
||||
//用户信息
|
||||
User user = userService.findUser();
|
||||
model.addAttribute("user",user);
|
||||
|
||||
//分类目录
|
||||
List<Category> categories = categoryService.findAllCategories();
|
||||
model.addAttribute("categories",categories);
|
||||
|
||||
//菜单列表
|
||||
List<Menu> menus = menuService.findAllMenus();
|
||||
model.addAttribute("menus",menus);
|
||||
|
||||
//归档数据,包含[year,month,count,List<Post>]
|
||||
List<Archive> archives = postService.findPostGroupByYearAndMonth();
|
||||
model.addAttribute("archives",archives);
|
||||
|
||||
//是否是归档页,用于判断输出链接
|
||||
model.addAttribute("isArchives","true");
|
||||
|
||||
//设置选项
|
||||
model.addAttribute("options",HaloConst.OPTIONS);
|
||||
return this.render("archives");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文章rss
|
||||
*
|
||||
* @return rss
|
||||
*/
|
||||
@GetMapping(value = {"feed","feed.xml","atom.xml"},produces = {MediaType.APPLICATION_ATOM_XML_VALUE,MediaType.APPLICATION_RSS_XML_VALUE})
|
||||
@ResponseBody
|
||||
public String feed(){
|
||||
String rssPosts = HaloConst.OPTIONS.get("rss_posts");
|
||||
if(StringUtils.isBlank(rssPosts)){
|
||||
rssPosts = "20";
|
||||
}
|
||||
//获取文章列表并根据时间排序
|
||||
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
|
||||
Pageable pageable = new PageRequest(0,Integer.parseInt(rssPosts),sort);
|
||||
Page<Post> postsPage = postService.findPostByStatus(0,pageable);
|
||||
List<Post> posts = postsPage.getContent();
|
||||
return postService.buildRss(posts);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取sitemap
|
||||
*
|
||||
* @return sitemap
|
||||
*/
|
||||
@GetMapping(value = {"sitemap","sitemap.xml"},produces = MediaType.APPLICATION_XML_VALUE)
|
||||
@ResponseBody
|
||||
public String siteMap(){
|
||||
//获取文章列表并根据时间排序
|
||||
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
|
||||
Pageable pageable = new PageRequest(0,999,sort);
|
||||
Page<Post> postsPage = postService.findPostByStatus(0,pageable);
|
||||
List<Post> posts = postsPage.getContent();
|
||||
return postService.buildSiteMap(posts);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 提交新评论
|
||||
*
|
||||
* @param comment comment实体
|
||||
* @param post post实体
|
||||
* @param request request
|
||||
* @return true:评论成功,false:评论失败
|
||||
*/
|
||||
@PostMapping(value = "/newComment")
|
||||
@ResponseBody
|
||||
public boolean newComment(@ModelAttribute("comment") Comment comment,
|
||||
@ModelAttribute("post") Post post,
|
||||
HttpServletRequest request){
|
||||
if(""==comment.getCommentAuthor() || "".equals(comment.getCommentAuthor())){
|
||||
comment.setCommentAuthor("小猪佩琪");
|
||||
}
|
||||
comment.setCommentAuthorEmail(comment.getCommentAuthorEmail().toLowerCase());
|
||||
comment.setPost(post);
|
||||
comment.setCommentDate(new Date());
|
||||
comment.setCommentAuthorIp(HaloUtil.getIpAddr(request));
|
||||
comment.setIsAdmin(0);
|
||||
commentService.saveByComment(comment);
|
||||
|
||||
if("true".equals(HaloConst.OPTIONS.get("smtp_email_enable")) && "true".equals(HaloConst.OPTIONS.get("new_comment_notice"))){
|
||||
try {
|
||||
//发送邮件到博主
|
||||
Map<String,Object> map = new HashMap<>();
|
||||
map.put("author",userService.findUser().getUserDisplayName());
|
||||
map.put("pageName",postService.findByPostId(post.getPostId()).get().getPostTitle());
|
||||
map.put("blogUrl",HaloConst.OPTIONS.get("blog_url"));
|
||||
map.put("visitor",comment.getCommentAuthor());
|
||||
map.put("commentContent",comment.getCommentContent());
|
||||
mailService.sendTemplateMail(userService.findUser().getUserEmail(),"有新的评论",map,"common/mail/mail_admin.ftl");
|
||||
}catch (Exception e){
|
||||
log.error("邮件服务器未配置:{0}",e.getMessage());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@ import cc.ryanc.halo.service.LogsService;
|
|||
import cc.ryanc.halo.service.PostService;
|
||||
import cc.ryanc.halo.service.UserService;
|
||||
import cc.ryanc.halo.util.HaloUtil;
|
||||
import cc.ryanc.halo.web.controller.BaseController;
|
||||
import cc.ryanc.halo.web.controller.core.BaseController;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
|
|
|
@ -8,7 +8,7 @@ import cc.ryanc.halo.service.CommentService;
|
|||
import cc.ryanc.halo.service.MailService;
|
||||
import cc.ryanc.halo.service.UserService;
|
||||
import cc.ryanc.halo.util.HaloUtil;
|
||||
import cc.ryanc.halo.web.controller.BaseController;
|
||||
import cc.ryanc.halo.web.controller.core.BaseController;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
|
@ -28,7 +28,6 @@ import javax.websocket.server.PathParam;
|
|||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ import cc.ryanc.halo.service.LogsService;
|
|||
import cc.ryanc.halo.service.PostService;
|
||||
import cc.ryanc.halo.service.TagService;
|
||||
import cc.ryanc.halo.util.HaloUtil;
|
||||
import cc.ryanc.halo.web.controller.BaseController;
|
||||
import cc.ryanc.halo.web.controller.core.BaseController;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
|
|
|
@ -6,7 +6,7 @@ import cc.ryanc.halo.model.dto.LogsRecord;
|
|||
import cc.ryanc.halo.service.LogsService;
|
||||
import cc.ryanc.halo.service.OptionsService;
|
||||
import cc.ryanc.halo.util.HaloUtil;
|
||||
import cc.ryanc.halo.web.controller.BaseController;
|
||||
import cc.ryanc.halo.web.controller.core.BaseController;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
|
|
@ -1,53 +1,53 @@
|
|||
package cc.ryanc.halo.web.controller;
|
||||
|
||||
import cc.ryanc.halo.model.domain.Comment;
|
||||
import cc.ryanc.halo.service.CommentService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
/**
|
||||
* @author : RYAN0UP
|
||||
* @date : 2017/12/15
|
||||
* @version : 1.0
|
||||
* description:
|
||||
*/
|
||||
public abstract class BaseController {
|
||||
|
||||
/**
|
||||
* 定义默认主题
|
||||
*/
|
||||
public static String THEME = "halo";
|
||||
|
||||
@Autowired
|
||||
private CommentService commentService;
|
||||
|
||||
/**
|
||||
* 根据主题名称渲染页面
|
||||
*
|
||||
* @param pageName pageName
|
||||
* @return 返回拼接好的模板路径
|
||||
*/
|
||||
public String render(String pageName){
|
||||
StringBuffer themeStr = new StringBuffer("themes/");
|
||||
themeStr.append(THEME);
|
||||
themeStr.append("/");
|
||||
return themeStr.append(pageName).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取新评论
|
||||
*
|
||||
* @param session session
|
||||
*/
|
||||
protected void getNewComments(HttpSession session){
|
||||
Sort sort = new Sort(Sort.Direction.DESC,"commentDate");
|
||||
Pageable pageable = new PageRequest(0,999,sort);
|
||||
Page<Comment> comments = commentService.findAllComments(1,pageable);
|
||||
session.removeAttribute("newComments");
|
||||
session.setAttribute("newComments",comments.getContent());
|
||||
}
|
||||
}
|
||||
package cc.ryanc.halo.web.controller.core;
|
||||
|
||||
import cc.ryanc.halo.model.domain.Comment;
|
||||
import cc.ryanc.halo.service.CommentService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
/**
|
||||
* @author : RYAN0UP
|
||||
* @date : 2017/12/15
|
||||
* @version : 1.0
|
||||
* description:
|
||||
*/
|
||||
public abstract class BaseController {
|
||||
|
||||
/**
|
||||
* 定义默认主题
|
||||
*/
|
||||
public static String THEME = "halo";
|
||||
|
||||
@Autowired
|
||||
private CommentService commentService;
|
||||
|
||||
/**
|
||||
* 根据主题名称渲染页面
|
||||
*
|
||||
* @param pageName pageName
|
||||
* @return 返回拼接好的模板路径
|
||||
*/
|
||||
public String render(String pageName){
|
||||
StringBuffer themeStr = new StringBuffer("themes/");
|
||||
themeStr.append(THEME);
|
||||
themeStr.append("/");
|
||||
return themeStr.append(pageName).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取新评论
|
||||
*
|
||||
* @param session session
|
||||
*/
|
||||
protected void getNewComments(HttpSession session){
|
||||
Sort sort = new Sort(Sort.Direction.DESC,"commentDate");
|
||||
Pageable pageable = new PageRequest(0,999,sort);
|
||||
Page<Comment> comments = commentService.findAllComments(1,pageable);
|
||||
session.removeAttribute("newComments");
|
||||
session.setAttribute("newComments",comments.getContent());
|
||||
}
|
||||
}
|
|
@ -1,74 +1,74 @@
|
|||
package cc.ryanc.halo.web.controller;
|
||||
|
||||
import cc.ryanc.halo.model.dto.HaloConst;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.web.servlet.error.ErrorController;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
/**
|
||||
* @author : RYAN0UP
|
||||
* @date : 2017/12/26
|
||||
* @version : 1.0
|
||||
* description:
|
||||
*/
|
||||
@Slf4j
|
||||
@Controller
|
||||
public class CommonController implements ErrorController{
|
||||
|
||||
private static final String ERROR_PATH = "/error";
|
||||
|
||||
/**
|
||||
* 渲染404,500
|
||||
*
|
||||
* @param request request
|
||||
* @return string
|
||||
*/
|
||||
@GetMapping(value = ERROR_PATH)
|
||||
public String handleError(HttpServletRequest request){
|
||||
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
|
||||
if(statusCode==404) {
|
||||
return "redirect:/404";
|
||||
}else{
|
||||
return "redirect:/500";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染404页面
|
||||
*
|
||||
* @param model model
|
||||
* @return string
|
||||
*/
|
||||
@GetMapping(value = "/404")
|
||||
public String fourZeroFour(Model model){
|
||||
//设置选项
|
||||
model.addAttribute("options", HaloConst.OPTIONS);
|
||||
return "common/404";
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染500页面
|
||||
*
|
||||
* @param model model
|
||||
* @return string
|
||||
*/
|
||||
@GetMapping(value = "/500")
|
||||
public String fiveZeroZero(Model model){
|
||||
//设置选项
|
||||
model.addAttribute("options", HaloConst.OPTIONS);
|
||||
return "common/500";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the path of the error page.
|
||||
*
|
||||
* @return the error path
|
||||
*/
|
||||
@Override
|
||||
public String getErrorPath() {
|
||||
return ERROR_PATH;
|
||||
}
|
||||
}
|
||||
package cc.ryanc.halo.web.controller.core;
|
||||
|
||||
import cc.ryanc.halo.model.dto.HaloConst;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.web.servlet.error.ErrorController;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
/**
|
||||
* @author : RYAN0UP
|
||||
* @date : 2017/12/26
|
||||
* @version : 1.0
|
||||
* description:
|
||||
*/
|
||||
@Slf4j
|
||||
@Controller
|
||||
public class CommonController implements ErrorController{
|
||||
|
||||
private static final String ERROR_PATH = "/error";
|
||||
|
||||
/**
|
||||
* 渲染404,500
|
||||
*
|
||||
* @param request request
|
||||
* @return string
|
||||
*/
|
||||
@GetMapping(value = ERROR_PATH)
|
||||
public String handleError(HttpServletRequest request){
|
||||
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
|
||||
if(statusCode==404) {
|
||||
return "redirect:/404";
|
||||
}else{
|
||||
return "redirect:/500";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染404页面
|
||||
*
|
||||
* @param model model
|
||||
* @return string
|
||||
*/
|
||||
@GetMapping(value = "/404")
|
||||
public String fourZeroFour(Model model){
|
||||
//设置选项
|
||||
model.addAttribute("options", HaloConst.OPTIONS);
|
||||
return "common/404";
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染500页面
|
||||
*
|
||||
* @param model model
|
||||
* @return string
|
||||
*/
|
||||
@GetMapping(value = "/500")
|
||||
public String fiveZeroZero(Model model){
|
||||
//设置选项
|
||||
model.addAttribute("options", HaloConst.OPTIONS);
|
||||
return "common/500";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the path of the error page.
|
||||
*
|
||||
* @return the error path
|
||||
*/
|
||||
@Override
|
||||
public String getErrorPath() {
|
||||
return ERROR_PATH;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package cc.ryanc.halo.web.controller;
|
||||
package cc.ryanc.halo.web.controller.core;
|
||||
|
||||
import cc.ryanc.halo.model.domain.*;
|
||||
import cc.ryanc.halo.model.dto.HaloConst;
|
|
@ -0,0 +1,214 @@
|
|||
package cc.ryanc.halo.web.controller.front;
|
||||
|
||||
import cc.ryanc.halo.model.domain.Category;
|
||||
import cc.ryanc.halo.model.domain.Menu;
|
||||
import cc.ryanc.halo.model.domain.Post;
|
||||
import cc.ryanc.halo.model.domain.User;
|
||||
import cc.ryanc.halo.model.dto.Archive;
|
||||
import cc.ryanc.halo.model.dto.HaloConst;
|
||||
import cc.ryanc.halo.service.CategoryService;
|
||||
import cc.ryanc.halo.service.MenuService;
|
||||
import cc.ryanc.halo.service.PostService;
|
||||
import cc.ryanc.halo.service.UserService;
|
||||
import cc.ryanc.halo.web.controller.core.BaseController;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : RYAN0UP
|
||||
* @version : 1.0
|
||||
* @date : 2018/4/26
|
||||
*/
|
||||
@Slf4j
|
||||
@Controller
|
||||
@RequestMapping(value = "/archives")
|
||||
public class ArchivesController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private PostService postService;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private CategoryService categoryService;
|
||||
|
||||
@Autowired
|
||||
private MenuService menuService;
|
||||
|
||||
/**
|
||||
* 文章归档
|
||||
*
|
||||
* @param model model
|
||||
* @return 模板路径
|
||||
*/
|
||||
@GetMapping
|
||||
public String archives(Model model){
|
||||
return this.archives(model,1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文章归档分页
|
||||
*
|
||||
* @param model model
|
||||
* @param page page 当前页码
|
||||
* @return 模板路径/themes/{theme}/archives
|
||||
*/
|
||||
@GetMapping(value = "page/{page}")
|
||||
public String archives(Model model,
|
||||
@PathVariable(value = "page") Integer page){
|
||||
|
||||
//所有文章数据,分页,material主题适用
|
||||
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
|
||||
Pageable pageable = new PageRequest(page-1,5,sort);
|
||||
Page<Post> posts = postService.findPostByStatus(0,pageable);
|
||||
model.addAttribute("posts",posts);
|
||||
|
||||
//文章总数
|
||||
model.addAttribute("postsCount",postService.findAllPosts().size());
|
||||
|
||||
model.addAttribute("is_archives",true);
|
||||
|
||||
//包含[List<Post>,year,month,count]
|
||||
List<Archive> archives = postService.findPostGroupByYearAndMonth();
|
||||
model.addAttribute("archives",archives);
|
||||
|
||||
//包含[List<Post>,year,count]
|
||||
List<Archive> archivesLess = postService.findPostGroupByYear();
|
||||
model.addAttribute("archivesLess",archivesLess);
|
||||
|
||||
//用户信息
|
||||
User user = userService.findUser();
|
||||
model.addAttribute("user",user);
|
||||
|
||||
//菜单列表
|
||||
List<Menu> menus = menuService.findAllMenus();
|
||||
model.addAttribute("menus",menus);
|
||||
|
||||
//所有分类目录
|
||||
List<Category> categories = categoryService.findAllCategories();
|
||||
model.addAttribute("categories",categories);
|
||||
|
||||
//是否是归档页,用于判断输出链接
|
||||
model.addAttribute("isArchives","true");
|
||||
|
||||
//设置选项
|
||||
model.addAttribute("options",HaloConst.OPTIONS);
|
||||
return this.render("archives");
|
||||
}
|
||||
|
||||
/**
|
||||
* 文章归档,根据年月
|
||||
*
|
||||
* @param model model
|
||||
* @param year year 年份
|
||||
* @param month month 月份
|
||||
* @return 模板路径/themes/{theme}/archives
|
||||
*/
|
||||
@GetMapping(value = "{year}/{month}")
|
||||
public String archives(Model model,
|
||||
@PathVariable(value = "year") String year,
|
||||
@PathVariable(value = "month") String month){
|
||||
log.info(year);
|
||||
log.info(month);
|
||||
|
||||
//根据年月查出的文章数据,分页
|
||||
Sort sort = new Sort(Sort.Direction.DESC,"post_date");
|
||||
Pageable pageable = new PageRequest(0,5,sort);
|
||||
Page<Post> posts = postService.findPostByYearAndMonth(year,month,pageable);
|
||||
model.addAttribute("posts",posts);
|
||||
|
||||
//文章总数
|
||||
model.addAttribute("postsCount",postService.findAllPosts().size());
|
||||
|
||||
//用户信息
|
||||
User user = userService.findUser();
|
||||
model.addAttribute("user",user);
|
||||
|
||||
//分类目录
|
||||
List<Category> categories = categoryService.findAllCategories();
|
||||
model.addAttribute("categories",categories);
|
||||
|
||||
//菜单列表
|
||||
List<Menu> menus = menuService.findAllMenus();
|
||||
model.addAttribute("menus",menus);
|
||||
|
||||
//归档数据,包含[year,month,count,List<Post>]
|
||||
List<Archive> archives = postService.findPostGroupByYearAndMonth();
|
||||
model.addAttribute("archives",archives);
|
||||
|
||||
//是否是归档页,用于判断输出链接
|
||||
model.addAttribute("isArchives","true");
|
||||
|
||||
//设置选项
|
||||
model.addAttribute("options",HaloConst.OPTIONS);
|
||||
return this.render("archives");
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染文章详情
|
||||
*
|
||||
* @param postUrl 文章路径名
|
||||
* @param model model
|
||||
* @return 模板路径/themes/{theme}/post
|
||||
*/
|
||||
@GetMapping(value = "{postUrl}")
|
||||
public String getPost(@PathVariable String postUrl, Model model){
|
||||
Post post = postService.findByPostUrl(postUrl);
|
||||
//获得当前文章的发布日期
|
||||
Date postDate = post.getPostDate();
|
||||
try {
|
||||
//查询当前文章日期之前的所有文章
|
||||
List<Post> beforePosts = postService.findByPostDateBefore(postDate);
|
||||
|
||||
//查询当前文章日期之后的所有文章
|
||||
List<Post> afterPosts = postService.findByPostDateAfter(postDate);
|
||||
|
||||
if(null!=beforePosts&&beforePosts.size()>0){
|
||||
model.addAttribute("beforePost",beforePosts.get(beforePosts.size()-1));
|
||||
}
|
||||
if(null!=afterPosts&&afterPosts.size()>0){
|
||||
model.addAttribute("afterPost",afterPosts.get(afterPosts.size()-1));
|
||||
}
|
||||
}catch (Exception e){
|
||||
log.error("未知错误:{0}",e.getMessage());
|
||||
}
|
||||
model.addAttribute("post",post);
|
||||
|
||||
//文章总数
|
||||
model.addAttribute("postsCount",postService.findAllPosts().size());
|
||||
|
||||
//用户信息
|
||||
User user = userService.findUser();
|
||||
model.addAttribute("user",user);
|
||||
|
||||
//所有分类目录
|
||||
List<Category> categories = categoryService.findAllCategories();
|
||||
model.addAttribute("categories",categories);
|
||||
|
||||
//归档数据,包含[year,month,count,List<Post>]
|
||||
List<Archive> archives = postService.findPostGroupByYearAndMonth();
|
||||
|
||||
//菜单列表
|
||||
List<Menu> menus = menuService.findAllMenus();
|
||||
model.addAttribute("menus",menus);
|
||||
|
||||
model.addAttribute("archives",archives);
|
||||
|
||||
//设置选项
|
||||
model.addAttribute("options",HaloConst.OPTIONS);
|
||||
return this.render("post");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package cc.ryanc.halo.web.controller.front;
|
||||
|
||||
import cc.ryanc.halo.model.domain.Post;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : RYAN0UP
|
||||
* @version : 1.0
|
||||
* @date : 2018/4/26
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping(value = "categories")
|
||||
public class CategoriesController {
|
||||
|
||||
/**
|
||||
* 根据分类路径查询文章
|
||||
*
|
||||
* @param model model
|
||||
* @param cateUrl cateUrl
|
||||
* @return string
|
||||
*/
|
||||
@GetMapping(value = "{cateUrl}")
|
||||
public String categories(Model model,
|
||||
@PathVariable("cateUrl") String cateUrl){
|
||||
List<Post> posts;
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,102 @@
|
|||
package cc.ryanc.halo.web.controller.front;
|
||||
|
||||
import cc.ryanc.halo.model.domain.Comment;
|
||||
import cc.ryanc.halo.model.domain.Post;
|
||||
import cc.ryanc.halo.model.dto.HaloConst;
|
||||
import cc.ryanc.halo.service.CommentService;
|
||||
import cc.ryanc.halo.service.MailService;
|
||||
import cc.ryanc.halo.service.PostService;
|
||||
import cc.ryanc.halo.service.UserService;
|
||||
import cc.ryanc.halo.util.HaloUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author : RYAN0UP
|
||||
* @version : 1.0
|
||||
* @date : 2018/4/26
|
||||
*/
|
||||
@Slf4j
|
||||
@Controller
|
||||
public class CommentsController {
|
||||
|
||||
@Autowired
|
||||
private CommentService commentService;
|
||||
|
||||
@Autowired
|
||||
private PostService postService;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private MailService mailService;
|
||||
|
||||
/**
|
||||
* 获取文章的评论
|
||||
*
|
||||
* @param postId postId 文章编号
|
||||
* @return List<Comment>集合</>
|
||||
*/
|
||||
@GetMapping(value = "/getComment/{postId}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@ResponseBody
|
||||
public List<Comment> getComment(@PathVariable Long postId){
|
||||
Optional<Post> post = postService.findByPostId(postId);
|
||||
Sort sort = new Sort(Sort.Direction.DESC,"commentDate");
|
||||
Pageable pageable = new PageRequest(0,10,sort);
|
||||
List<Comment> comments = commentService.findCommentsByPostAndCommentStatus(post.get(),pageable,2).getContent();
|
||||
if(null==comments){
|
||||
return null;
|
||||
}
|
||||
return comments;
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交新评论
|
||||
*
|
||||
* @param comment comment实体
|
||||
* @param post post实体
|
||||
* @param request request
|
||||
* @return true:评论成功,false:评论失败
|
||||
*/
|
||||
@PostMapping(value = "/newComment")
|
||||
@ResponseBody
|
||||
public boolean newComment(@ModelAttribute("comment") Comment comment,
|
||||
@ModelAttribute("post") Post post,
|
||||
HttpServletRequest request){
|
||||
if(""==comment.getCommentAuthor() || "".equals(comment.getCommentAuthor())){
|
||||
comment.setCommentAuthor("小猪佩琪");
|
||||
}
|
||||
comment.setCommentAuthorEmail(comment.getCommentAuthorEmail().toLowerCase());
|
||||
comment.setPost(post);
|
||||
comment.setCommentDate(new Date());
|
||||
comment.setCommentAuthorIp(HaloUtil.getIpAddr(request));
|
||||
comment.setIsAdmin(0);
|
||||
commentService.saveByComment(comment);
|
||||
|
||||
if("true".equals(HaloConst.OPTIONS.get("smtp_email_enable")) && "true".equals(HaloConst.OPTIONS.get("new_comment_notice"))){
|
||||
try {
|
||||
//发送邮件到博主
|
||||
Map<String,Object> map = new HashMap<>();
|
||||
map.put("author",userService.findUser().getUserDisplayName());
|
||||
map.put("pageName",postService.findByPostId(post.getPostId()).get().getPostTitle());
|
||||
map.put("blogUrl",HaloConst.OPTIONS.get("blog_url"));
|
||||
map.put("visitor",comment.getCommentAuthor());
|
||||
map.put("commentContent",comment.getCommentContent());
|
||||
mailService.sendTemplateMail(userService.findUser().getUserEmail(),"有新的评论",map,"common/mail/mail_admin.ftl");
|
||||
}catch (Exception e){
|
||||
log.error("邮件服务器未配置:{0}",e.getMessage());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,134 @@
|
|||
package cc.ryanc.halo.web.controller.front;
|
||||
|
||||
import cc.ryanc.halo.model.domain.Category;
|
||||
import cc.ryanc.halo.model.domain.Menu;
|
||||
import cc.ryanc.halo.model.domain.Post;
|
||||
import cc.ryanc.halo.model.domain.User;
|
||||
import cc.ryanc.halo.model.dto.Archive;
|
||||
import cc.ryanc.halo.model.dto.HaloConst;
|
||||
import cc.ryanc.halo.service.CategoryService;
|
||||
import cc.ryanc.halo.service.MenuService;
|
||||
import cc.ryanc.halo.service.PostService;
|
||||
import cc.ryanc.halo.service.UserService;
|
||||
import cc.ryanc.halo.web.controller.core.BaseController;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
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.ResponseBody;
|
||||
|
||||
import javax.websocket.server.PathParam;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : RYAN0UP
|
||||
* @version : 1.0
|
||||
* @date : 2018/4/26
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping(value = {"/","index"})
|
||||
public class IndexController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private PostService postService;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private CategoryService categoryService;
|
||||
|
||||
@Autowired
|
||||
private MenuService menuService;
|
||||
|
||||
/**
|
||||
* 请求首页
|
||||
*
|
||||
* @param model model
|
||||
* @return 模板路径
|
||||
*/
|
||||
@GetMapping
|
||||
public String index(Model model){
|
||||
//调用方法渲染首页
|
||||
return this.index(model,1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 首页分页
|
||||
*
|
||||
* @param model model
|
||||
* @param page 当前页码
|
||||
* @param size 每页数量
|
||||
* @return 模板路径/themes/{theme}/index
|
||||
*/
|
||||
@GetMapping(value = "page/{page}")
|
||||
public String index(Model model,
|
||||
@PathVariable(value = "page") Integer page){
|
||||
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
|
||||
//默认显示10条
|
||||
Integer size = 10;
|
||||
//尝试加载设置选项,用于设置显示条数
|
||||
if(!StringUtils.isBlank(HaloConst.OPTIONS.get("index_posts"))){
|
||||
size = Integer.parseInt(HaloConst.OPTIONS.get("index_posts"));
|
||||
}
|
||||
//所有文章数据,分页
|
||||
Pageable pageable = new PageRequest(page-1,size,sort);
|
||||
Page<Post> posts = postService.findPostByStatus(0,pageable);
|
||||
model.addAttribute("posts",posts);
|
||||
|
||||
//文章总数
|
||||
model.addAttribute("postsCount",postService.findAllPosts().size());
|
||||
|
||||
model.addAttribute("is_home",true);
|
||||
|
||||
//用户信息
|
||||
User user = userService.findUser();
|
||||
model.addAttribute("user",user);
|
||||
|
||||
//所有分类目录
|
||||
List<Category> categories = categoryService.findAllCategories();
|
||||
model.addAttribute("categories",categories);
|
||||
|
||||
//菜单列表
|
||||
List<Menu> menus = menuService.findAllMenus();
|
||||
model.addAttribute("menus",menus);
|
||||
|
||||
//归档数据,包含[year,month,count,List<Post>]
|
||||
List<Archive> archives = postService.findPostGroupByYearAndMonth();
|
||||
model.addAttribute("archives",archives);
|
||||
|
||||
//设置选项
|
||||
model.addAttribute("options",HaloConst.OPTIONS);
|
||||
return this.render("index");
|
||||
}
|
||||
|
||||
/**
|
||||
* ajax分页
|
||||
*
|
||||
* @param page page 当前页码
|
||||
* @return List<Post>集合</>
|
||||
*/
|
||||
@GetMapping(value = "next")
|
||||
@ResponseBody
|
||||
public List<Post> ajaxIndex(@PathParam(value = "page") Integer page){
|
||||
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
|
||||
//默认显示10条
|
||||
Integer size = 10;
|
||||
//尝试加载设置选项,用于设置显示条数
|
||||
if(!StringUtils.isBlank(HaloConst.OPTIONS.get("index_posts"))){
|
||||
size = Integer.parseInt(HaloConst.OPTIONS.get("index_posts"));
|
||||
}
|
||||
|
||||
//文章数据,只获取文章,没有分页
|
||||
Pageable pageable = new PageRequest(page-1,size,sort);
|
||||
List<Post> posts = postService.findPostByStatus(0,pageable).getContent();
|
||||
return posts;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package cc.ryanc.halo.web.controller.front;
|
||||
|
||||
import cc.ryanc.halo.model.domain.Post;
|
||||
import cc.ryanc.halo.model.dto.HaloConst;
|
||||
import cc.ryanc.halo.service.PostService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : RYAN0UP
|
||||
* @version : 1.0
|
||||
* @date : 2018/4/26
|
||||
*/
|
||||
@Controller
|
||||
public class OthersController {
|
||||
|
||||
@Autowired
|
||||
private PostService postService;
|
||||
|
||||
/**
|
||||
* 获取文章rss
|
||||
*
|
||||
* @return rss
|
||||
*/
|
||||
@GetMapping(value = {"feed","feed.xml","atom.xml"},produces = {MediaType.APPLICATION_ATOM_XML_VALUE,MediaType.APPLICATION_RSS_XML_VALUE})
|
||||
@ResponseBody
|
||||
public String feed(){
|
||||
String rssPosts = HaloConst.OPTIONS.get("rss_posts");
|
||||
if(StringUtils.isBlank(rssPosts)){
|
||||
rssPosts = "20";
|
||||
}
|
||||
//获取文章列表并根据时间排序
|
||||
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
|
||||
Pageable pageable = new PageRequest(0,Integer.parseInt(rssPosts),sort);
|
||||
Page<Post> postsPage = postService.findPostByStatus(0,pageable);
|
||||
List<Post> posts = postsPage.getContent();
|
||||
return postService.buildRss(posts);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取sitemap
|
||||
*
|
||||
* @return sitemap
|
||||
*/
|
||||
@GetMapping(value = {"sitemap","sitemap.xml"},produces = MediaType.APPLICATION_XML_VALUE)
|
||||
@ResponseBody
|
||||
public String siteMap(){
|
||||
//获取文章列表并根据时间排序
|
||||
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
|
||||
Pageable pageable = new PageRequest(0,999,sort);
|
||||
Page<Post> postsPage = postService.findPostByStatus(0,pageable);
|
||||
List<Post> posts = postsPage.getContent();
|
||||
return postService.buildSiteMap(posts);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,116 @@
|
|||
package cc.ryanc.halo.web.controller.front;
|
||||
|
||||
import cc.ryanc.halo.model.domain.*;
|
||||
import cc.ryanc.halo.model.dto.Archive;
|
||||
import cc.ryanc.halo.model.dto.HaloConst;
|
||||
import cc.ryanc.halo.service.*;
|
||||
import cc.ryanc.halo.web.controller.core.BaseController;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : RYAN0UP
|
||||
* @version : 1.0
|
||||
* @date : 2018/4/26
|
||||
*/
|
||||
@Controller
|
||||
public class PagesController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private GalleryService galleryService;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private PostService postService;
|
||||
|
||||
@Autowired
|
||||
private MenuService menuService;
|
||||
|
||||
@Autowired
|
||||
private CategoryService categoryService;
|
||||
|
||||
@Autowired
|
||||
private LinkService linkService;
|
||||
|
||||
|
||||
/**
|
||||
* 渲染关于页面
|
||||
*
|
||||
* @param model model
|
||||
* @return 模板路径/themes/{theme}/about
|
||||
*/
|
||||
@GetMapping(value = "/about")
|
||||
public String about(Model model){
|
||||
model.addAttribute("about","709831589");
|
||||
|
||||
//设置选项
|
||||
model.addAttribute("options",HaloConst.OPTIONS);
|
||||
return this.render("about");
|
||||
}
|
||||
|
||||
/**
|
||||
* 跳转到图库页面
|
||||
*
|
||||
* @return 模板路径/themes/{theme}/gallery
|
||||
*/
|
||||
@GetMapping(value = "/gallery")
|
||||
public String gallery(Model model){
|
||||
List<Gallery> galleries = galleryService.findAllGalleries();
|
||||
model.addAttribute("galleries",galleries);
|
||||
|
||||
//用户信息
|
||||
User user = userService.findUser();
|
||||
model.addAttribute("user",user);
|
||||
|
||||
model.addAttribute("is_gallery",true);
|
||||
|
||||
//设置选项
|
||||
model.addAttribute("options",HaloConst.OPTIONS);
|
||||
return this.render("gallery");
|
||||
}
|
||||
|
||||
/**
|
||||
* 友情链接
|
||||
*
|
||||
* @param model model
|
||||
* @return 模板路径/themes/{theme}/links
|
||||
*/
|
||||
@GetMapping(value = "/links")
|
||||
public String links(Model model){
|
||||
|
||||
//所有友情链接
|
||||
List<Link> links = linkService.findAllLinks();
|
||||
model.addAttribute("links",links);
|
||||
|
||||
//用户信息
|
||||
User user = userService.findUser();
|
||||
model.addAttribute("user",user);
|
||||
|
||||
model.addAttribute("is_links",true);
|
||||
|
||||
//文章总数
|
||||
model.addAttribute("postsCount",postService.findAllPosts().size());
|
||||
|
||||
//菜单列表
|
||||
List<Menu> menus = menuService.findAllMenus();
|
||||
model.addAttribute("menus",menus);
|
||||
|
||||
//所有分类目录
|
||||
List<Category> categories = categoryService.findAllCategories();
|
||||
model.addAttribute("categories",categories);
|
||||
|
||||
//归档数据,包含[year,month,count,List<Post>]
|
||||
List<Archive> archives = postService.findPostGroupByYearAndMonth();
|
||||
model.addAttribute("archives",archives);
|
||||
|
||||
//设置选项
|
||||
model.addAttribute("options",HaloConst.OPTIONS);
|
||||
return this.render("links");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,124 @@
|
|||
package cc.ryanc.halo.web.controller.front;
|
||||
|
||||
import cc.ryanc.halo.model.domain.*;
|
||||
import cc.ryanc.halo.model.dto.Archive;
|
||||
import cc.ryanc.halo.model.dto.HaloConst;
|
||||
import cc.ryanc.halo.service.*;
|
||||
import cc.ryanc.halo.web.controller.core.BaseController;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : RYAN0UP
|
||||
* @version : 1.0
|
||||
* @date : 2018/4/26
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping(value = "/tags")
|
||||
public class TagsController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private TagService tagService;
|
||||
|
||||
@Autowired
|
||||
private CategoryService categoryService;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private PostService postService;
|
||||
|
||||
@Autowired
|
||||
private MenuService menuService;
|
||||
|
||||
/**
|
||||
* 标签
|
||||
*
|
||||
* @param model model
|
||||
* @return 模板路径/themes/{theme}/tags
|
||||
*/
|
||||
@GetMapping
|
||||
public String tags(Model model){
|
||||
//所有标签
|
||||
List<Tag> tags = tagService.findAllTags();
|
||||
model.addAttribute("tags",tags);
|
||||
|
||||
//所有分类目录
|
||||
List<Category> categories = categoryService.findAllCategories();
|
||||
model.addAttribute("categories",categories);
|
||||
|
||||
User user = userService.findUser();
|
||||
model.addAttribute("user",user);
|
||||
|
||||
//文章总数
|
||||
model.addAttribute("postsCount",postService.findAllPosts().size());
|
||||
|
||||
//菜单列表
|
||||
List<Menu> menus = menuService.findAllMenus();
|
||||
model.addAttribute("menus",menus);
|
||||
|
||||
//归档数据,包含[year,month,count,List<Post>]
|
||||
List<Archive> archives = postService.findPostGroupByYearAndMonth();
|
||||
model.addAttribute("archives",archives);
|
||||
|
||||
|
||||
//设置选项
|
||||
model.addAttribute("options",HaloConst.OPTIONS);
|
||||
return this.render("tags");
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据标签路径查询所有文章
|
||||
*
|
||||
* @param tagUrl 标签路径
|
||||
* @param model model
|
||||
* @return string
|
||||
*/
|
||||
@GetMapping(value = "{tagUrl}")
|
||||
public String tags(Model model,
|
||||
@PathVariable("tagUrl") String tagUrl){
|
||||
return this.tags(model,tagUrl,1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据标签路径查询所有文章 分页
|
||||
* @param model model
|
||||
* @param tagUrl 标签路径
|
||||
* @param page 页码
|
||||
* @return string
|
||||
*/
|
||||
@GetMapping(value = "{tagUrl}/page/{page}")
|
||||
public String tags(Model model,
|
||||
@PathVariable("tagUrl") String tagUrl,
|
||||
@PathVariable("page") Integer page){
|
||||
Tag tag = tagService.findByTagUrl(tagUrl);
|
||||
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
|
||||
Integer size = 10;
|
||||
if(!StringUtils.isBlank(HaloConst.OPTIONS.get("index_posts"))){
|
||||
size = Integer.parseInt(HaloConst.OPTIONS.get("index_posts"));
|
||||
}
|
||||
Pageable pageable = new PageRequest(page-1,size,sort);
|
||||
Page<Post> posts = postService.findPostsByTags(tag,pageable);
|
||||
model.addAttribute("posts",posts);
|
||||
User user = userService.findUser();
|
||||
model.addAttribute("user",user);
|
||||
//菜单列表
|
||||
List<Menu> menus = menuService.findAllMenus();
|
||||
model.addAttribute("menus",menus);
|
||||
//设置选项
|
||||
model.addAttribute("options",HaloConst.OPTIONS);
|
||||
return this.render("index");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue