|
@ -0,0 +1,135 @@
|
|
|
package com.yihu.jw.care.service;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
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.v20210111.SmsClient;
|
|
|
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
|
|
|
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
/**
|
|
|
* 腾讯短信发送
|
|
|
* @Author: yeshijie
|
|
|
* @Date: 2024/11/25
|
|
|
* @Description:
|
|
|
*/
|
|
|
@Service
|
|
|
public class TXYSmsService {
|
|
|
private Logger logger= LoggerFactory.getLogger(TXYSmsService.class);
|
|
|
|
|
|
|
|
|
private String SecretId = "AKIDa2IQM8i7QWQ6fATZmncDey50hGj0302x";
|
|
|
private String SecretKey = "teDe0BBye8QdmkdLxPhlfOwJUYSCrSyP";
|
|
|
// private String signName = "厦门i健康";
|
|
|
private String smsSdkAppId = "1400951100";
|
|
|
// private String VerificationCode = "2317340";//冠心病风险评估结果
|
|
|
|
|
|
|
|
|
public String sendMessage(String mobile,String signName,String verificationCode,Integer size,String templateParam1,String templateParam2,
|
|
|
String templateParam3,String templateParam4){
|
|
|
try{
|
|
|
Credential cred = new Credential(SecretId, SecretKey);
|
|
|
|
|
|
HttpProfile httpProfile = new HttpProfile();
|
|
|
httpProfile.setEndpoint("sms.tencentcloudapi.com");
|
|
|
|
|
|
ClientProfile clientProfile = new ClientProfile();
|
|
|
clientProfile.setHttpProfile(httpProfile);
|
|
|
|
|
|
SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
|
|
|
|
|
|
SendSmsRequest req = new SendSmsRequest();
|
|
|
String[] phoneNumberSet1 = {"+86"+mobile};
|
|
|
req.setPhoneNumberSet(phoneNumberSet1);
|
|
|
|
|
|
req.setSmsSdkAppId(smsSdkAppId);
|
|
|
req.setSignName(signName);
|
|
|
req.setTemplateId(verificationCode);
|
|
|
|
|
|
if(size>0){
|
|
|
String[] templateParamSet = new String[size];
|
|
|
templateParamSet[0] = StringUtils.isBlank(templateParam1)?"":templateParam1;
|
|
|
if(size>1){
|
|
|
templateParamSet[1] = StringUtils.isBlank(templateParam2)?"":templateParam2;
|
|
|
}
|
|
|
if(size>2){
|
|
|
templateParamSet[2] = StringUtils.isBlank(templateParam3)?"":templateParam3;
|
|
|
}
|
|
|
if(size>3){
|
|
|
templateParamSet[3] = StringUtils.isBlank(templateParam4)?"":templateParam4;
|
|
|
}
|
|
|
req.setTemplateParamSet(templateParamSet);
|
|
|
}
|
|
|
SendSmsResponse resp = client.SendSms(req);
|
|
|
JSONObject json = JSON.parseObject(SendSmsResponse.toJsonString(resp));
|
|
|
String res = json.getJSONArray("SendStatusSet").getJSONObject(0).getString("Code");
|
|
|
if(!"Ok".equals(res)){
|
|
|
logger.info("腾讯短信发送失败:"+SendSmsResponse.toJsonString(resp));
|
|
|
}
|
|
|
return res;
|
|
|
} catch (TencentCloudSDKException e) {
|
|
|
e.printStackTrace();
|
|
|
logger.info("腾讯短信报错:"+e.getMessage());
|
|
|
}
|
|
|
return "error";
|
|
|
}
|
|
|
|
|
|
// /**
|
|
|
// *
|
|
|
// * @param mobile
|
|
|
// * @param jsonObj {"templateCode":"974603","templateParamArr":["param1","param2","param3"]}
|
|
|
// * @return 模板id暂时写死
|
|
|
// */
|
|
|
// public String sendMessageJson(String mobile,JSONObject jsonObj){
|
|
|
// try {
|
|
|
// Credential cred = new Credential(SecretId, SecretKey);
|
|
|
//
|
|
|
// String templateId = jsonObj.getString("templateCode");
|
|
|
// JSONArray templateParamArr = jsonObj.getJSONArray("templateParamArr");
|
|
|
// String[] templateParamSet1 = templateParamArr.toArray(new String[0]);
|
|
|
//
|
|
|
// HttpProfile httpProfile = new HttpProfile();
|
|
|
// httpProfile.setEndpoint("sms.tencentcloudapi.com");
|
|
|
//
|
|
|
// ClientProfile clientProfile = new ClientProfile();
|
|
|
// clientProfile.setHttpProfile(httpProfile);
|
|
|
//
|
|
|
// SmsClient client = new SmsClient(cred, "ap-nanjing", clientProfile);
|
|
|
//
|
|
|
// SendSmsRequest req = new SendSmsRequest();
|
|
|
// String[] phoneNumberSet1 = {"+86"+mobile};
|
|
|
//
|
|
|
// req.setPhoneNumberSet(phoneNumberSet1);
|
|
|
//
|
|
|
// req.setSmsSdkAppId(smsSdkAppId);
|
|
|
// req.setSignName(signName);
|
|
|
// req.setTemplateId(templateId);
|
|
|
//
|
|
|
// if(templateParamSet1.length>0){
|
|
|
// req.setTemplateParamSet(templateParamSet1);
|
|
|
// }
|
|
|
// SendSmsResponse resp = client.SendSms(req);
|
|
|
//
|
|
|
// JSONObject json = JSON.parseObject(SendSmsResponse.toJsonString(resp));
|
|
|
// String res = json.getJSONArray("SendStatusSet").getJSONObject(0).getString("Code");
|
|
|
// if(!"Ok".equals(res)){
|
|
|
// logger.info("腾讯短信发送失败:"+SendSmsResponse.toJsonString(resp));
|
|
|
// }
|
|
|
// return res;
|
|
|
//
|
|
|
//
|
|
|
// }catch (TencentCloudSDKException e){
|
|
|
// e.printStackTrace();
|
|
|
// logger.info("腾讯短信报错:"+e.toString());
|
|
|
// }
|
|
|
// return "error";
|
|
|
// }
|
|
|
|
|
|
}
|