Parcourir la source

集成短信发送

wukai il y a 2 ans
Parent
commit
a14a109aba
2 fichiers modifiés avec 151 ajouts et 5 suppressions
  1. 13 5
      doc-biz/pom.xml
  2. 138 0
      doc-biz/src/main/java/com/doc/sms/TencentSmsService.java

+ 13 - 5
doc-biz/pom.xml

@@ -16,6 +16,14 @@
     </description>
 
     <dependencies>
+        <!--腾讯短信-->
+        <dependency>
+            <groupId>com.tencentcloudapi</groupId>
+            <artifactId>tencentcloud-sdk-java</artifactId>
+            <!-- go to https://search.maven.org/search?q=tencentcloud-sdk-java and get the latest version. -->
+            <!-- 请到https://search.maven.org/search?q=tencentcloud-sdk-java查询所有版本,最新版本如下 -->
+            <version>3.1.899</version>
+        </dependency>
         <!-- 通用工具-->
         <dependency>
             <groupId>com.jjt</groupId>
@@ -46,11 +54,11 @@
             <!--                </exclusion>-->
             <!--            </exclusions>-->
         </dependency>
-<!--        <dependency>-->
-<!--            <groupId>org.elasticsearch</groupId>-->
-<!--            <artifactId>elasticsearch</artifactId>-->
-<!--            <version>7.17.3</version>-->
-<!--        </dependency>-->
+        <!--        <dependency>-->
+        <!--            <groupId>org.elasticsearch</groupId>-->
+        <!--            <artifactId>elasticsearch</artifactId>-->
+        <!--            <version>7.17.3</version>-->
+        <!--        </dependency>-->
         <!-- 集成elasticsearch end -->
 
     </dependencies>

+ 138 - 0
doc-biz/src/main/java/com/doc/sms/TencentSmsService.java

@@ -0,0 +1,138 @@
+package com.doc.sms;
+
+import com.doc.common.utils.DateUtils;
+import com.tencentcloudapi.common.Credential;
+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.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
+
+/**
+ * 腾讯云短信接口
+ *
+ * @author wukai
+ * @date 2022-12-19
+ */
+@Service
+public class TencentSmsService {
+    private static final String SECRET_ID = "AKIDKTMzLQErDJ8gxwe5F8j36sMZWCbjaBUW";
+    private static final String SECRET_KEY = "mHVR6UbRzGGzRbU6RUGlzwc2A1astgIi";
+    private static final String APPID = "1400871556";
+    private static final String SIGN_NAME = "四川聚聚通科技有限公司";
+
+    private static final Logger log = LoggerFactory.getLogger(TencentSmsService.class);
+
+    private SendSmsResponse sendSms(String phone, String templateId, String[] templateParamSet) throws Exception {
+        // 实例化一个认证对象cred,入参需要传入腾讯云账户密钥 secretId,secretKey, 前往 API 密钥管理 页面,即可进行获取密钥。此处还需注意密钥对的保密。
+        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
+        //  从3.1.16版本开始, 单独设置 HTTP 代理
+        HttpProfile httpProfile = new HttpProfile();
+        // get请求(默认为post请求)
+        httpProfile.setReqMethod("POST");
+        // 请求连接超时时间,单位为秒(默认60秒)
+        httpProfile.setConnTimeout(60);
+        // 指定接入地域域名(默认就近接入)
+        httpProfile.setEndpoint("sms.tencentcloudapi.com");
+        ClientProfile clientProfile = new ClientProfile();
+        // 指定签名算法(默认为HmacSHA256)
+        clientProfile.setSignMethod("HmacSHA256");
+        // 自3.1.80版本开始,SDK 支持打印日志。
+        clientProfile.setHttpProfile(httpProfile);
+        clientProfile.setDebug(true);
+        //实例化要请求产品的client对象。
+        SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
+        SendSmsRequest req = new SendSmsRequest();
+
+        req.setSmsSdkAppId(APPID);
+        req.setSignName(SIGN_NAME);
+        req.setTemplateId(templateId);
+        req.setTemplateParamSet(templateParamSet);
+
+        String preStr = "+86";
+        String[] phoneNumberSet = new String[]{preStr + phone};
+        req.setPhoneNumberSet(phoneNumberSet);
+        String sessionContext = "";
+        req.setSessionContext(sessionContext);
+        String extendCode = "";
+        req.setExtendCode(extendCode);
+        String senderId = "";
+        req.setSenderId(senderId);
+        return client.SendSms(req);
+    }
+
+    /**
+     * 发送验证码
+     *
+     * @param phone 电话
+     * @param code  验证码
+     */
+    @Async("threadPoolTaskExecutor")
+    public void verificationCode(String phone, String code) {
+        try {
+            String templateId = "1647196";
+            String[] templateParamSet = new String[]{code};
+            SendSmsResponse res = sendSms(phone, templateId, templateParamSet);
+            log.info(SendSmsResponse.toJsonString(res));
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.error("passSms:{}", e.getMessage());
+        }
+    }
+
+
+    /**
+     * 业务信息告警
+     *
+     * @param phone   电话
+     * @param time    时间
+     * @param account 账号
+     * @param msg     ip
+     */
+    @Async("threadPoolTaskExecutor")
+    public void alarmIllegal(String phone, String time, String account, String msg) {
+        try {
+            String templateId = "1999844";
+            String[] templateParamSet = new String[]{time, account, msg};
+            SendSmsResponse res = sendSms(phone, templateId, templateParamSet);
+            log.info(SendSmsResponse.toJsonString(res));
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.error("passSms:{}", e.getMessage());
+        }
+    }
+
+    /**
+     * 性能监控
+     *
+     * @param phone 电话
+     * @param ip    服务器IP
+     * @param type  类型 CPU 内存 硬盘
+     * @param data  百分比
+     */
+    @Async("threadPoolTaskExecutor")
+    public void performanceMonitoring(String phone, String ip, String type, String data) {
+        try {
+            String templateId = "1454381";
+            String[] templateParamSet = new String[]{ip, type, data};
+            SendSmsResponse res = sendSms(phone, templateId, templateParamSet);
+            log.info(SendSmsResponse.toJsonString(res));
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.error("passSms:{}", e.getMessage());
+        }
+    }
+
+
+    public static void main(String[] args) throws Exception {
+        TencentSmsService service = new TencentSmsService();
+        String phone = "";
+        service.alarmIllegal(phone, DateUtils.dateTime(), "192.168.1.11", "非法访问!");
+        service.performanceMonitoring(phone, "192.168.1.11", "CPU", "80%");
+        service.verificationCode(phone, "9527");
+    }
+}