TencentSmsService.java 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package com.izouma.dingdong.service.sms;
  2. import com.izouma.dingdong.domain.SmsRecord;
  3. import com.izouma.dingdong.exception.BusinessException;
  4. import com.izouma.dingdong.repo.SmsRecordRepo;
  5. import com.izouma.dingdong.service.sms.SmsService;
  6. import com.tencentcloudapi.common.Credential;
  7. import com.tencentcloudapi.common.exception.TencentCloudSDKException;
  8. import com.tencentcloudapi.common.profile.ClientProfile;
  9. import com.tencentcloudapi.common.profile.HttpProfile;
  10. import com.tencentcloudapi.sms.v20190711.SmsClient;
  11. import com.tencentcloudapi.sms.v20190711.models.*;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.apache.commons.lang3.RandomStringUtils;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.beans.factory.annotation.Value;
  16. import org.springframework.stereotype.Service;
  17. import java.time.LocalDateTime;
  18. import java.time.ZoneOffset;
  19. /**
  20. * Tencent Cloud Sms Sendsms
  21. * https://cloud.tencent.com/document/product/382/38778
  22. */
  23. @Service
  24. @Slf4j
  25. public class TencentSmsService {
  26. @Value("${tencent.secret_id}")
  27. private String secretId;
  28. @Value("${tencent.secret_key}")
  29. private String secretKey;
  30. @Value("${tencent.sms_appid}")
  31. private String appId;
  32. @Autowired
  33. private SmsRecordRepo smsRecordRepo;
  34. public String sendVerify(String phone, String templateId) {
  35. // 签名,使用的是`签名内容`,而不是`签名ID`
  36. String smsSign = "弗雷登";
  37. String param = RandomStringUtils.randomNumeric(4);
  38. try {
  39. // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
  40. Credential cred = new Credential(secretId, secretKey);
  41. //请求李心洁
  42. HttpProfile httpProfile = new HttpProfile();
  43. httpProfile.setEndpoint("sms.tencentcloudapi.com");
  44. ClientProfile clientProfile = new ClientProfile();
  45. clientProfile.setHttpProfile(httpProfile);
  46. SmsClient client = new SmsClient(cred, "", clientProfile);
  47. String params = "{\"PhoneNumberSet\":[\"" + phone
  48. + "\"],\"TemplateID\":\"" + templateId
  49. + "\",\"Sign\":\"" + smsSign
  50. + "\",\"TemplateParamSet\":[\"" + param
  51. + "\"],\"SmsSdkAppid\":\"" + appId + "\"}";
  52. //发送请求
  53. SendSmsRequest req = SendSmsRequest.fromJsonString(params, SendSmsRequest.class);
  54. //国际短信必传
  55. req.setSenderId("Qcloud");
  56. //回调
  57. SendSmsResponse resp = client.SendSms(req);
  58. String s = SendSmsResponse.toJsonString(resp);
  59. SendStatus status = resp.getSendStatusSet()[0];
  60. if (!status.getCode().equals("Ok")) {
  61. throw new BusinessException("发送失败,请稍后再试", status.getCode() + "," + status.getMessage());
  62. }
  63. //报错到sms表
  64. smsRecordRepo.expire(phone);
  65. String sessionId = RandomStringUtils.randomAlphabetic(10);
  66. // String sessionId = resp.getRequestId();
  67. smsRecordRepo.save(SmsRecord.builder()
  68. .sessionId(sessionId)
  69. .phone(phone)
  70. .code(param)
  71. .expiresAt(LocalDateTime.now().plusMinutes(5))
  72. .expired(false)
  73. .build());
  74. System.out.println(s);
  75. return sessionId;
  76. } catch (TencentCloudSDKException e) {
  77. e.printStackTrace();
  78. throw new BusinessException("发送失败,请稍后再试", e.getMessage());
  79. }
  80. }
  81. }