|
|
@ -1,97 +1,82 @@
|
|
|
|
package com.java3y.austin.script;
|
|
|
|
package com.java3y.austin.script;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
|
|
|
|
import cn.hutool.core.text.CharPool;
|
|
|
|
|
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
|
|
|
import com.google.common.base.Throwables;
|
|
|
|
import com.java3y.austin.pojo.SmsParam;
|
|
|
|
import com.java3y.austin.pojo.SmsParam;
|
|
|
|
import com.java3y.austin.utils.OkHttpUtils;
|
|
|
|
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 lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @author 3y
|
|
|
|
* @author 3y
|
|
|
|
* @date 2021/11/4
|
|
|
|
* @date 2021/11/6
|
|
|
|
* <p>
|
|
|
|
* 1. 发送短信接入文档:https://cloud.tencent.com/document/api/382/55981
|
|
|
|
* 接入文档:https://cloud.tencent.com/document/api/382/55981
|
|
|
|
* 2. 推荐直接使用SDK
|
|
|
|
|
|
|
|
* 3. 推荐使用API Explorer 生成代码
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@Slf4j
|
|
|
|
|
|
|
|
@Service
|
|
|
|
@Service
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
public class TencentSmsScript {
|
|
|
|
public class TencentSmsScript {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private OkHttpUtils okHttpUtils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String URL = "https://sms.tencentcloudapi.com/";
|
|
|
|
|
|
|
|
private static final String ACTION = "SendSms";
|
|
|
|
|
|
|
|
private static final String VERSION = "2021-01-11";
|
|
|
|
|
|
|
|
private static final String SMS_SDK_APP_ID = "1400592125";
|
|
|
|
|
|
|
|
private static final String TEMPLATE_ID = "1182097";
|
|
|
|
|
|
|
|
private static final String SIGN_NAME = "Java3y公众号";
|
|
|
|
|
|
|
|
private static final List<String> REGION = Arrays.asList("ap-beijing", "ap-guangzhou", "ap-nanjing");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 加密签名相关
|
|
|
|
* api相关
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private static final String AUTHORIZATION_SIGN = "TC3-HMAC-SHA256";
|
|
|
|
private static final String URL = "sms.tencentcloudapi.com";
|
|
|
|
private static final String CREDENTIAL = "Credential=AKIDEXAMPLE";
|
|
|
|
private static final String REGION = "ap-guangzhou";
|
|
|
|
private static final String service = "sms";
|
|
|
|
|
|
|
|
private static final String TC3_REQUEST = "tc3_request";
|
|
|
|
|
|
|
|
private static final String SIGNED_HEADERS = "SignedHeaders=content-type;host";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 账号相关
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private final static String SECRET_ID = "//";
|
|
|
|
|
|
|
|
private final static String SECRET_KEY = "//";
|
|
|
|
|
|
|
|
private static final String SMS_SDK_APP_ID = "//";
|
|
|
|
|
|
|
|
private static final String TEMPLATE_ID = "//";
|
|
|
|
|
|
|
|
private static final String SIGN_NAME = "//";
|
|
|
|
|
|
|
|
|
|
|
|
public String send(SmsParam smsParam) {
|
|
|
|
public String send(SmsParam smsParam) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 初始化 client
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
Credential cred = new Credential(SECRET_ID, SECRET_KEY);
|
|
|
|
|
|
|
|
HttpProfile httpProfile = new HttpProfile();
|
|
|
|
|
|
|
|
httpProfile.setEndpoint(URL);
|
|
|
|
|
|
|
|
ClientProfile clientProfile = new ClientProfile();
|
|
|
|
|
|
|
|
clientProfile.setHttpProfile(httpProfile);
|
|
|
|
|
|
|
|
SmsClient client = new SmsClient(cred, REGION, clientProfile);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 组装发送短信参数
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
SendSmsRequest req = new SendSmsRequest();
|
|
|
|
|
|
|
|
String[] phoneNumberSet1 = smsParam.getPhones().toArray(new String[smsParam.getPhones().size() - 1]);
|
|
|
|
|
|
|
|
req.setPhoneNumberSet(phoneNumberSet1);
|
|
|
|
|
|
|
|
req.setSmsSdkAppId(SMS_SDK_APP_ID);
|
|
|
|
|
|
|
|
req.setSignName(SIGN_NAME);
|
|
|
|
|
|
|
|
req.setTemplateId(TEMPLATE_ID);
|
|
|
|
|
|
|
|
String[] templateParamSet1 = {"3333"};
|
|
|
|
|
|
|
|
req.setTemplateParamSet(templateParamSet1);
|
|
|
|
|
|
|
|
req.setSessionContext(IdUtil.fastSimpleUUID());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 请求,返回结果
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
SendSmsResponse resp = client.SendSms(req);
|
|
|
|
|
|
|
|
return SendSmsResponse.toJsonString(resp);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (TencentCloudSDKException e) {
|
|
|
|
|
|
|
|
log.error("send tencent sms fail!{},params:{}",
|
|
|
|
|
|
|
|
Throwables.getStackTraceAsString(e), JSON.toJSONString(smsParam));
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, String> header = getHeader();
|
|
|
|
|
|
|
|
Map<String, Object> params = getParams(smsParam);
|
|
|
|
|
|
|
|
String paramsJSON = JSON.toJSONString(params);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String result = okHttpUtils.doPostJsonWithHeaders(URL, paramsJSON, header);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Map<String, Object> getParams(SmsParam smsParam) {
|
|
|
|
|
|
|
|
HashMap<String, Object> params = new HashMap<>();
|
|
|
|
|
|
|
|
int phoneSize = smsParam.getPhones().size() - 1;
|
|
|
|
|
|
|
|
int paramSize = Arrays.asList(smsParam.getContent()).size() - 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// params.put("PhoneNumberSet", CollUtil.join(smsParam.getPhones(), StrUtil.COMMA));
|
|
|
|
|
|
|
|
params.put("PhoneNumberSet."+phoneSize, JSON.toJSONString(smsParam.getPhones()));
|
|
|
|
|
|
|
|
params.put("SmsSdkAppId", SMS_SDK_APP_ID);
|
|
|
|
|
|
|
|
params.put("TemplateId", TEMPLATE_ID);
|
|
|
|
|
|
|
|
params.put("SignName", SIGN_NAME);
|
|
|
|
|
|
|
|
params.put("TemplateParamSet."+paramSize, JSON.toJSONString(Arrays.asList(smsParam.getContent())));
|
|
|
|
|
|
|
|
params.put("SessionContext", IdUtil.simpleUUID());
|
|
|
|
|
|
|
|
return params;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Map<String, String> getHeader() {
|
|
|
|
|
|
|
|
HashMap<String, String> headers = new HashMap<>();
|
|
|
|
|
|
|
|
headers.put("X-TC-Action", ACTION);
|
|
|
|
|
|
|
|
headers.put("X-TC-Version", VERSION);
|
|
|
|
|
|
|
|
headers.put("X-TC-Region", REGION.get(RandomUtil.randomInt(REGION.size())));
|
|
|
|
|
|
|
|
headers.put("X-TC-Timestamp", String.valueOf(DateUtil.currentSeconds()));
|
|
|
|
|
|
|
|
return headers;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|