Design of SpringBoot (java) dynamic short message template

 1. Design ideas

      Through a certain format (such as: %s#%s), by finding the corresponding attribute, replace it with the corresponding value. The SMS template can dynamically read classes and related attributes to configure on the front-end page,

2. Examples of static classes

You can use static or the value of the entity object returned by the data query


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import lombok.Data;


@Data
public class SmsTagTemplate {

    /**
     * 当前时间
     */
    private LocalDateTime localDateTime = LocalDateTime.now();

    /**
     * 当前日期时间 yyyy-MM-dd HH:mm:ss
     */
    private String currentDateTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

    /**
     * 当前日期 yyyy-MM-dd
     */
    private String currentDate = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

    /**
     * 当前时间 HH:mm:ss
     */
    private String currentTime = localDateTime.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
}

3. Example of SMS template

SmsTagTemplate class name, in order to avoid the same class name, you can use the package name + class name, the example just uses the class name plus the attribute name

Dear customer, congratulations on your successful registration and become a VIP user of XX system. SmsTagTemplate#currentDateTime

4. Core code


    private static final String TEMP_FORMAT = "%s#%s";

    /**
     * 获取短信模板
     *
     * @param objects     值替换的对象
     * @param smsTemplate 短信模板
     * @return 返回 转换之后的短信内容
     */
    public static String matchSmsTemplate(List<Object> objects, String smsTemplate) {
        if (CollectionUtils.isEmpty(objects)) {
            return smsTemplate;
        }
        for (Object specialTemplate : objects) {
            smsTemplate = matchSmsTemplate(specialTemplate, smsTemplate);
        }
        return smsTemplate;
    }

    /**
     * 获取短信模板
     *
     * @param object      值替换的对象
     * @param smsTemplate 短信模板
     * @return 返回 转换之后的短信内容
     */
    private static String matchSmsTemplate(Object object, String smsTemplate) {
        Field[] fields = object.getClass().getDeclaredFields();
        if (fields == null) {
            return smsTemplate;
        }
        for (Field field : fields) {
            String fieldName = String.format(TEMP_FORMAT, object.getClass().getSimpleName(), field.getName());
            boolean matches = Pattern.compile(fieldName).matcher(smsTemplate).find();
            if (!matches) {
                continue;
            }
            String value;
            Object val;
            field.setAccessible(true);
            try {
                val = field.get(object);
            } catch (IllegalAccessException e) {
                continue;
            }
            value = val == null ? "" : String.valueOf(val);
            smsTemplate = smsTemplate.replaceAll(fieldName, value);
        }
        return smsTemplate;
    }

5. Return results

Dear customer, congratulations on your successful registration and become a VIP user of XX system. 2021-03-12 10:00:00

Guess you like

Origin blog.csdn.net/qq_38428623/article/details/114690906