You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
4.8 KiB
138 lines
4.8 KiB
package cc.bnblogs.controller; |
|
|
|
import cc.bnblogs.common.ArticleSearch; |
|
import cc.bnblogs.common.Result; |
|
import cc.bnblogs.common.WebSite; |
|
import cc.bnblogs.pojo.Article; |
|
import cc.bnblogs.pojo.Category; |
|
import cc.bnblogs.service.ArticleService; |
|
import cc.bnblogs.service.CategoryService; |
|
import cc.bnblogs.service.CommentService; |
|
import org.springframework.stereotype.Controller; |
|
import org.springframework.ui.Model; |
|
import org.springframework.util.DigestUtils; |
|
import org.springframework.web.bind.annotation.GetMapping; |
|
import org.springframework.web.bind.annotation.PostMapping; |
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
import org.springframework.web.bind.annotation.RequestParam; |
|
import org.thymeleaf.util.StringUtils; |
|
|
|
import javax.servlet.http.HttpSession; |
|
import java.nio.charset.StandardCharsets; |
|
import java.util.List; |
|
import java.util.Objects; |
|
|
|
/** |
|
* @author zfp@bnblogs.cc |
|
* @createTime: 2022/10/16 |
|
*/ |
|
@Controller |
|
@RequestMapping("/admin") |
|
public class AdminController { |
|
private final CategoryService categoryService; |
|
private final ArticleService articleService; |
|
private final CommentService commentService; |
|
private final WebSite webSite; |
|
public AdminController(CategoryService categoryService, ArticleService articleService, CommentService commentService, WebSite webSite) { |
|
this.categoryService = categoryService; |
|
this.articleService = articleService; |
|
this.commentService = commentService; |
|
this.webSite = webSite; |
|
} |
|
|
|
@GetMapping("/") |
|
public String index(Model model) { |
|
// 获取文章总数、分类总数、总评论数、未读评论数、最新的5篇文章、最新的5条评论 |
|
model.addAttribute("ArticleCount",articleService.count()); |
|
model.addAttribute("CategoryCount",categoryService.count()); |
|
model.addAttribute("CommentAll",commentService.count(null)); |
|
model.addAttribute("CommentNotReadCount",commentService.count(false)); |
|
model.addAttribute("NewArticleList",articleService.search(ArticleSearch.builder() |
|
.type(Article.TYPE_ARTICLE).pageNum(1).pageSize(5).build()).getRows()); |
|
model.addAttribute("NewCommentList",commentService.list(null,1,5).getRows()); |
|
return "admin/index"; |
|
} |
|
@GetMapping("/category.html") |
|
public String category() { |
|
return "admin/category"; |
|
} |
|
|
|
@GetMapping("/friends.html") |
|
public String friends() { |
|
return "admin/friends"; |
|
} |
|
|
|
@GetMapping("/banner.html") |
|
public String banner() { |
|
return "admin/banner"; |
|
} |
|
|
|
@GetMapping("/article.html") |
|
public String article(Model model) { |
|
// 获取分类列表 |
|
List<Category> categories = categoryService.list(); |
|
model.addAttribute("categories",categories); |
|
return "admin/article"; |
|
} |
|
|
|
@GetMapping("/comment.html") |
|
public String comment(@RequestParam(required = false)Boolean view, Model model) { |
|
// 给前端返回一个isView参数用来选中对应按扭 |
|
model.addAttribute("isView",view); |
|
return "admin/comment"; |
|
} |
|
|
|
@GetMapping("/navigation.html") |
|
public String navigation() { |
|
return "admin/navigation"; |
|
} |
|
|
|
// 打开编辑文章界面可以带一个id参数 |
|
@GetMapping("/write.html") |
|
public String write(@RequestParam(required = false)Integer id, Model model) { |
|
// 获取分类列表 |
|
List<Category> categories = categoryService.list(); |
|
// 默认开启评论 |
|
Article article = Objects.isNull(id) ? Article.builder().allowComment(1).build() : articleService.detail(id); |
|
model.addAttribute("categories",categories); |
|
model.addAttribute("article",article); |
|
model.addAttribute("pageTitle", Objects.isNull(article.getId()) ? "创建新文章" : "编辑文章---" + article.getTitle()); |
|
return "admin/write"; |
|
} |
|
|
|
/** |
|
* 前台访问登陆页面 |
|
* @return |
|
*/ |
|
@GetMapping("/login.html") |
|
public String login() { |
|
return "admin/login"; |
|
} |
|
|
|
/** |
|
* 登录校验 |
|
* @param username 用户名 |
|
* @param password 密码 |
|
* @return |
|
*/ |
|
@PostMapping("/login") |
|
public String login(String username, String password, Model model, HttpSession session) { |
|
// 用户名和密码都一致,登录成功 |
|
if (StringUtils.equals(webSite.getPassword(), DigestUtils |
|
.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8))) |
|
&& StringUtils.equals(username,webSite.getUsername())) { |
|
session.setAttribute(WebSite.LOGIN_SIGN,true); |
|
return "redirect:/admin/"; |
|
} |
|
else { |
|
model.addAttribute("errorMsg","用户名或密码错误!"); |
|
return "admin/login"; |
|
} |
|
} |
|
|
|
@GetMapping("/logout") |
|
public String logout(HttpSession session) { |
|
session.removeAttribute(WebSite.LOGIN_SIGN); |
|
return "admin/login"; |
|
} |
|
}
|
|
|