diff --git a/austin-handler/pom.xml b/austin-handler/pom.xml index a21b46f..9834ecb 100644 --- a/austin-handler/pom.xml +++ b/austin-handler/pom.xml @@ -12,6 +12,7 @@ austin-handler + com.java3y.austin austin-common @@ -22,5 +23,16 @@ austin-support 0.0.2 + + + com.tencentcloudapi + tencentcloud-sdk-java + + + com.squareup.okio + okio + + + \ No newline at end of file diff --git a/austin-handler/src/main/java/com/java3y/austin/script/TencentSmsScript.java b/austin-handler/src/main/java/com/java3y/austin/script/TencentSmsScript.java index 98bf5b3..620371e 100644 --- a/austin-handler/src/main/java/com/java3y/austin/script/TencentSmsScript.java +++ b/austin-handler/src/main/java/com/java3y/austin/script/TencentSmsScript.java @@ -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 - *

- * 接入文档: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 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 header = getHeader(); - Map params = getParams(smsParam); - String paramsJSON = JSON.toJSONString(params); - - String result = okHttpUtils.doPostJsonWithHeaders(URL, paramsJSON, header); - - - return result; - } - - private Map getParams(SmsParam smsParam) { - HashMap 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 getHeader() { - HashMap 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; } - - - } diff --git a/austin-support/pom.xml b/austin-support/pom.xml index cfb7b18..7afc259 100644 --- a/austin-support/pom.xml +++ b/austin-support/pom.xml @@ -12,6 +12,7 @@ austin-support + org.springframework.boot spring-boot-starter diff --git a/austin-web/pom.xml b/austin-web/pom.xml index 27f3b1c..526bff1 100644 --- a/austin-web/pom.xml +++ b/austin-web/pom.xml @@ -23,4 +23,20 @@ + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + \ No newline at end of file diff --git a/austin-web/src/main/java/com/java3y/austin/AustinApplication.java b/austin-web/src/main/java/com/java3y/austin/AustinApplication.java index 5787e24..3dedfd0 100644 --- a/austin-web/src/main/java/com/java3y/austin/AustinApplication.java +++ b/austin-web/src/main/java/com/java3y/austin/AustinApplication.java @@ -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); - } } diff --git a/pom.xml b/pom.xml index 0312992..f9ebfe6 100644 --- a/pom.xml +++ b/pom.xml @@ -53,13 +53,6 @@ 31.0.1-jre - - - org.apache.commons - commons-collections4 - 4.4 - - com.squareup.okhttp3 @@ -73,24 +66,16 @@ fastjson 1.2.78 + + + + com.tencentcloudapi + tencentcloud-sdk-java + 3.1.390 + - - - - org.springframework.boot - spring-boot-maven-plugin - - - - org.projectlombok - lombok - - - - - - +