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

76 lines
3.0 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
public void sendMsgByConfig(List<String> phoneNums, int pushFunctionType, List<String> templateParams, String text) {
try {
if (pushFunctionType == 1 && config.getEnableDustSend() == 1) {
//报警类型,报警名称,设备名称,报警值,阈值,超标时间,超标量
doSendMsg(phoneNums, config.getDustTemplateId(), templateParams.toArray(new String[]{}));
} else if (pushFunctionType == 2 && config.getEnableAiSend() == 1) {
doSendMsg(phoneNums, config.getAiTemplateId(), templateParams.toArray(new String[]{}));
} else if (pushFunctionType == 3 && config.getEnableLoginSend() == 1) {
doSendMsg(phoneNums, config.getLoginTemplateId(), templateParams.toArray(new String[]{}));
}
} 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));
}
}