Browse Source

自定义freemarker函数

陈长荣 1 week ago
parent
commit
8605f63718

+ 33 - 0
jfcloud-gene-biz/src/main/java/com/github/jfcloud/gene/handler/MyCheck.java

@@ -0,0 +1,33 @@
+package com.github.jfcloud.gene.handler;
+
+import cn.hutool.core.collection.CollUtil;
+import freemarker.template.TemplateMethodModelEx;
+import freemarker.template.TemplateModelException;
+
+import java.util.List;
+
+/**
+ * freemarker自定义函数:勾选框显示
+ */
+public class MyCheck implements TemplateMethodModelEx {
+    @Override
+    public Object exec(List arguments) throws TemplateModelException {
+        if (CollUtil.isEmpty(arguments)) {
+            return getChecked(false);
+        }
+        Object content = arguments.get(0);
+        if (content == null) {
+            return getChecked(false);
+        }
+        return getChecked(Boolean.parseBoolean(content.toString()));
+    }
+
+    /**
+     * 获取勾选框
+     * @param checked
+     * @return
+     */
+    public static String getChecked(boolean checked) {
+        return checked ? "<w:sym w:font=\"Wingdings 2\" w:char=\"F052\" />" : "<w:sym w:font=\"Wingdings 2\" w:char=\"F0A3\" />";
+    }
+}

+ 72 - 0
jfcloud-gene-biz/src/main/java/com/github/jfcloud/gene/handler/MyJsonFilter.java

@@ -0,0 +1,72 @@
+package com.github.jfcloud.gene.handler;
+
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.json.JSONUtil;
+import freemarker.template.TemplateMethodModelEx;
+import freemarker.template.TemplateModelException;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+
+/**
+ * freemarker自定义函数:json过滤器
+ */
+public class MyJsonFilter implements TemplateMethodModelEx {
+    @Override
+    public Object exec(List arguments) throws TemplateModelException {
+        if (arguments.size() != 5) {
+            throw new TemplateModelException("参数个数错误");
+        }
+        //第一个参数为json字符串
+        String content = arguments.get(0).toString();
+        //第二个参数为json路径
+        String path = arguments.get(1).toString();
+        //第三个参数为方法
+        String method = arguments.get(2).toString();
+        //第四个参数为期望值
+        String expect = arguments.get(3).toString();
+        //第五个参数为结果类型
+        String resultType = arguments.get(4).toString();
+
+        //根据json路径获取值
+        Object value = content;
+        if (StrUtil.isNotBlank(path)) {
+            value = JSONUtil.parseObj(content).getByPath(path);
+        }
+        if ("obj".equals(resultType)) {
+            return value;
+        }
+        if ("wt".equals(resultType)) {
+            return String.format("<w:t>%s</w:t>", Optional.ofNullable(value).orElse(""));
+        }
+
+        //判断等于
+        boolean result = false;
+        if ("eq".equals(method)) {
+            result = Objects.equals(value, expect);
+        }
+
+        //判断包含
+        if ("contain".equals(method)) {
+            if (value instanceof String) {
+                String str = (String) value;
+                result = str.contains(expect);
+            } else if (value instanceof Collection) {
+                Collection list = (Collection) value;
+                result = list.contains(expect);
+            }
+        }
+
+        //判断包含,逗号分割
+        if ("comma_contain".equals(method)) {
+            if (value instanceof String) {
+                String str = (String) value;
+                result = StrUtil.split(str, ',').contains(expect);
+            }
+        }
+
+        return "check".equals(resultType) ? MyCheck.getChecked(result) : result;
+    }
+}

+ 5 - 0
jfcloud-gene-biz/src/main/java/com/github/jfcloud/gene/util/WordUtil.java

@@ -1,6 +1,8 @@
 package com.github.jfcloud.gene.util;
 
 import cn.hutool.core.util.StrUtil;
+import com.github.jfcloud.gene.handler.MyCheck;
+import com.github.jfcloud.gene.handler.MyJsonFilter;
 import freemarker.template.Configuration;
 import lombok.extern.slf4j.Slf4j;
 import sun.misc.BASE64Encoder;
@@ -29,6 +31,9 @@ public class WordUtil {
         Configuration configuration = new Configuration(Configuration.VERSION_2_3_32);
         configuration.setDefaultEncoding("utf-8");
         configuration.setClassForTemplateLoading(WordUtil.class, "/ftlTemplate");
+        //自定义函数
+        configuration.setSharedVariable("myJsonFilter", new MyJsonFilter());
+        configuration.setSharedVariable("myCheck", new MyCheck());
 
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
         try (Writer out = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))){