AliyunSmsUtil.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package caimei.utils;
  2. import com.aliyuncs.DefaultAcsClient;
  3. import com.aliyuncs.IAcsClient;
  4. import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
  5. import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
  6. import com.aliyuncs.exceptions.ClientException;
  7. import com.aliyuncs.http.MethodType;
  8. import com.aliyuncs.profile.DefaultProfile;
  9. import com.aliyuncs.profile.IClientProfile;
  10. import lombok.extern.slf4j.Slf4j;
  11. /**
  12. * 阿里云短信Sms
  13. *
  14. * @author : Charles
  15. * @date : 2021/3/5
  16. */
  17. @Slf4j
  18. public class AliyunSmsUtil {
  19. /** 短信API产品名称(短信产品名固定,无需修改) */
  20. private static final String PRODUCT = "Dysmsapi";
  21. /** 短信API产品域名(接口地址固定,无需修改) */
  22. private static final String DOMAIN = "dysmsapi.aliyuncs.com";
  23. /** 开发者自己的AK(在阿里云访问控制台寻找) */
  24. private static final String ACCESS_KEY_ID = "LTAI4GBL3o4YkWnbKYgf2Xia";
  25. private static final String ACCESS_KEY_SECRET = "dBjAXqbYiEPP6Ukuk2ZsXQeET7FVkK";
  26. /**
  27. * 可登录阿里云查看和定义短信模板:https://dysms.console.aliyun.com/dysms.htm#/domestic/text/template
  28. */
  29. public static boolean sendSms(String mobile, Integer type, String templateParam) {
  30. String templateCode = null;
  31. String signName = "采美";
  32. if (null == type || type<0) {
  33. return false;
  34. } else if (type == 0) {
  35. // 模版内容: 验证码${code},您正在尝试变更重要信息,请妥善保管账户信息。
  36. templateCode = "SMS_205240539";
  37. } else if (type == 1) {
  38. // 模版内容: 验证码${code},您正在尝试修改登录密码,请妥善保管账户信息。
  39. templateCode = "SMS_205240540";
  40. } else if (type == 2) {
  41. // 模版内容: 验证码${code},您正在注册成为新用户,感谢您的支持!
  42. templateCode = "SMS_205240541";
  43. } else if (type == 3) {
  44. // 模版内容: 验证码${code},您正尝试异地登录,若非本人操作,请勿泄露。
  45. templateCode = "SMS_205240542";
  46. } else if (type == 4) {
  47. // 模版内容: 验证码${code},您正在登录,若非本人操作,请勿泄露。
  48. templateCode = "SMS_205240543";
  49. } else if (type == 5) {
  50. // 模版内容: 验证码${code},您正在进行身份验证,打死不要告诉别人哦!
  51. templateCode = "SMS_205240544";
  52. } else if (type == 6) {
  53. // 模版内容: 欢迎成为采美机构用户,您的登录账号为:${name},初始密码为:cm${content},您可使用该账号密码登录采美365网和“采美采购商城”小程序。
  54. templateCode = "SMS_215122672";
  55. } else if (type == 7) {
  56. // 模版内容: 您"呵呵商城"小程序的验证码为:${code},验证码 5 分钟内有效。
  57. templateCode = "SMS_215334982";
  58. signName = "采美365";
  59. } else if (type == 8) {
  60. // 模版内容: 欢迎注册采美365网,您的短信验证码为:${code},该验证码 5 分钟内有效,请勿泄漏于他人。
  61. templateCode = "SMS_205435882";
  62. } else if (type == 9) {
  63. // 模版内容: 欢迎注册采美365网供应商账号,您的短信验证码为:${code},该验证码 5 分钟内有效,请勿泄漏于他人。
  64. templateCode = "SMS_205445750";
  65. } else if (type == 10) {
  66. // 模版内容: 注册成功!您可以通过手机号${name}登录。
  67. templateCode = "SMS_205445760";
  68. } else if (type == 11) {
  69. // 模版内容: 欢迎成为${name}的运营人员,您的邀请码为${code}。您可使用以下两种方式激活您的身份:1. 您可在微信搜索“采美采购商城”小程序,使用邀请码登录并绑定微信;2. 进入“采美采购商城”小程序后,使用邀请码登录并绑定微信。绑定微信后,您可通过微信授权直接登录“采美采购商城”小程序或微信扫码直接登录采美365网站。
  70. templateCode = "SMS_205445789";
  71. } else if (type == 12) {
  72. // 模版内容: 欢迎成为机构运营人员,您的验证码为${code}。运营人员可通过微信授权直接登录采美365网。
  73. templateCode = "SMS_205435899";
  74. } else if (type == 13) {
  75. // 模版内容: 您正在更换联系人手机号,您的验证码为:${code}。
  76. templateCode = "SMS_205435893";
  77. } else if (type == 14) {
  78. // 模版内容: 您正品联盟登录密码为:${password}。
  79. templateCode = "SMS_217145278";
  80. }
  81. try {
  82. //可自助调整超时时间
  83. System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
  84. System.setProperty("sun.net.client.defaultReadTimeout", "10000");
  85. //初始化acsClient,暂不支持region化
  86. IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
  87. DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN);
  88. IAcsClient acsClient = new DefaultAcsClient(profile);
  89. //组装请求对象
  90. SendSmsRequest request = new SendSmsRequest();
  91. //使用post提交
  92. request.setMethod(MethodType.POST);
  93. //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式;发送国际/港澳台消息时,接收号码格式为国际区号+号码,如“85200000000”
  94. request.setPhoneNumbers(mobile);
  95. //必填:短信签名-可在短信控制台中找到
  96. request.setSignName(signName);
  97. //必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版
  98. request.setTemplateCode(templateCode);
  99. //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为{\"name\":\"Tom\", \"code\":\"123\"}";
  100. request.setTemplateParam(templateParam);
  101. //请求失败这里会抛ClientException异常
  102. SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
  103. log.info(">阿里云短信接口返回的数据: "
  104. +"Code="+ sendSmsResponse.getCode()
  105. +",Message=" + sendSmsResponse.getMessage()
  106. +",RequestId=" + sendSmsResponse.getRequestId()
  107. +",BizId=" + sendSmsResponse.getBizId());
  108. //请求成功
  109. return sendSmsResponse.getCode() != null && "OK".equals(sendSmsResponse.getCode());
  110. } catch (ClientException e) {
  111. // log.error(e.getErrCode(), e.getErrMsg(), e.getErrorDescription());
  112. return false;
  113. }
  114. }
  115. }