田源
2025-04-03 9b4433fddf5b401edb0aace8a404ac733b122702
Source/BladeX-Tool/blade-starter-sms/src/main/java/org/springblade/core/sms/SmsTemplate.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,111 @@
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill åº„骞 (smallchill@163.com)
 */
package org.springblade.core.sms;
import org.springblade.core.sms.model.SmsCode;
import org.springblade.core.sms.model.SmsData;
import org.springblade.core.sms.model.SmsInfo;
import org.springblade.core.sms.model.SmsResponse;
import org.springblade.core.tool.utils.StringPool;
import org.springframework.util.StringUtils;
import java.util.Collection;
import java.util.Collections;
import static org.springblade.core.sms.constant.SmsConstant.CAPTCHA_KEY;
/**
 * çŸ­ä¿¡é€šç”¨å°è£…
 *
 * @author Chill
 */
public interface SmsTemplate {
   /**
    * ç¼“存键值
    *
    * @param phone æ‰‹æœºå·
    * @param id    é”®å€¼
    * @return ç¼“存键值返回
    */
   default String cacheKey(String phone, String id) {
      return CAPTCHA_KEY + phone + StringPool.COLON + id;
   }
   /**
    * å‘送短信
    *
    * @param smsInfo çŸ­ä¿¡ä¿¡æ¯
    * @return å‘送返回
    */
   default boolean send(SmsInfo smsInfo) {
      return sendMulti(smsInfo.getSmsData(), smsInfo.getPhones());
   }
   /**
    * å‘送短信
    *
    * @param smsData çŸ­ä¿¡å†…容
    * @param phone   æ‰‹æœºå·
    * @return å‘送返回
    */
   default boolean sendSingle(SmsData smsData, String phone) {
      if (StringUtils.isEmpty(phone)) {
         return Boolean.FALSE;
      }
      return sendMulti(smsData, Collections.singletonList(phone));
   }
   /**
    * å‘送短信
    *
    * @param smsData çŸ­ä¿¡å†…容
    * @param phones  æ‰‹æœºå·åˆ—表
    * @return å‘送返回
    */
   default boolean sendMulti(SmsData smsData, Collection<String> phones) {
      SmsResponse response = sendMessage(smsData, phones);
      return response.isSuccess();
   }
   /**
    * å‘送短信
    *
    * @param smsData çŸ­ä¿¡å†…容
    * @param phones  æ‰‹æœºå·åˆ—表
    * @return å‘送返回
    */
   SmsResponse sendMessage(SmsData smsData, Collection<String> phones);
   /**
    * å‘送验证码
    *
    * @param smsData çŸ­ä¿¡å†…容
    * @param phone   æ‰‹æœºå·
    * @return å‘送返回
    */
   SmsCode sendValidate(SmsData smsData, String phone);
   /**
    * æ ¡éªŒéªŒè¯ç 
    *
    * @param smsCode éªŒè¯ç å†…容
    * @return æ˜¯å¦æ ¡éªŒæˆåŠŸ
    */
   boolean validateMessage(SmsCode smsCode);
}