|
@@ -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;
|
|
|
+ }
|
|
|
+}
|