SendSmsUtils.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.ruoyi.common.utils;
  2. import com.aliyun.dysmsapi20170525.models.SendBatchSmsRequest;
  3. import com.aliyun.dysmsapi20170525.models.SendBatchSmsResponse;
  4. import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
  5. import com.aliyun.tea.TeaException;
  6. /**
  7. * @author tjf
  8. * @Date: 2021/07/15/10:21
  9. */
  10. public class SendSmsUtils {
  11. //短信参数
  12. static final String ACCESS_KEY_ID = "LTAI5tNA2fcBJH6EWRH6Pxr6";
  13. static final String ACCESS_KEY_SECRET = "5WdaPEOvC3u9LC7pwy2DQ9pgmJvgUr";
  14. //生成X位验证码
  15. public static String getCode(Integer num) {
  16. String[] codes = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
  17. StringBuilder code = new StringBuilder();
  18. for (int i = 0; i < num; i++) {
  19. int j = (int) (Math.random() * 10);
  20. if (j <= 0) {
  21. j = 1;
  22. }
  23. code.append(codes[j - 1]);
  24. }
  25. return code.toString();
  26. }
  27. /**
  28. * 使用AK&SK初始化账号Client
  29. *
  30. * @return Client
  31. * @throws Exception
  32. */
  33. public static com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
  34. // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
  35. // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
  36. com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
  37. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
  38. .setAccessKeyId(System.getenv(ACCESS_KEY_ID))
  39. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
  40. .setAccessKeySecret(System.getenv(ACCESS_KEY_SECRET));
  41. // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
  42. config.endpoint = "dysmsapi.aliyuncs.com";
  43. return new com.aliyun.dysmsapi20170525.Client(config);
  44. }
  45. /**
  46. * 发送短信消息
  47. *
  48. * @return
  49. */
  50. public static String sendSms(String phone, String templateCode, String smsCode) {
  51. String code = "";
  52. try {
  53. // 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html
  54. com.aliyun.dysmsapi20170525.Client client = SendSmsUtils.createClient();
  55. com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
  56. //手机号码
  57. .setPhoneNumbers(phone)
  58. //短信签名名称。中新云
  59. .setSignName("中新云")
  60. //短信模板变量对应的实际值{"name": code}
  61. .setTemplateParam(smsCode)
  62. //短信模板CODE
  63. .setTemplateCode(templateCode);
  64. // 复制代码运行请自行打印 API 的返回值
  65. SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, new com.aliyun.teautil.models.RuntimeOptions());
  66. code = sendSmsResponse.getBody().code;
  67. } catch (Exception _error) {
  68. }
  69. return code;
  70. }
  71. /**
  72. * 阿里云批量发送 短信接口,一次最多100个手机号码
  73. *
  74. * @return
  75. * @throws
  76. */
  77. public static SendBatchSmsResponse sendBatchSms(SendBatchSmsRequest sendBatchSmsRequest) {
  78. try {
  79. com.aliyun.dysmsapi20170525.Client client = SendSmsUtils.createClient();
  80. com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
  81. SendBatchSmsResponse sendBatchSmsResponse = client.sendBatchSmsWithOptions(sendBatchSmsRequest, runtime);
  82. return sendBatchSmsResponse;
  83. // 复制代码运行请自行打印 API 的返回值
  84. } catch (TeaException error) {
  85. // 如有需要,请打印 error
  86. com.aliyun.teautil.Common.assertAsString(error.message);
  87. } catch (Exception _error) {
  88. TeaException error = new TeaException(_error.getMessage(), _error);
  89. // 如有需要,请打印 error
  90. com.aliyun.teautil.Common.assertAsString(error.message);
  91. }
  92. return null;
  93. }
  94. /**
  95. * 发送注册的随机密码
  96. *
  97. * @return
  98. */
  99. public static String sendPassword(String code, String phone) {
  100. try {
  101. // 工程代码泄露可能会导致AccessKey泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html
  102. com.aliyun.dysmsapi20170525.Client client = SendSmsUtils.createClient();
  103. String smsCode = "{\"code\":\"" + code + "\"}";
  104. com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
  105. //手机号码
  106. .setPhoneNumbers(phone)
  107. //短信签名名称。潜山市数据资源局
  108. .setSignName("潜山市政协办公室")
  109. //短信模板CODE
  110. .setTemplateCode("SMS_219525380")
  111. //短信模板变量对应的实际值{"name": code}
  112. .setTemplateParam(smsCode);
  113. // 复制代码运行请自行打印 API 的返回值
  114. SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, new com.aliyun.teautil.models.RuntimeOptions());
  115. code = sendSmsResponse.getBody().code;
  116. } catch (Exception _error) {
  117. }
  118. return code;
  119. }
  120. }