|
@ -1,142 +1,142 @@
|
|
|
package com.yihu.jw.care.util;
|
|
|
|
|
|
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.yihu.jw.care.config.TencentSmsConfig;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* Created by Bing on 2021/5/19.
|
|
|
*/
|
|
|
@Component
|
|
|
public class TencentSmsUtil {
|
|
|
|
|
|
@Autowired
|
|
|
private StringRedisTemplate redisTemplate;
|
|
|
private TencentSmsConfig tencentSmsConfig = new TencentSmsConfig();
|
|
|
|
|
|
/**
|
|
|
* 获取短信验证码
|
|
|
* @param phoneNumberList 手机列表
|
|
|
* @param templateParamList 参数列表
|
|
|
* @param type 模板key VerificationCode:手机验证码
|
|
|
* @return
|
|
|
*/
|
|
|
public String SendSms(List<String> phoneNumberList, List<String> templateParamList,String type) {
|
|
|
try {
|
|
|
Iterator<String> iterator = phoneNumberList.iterator();
|
|
|
if ("VerificationCode".equals(type)){//超过10次无法再发送短信验证码
|
|
|
while (iterator.hasNext()) {
|
|
|
String phone = iterator.next();
|
|
|
if (redisTemplate.hasKey("tencentSmsVFCode-" + phone)) {//验证码每天上限10次
|
|
|
Integer count = Integer.valueOf(redisTemplate.opsForValue().get("tencentSmsVFCode-" + phone));
|
|
|
if (count > 10) {
|
|
|
iterator.remove();
|
|
|
}else {
|
|
|
count++;
|
|
|
Long expire = redisTemplate.boundHashOps("tencentSmsVFCode-" + phone).getExpire();
|
|
|
redisTemplate.opsForValue().set("tencentSmsVFCode-" + phone,count+"",expire, TimeUnit.SECONDS);
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
int count =1;
|
|
|
redisTemplate.opsForValue().set("tencentSmsVFCode-" + phone,count+"",tencentSmsConfig.overTime, TimeUnit.HOURS);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
String[] phoneNumbers = phoneNumberList.toArray(new String[0]);
|
|
|
String[] templateParams = templateParamList!=null?templateParamList.toArray(new String[0]):null;
|
|
|
Credential cred = new Credential(tencentSmsConfig.SecretId, tencentSmsConfig.SecretKey);
|
|
|
HttpProfile httpProfile = new HttpProfile();
|
|
|
// 设置代理
|
|
|
// httpProfile.setProxyHost("host");
|
|
|
// httpProfile.setProxyPort(port);
|
|
|
httpProfile.setReqMethod("POST");
|
|
|
httpProfile.setConnTimeout(60);
|
|
|
httpProfile.setEndpoint("sms.tencentcloudapi.com");
|
|
|
ClientProfile clientProfile = new ClientProfile();
|
|
|
|
|
|
/** SDK默认用TC3-HMAC-SHA256进行签名
|
|
|
* 非必要请不要修改这个字段 */
|
|
|
clientProfile.setSignMethod("HmacSHA256");
|
|
|
clientProfile.setHttpProfile(httpProfile);
|
|
|
|
|
|
/** 实例化要请求产品(以sms为例)的client对象
|
|
|
* 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,或者引用预设的常量 */
|
|
|
SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
|
|
|
SendSmsRequest req = new SendSmsRequest();
|
|
|
|
|
|
/* 短信应用ID: 短信SdkAppid在 [短信控制台] 添加应用后生成的实际SdkAppid,示例如1400006666 */
|
|
|
String appid = "1400009099";
|
|
|
req.setSmsSdkAppid(appid);
|
|
|
|
|
|
/* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,签名信息可登录 [短信控制台] 查看 */
|
|
|
String sign = "签名内容";
|
|
|
req.setSign(sign);
|
|
|
|
|
|
/* 模板 ID: 必须填写已审核通过的模板 ID。模板ID可登录 [短信控制台] 查看 */
|
|
|
String templateID = tencentSmsConfig.getTemplateId(type);
|
|
|
req.setTemplateID(templateID);
|
|
|
|
|
|
/** 下发手机号码,采用 e.164 标准,+[国家或地区码][手机号]
|
|
|
* 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号
|
|
|
* */
|
|
|
req.setPhoneNumberSet(phoneNumbers);
|
|
|
|
|
|
/** 模板参数: 若无模板参数,则设置为空
|
|
|
String[] templateParams = {"5678"};*/
|
|
|
req.setTemplateParamSet(templateParams);
|
|
|
|
|
|
/** 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的
|
|
|
* 返回的 res 是一个 SendSmsResponse 类的实例,与请求对象对应 */
|
|
|
SendSmsResponse res = client.SendSms(req);
|
|
|
|
|
|
// 输出json格式的字符串回包
|
|
|
System.out.println(SendSmsResponse.toJsonString(res));
|
|
|
// 也可以取出单个值,你可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义
|
|
|
System.out.println(res.getRequestId());
|
|
|
return SendSmsResponse.toJsonString(null);
|
|
|
} catch (TencentCloudSDKException e) {
|
|
|
e.printStackTrace();
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
/** 返回值
|
|
|
*{
|
|
|
* "Response": {
|
|
|
* "SendStatusSet": [
|
|
|
* {
|
|
|
* "SerialNo": "5000:1045710669157053657849499619", 发送流水号。
|
|
|
* "PhoneNumber": "+8618511122233",
|
|
|
* "Fee": 1, 计费条数,计费规则请查询
|
|
|
* "SessionContext": "test",
|
|
|
* "Code": "Ok", 短信请求状态码
|
|
|
* "Message": "send success",
|
|
|
* "IsoCode": "CN"
|
|
|
* },
|
|
|
* {
|
|
|
* "SerialNo": "5000:104571066915705365784949619",
|
|
|
* "PhoneNumber": "+8618511122266",
|
|
|
* "Fee": 1,
|
|
|
* "SessionContext": "test",
|
|
|
* "Code": "Ok",
|
|
|
* "Message": "send success",
|
|
|
* "IsoCode": "CN"
|
|
|
* }
|
|
|
* ],
|
|
|
* "RequestId": "a0aabda6-cf91-4f3e-a81f-9198114a2279"
|
|
|
* }
|
|
|
* }
|
|
|
*/
|
|
|
//package com.yihu.jw.care.util;
|
|
|
//
|
|
|
//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.yihu.jw.care.config.TencentSmsConfig;
|
|
|
//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
//import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
//import org.springframework.stereotype.Component;
|
|
|
//
|
|
|
//import java.util.Iterator;
|
|
|
//import java.util.List;
|
|
|
//import java.util.concurrent.TimeUnit;
|
|
|
//
|
|
|
///**
|
|
|
// * Created by Bing on 2021/5/19.
|
|
|
// */
|
|
|
//@Component
|
|
|
//public class TencentSmsUtil {
|
|
|
//
|
|
|
// @Autowired
|
|
|
// private StringRedisTemplate redisTemplate;
|
|
|
// private TencentSmsConfig tencentSmsConfig = new TencentSmsConfig();
|
|
|
//
|
|
|
// /**
|
|
|
// * 获取短信验证码
|
|
|
// * @param phoneNumberList 手机列表
|
|
|
// * @param templateParamList 参数列表
|
|
|
// * @param type 模板key VerificationCode:手机验证码
|
|
|
// * @return
|
|
|
// */
|
|
|
// public String SendSms(List<String> phoneNumberList, List<String> templateParamList,String type) {
|
|
|
// try {
|
|
|
// Iterator<String> iterator = phoneNumberList.iterator();
|
|
|
// if ("VerificationCode".equals(type)){//超过10次无法再发送短信验证码
|
|
|
// while (iterator.hasNext()) {
|
|
|
// String phone = iterator.next();
|
|
|
// if (redisTemplate.hasKey("tencentSmsVFCode-" + phone)) {//验证码每天上限10次
|
|
|
// Integer count = Integer.valueOf(redisTemplate.opsForValue().get("tencentSmsVFCode-" + phone));
|
|
|
// if (count > 10) {
|
|
|
// iterator.remove();
|
|
|
// }else {
|
|
|
// count++;
|
|
|
// Long expire = redisTemplate.boundHashOps("tencentSmsVFCode-" + phone).getExpire();
|
|
|
// redisTemplate.opsForValue().set("tencentSmsVFCode-" + phone,count+"",expire, TimeUnit.SECONDS);
|
|
|
// }
|
|
|
// }
|
|
|
// else {
|
|
|
// int count =1;
|
|
|
// redisTemplate.opsForValue().set("tencentSmsVFCode-" + phone,count+"",tencentSmsConfig.overTime, TimeUnit.HOURS);
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
// String[] phoneNumbers = phoneNumberList.toArray(new String[0]);
|
|
|
// String[] templateParams = templateParamList!=null?templateParamList.toArray(new String[0]):null;
|
|
|
// Credential cred = new Credential(tencentSmsConfig.SecretId, tencentSmsConfig.SecretKey);
|
|
|
// HttpProfile httpProfile = new HttpProfile();
|
|
|
// // 设置代理
|
|
|
//// httpProfile.setProxyHost("host");
|
|
|
//// httpProfile.setProxyPort(port);
|
|
|
// httpProfile.setReqMethod("POST");
|
|
|
// httpProfile.setConnTimeout(60);
|
|
|
// httpProfile.setEndpoint("sms.tencentcloudapi.com");
|
|
|
// ClientProfile clientProfile = new ClientProfile();
|
|
|
//
|
|
|
// /** SDK默认用TC3-HMAC-SHA256进行签名
|
|
|
// * 非必要请不要修改这个字段 */
|
|
|
// clientProfile.setSignMethod("HmacSHA256");
|
|
|
// clientProfile.setHttpProfile(httpProfile);
|
|
|
//
|
|
|
// /** 实例化要请求产品(以sms为例)的client对象
|
|
|
// * 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,或者引用预设的常量 */
|
|
|
// SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
|
|
|
// SendSmsRequest req = new SendSmsRequest();
|
|
|
//
|
|
|
// /* 短信应用ID: 短信SdkAppid在 [短信控制台] 添加应用后生成的实际SdkAppid,示例如1400006666 */
|
|
|
// String appid = "1400009099";
|
|
|
// req.setSmsSdkAppid(appid);
|
|
|
//
|
|
|
// /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,签名信息可登录 [短信控制台] 查看 */
|
|
|
// String sign = "签名内容";
|
|
|
// req.setSign(sign);
|
|
|
//
|
|
|
// /* 模板 ID: 必须填写已审核通过的模板 ID。模板ID可登录 [短信控制台] 查看 */
|
|
|
// String templateID = tencentSmsConfig.getTemplateId(type);
|
|
|
// req.setTemplateID(templateID);
|
|
|
//
|
|
|
// /** 下发手机号码,采用 e.164 标准,+[国家或地区码][手机号]
|
|
|
// * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号
|
|
|
// * */
|
|
|
// req.setPhoneNumberSet(phoneNumbers);
|
|
|
//
|
|
|
// /** 模板参数: 若无模板参数,则设置为空
|
|
|
// String[] templateParams = {"5678"};*/
|
|
|
// req.setTemplateParamSet(templateParams);
|
|
|
//
|
|
|
// /** 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的
|
|
|
// * 返回的 res 是一个 SendSmsResponse 类的实例,与请求对象对应 */
|
|
|
// SendSmsResponse res = client.SendSms(req);
|
|
|
//
|
|
|
// // 输出json格式的字符串回包
|
|
|
// System.out.println(SendSmsResponse.toJsonString(res));
|
|
|
// // 也可以取出单个值,你可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义
|
|
|
// System.out.println(res.getRequestId());
|
|
|
// return SendSmsResponse.toJsonString(null);
|
|
|
// } catch (TencentCloudSDKException e) {
|
|
|
// e.printStackTrace();
|
|
|
// return null;
|
|
|
// }
|
|
|
// }
|
|
|
//}
|
|
|
///** 返回值
|
|
|
// *{
|
|
|
// * "Response": {
|
|
|
// * "SendStatusSet": [
|
|
|
// * {
|
|
|
// * "SerialNo": "5000:1045710669157053657849499619", 发送流水号。
|
|
|
// * "PhoneNumber": "+8618511122233",
|
|
|
// * "Fee": 1, 计费条数,计费规则请查询
|
|
|
// * "SessionContext": "test",
|
|
|
// * "Code": "Ok", 短信请求状态码
|
|
|
// * "Message": "send success",
|
|
|
// * "IsoCode": "CN"
|
|
|
// * },
|
|
|
// * {
|
|
|
// * "SerialNo": "5000:104571066915705365784949619",
|
|
|
// * "PhoneNumber": "+8618511122266",
|
|
|
// * "Fee": 1,
|
|
|
// * "SessionContext": "test",
|
|
|
// * "Code": "Ok",
|
|
|
// * "Message": "send success",
|
|
|
// * "IsoCode": "CN"
|
|
|
// * }
|
|
|
// * ],
|
|
|
// * "RequestId": "a0aabda6-cf91-4f3e-a81f-9198114a2279"
|
|
|
// * }
|
|
|
// * }
|
|
|
// */
|