|
|
@@ -6,17 +6,18 @@ import com.izouma.dingdong.repo.SmsRecordRepo;
|
|
|
import com.izouma.dingdong.service.sms.SmsService;
|
|
|
import com.tencentcloudapi.common.Credential;
|
|
|
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
|
|
+import com.tencentcloudapi.common.profile.ClientProfile;
|
|
|
+import com.tencentcloudapi.common.profile.HttpProfile;
|
|
|
import com.tencentcloudapi.sms.v20190711.SmsClient;
|
|
|
-import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
|
|
|
-import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
|
|
|
-import com.tencentcloudapi.sms.v20190711.models.SendStatus;
|
|
|
+import com.tencentcloudapi.sms.v20190711.models.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.RandomStringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import javax.websocket.SendResult;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneOffset;
|
|
|
|
|
|
/**
|
|
|
* Tencent Cloud Sms Sendsms
|
|
|
@@ -31,51 +32,61 @@ public class TencentSmsService {
|
|
|
private String secretKey;
|
|
|
@Value("${tencent.sms_appid}")
|
|
|
private String appId;
|
|
|
+ @Autowired
|
|
|
private SmsRecordRepo smsRecordRepo;
|
|
|
|
|
|
|
|
|
- public String sendVerify(String phone) {
|
|
|
- // 短信模板ID,需要在短信应用中申请
|
|
|
- String templateId = "676939";
|
|
|
+ public String sendVerify(String phone, String templateId) {
|
|
|
// 签名,使用的是`签名内容`,而不是`签名ID`
|
|
|
String smsSign = "弗雷登";
|
|
|
- String[] phones = {phone};
|
|
|
|
|
|
- String[] param = {RandomStringUtils.randomNumeric(4)};
|
|
|
+ String param = RandomStringUtils.randomNumeric(4);
|
|
|
+
|
|
|
try {
|
|
|
- //发送请求
|
|
|
- SendSmsRequest req = new SendSmsRequest();
|
|
|
- req.setPhoneNumberSet(phones);
|
|
|
- req.setTemplateID(templateId);
|
|
|
- req.setSign(smsSign);
|
|
|
- req.setSmsSdkAppid(appId);
|
|
|
- req.setTemplateParamSet(param);
|
|
|
- // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
|
|
|
+ // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
|
|
|
Credential cred = new Credential(secretId, secretKey);
|
|
|
- //实例化要请求产品的client对象,clientProfile是可选的
|
|
|
- SmsClient client = new SmsClient(cred, "");
|
|
|
- client.SendSms(req);
|
|
|
- SendSmsResponse resp = client.SendSms(req);
|
|
|
+ //请求
|
|
|
+ HttpProfile httpProfile = new HttpProfile();
|
|
|
+ httpProfile.setEndpoint("sms.tencentcloudapi.com");
|
|
|
|
|
|
- SendStatus sendStatus = resp.getSendStatusSet()[0];
|
|
|
- String code = sendStatus.getCode();
|
|
|
- if (!code.equals("Ok")) {
|
|
|
- throw new BusinessException("发送失败,请稍后再试", code + "," + sendStatus.getMessage());
|
|
|
+ ClientProfile clientProfile = new ClientProfile();
|
|
|
+ clientProfile.setHttpProfile(httpProfile);
|
|
|
+
|
|
|
+ SmsClient client = new SmsClient(cred, "ap-nanjing", clientProfile);
|
|
|
+
|
|
|
+ String params = "{\"PhoneNumberSet\":[\"" + phone
|
|
|
+ + "\"],\"TemplateID\":\"" + templateId
|
|
|
+ + "\",\"Sign\":\"" + smsSign
|
|
|
+ + "\",\"TemplateParamSet\":[\"" + param
|
|
|
+ + "\"],\"SmsSdkAppid\":\"" + appId + "\"}";
|
|
|
+ //发送请求
|
|
|
+ SendSmsRequest req = SendSmsRequest.fromJsonString(params, SendSmsRequest.class);
|
|
|
+ //回调
|
|
|
+ SendSmsResponse resp = client.SendSms(req);
|
|
|
+ String s = SendSmsResponse.toJsonString(resp);
|
|
|
+ SendStatus status = resp.getSendStatusSet()[0];
|
|
|
+ if (!status.getCode().equals("Ok")) {
|
|
|
+ throw new BusinessException("发送失败,请稍后再试", status.getCode() + "," + status.getMessage());
|
|
|
}
|
|
|
+ //报错到sms表
|
|
|
smsRecordRepo.expire(phone);
|
|
|
- String sessionId = RandomStringUtils.randomAlphabetic(10);
|
|
|
+// String sessionId = RandomStringUtils.randomAlphabetic(10);
|
|
|
+ String sessionId = resp.getRequestId();
|
|
|
smsRecordRepo.save(SmsRecord.builder()
|
|
|
.sessionId(sessionId)
|
|
|
.phone(phone)
|
|
|
- .code(param[0])
|
|
|
+ .code(param)
|
|
|
.expiresAt(LocalDateTime.now().plusMinutes(5))
|
|
|
.expired(false)
|
|
|
.build());
|
|
|
- return sessionId;
|
|
|
|
|
|
+ System.out.println(s);
|
|
|
+ return sessionId;
|
|
|
} catch (TencentCloudSDKException e) {
|
|
|
e.printStackTrace();
|
|
|
throw new BusinessException("发送失败,请稍后再试", e.getMessage());
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
}
|