SmsClientAdapter.java 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package cn.iocoder.dashboard.framework.sms;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import cn.iocoder.dashboard.common.exception.ServiceException;
  4. import java.util.Collection;
  5. import java.util.Collections;
  6. import java.util.Map;
  7. import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.SMS_CHANNEL_NOT_INIT;
  8. /**
  9. * 抽象短信客户端工厂
  10. *
  11. * @author zzf
  12. * @date 2021/1/28 14:01
  13. */
  14. public class SmsClientAdapter {
  15. private final Map<Long, SmsClient<?>> smsSenderMap;
  16. public SmsClientAdapter(Map<Long, SmsClient<?>> smsSenderMap) {
  17. if (ObjectUtil.isEmpty(smsSenderMap)) {
  18. throw new ServiceException(SMS_CHANNEL_NOT_INIT);
  19. }
  20. this.smsSenderMap = smsSenderMap;
  21. }
  22. public void flushClient(Map<Long, SmsClient<?>> smsSenderMap) {
  23. this.smsSenderMap.clear();
  24. smsSenderMap.putAll(Collections.unmodifiableMap(smsSenderMap));
  25. }
  26. public SmsResult<?> send(Long channelId, SmsBody smsBody, Collection<String> targetPhone) {
  27. SmsClient<?> smsClient = getSmsSender(channelId);
  28. return smsClient.send(smsBody, targetPhone);
  29. }
  30. private SmsClient<?> getSmsSender(Long channelId) {
  31. return smsSenderMap.get(channelId);
  32. }
  33. }