pull/2/head
三歪 3 years ago
parent bf7c500d82
commit 89e5dfedaa

@ -12,6 +12,7 @@
<artifactId>austin-handler</artifactId>
<dependencies>
<dependency>
<groupId>com.java3y.austin</groupId>
<artifactId>austin-common</artifactId>
@ -22,5 +23,16 @@
<artifactId>austin-support</artifactId>
<version>0.0.2</version>
</dependency>
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<exclusions>
<exclusion>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>

@ -1,97 +1,82 @@
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.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Throwables;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author 3y
* @date 2021/11/4
* <p>
* https://cloud.tencent.com/document/api/382/55981
* @date 2021/11/6
* 1. https://cloud.tencent.com/document/api/382/55981
* 2. 使SDK
* 3. 使API Explorer
*/
@Slf4j
@Service
@Slf4j
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 CREDENTIAL = "Credential=AKIDEXAMPLE";
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 static final String URL = "sms.tencentcloudapi.com";
private static final String REGION = "ap-guangzhou";
/**
*
*/
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) {
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;
}
}

@ -12,6 +12,7 @@
<artifactId>austin-support</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>

@ -23,4 +23,20 @@
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

@ -1,6 +1,5 @@
package com.java3y.austin;
import cn.hutool.setting.SettingUtil;
import com.java3y.austin.pojo.SmsParam;
import com.java3y.austin.script.TencentSmsScript;
import org.springframework.beans.factory.annotation.Autowired;
@ -27,13 +26,12 @@ public class AustinApplication {
public String hello() {
SmsParam smsParam = SmsParam.builder()
.phones(new HashSet<>(Arrays.asList("13719193845")))
.phones(new HashSet<>(Arrays.asList("//")))
.content("3333")
.build();
return tencentSmsScript.send(smsParam);
}
}

@ -53,13 +53,6 @@
<version>31.0.1-jre</version>
</dependency>
<!--common工具包-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<!--http库-->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
@ -73,24 +66,16 @@
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
<!--腾讯sdk(目前用在短信上)-->
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>3.1.390</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

Loading…
Cancel
Save