From 450f473fcfa22260b03b84a7217354fc9eb0d81c Mon Sep 17 00:00:00 2001 From: ruibaby Date: Sat, 16 Mar 2019 16:34:11 +0800 Subject: [PATCH] v1.0 --- .../config/FreeMarkerAutoConfiguration.java | 87 +++++++++++-------- .../freemarker/tag/CommonTagDirective.java | 54 ++++++++++-- .../templates/admin/module/_header.ftl | 10 +-- 3 files changed, 103 insertions(+), 48 deletions(-) diff --git a/src/main/java/cc/ryanc/halo/config/FreeMarkerAutoConfiguration.java b/src/main/java/cc/ryanc/halo/config/FreeMarkerAutoConfiguration.java index b4ec84666..b317d36ca 100644 --- a/src/main/java/cc/ryanc/halo/config/FreeMarkerAutoConfiguration.java +++ b/src/main/java/cc/ryanc/halo/config/FreeMarkerAutoConfiguration.java @@ -1,8 +1,19 @@ package cc.ryanc.halo.config; +import cc.ryanc.halo.model.freemarker.method.RandomMethod; +import cc.ryanc.halo.model.freemarker.method.RecentCommentsMethod; +import cc.ryanc.halo.model.freemarker.method.RecentPostsMethod; +import cc.ryanc.halo.model.freemarker.tag.ArticleTagDirective; +import cc.ryanc.halo.model.freemarker.tag.CommonTagDirective; +import cc.ryanc.halo.service.OptionService; +import cc.ryanc.halo.service.UserService; +import freemarker.template.TemplateModelException; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; +import javax.annotation.PostConstruct; + /** *
  *     FreeMarker配置
@@ -15,43 +26,43 @@ import org.springframework.context.annotation.Configuration;
 @Configuration
 public class FreeMarkerAutoConfiguration {
 
-//    @Autowired
-//    private freemarker.template.Configuration configuration;
-//
-//    @Autowired
-//    private OptionsService optionsService;
-//
-//    @Autowired
-//    private UserService userService;
-//
-//    @Autowired
-//    private CommonTagDirective commonTagDirective;
-//
-//    @Autowired
-//    private ArticleTagDirective articleTagDirective;
-//
-//    @Autowired
-//    private RandomMethod randomMethod;
-//
-//    @Autowired
-//    private RecentPostsMethod recentPostsMethod;
-//
-//    @Autowired
-//    private RecentCommentsMethod recentCommentsMethod;
-//
-//    @PostConstruct
-//    public void setSharedVariable() {
-//        try {
-//            //自定义标签
-//            configuration.setSharedVariable("commonTag", commonTagDirective);
-//            configuration.setSharedVariable("articleTag", articleTagDirective);
-//            configuration.setSharedVariable("options", optionsService.findAllOptions());
+    @Autowired
+    private freemarker.template.Configuration configuration;
+
+    @Autowired
+    private OptionService optionsService;
+
+    @Autowired
+    private UserService userService;
+
+    @Autowired
+    private CommonTagDirective commonTagDirective;
+
+    @Autowired
+    private ArticleTagDirective articleTagDirective;
+
+    @Autowired
+    private RandomMethod randomMethod;
+
+    @Autowired
+    private RecentPostsMethod recentPostsMethod;
+
+    @Autowired
+    private RecentCommentsMethod recentCommentsMethod;
+
+    @PostConstruct
+    public void setSharedVariable() {
+        try {
+            //自定义标签
+            configuration.setSharedVariable("commonTag", commonTagDirective);
+            configuration.setSharedVariable("articleTag", articleTagDirective);
+            configuration.setSharedVariable("options", optionsService.listOptions());
 //            configuration.setSharedVariable("user", userService.findUser());
-//            configuration.setSharedVariable("randomMethod", randomMethod);
-//            configuration.setSharedVariable("recentPostsMethod", recentPostsMethod);
-//            configuration.setSharedVariable("recentCommentsMethod", recentCommentsMethod);
-//        } catch (TemplateModelException e) {
-//            log.error("Custom tags failed to load:{}", e.getMessage());
-//        }
-//    }
+            configuration.setSharedVariable("randomMethod", randomMethod);
+            configuration.setSharedVariable("recentPostsMethod", recentPostsMethod);
+            configuration.setSharedVariable("recentCommentsMethod", recentCommentsMethod);
+        } catch (TemplateModelException e) {
+            log.error("Custom tags failed to load:{}", e.getMessage());
+        }
+    }
 }
diff --git a/src/main/java/cc/ryanc/halo/model/freemarker/tag/CommonTagDirective.java b/src/main/java/cc/ryanc/halo/model/freemarker/tag/CommonTagDirective.java
index c1cc2947a..143e2594f 100644
--- a/src/main/java/cc/ryanc/halo/model/freemarker/tag/CommonTagDirective.java
+++ b/src/main/java/cc/ryanc/halo/model/freemarker/tag/CommonTagDirective.java
@@ -1,10 +1,8 @@
 package cc.ryanc.halo.model.freemarker.tag;
 
+import cc.ryanc.halo.service.*;
 import freemarker.core.Environment;
-import freemarker.template.TemplateDirectiveBody;
-import freemarker.template.TemplateDirectiveModel;
-import freemarker.template.TemplateException;
-import freemarker.template.TemplateModel;
+import freemarker.template.*;
 import org.springframework.stereotype.Component;
 
 import java.io.IOException;
@@ -23,9 +21,55 @@ public class CommonTagDirective implements TemplateDirectiveModel {
 
     private static final String METHOD_KEY = "method";
 
+    private final MenuService menuService;
+
+    private final CategoryService categoryService;
+
+    private final TagService tagService;
+
+    private final LinkService linkService;
+
+    private final CommentService commentService;
+
+    public CommonTagDirective(MenuService menuService,
+                              CategoryService categoryService,
+                              TagService tagService,
+                              LinkService linkService,
+                              CommentService commentService) {
+        this.menuService = menuService;
+        this.categoryService = categoryService;
+        this.tagService = tagService;
+        this.linkService = linkService;
+        this.commentService = commentService;
+    }
+
     @Override
     public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
-        // TODO Complete comment tag directive.
+        final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
+
+        if (params.containsKey(METHOD_KEY)) {
+            String method = params.get(METHOD_KEY).toString();
+            switch (method) {
+                case "menus":
+                    env.setVariable("menus", builder.build().wrap(menuService.listAll()));
+                    break;
+                case "categories":
+                    env.setVariable("categories", builder.build().wrap(categoryService.listAll()));
+                    break;
+                case "tags":
+                    env.setVariable("tags", builder.build().wrap(tagService.listAll()));
+                    break;
+                case "links":
+                    env.setVariable("links", builder.build().wrap(linkService.listAll()));
+                    break;
+                case "newComments":
+                    env.setVariable("newComments", builder.build().wrap(commentService.listAll()));
+                    break;
+                default:
+                    break;
+            }
+        }
+        body.render(env.getOut());
     }
 
 }
diff --git a/src/main/resources/templates/admin/module/_header.ftl b/src/main/resources/templates/admin/module/_header.ftl
index d95719a8c..05ec6c9f5 100755
--- a/src/main/resources/templates/admin/module/_header.ftl
+++ b/src/main/resources/templates/admin/module/_header.ftl
@@ -26,18 +26,18 @@