SmsEndpoint.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * Neither the name of the dreamlu.net developer nor the names of its
  13. * contributors may be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. * Author: Chill 庄骞 (smallchill@163.com)
  16. */
  17. package org.springblade.modules.resource.endpoint;
  18. import io.swagger.annotations.Api;
  19. import lombok.AllArgsConstructor;
  20. import lombok.SneakyThrows;
  21. import org.springblade.core.launch.constant.AppConstant;
  22. import org.springblade.core.sms.model.SmsCode;
  23. import org.springblade.core.sms.model.SmsData;
  24. import org.springblade.core.sms.model.SmsResponse;
  25. import org.springblade.core.tenant.annotation.NonDS;
  26. import org.springblade.core.tool.api.R;
  27. import org.springblade.core.tool.jackson.JsonUtil;
  28. import org.springblade.core.tool.utils.Func;
  29. import org.springblade.modules.resource.builder.sms.SmsBuilder;
  30. import org.springblade.modules.resource.utils.SmsUtil;
  31. import org.springframework.web.bind.annotation.PostMapping;
  32. import org.springframework.web.bind.annotation.RequestMapping;
  33. import org.springframework.web.bind.annotation.RequestParam;
  34. import org.springframework.web.bind.annotation.RestController;
  35. import java.util.HashMap;
  36. import java.util.Map;
  37. import static org.springblade.modules.resource.utils.SmsUtil.*;
  38. /**
  39. * 短信服务端点
  40. *
  41. * @author Chill
  42. */
  43. @NonDS
  44. @RestController
  45. @AllArgsConstructor
  46. @RequestMapping(AppConstant.APPLICATION_RESOURCE_NAME + "/sms/endpoint")
  47. @Api(value = "短信服务端点", tags = "短信服务端点")
  48. public class SmsEndpoint {
  49. /**
  50. * 短信服务构建类
  51. */
  52. private final SmsBuilder smsBuilder;
  53. //================================= 短信服务校验 =================================
  54. /**
  55. * 短信验证码发送
  56. *
  57. * @param phone 手机号
  58. */
  59. @SneakyThrows
  60. @PostMapping("/send-validate")
  61. public R sendValidate(@RequestParam String phone) {
  62. Map<String, String> params = SmsUtil.getValidateParams();
  63. SmsCode smsCode = smsBuilder.template().sendValidate(new SmsData(params).setKey(PARAM_KEY), phone);
  64. return smsCode.isSuccess() ? R.data(smsCode, SEND_SUCCESS) : R.fail(SEND_FAIL);
  65. }
  66. /**
  67. * 校验短信
  68. *
  69. * @param smsCode 短信校验信息
  70. */
  71. @SneakyThrows
  72. @PostMapping("/validate-message")
  73. public R validateMessage(SmsCode smsCode) {
  74. boolean validate = smsBuilder.template().validateMessage(smsCode);
  75. return validate ? R.success(VALIDATE_SUCCESS) : R.fail(VALIDATE_FAIL);
  76. }
  77. //========== 通用短信自定义发送(支持自定义params参数传递, 推荐用于测试, 不推荐用于生产环境) ==========
  78. /**
  79. * 发送信息
  80. *
  81. * @param params 自定义短信参数
  82. * @param phones 手机号集合
  83. */
  84. @SneakyThrows
  85. @PostMapping("/send-message")
  86. public R sendMessage(@RequestParam String params, @RequestParam String phones) {
  87. SmsData smsData = new SmsData(JsonUtil.readMap(params, String.class, String.class));
  88. return send(smsData, phones);
  89. }
  90. //========== 指定短信服务发送(可根据各种场景自定拓展定制, 损失灵活性增加安全性, 推荐用于生产环境) ==========
  91. /**
  92. * 短信通知
  93. *
  94. * @param phones 手机号集合
  95. */
  96. @SneakyThrows
  97. @PostMapping("/send-notice")
  98. public R sendNotice(@RequestParam String phones) {
  99. Map<String, String> params = new HashMap<>(3);
  100. params.put("title", "通知标题");
  101. params.put("content", "通知内容");
  102. params.put("date", "通知时间");
  103. SmsData smsData = new SmsData(params);
  104. return send(smsData, phones);
  105. }
  106. /**
  107. * 订单通知
  108. *
  109. * @param phones 手机号集合
  110. */
  111. @SneakyThrows
  112. @PostMapping("/send-order")
  113. public R sendOrder(@RequestParam String phones) {
  114. Map<String, String> params = new HashMap<>(3);
  115. params.put("orderNo", "订单编号");
  116. params.put("packageNo", "快递单号");
  117. params.put("user", "收件人");
  118. SmsData smsData = new SmsData(params);
  119. return send(smsData, phones);
  120. }
  121. /**
  122. * 会议通知
  123. *
  124. * @param phones 手机号集合
  125. */
  126. @SneakyThrows
  127. @PostMapping("/send-meeting")
  128. public R sendMeeting(@RequestParam String phones) {
  129. Map<String, String> params = new HashMap<>(2);
  130. params.put("roomId", "会议室");
  131. params.put("topic", "会议主题");
  132. params.put("date", "会议时间");
  133. SmsData smsData = new SmsData(params);
  134. return send(smsData, phones);
  135. }
  136. //================================= 通用短信发送接口 =================================
  137. /**
  138. * 通用短信发送接口
  139. *
  140. * @param smsData 短信内容
  141. * @param phones 手机号列表
  142. * @return 是否发送成功
  143. */
  144. private R send(SmsData smsData, String phones) {
  145. SmsResponse response = smsBuilder.template().sendMessage(smsData, Func.toStrList(phones));
  146. return response.isSuccess() ? R.success(SEND_SUCCESS) : R.fail(SEND_FAIL);
  147. }
  148. }