发送短信信息工具类
import cn.hutool.core.collection.CollUtil;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
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.SendStatus;
import com.w.common.dto.MessageDTO;
import com.w.common.exception.WangyaoException;
import com.w.common.util.Json;
import jakarta.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Locale; @SuppressWarnings ( "ALL" )
@Component
public class SendMessage2TencentUtil { private static final Logger log = LoggerFactory.getLogger ( SendMessage2TencentUtil.class) ; @Value ( "${tencent.secretId}" ) private String secretId; @Value ( "${tencent.secretKey}" ) private String secretKey; @Value ( "${tencent.sms.sdkAppId}" ) private String sdkAppId; @Value ( "${tencent.sms.signName}" ) private String signName; public boolean sendMsg(String templateId, String[] templateParam, String[] phoneNumber) { return sendMsg ( signName, templateId, templateParam, phoneNumber) ; } public boolean sendMsg(String signName, String templateId, String[] templateParam, String[] phoneNumber) { return sendMsg ( sdkAppId, signName, templateId, templateParam, phoneNumber) ; } public boolean sendMsg ( String sdkAppId, String signName, String templateId, String[] templateParam, @NotNull String[] phoneNumber) { if (StringUtils.isEmpty(sdkAppId)) { throw new WangyaoException ( "应用ID不能为空" ) ; } if (StringUtils.isEmpty(signName)) { throw new WangyaoException ( "签名内容不能为空" ) ; } if (StringUtils.isEmpty(templateId)) { throw new WangyaoException ( "模板ID不能为空" ) ; } if (phoneNumber == null || phoneNumber.length == 0) { throw new WangyaoException ( "电话号不能为空" ) ; } // 实例化一个认证对象Credential cred = new Credential ( secretId, secretKey) ; // 实例化client对象SmsClient client = new SmsClient ( cred, "ap-beijing" ) ; // 实例化一个请求对象SendSmsRequest req = new SendSmsRequest ( ) ; // 短信应用IDreq.setSmsSdkAppId ( sdkAppId) ; // 短信签名内容req.setSignName ( signName) ; // 模板 IDreq.setTemplateId ( templateId) ; // 模板参数req.setTemplateParamSet ( templateParam) ; // 下发手机号码req.setPhoneNumberSet ( phoneNumber) ; try { // 通过 client 对象调用 SendSms 方法发起请求SendSmsResponse res = client.SendSms ( req) ; SendStatus[] sendStatusSet = res.getSendStatusSet ( ) ; StringBuilder sb = new StringBuilder ( ) ; for (SendStatus sendStatus : sendStatusSet) { if (!"OK".equals(sendStatus.getCode().toUpperCase(Locale.ROOT))) { sb.append ( Json.toJsonString ( sendStatus) ) ; } } if (sb.length() > 0) { throw new WangyaoException ( sb.toString ( ) ) ; } } catch (TencentCloudSDKException e) { log.error ( "SendMessage2TencentUtil.sendMsg Failed. " , e) ; return false; } return true; } public void sendMsg(List<MessageDTO> list) { if (CollUtil.isNotEmpty(list)) { list.forEach(dto -> { try { sendMsg ( StringUtils.isEmpty ( dto.getSdkAppId ( ) ) ? sdkAppId : dto.getSdkAppId ( ) , StringUtils.isEmpty ( dto.getSignName ( ) ) ? signName : dto.getSignName ( ) , dto.getTemplateId ( ) , dto.getTemplateParam ( ) , dto.getPhoneNumber ( ) ) ; } catch (Exception e) { log.error ( "sendMsg: " , e) ; } } ) ; } }