From 82b89f437ead107ccfaf084d67f62c429369246b Mon Sep 17 00:00:00 2001 From: Jaemon Date: Sun, 16 Oct 2022 22:18:19 +0800 Subject: [PATCH] feat:v2.1.0-beta --- README.md | 5 +- pom.xml | 10 +-- .../dinger/bytetalk/entity/ByteText.java | 1 - .../dinger/constant/DingerConstant.java | 4 + .../AnnotationParameterNameDiscoverer.java | 18 +++- .../dinger/core/DingerMessageHandler.java | 82 ++++++++++++++++++- .../dinger/core/annatations/DingerPhone.java | 46 +++++++++++ .../jaemon/dinger/core/entity/MsgType.java | 40 ++++++++- .../core/entity/enums/MessageSubType.java | 4 +- .../core/entity/enums/PhoneParamType.java | 32 ++++++++ .../dinger/dingtalk/entity/DingMarkDown.java | 2 +- .../dinger/dingtalk/entity/DingText.java | 2 +- .../dinger/dingtalk/entity/Message.java | 47 ++++++++++- .../dinger/support/MarkDownMessage.java | 3 +- .../jaemon/dinger/wetalk/entity/WeText.java | 6 ++ .../github/jaemon/dinger/core/UserDinger.java | 8 +- 16 files changed, 288 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/github/jaemon/dinger/core/annatations/DingerPhone.java create mode 100644 src/main/java/com/github/jaemon/dinger/core/entity/enums/PhoneParamType.java diff --git a/README.md b/README.md index bb0b577..144538d 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,10 @@ Dinger是一个以SpringBoot框架为基础开发的消息发送中间件, 对   -> ★ **如果觉得项目对您的工作有帮助的话, 欢迎『[Github](https://github.com/AnswerAIL/dingtalk-spring-boot-starter)』 或 『[Gitee](https://gitee.com/jaemon/dingtalk-spring-boot-starter)』加星关注☺** +### 支持Dinger +> ★ **欢迎『[Github](https://github.com/AnswerAIL/dingtalk-spring-boot-starter)』 或 『[Gitee](https://gitee.com/jaemon/dingtalk-spring-boot-starter)』点下Star让更多码友知道Dinger的存在** + +> ❤ [Gitee捐赠](https://gitee.com/jaemon/dingtalk-spring-boot-starter): 如果觉得Dinger不错, 条件允许的话捐赠杯奶茶犒劳下维护者, 感谢您的支持和鼓励^_^。   diff --git a/pom.xml b/pom.xml index 99d9d49..b44a5b6 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.github.answerail dinger-spring-boot-starter jar - 2.0.0 + 2.1.0-beta dinger-spring-boot-starter Dinger-SpringBoot集成钉钉/企业微信/飞书群机器人实现消息通知中间件 @@ -15,11 +15,11 @@ - 2.7.0 - 5.3.20 + 2.7.4 + 5.3.23 1.8 - 1.7.36 - 2.13.2.2 + 2.0.3 + 2.13.4 UTF-8 UTF-8 diff --git a/src/main/java/com/github/jaemon/dinger/bytetalk/entity/ByteText.java b/src/main/java/com/github/jaemon/dinger/bytetalk/entity/ByteText.java index 8faf9f1..a141e52 100644 --- a/src/main/java/com/github/jaemon/dinger/bytetalk/entity/ByteText.java +++ b/src/main/java/com/github/jaemon/dinger/bytetalk/entity/ByteText.java @@ -20,7 +20,6 @@ import com.github.jaemon.dinger.support.sign.SignResult; import java.io.Serializable; -import java.util.List; import java.util.Map; /** diff --git a/src/main/java/com/github/jaemon/dinger/constant/DingerConstant.java b/src/main/java/com/github/jaemon/dinger/constant/DingerConstant.java index 1615d71..21a749b 100644 --- a/src/main/java/com/github/jaemon/dinger/constant/DingerConstant.java +++ b/src/main/java/com/github/jaemon/dinger/constant/DingerConstant.java @@ -53,4 +53,8 @@ public interface DingerConstant { String DINGER_PROPERTIES_PREFIX = DINGER_PROP_PREFIX + SPOT_SEPERATOR; String DINGER_COMMA = ","; + String DINGER_PHONE_KEY = "@Dinger_Phone_Key@"; + String DINGER_PHONE_TAG = "@Dinger_Phone_Tag@"; + String DINGER_PHONE_FORCE_TAG = "@Dinger_Phone_Force_Tag@"; + String DINGER_AT = "@"; } \ No newline at end of file diff --git a/src/main/java/com/github/jaemon/dinger/core/AnnotationParameterNameDiscoverer.java b/src/main/java/com/github/jaemon/dinger/core/AnnotationParameterNameDiscoverer.java index 8ede023..962aa59 100644 --- a/src/main/java/com/github/jaemon/dinger/core/AnnotationParameterNameDiscoverer.java +++ b/src/main/java/com/github/jaemon/dinger/core/AnnotationParameterNameDiscoverer.java @@ -15,13 +15,18 @@ */ package com.github.jaemon.dinger.core; +import com.github.jaemon.dinger.core.annatations.Dinger; +import com.github.jaemon.dinger.core.annatations.DingerPhone; import com.github.jaemon.dinger.core.annatations.Parameter; +import com.github.jaemon.dinger.utils.DingerUtils; import org.springframework.core.ParameterNameDiscoverer; import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import static com.github.jaemon.dinger.constant.DingerConstant.DINGER_PHONE_KEY; + /** * 注解参数名称解析 * @@ -52,13 +57,22 @@ public String[] getParameterNames(Constructor ctor) { protected String[] getParameterNames(java.lang.reflect.Parameter[] parameters, Annotation[][] parameterAnnotations) { String[] params = new String[parameterAnnotations.length]; - for (int i = 0; i < parameterAnnotations.length; i++) { + for (int i = 0, j = 0; i < parameterAnnotations.length; i++) { Annotation[] parameterAnnotation = parameterAnnotations[i]; params[i] = parameters[i].getName(); for (Annotation annotation : parameterAnnotation) { if (Parameter.class.isInstance(annotation)) { Parameter dingerParam = (Parameter) annotation; - params[i] = dingerParam.value(); + String value = dingerParam.value(); + if (DingerUtils.isNotEmpty(value)) { + params[i] = value; + break; + } + } + + if (DingerPhone.class.isInstance(annotation) && j == 0) { + params[i] = DINGER_PHONE_KEY; + j++; break; } } diff --git a/src/main/java/com/github/jaemon/dinger/core/DingerMessageHandler.java b/src/main/java/com/github/jaemon/dinger/core/DingerMessageHandler.java index b3595cc..d88fd58 100644 --- a/src/main/java/com/github/jaemon/dinger/core/DingerMessageHandler.java +++ b/src/main/java/com/github/jaemon/dinger/core/DingerMessageHandler.java @@ -15,14 +15,18 @@ */ package com.github.jaemon.dinger.core; +import com.github.jaemon.dinger.constant.DingerConstant; import com.github.jaemon.dinger.core.annatations.Dinger; +import com.github.jaemon.dinger.core.annatations.DingerPhone; import com.github.jaemon.dinger.core.entity.DingerProperties; import com.github.jaemon.dinger.core.entity.MsgType; import com.github.jaemon.dinger.core.entity.enums.DingerType; import com.github.jaemon.dinger.core.entity.DingerResponse; +import com.github.jaemon.dinger.core.entity.enums.PhoneParamType; import com.github.jaemon.dinger.multi.MultiDingerConfigContainer; import com.github.jaemon.dinger.multi.MultiDingerProperty; import com.github.jaemon.dinger.multi.entity.MultiDingerConfig; +import com.github.jaemon.dinger.utils.DingerUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,10 +36,12 @@ import java.io.ObjectOutputStream; import java.lang.reflect.Method; import java.lang.reflect.Parameter; +import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Optional; +import static com.github.jaemon.dinger.constant.DingerConstant.DINGER_PREFIX; import static com.github.jaemon.dinger.constant.DingerConstant.SPOT_SEPERATOR; /** @@ -73,7 +79,13 @@ public Map paramsHandler(Method method, DingerDefinition dingerD int keyLength = keys.length; if (keyLength == valueLength) { for (int i = 0; i < valueLength; i++) { - params.put(keys[i], values[i]); + String key = keys[i]; + Object value = values[i]; + + if (DingerConstant.DINGER_PHONE_KEY.equals(key) && handlePhoneParam(i, params, method, value)) { + continue; + } + params.put(key, value); } return params; } @@ -82,6 +94,12 @@ public Map paramsHandler(Method method, DingerDefinition dingerD for (int i = 0; i < parameters.length; i++) { Parameter parameter = parameters[i]; String paramName = parameter.getName(); + Object value = values[i]; + + if (fillParamPhone(parameter, params, value)) { + continue; + } + com.github.jaemon.dinger.core.annatations.Parameter[] panno = parameter.getDeclaredAnnotationsByType(com.github.jaemon.dinger.core.annatations.Parameter.class); if (panno != null && panno.length > 0) { @@ -93,6 +111,68 @@ public Map paramsHandler(Method method, DingerDefinition dingerD return params; } + /** + * 处理phone参数 + * + * @param i i + * @param params params + * @param method method + * @param value value + * @return true | false + */ + private static boolean handlePhoneParam(int i, Map params, Method method, Object value) { + return fillParamPhone(method.getParameters()[i], params, value); + } + + + /** + * 填充phone参数信息 + * + * @param parameter parameter + * @param params params + * @param value value + * @return true | false + */ + private static boolean fillParamPhone(Parameter parameter, Map params, Object value) { + String paramName = parameter.getName(); + + DingerPhone[] dingerPhones = parameter.getDeclaredAnnotationsByType(DingerPhone.class); + if (dingerPhones != null && dingerPhones.length > 0) { + DingerPhone dingerPhone = dingerPhones[0]; + paramName = DingerUtils.isEmpty(dingerPhone.value()) ? paramName : dingerPhone.value(); + PhoneParamType type = dingerPhone.type(); + + if (paramTypeIsValid(type, value)) { + params.put(DingerConstant.DINGER_PHONE_TAG, paramName); + params.put(paramName, value); + if (dingerPhone.force()) { + params.put(DingerConstant.DINGER_PHONE_FORCE_TAG, DINGER_PREFIX); + } + } + + return true; + } + + return false; + } + + /** + * 判断参数类型是否有效 + * + * @param type type + * @param value value + * @return true | false + */ + private static boolean paramTypeIsValid(PhoneParamType type, Object value) { + if (type == PhoneParamType.ARRAY) { + if (value instanceof Collection || value instanceof String[]) { + return true; + } + } + + return type == PhoneParamType.STRING && value instanceof String; + } + @Override public MsgType transfer(DingerDefinition dingerDefinition, Map params) { diff --git a/src/main/java/com/github/jaemon/dinger/core/annatations/DingerPhone.java b/src/main/java/com/github/jaemon/dinger/core/annatations/DingerPhone.java new file mode 100644 index 0000000..4cec157 --- /dev/null +++ b/src/main/java/com/github/jaemon/dinger/core/annatations/DingerPhone.java @@ -0,0 +1,46 @@ +/* + * Copyright ©2015-2022 Jaemon. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.jaemon.dinger.core.annatations; + +import com.github.jaemon.dinger.core.entity.enums.PhoneParamType; + +import java.lang.annotation.*; + +/** + * DingerPhone + * + * @author Jaemon + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.PARAMETER) +public @interface DingerPhone { + String value() default ""; + + /** + * 参数类型 + * + * @return {@link PhoneParamType} + */ + PhoneParamType type() default PhoneParamType.ARRAY; + + /** + * 是否强制使用 + * + * @return true | false(优先使用注解/XML配置的值) + */ + boolean force() default false; +} \ No newline at end of file diff --git a/src/main/java/com/github/jaemon/dinger/core/entity/MsgType.java b/src/main/java/com/github/jaemon/dinger/core/entity/MsgType.java index f56e632..875c17f 100644 --- a/src/main/java/com/github/jaemon/dinger/core/entity/MsgType.java +++ b/src/main/java/com/github/jaemon/dinger/core/entity/MsgType.java @@ -15,12 +15,13 @@ */ package com.github.jaemon.dinger.core.entity; +import com.github.jaemon.dinger.constant.DingerConstant; import com.github.jaemon.dinger.core.entity.enums.DingerType; import com.github.jaemon.dinger.support.sign.SignBase; -import com.github.jaemon.dinger.support.sign.SignResult; import java.io.Serializable; -import java.util.Map; +import java.util.*; +import java.util.stream.Collectors; /** * 消息类型实体 @@ -64,6 +65,11 @@ public void setMsgtype(String msgtype) { public void transfer(Map params) { } + /** + * 签名参数处理 + * + * @param sign sign + */ public void signAttributes(SignBase sign) {} /** @@ -86,7 +92,7 @@ protected String replaceContent(String content, Map params) { || v instanceof Number) { content = content.replaceAll(key, v.toString()); } else { -// content = content.replaceAll(key, v.toString()); + /*content = content.replaceAll(key, v.toString());*/ continue; } @@ -94,4 +100,32 @@ protected String replaceContent(String content, Map params) { return content; } + + /** + * 解析at参数信息 + * @param params params + * @return phones + */ + protected List parseAtParam(Map params) { + Object phoneParamKey = params.get(DingerConstant.DINGER_PHONE_TAG); + if (phoneParamKey == null) { + return null; + } + + Object phoneVal = params.get(phoneParamKey.toString()); + if (phoneVal == null) { + return null; + } + + if (phoneVal instanceof String) { + String[] phones = phoneVal.toString().split(DingerConstant.DINGER_COMMA); + return Arrays.asList(phones); + } else if (phoneVal instanceof Collection) { + return (List) ((Collection) phoneVal).stream().map(Object::toString).collect(Collectors.toList()); + } else if (phoneVal instanceof String[]) { + return Arrays.asList((String[])phoneVal); + } + + return null; + } } \ No newline at end of file diff --git a/src/main/java/com/github/jaemon/dinger/core/entity/enums/MessageSubType.java b/src/main/java/com/github/jaemon/dinger/core/entity/enums/MessageSubType.java index b8c7275..1cc9976 100644 --- a/src/main/java/com/github/jaemon/dinger/core/entity/enums/MessageSubType.java +++ b/src/main/java/com/github/jaemon/dinger/core/entity/enums/MessageSubType.java @@ -20,11 +20,11 @@ import com.github.jaemon.dinger.dingtalk.entity.*; import com.github.jaemon.dinger.core.entity.MsgType; import com.github.jaemon.dinger.exception.DingerException; +import com.github.jaemon.dinger.utils.DingerUtils; import com.github.jaemon.dinger.wetalk.entity.WeMarkdown; import com.github.jaemon.dinger.wetalk.entity.WeNews; import com.github.jaemon.dinger.wetalk.entity.WeText; import org.springframework.util.CollectionUtils; -import org.springframework.util.StringUtils; import java.util.ArrayList; import java.util.Arrays; @@ -74,7 +74,7 @@ public MsgType msgType(DingerType dingerType, DingerRequest request) { } else if (!CollectionUtils.isEmpty(phones)) { StringBuilder sb = new StringBuilder(); for (String phone : phones) { - if (StringUtils.isEmpty(phone)) { + if (DingerUtils.isEmpty(phone)) { continue; } String[] userIdAndName = phone.split(DINGER_COMMA); diff --git a/src/main/java/com/github/jaemon/dinger/core/entity/enums/PhoneParamType.java b/src/main/java/com/github/jaemon/dinger/core/entity/enums/PhoneParamType.java new file mode 100644 index 0000000..58bc003 --- /dev/null +++ b/src/main/java/com/github/jaemon/dinger/core/entity/enums/PhoneParamType.java @@ -0,0 +1,32 @@ +/* + * Copyright ©2015-2022 Jaemon. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.jaemon.dinger.core.entity.enums; + +/** + * PhoneParamType + * + * @author Jaemon + * @since 1.0 + */ +public enum PhoneParamType { + STRING, + ARRAY, + ; + + + PhoneParamType() {} + +} \ No newline at end of file diff --git a/src/main/java/com/github/jaemon/dinger/dingtalk/entity/DingMarkDown.java b/src/main/java/com/github/jaemon/dinger/dingtalk/entity/DingMarkDown.java index 61f64a6..dc002b1 100644 --- a/src/main/java/com/github/jaemon/dinger/dingtalk/entity/DingMarkDown.java +++ b/src/main/java/com/github/jaemon/dinger/dingtalk/entity/DingMarkDown.java @@ -83,6 +83,6 @@ public MarkDown(String title, String text) { @Override public void transfer(Map params) { - this.markdown.text = replaceContent(this.markdown.text, params); + this.markdown.text = replaceContent(this.markdown.text, params) + parsePhone(params); } } \ No newline at end of file diff --git a/src/main/java/com/github/jaemon/dinger/dingtalk/entity/DingText.java b/src/main/java/com/github/jaemon/dinger/dingtalk/entity/DingText.java index d4c26bc..e685549 100644 --- a/src/main/java/com/github/jaemon/dinger/dingtalk/entity/DingText.java +++ b/src/main/java/com/github/jaemon/dinger/dingtalk/entity/DingText.java @@ -67,6 +67,6 @@ public Text(String content) { @Override public void transfer(Map params) { - this.text.content = replaceContent(this.text.content, params); + this.text.content = replaceContent(this.text.content, params) + parsePhone(params); } } \ No newline at end of file diff --git a/src/main/java/com/github/jaemon/dinger/dingtalk/entity/Message.java b/src/main/java/com/github/jaemon/dinger/dingtalk/entity/Message.java index 38d984d..4086585 100644 --- a/src/main/java/com/github/jaemon/dinger/dingtalk/entity/Message.java +++ b/src/main/java/com/github/jaemon/dinger/dingtalk/entity/Message.java @@ -15,8 +15,12 @@ */ package com.github.jaemon.dinger.dingtalk.entity; +import com.github.jaemon.dinger.constant.DingerConstant; +import org.springframework.util.CollectionUtils; + import java.io.Serializable; -import java.util.List; +import java.util.*; +import java.util.stream.Collectors; /** * 请求体实体对象 @@ -42,6 +46,47 @@ public void setAt(At at) { this.at = at; } + + /** + * 解析处理 + * + * @param params param + * @return phone content + */ + protected String parsePhone(Map params) { + List phones = parseAtParam(params); + + if (CollectionUtils.isEmpty(phones)) { + return atPhones(); + } + + boolean atIsNull = at == null; + if (atIsNull) { + at = new At(new ArrayList<>()); + } + + if (!atIsNull && at.getAtMobiles() == null) { + at.setAtMobiles(new ArrayList<>()); + } + + boolean force = params.containsKey(DingerConstant.DINGER_PHONE_FORCE_TAG); + if (force || atIsNull || (at.isAtAll != null && !at.isAtAll && CollectionUtils.isEmpty(at.atMobiles))) { + if (force) { + at.isAtAll = false; + } + at.setAtMobiles(phones); + return at.getAtMobiles().stream().map(e -> DingerConstant.DINGER_AT + e).collect(Collectors.joining()); + } + + return atPhones(); + } + + private String atPhones() { + List ats = Optional.ofNullable(this).map(e -> e.at).map(e -> e.getAtMobiles()).orElse(null); + return ats == null ? "" : ats.stream().map(e -> DingerConstant.DINGER_AT + e).collect(Collectors.joining()); + } + + public static class At implements Serializable { /** * 被@人的手机号(在content里添加@人的手机号) diff --git a/src/main/java/com/github/jaemon/dinger/support/MarkDownMessage.java b/src/main/java/com/github/jaemon/dinger/support/MarkDownMessage.java index 3832e9e..190cad5 100644 --- a/src/main/java/com/github/jaemon/dinger/support/MarkDownMessage.java +++ b/src/main/java/com/github/jaemon/dinger/support/MarkDownMessage.java @@ -15,6 +15,7 @@ */ package com.github.jaemon.dinger.support; +import com.github.jaemon.dinger.constant.DingerConstant; import com.github.jaemon.dinger.core.entity.DingerRequest; import java.text.MessageFormat; @@ -37,7 +38,7 @@ public String message(String projectId, DingerRequest request) { StringBuilder text = new StringBuilder(title); if (phones != null && !phones.isEmpty()) { for (String phone : phones) { - text.append("@").append(phone); + text.append(DingerConstant.DINGER_AT).append(phone); } } return MessageFormat.format( diff --git a/src/main/java/com/github/jaemon/dinger/wetalk/entity/WeText.java b/src/main/java/com/github/jaemon/dinger/wetalk/entity/WeText.java index 6ede329..df96d7d 100644 --- a/src/main/java/com/github/jaemon/dinger/wetalk/entity/WeText.java +++ b/src/main/java/com/github/jaemon/dinger/wetalk/entity/WeText.java @@ -15,6 +15,7 @@ */ package com.github.jaemon.dinger.wetalk.entity; +import com.github.jaemon.dinger.constant.DingerConstant; import com.github.jaemon.dinger.wetalk.entity.enums.WeTalkMsgType; import java.io.Serializable; @@ -100,6 +101,11 @@ public Text(String content) { @Override public void transfer(Map params) { + if (params.containsKey(DingerConstant.DINGER_PHONE_FORCE_TAG) + || this.text.mentioned_mobile_list == null) { + this.text.mentioned_mobile_list = parseAtParam(params); + } + this.text.content = replaceContent(this.text.content, params); } } \ No newline at end of file diff --git a/src/test/java/com/github/jaemon/dinger/core/UserDinger.java b/src/test/java/com/github/jaemon/dinger/core/UserDinger.java index 221bb7f..6006d59 100644 --- a/src/test/java/com/github/jaemon/dinger/core/UserDinger.java +++ b/src/test/java/com/github/jaemon/dinger/core/UserDinger.java @@ -15,6 +15,7 @@ */ package com.github.jaemon.dinger.core; +import com.github.jaemon.dinger.core.annatations.DingerPhone; import com.github.jaemon.dinger.core.annatations.Parameter; import com.github.jaemon.dinger.core.entity.enums.DingerType; import com.github.jaemon.dinger.multi.annotations.MultiDinger; @@ -24,6 +25,8 @@ import com.github.jaemon.dinger.core.entity.DingerResponse; import com.github.jaemon.dinger.multi.handler.UserDingerConfigHandler; +import java.util.List; + /** * UserDinger * @@ -58,8 +61,7 @@ public interface UserDinger { */ @DingerMarkdown( value = "#### 用户注销通知 @13520200906\n - 用户Id: ${userId}\n - 用户名: ${userName}", - title = "用户注销反馈", - phones = {"13520200906"} + title = "用户注销反馈" ) - DingerResponse userLogout(@Parameter("userId") Long id, String userName); + DingerResponse userLogout(@DingerPhone List phone, @Parameter("userId") Long id, String userName); } \ No newline at end of file