wisdomisite-java/src/main/java/com/zhgd/xmgl/call/TencentCloudMessageCall.java

69 lines
2.4 KiB
Java
Raw Normal View History

2025-07-29 18:17:51 +08:00
package com.zhgd.xmgl.call;
import com.gexin.fastjson.JSON;
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.zhgd.xmgl.call.api.MessageManufacturer;
import com.zhgd.xmgl.modules.project.entity.MessageConfigV2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;
/**
* 腾讯云短信
*/
@Slf4j
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class TencentCloudMessageCall implements MessageManufacturer {
private MessageConfigV2 config;
@Override
public MessageConfigV2 getConfig() {
return config;
}
@Override
public void setConfig(MessageConfigV2 config) {
this.config = config;
}
@Override
2025-09-03 16:47:58 +08:00
public void sendMsgByConfig(List<String> phoneNums, String templateId, List<String> templateParams, String text) {
2025-07-29 18:17:51 +08:00
try {
2025-09-03 16:47:58 +08:00
doSendMsg(phoneNums, templateId, templateParams.toArray(new String[]{}));
2025-07-29 18:17:51 +08:00
} catch (Exception e) {
log.error("按短信配置发送短信异常", e);
}
}
/**
* 发送短信
*
* @param phoneNums 手机号码
* @param templateId 短信模板id
* @param templateParamSet 短信模板参数
* @throws TencentCloudSDKException
*/
private void doSendMsg(List<String> phoneNums, String templateId, String[] templateParamSet) throws TencentCloudSDKException {
Credential credential = new Credential(config.getSecretId(), config.getSecretKey());
SmsClient smsClient = new SmsClient(credential, "ap-singapore");
SendSmsRequest req = new SendSmsRequest();
req.setPhoneNumberSet(phoneNums.stream().map(s -> "+86" + s).collect(Collectors.toList()).toArray(new String[]{}));
req.setSmsSdkAppId(config.getSmsSdkAppId());
req.setTemplateId(templateId);
req.setTemplateParamSet(templateParamSet);
SendSmsResponse response = smsClient.SendSms(req);
log.info("腾讯云发送短信结果:{}", JSON.toJSONString(response));
}
}