|
@@ -0,0 +1,151 @@
|
|
|
+package com.firerock.webttkuaiban.demos.controller;
|
|
|
+
|
|
|
+import com.firerock.webttkuaiban.demos.pojo.Article;
|
|
|
+import com.firerock.webttkuaiban.demos.service.ArticleService;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.ui.Model;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Base64;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Controller
|
|
|
+public class ArticleTemplateNewController {
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(ArticleTemplateNewController.class);
|
|
|
+ @Autowired
|
|
|
+ ArticleService articleService;
|
|
|
+
|
|
|
+ @GetMapping("/page-list-{pageIndex}.html") // 这里的 PageBean 是事先定义好的实体类
|
|
|
+ public Object PageList(Model model, @PathVariable("pageIndex") Integer pageIndex, @RequestParam(required = false) String info) {
|
|
|
+ Integer pageSize = 10;
|
|
|
+ // 定义格式化器
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
+ List<Article> articleList = articleService.PageList(pageIndex, pageSize, info);
|
|
|
+ Integer total = articleService.getTotal(info);
|
|
|
+ if (!articleList.isEmpty()) {
|
|
|
+ for (Article article : articleList) {
|
|
|
+ byte[] imageData = article.getCoverImg();
|
|
|
+ if (imageData != null) {
|
|
|
+ String base64Image = Base64.getEncoder().encodeToString(imageData);
|
|
|
+ article.setBaseImage(base64Image);
|
|
|
+ } else {
|
|
|
+ article.setBaseImage("");
|
|
|
+ }
|
|
|
+ article.setCreateTimeStr(article.getCreateTime().format(formatter));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ model.addAttribute("knowledgeFieldTableList", articleList);
|
|
|
+ model.addAttribute("total", total);
|
|
|
+ return "knowledge";
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(value = "/articleList", method = RequestMethod.GET)
|
|
|
+ public String articleList(Model model) {
|
|
|
+ List<HashMap> articles = new ArrayList<>();
|
|
|
+ for (int i = 0; i < 10; i++) {
|
|
|
+ HashMap<String, String> article = new HashMap<>();
|
|
|
+ article.put("id", "" + (i + 1));
|
|
|
+ article.put("title", "Article Title " + i);
|
|
|
+ article.put("content", "Article Content " + i);
|
|
|
+ articles.add(article);
|
|
|
+ }
|
|
|
+ model.addAttribute("articles", articles);
|
|
|
+ return "articleList";
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/article-{id}.html") // 这里的 PageBean 是事先定义好的实体类
|
|
|
+ public Object articleDetail(Model model, @PathVariable("id") Integer id) {
|
|
|
+ // 定义格式化器
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
+
|
|
|
+ long l = System.currentTimeMillis();
|
|
|
+ List<Article> latestList = articleService.latestList();
|
|
|
+ if (!latestList.isEmpty()) {
|
|
|
+ for (Article article : latestList) {
|
|
|
+ byte[] imageData = article.getCoverImg();
|
|
|
+ if (imageData != null) {
|
|
|
+ String base64Image = Base64.getEncoder().encodeToString(imageData);
|
|
|
+ article.setBaseImage(base64Image);
|
|
|
+ } else {
|
|
|
+ article.setBaseImage("");
|
|
|
+ }
|
|
|
+ article.setCreateTimeStr(article.getCreateTime().format(formatter));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<Article> relatedList = articleService.relatedList(id);
|
|
|
+ if (!relatedList.isEmpty()) {
|
|
|
+ for (Article article : relatedList) {
|
|
|
+ byte[] imageData = article.getCoverImg();
|
|
|
+ if (imageData != null) {
|
|
|
+ String base64Image = Base64.getEncoder().encodeToString(imageData);
|
|
|
+ article.setBaseImage(base64Image);
|
|
|
+ } else {
|
|
|
+ article.setBaseImage("");
|
|
|
+ }
|
|
|
+ article.setCreateTimeStr(article.getCreateTime().format(formatter));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Article article = articleService.getArticleById(id);
|
|
|
+ article.setCreateTimeStr(article.getCreateTime().format(formatter));
|
|
|
+
|
|
|
+ articleService.updateViewCountById(id);
|
|
|
+
|
|
|
+ model.addAttribute("latestList", latestList);
|
|
|
+ model.addAttribute("relatedList", relatedList);
|
|
|
+ model.addAttribute("article", article);
|
|
|
+ model.addAttribute("categoryNameList", article.getCategoryNameList() == null ? new ArrayList<String>() : article.getCategoryNameList());
|
|
|
+ return "knowledgeDetails";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @GetMapping("/articleDetail") // 这里的 PageBean 是事先定义好的实体类
|
|
|
+ public Object articleDetailById(Model model, @RequestParam Integer id) {
|
|
|
+ // 定义格式化器
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
+
|
|
|
+ long l = System.currentTimeMillis();
|
|
|
+ List<Article> latestList = articleService.latestList();
|
|
|
+ if (!latestList.isEmpty()) {
|
|
|
+ for (Article article : latestList) {
|
|
|
+ byte[] imageData = article.getCoverImg();
|
|
|
+ if (imageData != null) {
|
|
|
+ String base64Image = Base64.getEncoder().encodeToString(imageData);
|
|
|
+ article.setBaseImage(base64Image);
|
|
|
+ } else {
|
|
|
+ article.setBaseImage("");
|
|
|
+ }
|
|
|
+ article.setCreateTimeStr(article.getCreateTime().format(formatter));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<Article> relatedList = articleService.relatedList(id);
|
|
|
+ if (!relatedList.isEmpty()) {
|
|
|
+ for (Article article : relatedList) {
|
|
|
+ byte[] imageData = article.getCoverImg();
|
|
|
+ if (imageData != null) {
|
|
|
+ String base64Image = Base64.getEncoder().encodeToString(imageData);
|
|
|
+ article.setBaseImage(base64Image);
|
|
|
+ } else {
|
|
|
+ article.setBaseImage("");
|
|
|
+ }
|
|
|
+ article.setCreateTimeStr(article.getCreateTime().format(formatter));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Article article = articleService.getArticleById(id);
|
|
|
+ article.setCreateTimeStr(article.getCreateTime().format(formatter));
|
|
|
+
|
|
|
+ articleService.updateViewCountById(id);
|
|
|
+
|
|
|
+ model.addAttribute("latestList", latestList);
|
|
|
+ model.addAttribute("relatedList", relatedList);
|
|
|
+ model.addAttribute("article", article);
|
|
|
+ model.addAttribute("categoryNameList", article.getCategoryNameList() == null ? new ArrayList<String>() : article.getCategoryNameList());
|
|
|
+ return "knowledgeDetails";
|
|
|
+ }
|
|
|
+}
|