Procházet zdrojové kódy

重构短信接口

tsurumure před 8 měsíci
rodič
revize
e2e5d9d479

+ 63 - 63
src/main/java/com/backendsys/controller/Systems/SysSMSController.java

@@ -1,63 +1,63 @@
-package com.backendsys.controller.Systems;
-
-import com.backendsys.entity.System.SysSMSDTO;
-import com.backendsys.service.SDKService.SDKTencent.SDKTencentSMSService;
-import com.backendsys.utils.response.Result;
-import jakarta.servlet.http.HttpServletRequest;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.validation.annotation.Validated;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RestController;
-
-@Validated
-@RestController
-public class SysSMSController {
-
-    @Autowired
-    private SDKTencentSMSService sdkTencentSMSService;
-
-//    /**
-//     * [腾讯云] 获得短信验证码
-//     */
-//    @PostMapping("/api/public/system/sms/getSMS")
-//    public Result getSMS(@Validated(SysSMSDTO.Send.class) @RequestBody SysSMSDTO sysSMSDTO, HttpServletRequest request) {
-//        sysSMSDTO.setIp(request.getRemoteAddr());
-//        sysSMSDTO.setUa(request.getHeader("User-Agent"));
-//        return Result.success(sdkTencentSMSService.sendSMS(sysSMSDTO));
-//    }
-
-    /**
-     * [腾讯云] 拉取回执状态
-     */
-    @GetMapping("/api/system/sms/getSMSCallbackStatistics")
-    public Result getSMSPullStatus(String beginTime, String endTime, String limit) {
-        return Result.success(sdkTencentSMSService.getSMSCallbackStatistics(beginTime, endTime, limit));
-    }
-
-    /**
-     * [腾讯云] 获得统计短信发送数据
-     */
-    @GetMapping("/api/system/sms/getSMSStatistics")
-    public Result getSMSStatistics(String beginTime, String endTime) {
-        return Result.success(sdkTencentSMSService.getSMSStatistics(beginTime, endTime));
-    }
-
-//    /**
-//     * [腾讯云] 套餐包信息统计
-//     */
-//    @GetMapping("/api/system/sms/getSmsPackagesStatistics")
-//    public Result getSmsPackagesStatistics(String beginTime, String endTime) {
-//        return Result.success(sdkTencentSMSService.getSmsPackagesStatistics(beginTime, endTime));
-//    }
-
-//    /**
-//     * [腾讯云] 号码信息查询 { phones: "13xxxxx, 13xxxxx" }
-//     */
-//    @GetMapping("/api/system/sms/getDescribePhoneNumberInfo")
-//    public Result getDescribePhoneNumberInfo(String phones) {
-//        return Result.success(sdkTencentSMSService.getDescribePhoneNumberInfo(phones));
-//    }
-
-}
+//package com.backendsys.controller.Systems;
+//
+//import com.backendsys.entity.System.SysSMSDTO;
+//import com.backendsys.service.SDKService.SDKTencent.SDKTencentSMSService;
+//import com.backendsys.utils.response.Result;
+//import jakarta.servlet.http.HttpServletRequest;
+//import org.springframework.beans.factory.annotation.Autowired;
+//import org.springframework.validation.annotation.Validated;
+//import org.springframework.web.bind.annotation.GetMapping;
+//import org.springframework.web.bind.annotation.PostMapping;
+//import org.springframework.web.bind.annotation.RequestBody;
+//import org.springframework.web.bind.annotation.RestController;
+//
+//@Validated
+//@RestController
+//public class SysSMSController {
+//
+//    @Autowired
+//    private SDKTencentSMSService sdkTencentSMSService;
+//
+////    /**
+////     * [腾讯云] 获得短信验证码
+////     */
+////    @PostMapping("/api/public/system/sms/getSMS")
+////    public Result getSMS(@Validated(SysSMSDTO.Send.class) @RequestBody SysSMSDTO sysSMSDTO, HttpServletRequest request) {
+////        sysSMSDTO.setIp(request.getRemoteAddr());
+////        sysSMSDTO.setUa(request.getHeader("User-Agent"));
+////        return Result.success(sdkTencentSMSService.sendSMS(sysSMSDTO));
+////    }
+//
+////    /**
+////     * [腾讯云] 拉取回执状态
+////     */
+////    @GetMapping("/api/system/sms/getSMSCallbackStatistics")
+////    public Result getSMSPullStatus(String beginTime, String endTime, String limit) {
+////        return Result.success(sdkTencentSMSService.getSMSCallbackStatistics(beginTime, endTime, limit));
+////    }
+//
+////    /**
+////     * [腾讯云] 获得统计短信发送数据
+////     */
+////    @GetMapping("/api/system/sms/getSMSStatistics")
+////    public Result getSMSStatistics(String beginTime, String endTime) {
+////        return Result.success(sdkTencentSMSService.getSMSStatistics(beginTime, endTime));
+////    }
+//
+////    /**
+////     * [腾讯云] 套餐包信息统计
+////     */
+////    @GetMapping("/api/system/sms/getSmsPackagesStatistics")
+////    public Result getSmsPackagesStatistics(String beginTime, String endTime) {
+////        return Result.success(sdkTencentSMSService.getSmsPackagesStatistics(beginTime, endTime));
+////    }
+//
+////    /**
+////     * [腾讯云] 号码信息查询 { phones: "13xxxxx, 13xxxxx" }
+////     */
+////    @GetMapping("/api/system/sms/getDescribePhoneNumberInfo")
+////    public Result getDescribePhoneNumberInfo(String phones) {
+////        return Result.success(sdkTencentSMSService.getDescribePhoneNumberInfo(phones));
+////    }
+//
+//}

+ 21 - 3
src/main/java/com/backendsys/modules/sms/controller/SmsController.java

@@ -1,14 +1,13 @@
 package com.backendsys.modules.sms.controller;
 
-import com.backendsys.entity.System.SysSMSDTO;
 import com.backendsys.modules.common.utils.Result;
 import com.backendsys.modules.sms.entity.Sms;
 import com.backendsys.modules.sms.service.SmsService;
+import com.backendsys.modules.tencent.service.TencentSmsService;
 import com.tencentcloudapi.common.exception.TencentCloudSDKException;
-import jakarta.servlet.http.HttpServletRequest;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RestController;
@@ -20,6 +19,9 @@ public class SmsController {
     @Autowired
     private SmsService smsService;
 
+    @Autowired
+    private TencentSmsService tencentSmsService;
+
     /**
      * 发送短信
      */
@@ -28,4 +30,20 @@ public class SmsController {
         return Result.success().put("data", smsService.sendValidCode(sms));
     }
 
+    /**
+     * [腾讯云] 获得统计短信发送数据
+     */
+    @GetMapping("/api/system/sms/getSMSStatistics")
+    public Result getSMSStatistics(String beginTime, String endTime) throws TencentCloudSDKException {
+        return Result.success().put("data", tencentSmsService.getSMSStatistics(beginTime, endTime));
+    }
+
+    /**
+     * [腾讯云] 获得回执数据统计
+     */
+    @GetMapping("/api/system/sms/getSMSCallbackStatistics")
+    public Result getSMSCallbackStatistics(String beginTime, String endTime) throws TencentCloudSDKException {
+        return Result.success().put("data", tencentSmsService.getSMSCallbackStatistics(beginTime, endTime));
+    }
+
 }

+ 16 - 0
src/main/java/com/backendsys/modules/tencent/entity/SmsCallbackStatis.java

@@ -0,0 +1,16 @@
+package com.backendsys.modules.tencent.entity;
+
+import lombok.Data;
+
+@Data
+public class SmsCallbackStatis {
+    private Long callback_count;
+    private Long request_success_count;
+    private Long callback_fail_count;
+    private Long callback_success_count;
+    private Long internal_error_count;
+    private Long invalid_number_count;
+    private Long shutdown_error_count;
+    private Long black_list_count;
+    private Long frequency_limit_count;
+}

+ 10 - 0
src/main/java/com/backendsys/modules/tencent/entity/SmsStatis.java

@@ -0,0 +1,10 @@
+package com.backendsys.modules.tencent.entity;
+
+import lombok.Data;
+
+@Data
+public class SmsStatis {
+    private Long fee_count;
+    private Long request_count;
+    private Long request_success_count;
+}

+ 7 - 0
src/main/java/com/backendsys/modules/tencent/service/TencentSmsService.java

@@ -1,5 +1,7 @@
 package com.backendsys.modules.tencent.service;
 
+import com.backendsys.modules.tencent.entity.SmsCallbackStatis;
+import com.backendsys.modules.tencent.entity.SmsStatis;
 import com.tencentcloudapi.common.exception.TencentCloudSDKException;
 import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
 
@@ -10,4 +12,9 @@ public interface TencentSmsService {
     // [腾讯云] 发送短信 { 模板ID, 模板参数, 下发手机号码 }
     SendSmsResponse send(String template_id, String[] templateParamSet, String[] phoneNumberSet) throws TencentCloudSDKException;
 
+    // [腾讯云] 获得统计短信发送数据
+    SmsStatis getSMSStatistics(String beginTime, String endTime) throws TencentCloudSDKException;
+
+    // [腾讯云] 获得回执数据统计
+    SmsCallbackStatis getSMSCallbackStatistics(String beginTime, String endTime) throws TencentCloudSDKException;
 }

+ 93 - 10
src/main/java/com/backendsys/modules/tencent/service/impl/TencentSmsServiceImpl.java

@@ -1,22 +1,25 @@
 package com.backendsys.modules.tencent.service.impl;
 
-import cn.hutool.core.util.RandomUtil;
+import cn.hutool.core.util.StrUtil;
 import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 import com.backendsys.exception.CustException;
 import com.backendsys.modules.common.config.redis.utils.RedisUtil;
+import com.backendsys.modules.tencent.entity.SmsCallbackStatis;
+import com.backendsys.modules.tencent.entity.SmsStatis;
 import com.backendsys.modules.tencent.service.TencentSmsService;
+import com.backendsys.utils.MapUtil;
 import com.tencentcloudapi.common.Credential;
 import com.tencentcloudapi.common.exception.TencentCloudSDKException;
 import com.tencentcloudapi.common.profile.ClientProfile;
 import com.tencentcloudapi.sms.v20210111.SmsClient;
-import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
-import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
+import com.tencentcloudapi.sms.v20210111.models.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
+import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
@@ -32,7 +35,18 @@ public class TencentSmsServiceImpl implements TencentSmsService {
     @Value("${tencent.sms.sign-name}")
     private String SIGN_NAME;
 
-    // [腾讯云] 发送短信 { 模板ID, 模板参数, 下发手机号码 }
+    // [腾讯云] 生成 SmsClient
+    private SmsClient getSmsClient() {
+        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
+        ClientProfile clientProfile = new ClientProfile();
+        SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
+        return client;
+    }
+
+    /**
+     * [腾讯云] 发送短信 { 模板ID, 模板参数, 下发手机号码 }
+     * https://cloud.tencent.com/document/product/382/55981
+     */
     @Override
     public SendSmsResponse send(String template_id, String[] templateParamSet, String[] phoneNumberSet) throws TencentCloudSDKException {
 
@@ -44,14 +58,83 @@ public class TencentSmsServiceImpl implements TencentSmsService {
         req.setPhoneNumberSet(phoneNumberSet);
 
         // [SDK] 发送短信
-        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
-        ClientProfile clientProfile = new ClientProfile();
-        SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
-
         // 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的
         // 返回的 res 是一个 SendSmsResponse 类的实例,与请求对象对应
-        SendSmsResponse res = client.SendSms(req);
-        return res;
+        SmsClient client = getSmsClient();
+        SendSmsResponse response = client.SendSms(req);
+        return response;
+    }
+
+    /**
+     * [腾讯云] 获得统计短信发送数据
+     * https://cloud.tencent.com/document/product/382/55965
+     */
+    @Override
+    public SmsStatis getSMSStatistics(String beginTime, String endTime) throws TencentCloudSDKException {
+
+        if (StrUtil.isEmpty(beginTime)) throw new CustException("beginTime 不能为空");
+        if (StrUtil.isEmpty(endTime)) throw new CustException("endTime 不能为空");
+
+        SendStatusStatisticsRequest req = new SendStatusStatisticsRequest();
+        req.setSmsSdkAppId(SDK_APP_ID);
+        req.setLimit(0L);   // 最大上限 (注:目前固定设置为0)
+        req.setOffset(0L);  // 偏移量 (注:目前固定设置为0
+        req.setBeginTime(beginTime);
+        req.setEndTime(endTime);
+
+        // [SDK] 统计短信发送数据
+        // 通过 client 对象调用 SendStatusStatistics 方法发起请求。注意请求方法名与请求对象是对应的
+        // 返回的 res 是一个 SendStatusStatisticsResponse 类的实例,与请求对象对应
+        SmsClient client = getSmsClient();
+        SendStatusStatisticsResponse response = client.SendStatusStatistics(req);
+        SendStatusStatistics statis = response.getSendStatusStatistics();
+
+        // String resStr = SendStatusStatisticsResponse.toJsonString(response);
+
+        SmsStatis smsStatis = new SmsStatis();
+        smsStatis.setFee_count(statis.getFeeCount());
+        smsStatis.setRequest_count(statis.getRequestCount());
+        smsStatis.setRequest_success_count(statis.getRequestSuccessCount());
+        return smsStatis;
     }
 
+    /**
+     * [腾讯云] 获得回执数据统计
+     * https://cloud.tencent.com/document/product/382/55966
+     */
+    @Override
+    public SmsCallbackStatis getSMSCallbackStatistics(String beginTime, String endTime) throws TencentCloudSDKException {
+
+        if (StrUtil.isEmpty(beginTime)) throw new CustException("beginTime 不能为空");
+        if (StrUtil.isEmpty(endTime)) throw new CustException("endTime 不能为空");
+
+        CallbackStatusStatisticsRequest req = new CallbackStatusStatisticsRequest();
+        req.setSmsSdkAppId(SDK_APP_ID);
+        req.setLimit(0L);
+        req.setOffset(0L);
+        req.setBeginTime(beginTime);
+        req.setEndTime(endTime);
+
+        SmsClient client = getSmsClient();
+        CallbackStatusStatisticsResponse response = client.CallbackStatusStatistics(req);
+        CallbackStatusStatistics statis = response.getCallbackStatusStatistics();
+
+        // String resStr = SendStatusStatisticsResponse.toJsonString(response);
+
+        SmsCallbackStatis smsCallbackStatis = new SmsCallbackStatis();
+        smsCallbackStatis.setCallback_count(statis.getCallbackCount());
+        smsCallbackStatis.setRequest_success_count(statis.getRequestSuccessCount());
+        smsCallbackStatis.setCallback_fail_count(statis.getCallbackFailCount());
+        smsCallbackStatis.setCallback_success_count(statis.getCallbackSuccessCount());
+        smsCallbackStatis.setInternal_error_count(statis.getInternalErrorCount());
+        smsCallbackStatis.setInvalid_number_count(statis.getInvalidNumberCount());
+        smsCallbackStatis.setShutdown_error_count(statis.getShutdownErrorCount());
+        smsCallbackStatis.setBlack_list_count(statis.getBlackListCount());
+        smsCallbackStatis.setFrequency_limit_count(statis.getFrequencyLimitCount());
+        return smsCallbackStatis;
+    }
+
+
+
+
 }