SmsClient.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package cn.iocoder.dashboard.framework.sms;
  2. import org.apache.commons.lang3.StringUtils;
  3. import java.util.Arrays;
  4. import java.util.Collection;
  5. import java.util.Collections;
  6. /**
  7. * 短信父接口
  8. *
  9. * @author zzf
  10. * @date 2021/1/25 14:14
  11. */
  12. public interface SmsClient<R> {
  13. /**
  14. * 发送消息
  15. *
  16. * @param msgBody 消息内容
  17. * @param targets 发送对象列表
  18. * @return 是否发送成功
  19. */
  20. SmsResult<R> send(SmsBody msgBody, Collection<String> targets);
  21. /**
  22. * 发送消息
  23. *
  24. * @param msgBody 消息内容
  25. * @param target 发送对象
  26. * @return 是否发送成功
  27. */
  28. default SmsResult<R> send(SmsBody msgBody, String target) {
  29. if (StringUtils.isBlank(target)) {
  30. return failResult();
  31. }
  32. return send(msgBody, Collections.singletonList(target));
  33. }
  34. /**
  35. * 发送消息
  36. *
  37. * @param msgBody 消息内容
  38. * @param targets 发送对象列表
  39. * @return 是否发送成功
  40. */
  41. default SmsResult<R> send(SmsBody msgBody, String... targets) {
  42. if (targets == null) {
  43. return failResult();
  44. }
  45. return send(msgBody, Arrays.asList(targets));
  46. }
  47. /**
  48. * 异步发送消息
  49. *
  50. * @param msgBody 消息内容
  51. * @param targets 发送对象列表
  52. * @return 是否发送成功
  53. */
  54. SmsResult<R> sendAsync(SmsBody msgBody, Collection<String> targets);
  55. /**
  56. * 异步发送消息
  57. *
  58. * @param msgBody 消息内容
  59. * @param target 发送对象
  60. * @return 是否发送成功
  61. */
  62. default SmsResult<R> sendAsync(SmsBody msgBody, String target) {
  63. if (StringUtils.isBlank(target)) {
  64. return failResult("target must not null.");
  65. }
  66. return sendAsync(msgBody, Collections.singletonList(target));
  67. }
  68. /**
  69. * 异步发送消息
  70. *
  71. * @param msgBody 消息内容
  72. * @param targets 发送对象列表
  73. * @return 是否发送成功
  74. */
  75. default SmsResult<R> sendAsync(SmsBody msgBody, String... targets) {
  76. if (targets == null) {
  77. return failResult("targets must not null.");
  78. }
  79. return sendAsync(msgBody, Arrays.asList(targets));
  80. }
  81. default SmsResult<R> failResult() {
  82. SmsResult<R> resultBody = new SmsResult<>();
  83. resultBody.setSuccess(false);
  84. return resultBody;
  85. }
  86. default SmsResult<R> failResult(String message) {
  87. SmsResult<R> resultBody = failResult();
  88. resultBody.setMessage(message);
  89. return resultBody;
  90. }
  91. }