Skip to content

Commit

Permalink
feat:v2.1.0-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
AnswerAIL committed Oct 16, 2022
1 parent f9cfce7 commit 82b89f4
Show file tree
Hide file tree
Showing 16 changed files with 288 additions and 22 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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不错, 条件允许的话捐赠杯奶茶犒劳下维护者, 感谢您的支持和鼓励^_^。
 

Expand Down
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
<groupId>com.github.answerail</groupId>
<artifactId>dinger-spring-boot-starter</artifactId>
<packaging>jar</packaging>
<version>2.0.0</version>
<version>2.1.0-beta</version>

<name>dinger-spring-boot-starter</name>
<description>Dinger-SpringBoot集成钉钉/企业微信/飞书群机器人实现消息通知中间件</description>
<url>https://github.com/AnswerAIL/dingtalk-spring-boot-starter</url>


<properties>
<spring-boot.version>2.7.0</spring-boot.version>
<spring-web-version>5.3.20</spring-web-version>
<spring-boot.version>2.7.4</spring-boot.version>
<spring-web-version>5.3.23</spring-web-version>
<java.version>1.8</java.version>
<slf4j-api.version>1.7.36</slf4j-api.version>
<jackson.version>2.13.2.2</jackson.version>
<slf4j-api.version>2.0.3</slf4j-api.version>
<jackson.version>2.13.4</jackson.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.github.jaemon.dinger.support.sign.SignResult;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "@";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* 注解参数名称解析
*
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

/**
Expand Down Expand Up @@ -73,7 +79,13 @@ public Map<String, Object> 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;
}
Expand All @@ -82,6 +94,12 @@ public Map<String, Object> 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) {
Expand All @@ -93,6 +111,68 @@ public Map<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> params) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
40 changes: 37 additions & 3 deletions src/main/java/com/github/jaemon/dinger/core/entity/MsgType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* 消息类型实体
Expand Down Expand Up @@ -64,6 +65,11 @@ public void setMsgtype(String msgtype) {
public void transfer(Map<String, Object> params) {
}

/**
* 签名参数处理
*
* @param sign sign
*/
public void signAttributes(SignBase sign) {}

/**
Expand All @@ -86,12 +92,40 @@ protected String replaceContent(String content, Map<String, Object> params) {
|| v instanceof Number) {
content = content.replaceAll(key, v.toString());
} else {
// content = content.replaceAll(key, v.toString());
/*content = content.replaceAll(key, v.toString());*/
continue;
}

}

return content;
}

/**
* 解析at参数信息
* @param params params
* @return phones
*/
protected List<String> parseAtParam(Map<String, Object> 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<String>) ((Collection) phoneVal).stream().map(Object::toString).collect(Collectors.toList());
} else if (phoneVal instanceof String[]) {
return Arrays.asList((String[])phoneVal);
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 82b89f4

Please sign in to comment.