You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.5 KiB
50 lines
1.5 KiB
package com.qs.serve.common.util;
|
|
|
|
import com.qs.serve.modules.wx.common.model.WxSmsProp;
|
|
import com.qs.serve.modules.wx.entity.dto.SmsBaseDto;
|
|
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
|
|
|
|
import java.lang.annotation.Annotation;
|
|
import java.lang.reflect.Field;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* @author YenHex
|
|
* @since 2022/3/16
|
|
*/
|
|
public class SmsReflectUtil {
|
|
|
|
/**
|
|
* 反射获取WX_SMS配置
|
|
* @param baseDto
|
|
* @param <T>
|
|
* @return
|
|
*/
|
|
public static<T extends SmsBaseDto> List<WxMpTemplateData> getTemplateDataList(T baseDto){
|
|
List<WxMpTemplateData> dataList = new ArrayList<>();
|
|
for (Field field : baseDto.getClass().getDeclaredFields()) {
|
|
WxSmsProp wxSmsProp = null;
|
|
for (Annotation annotation : field.getAnnotations()) {
|
|
if(annotation instanceof WxSmsProp){
|
|
wxSmsProp = (WxSmsProp)annotation;
|
|
break;
|
|
}
|
|
}
|
|
try {
|
|
field.setAccessible(true);
|
|
Object fieldObject = field.get(baseDto);
|
|
String value = "";
|
|
if(fieldObject!=null){
|
|
value = fieldObject.toString();
|
|
}
|
|
WxMpTemplateData templateData = new WxMpTemplateData(wxSmsProp.keyword(),value);
|
|
dataList.add(templateData);
|
|
} catch (IllegalAccessException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
return dataList;
|
|
}
|
|
|
|
}
|
|
|