SmsServiceImpl.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.backendsys.modules.sms.service.impl;
  2. import cn.hutool.core.convert.Convert;
  3. import cn.hutool.core.util.RandomUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import cn.hutool.json.JSONObject;
  6. import cn.hutool.json.JSONUtil;
  7. import com.backendsys.exception.CustException;
  8. import com.backendsys.modules.common.config.redis.utils.RedisUtil;
  9. import com.backendsys.modules.common.config.security.utils.HttpRequestUtil;
  10. import com.backendsys.modules.sdk.tencentcloud.sms.service.TencentSmsService;
  11. import com.backendsys.modules.sms.dao.SmsDao;
  12. import com.backendsys.modules.sms.entity.Sms;
  13. import com.backendsys.utils.response.ResultEnum;
  14. import com.tencentcloudapi.common.exception.TencentCloudSDKException;
  15. import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
  16. import com.tencentcloudapi.sms.v20210111.models.SendStatus;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.beans.factory.annotation.Value;
  20. import org.springframework.stereotype.Service;
  21. import java.util.Map;
  22. import java.util.concurrent.TimeUnit;
  23. @Slf4j
  24. @Service
  25. public class SmsServiceImpl implements com.backendsys.modules.sms.service.SmsService {
  26. @Autowired
  27. private RedisUtil redisUtil;
  28. @Autowired
  29. private HttpRequestUtil httpRequestUtil;
  30. @Autowired
  31. private TencentSmsService tencentSmsService;
  32. @Autowired
  33. private SmsDao smsDao;
  34. // [模板] 通用验证码
  35. @Value("${tencent.sms.template-id-of-common}")
  36. private String TEMPLATE_ID_COMMON;
  37. @Value("${spring.application.name}")
  38. private String APPLICATION_NAME;
  39. /**
  40. * 发送验证码
  41. */
  42. @Override
  43. public Map<String, Object> sendValidCode(Sms sms) throws TencentCloudSDKException {
  44. String origin = sms.getOrigin();
  45. String phone = sms.getPhone();
  46. Integer phoneAreaCode = sms.getPhone_area_code();
  47. Integer smsExpire = 5;
  48. Integer smsCode = RandomUtil.randomInt(100000, 999999); // 生成一个6位的随机整数 (验证码) (5分钟失效)
  49. sms.setSms_code(smsCode);
  50. // -- 腾讯云短信 ------------------------------------------------
  51. // 模板参数 {1} 验证码, {2} 过期时间(分钟)
  52. // 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空
  53. String[] templateParamSet = {String.valueOf(smsCode), String.valueOf(smsExpire)};
  54. // 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
  55. // 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号
  56. String[] phoneNumberSet = {"+" + phoneAreaCode + phone};
  57. SendSmsResponse sendSmsResponse = tencentSmsService.send(TEMPLATE_ID_COMMON, templateParamSet, phoneNumberSet);
  58. if (sendSmsResponse != null) {
  59. SendStatus[] response = sendSmsResponse.getSendStatusSet();
  60. if (response.length > 0) {
  61. // for (int i = 0; i < sendStatuses.length; ++i) {
  62. if (response[0] != null && !response[0].getCode().equals("Ok")) {
  63. // String sendStatusStr = JSONUtil.toJsonStr(sendStatuses[0]);
  64. if (response.length > 1) {
  65. throw new CustException("发送失败", ResultEnum.REMOTE_EXCEPTION.getCode(), response[1]);
  66. } else {
  67. JSONObject sendStatusesObj = JSONUtil.parseObj(response[0]);
  68. System.out.println("sendStatusesObj = " + sendStatusesObj);
  69. String message = Convert.toStr(sendStatusesObj.get("Message"));
  70. System.out.println("message = " + message);
  71. throw new CustException(message, ResultEnum.REMOTE_EXCEPTION.getCode(), message);
  72. }
  73. }
  74. }
  75. }
  76. // ------------------------------------------------------------
  77. // -- 发送成功 --------------------------------------------------
  78. // [Redis] 将 来源+手机号码 为标识,将验证码存入缓存
  79. String originStr = StrUtil.isNotBlank(origin) ? (origin + "-") : "";
  80. String redisKey = APPLICATION_NAME + "-sms-" + originStr + phone;
  81. redisUtil.setCacheObject(redisKey, smsCode, smsExpire, TimeUnit.MINUTES);
  82. // [插入] 新增短信记录
  83. sms.setIp(httpRequestUtil.getIpAddr());
  84. sms.setUa(httpRequestUtil.getUa());
  85. smsDao.insert(sms);
  86. return Map.of("phone", sms.getPhone());
  87. }
  88. }