增加直接发短信的功能和完善腾讯发短信功能
This commit is contained in:
parent
82b87ae6e4
commit
fef1dfc259
@ -5,9 +5,11 @@ 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 ink.wgink.exceptions.ParamsException;
|
||||
import ink.wgink.exceptions.base.SystemException;
|
||||
import ink.wgink.module.sms.factory.sms.ISmsSend;
|
||||
import ink.wgink.properties.sms.SmsTencentProperties;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -59,6 +61,52 @@ public class TencentSmsSendImpl implements ISmsSend {
|
||||
|
||||
@Override
|
||||
public void content(String phone, String content) {
|
||||
throw new SystemException("短信发送失败,腾讯短信未实现该功能");
|
||||
String[] contentArray = content.split(":");
|
||||
String templateId;
|
||||
String params = "";
|
||||
if (contentArray.length == 2) {
|
||||
templateId = contentArray[0];
|
||||
params = contentArray[1];
|
||||
} else {
|
||||
templateId = contentArray[0];
|
||||
}
|
||||
if (StringUtils.isBlank(templateId)) {
|
||||
throw new ParamsException("内容格式异常:templateId:param1,param2");
|
||||
}
|
||||
String[] paramArray = StringUtils.isBlank(params) ? new String[0] : params.split(",");
|
||||
content(templateId, phone, paramArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短息
|
||||
*
|
||||
* @param templateId
|
||||
* @param phone
|
||||
* @param templateParams
|
||||
*/
|
||||
private void content(String templateId, String phone, String... templateParams) {
|
||||
try {
|
||||
Credential credential = new Credential(smsTencentProperties.getSecretId(), smsTencentProperties.getSecretKey());
|
||||
SmsClient client = new SmsClient(credential, "ap-guangzhou");
|
||||
SendSmsRequest sendSmsRequest = new SendSmsRequest();
|
||||
// 设置短信应用ID
|
||||
sendSmsRequest.setSmsSdkAppId(smsTencentProperties.getSdkAppId());
|
||||
// 设置签名
|
||||
sendSmsRequest.setSignName(smsTencentProperties.getSignName());
|
||||
// 设置短信模板ID
|
||||
sendSmsRequest.setTemplateId(templateId);
|
||||
// 设置模板参数
|
||||
String[] templateParamSet = templateParams;
|
||||
sendSmsRequest.setTemplateParamSet(templateParamSet);
|
||||
// 设置接受猴急
|
||||
String[] phoneNumberSet = {"+86" + phone};
|
||||
sendSmsRequest.setPhoneNumberSet(phoneNumberSet);
|
||||
SendSmsResponse response = client.SendSms(sendSmsRequest);
|
||||
// 获取返回结果
|
||||
String responseJsonString = SendSmsResponse.toJsonString(response);
|
||||
LOG.debug("Tencent sms response: {}", responseJsonString);
|
||||
} catch (TencentCloudSDKException e) {
|
||||
throw new SystemException(String.format("ErrorCode: %s, Message: %s", e.getErrorCode(), e.getMessage()), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -97,6 +97,14 @@ public interface ISmsService extends ISmsBaseService {
|
||||
*/
|
||||
void sendContentByUserIdAndPhoneAndUserName(String userId, String phone, String userName, String content);
|
||||
|
||||
/**
|
||||
* 直接发送短信,不查询用户
|
||||
*
|
||||
* @param phone
|
||||
* @param content
|
||||
*/
|
||||
void sendContentDirectlyByPhone(String phone, String content);
|
||||
|
||||
/**
|
||||
* 发送验证码
|
||||
*
|
||||
@ -118,6 +126,14 @@ public interface ISmsService extends ISmsBaseService {
|
||||
*/
|
||||
void sendByPhones(SmsPhoneSendVO smsPhoneSendVO);
|
||||
|
||||
/**
|
||||
* 直接发送短信,不查询用户
|
||||
* 如果使用的是 腾讯短信 content格式:templateId:param1,param2,当没有参数时,templateId:即可,但content不能为空
|
||||
*
|
||||
* @param smsPhoneSendVO
|
||||
*/
|
||||
void sendDirectlyByPhones(SmsPhoneSendVO smsPhoneSendVO);
|
||||
|
||||
/**
|
||||
* 发送短信列表
|
||||
*
|
||||
|
@ -131,6 +131,27 @@ public class SmsServiceImpl extends DefaultBaseService implements ISmsService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendContentDirectlyByPhone(String phone, String content) {
|
||||
SmsVO smsVO = new SmsVO();
|
||||
smsVO.setUserId(phone);
|
||||
smsVO.setUserName(phone);
|
||||
smsVO.setPhone(phone);
|
||||
smsVO.setContent(content);
|
||||
try {
|
||||
LOG.info(">>>>> 向手机号:{},发送内容:{}", phone, content);
|
||||
SmsSendFactory.getSendSms(smsProperties, smsCustomSend).content(phone, content);
|
||||
smsVO.setSendStatus(1);
|
||||
} catch (Exception e) {
|
||||
String errorMessage = e.getMessage();
|
||||
smsVO.setSendStatus(0);
|
||||
smsVO.setErrorMessage(errorMessage);
|
||||
throw new SystemException("短信发送失败");
|
||||
} finally {
|
||||
save(smsVO);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendVerifyCode(String phone) {
|
||||
VerifyCodeManager verifyCodeManager = VerifyCodeManager.getInstance();
|
||||
@ -183,6 +204,26 @@ public class SmsServiceImpl extends DefaultBaseService implements ISmsService {
|
||||
}, 3, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendDirectlyByPhones(SmsPhoneSendVO smsPhoneSendVO) {
|
||||
List<String> phones = new ArrayList<>(smsPhoneSendVO.getPhones());
|
||||
for (int i = 0; i < phones.size(); i++) {
|
||||
if (!RegexUtil.isPhone(phones.get(i))) {
|
||||
phones.remove(i);
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (phones.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
scheduledExecutorService.schedule(() -> {
|
||||
phones.forEach(phone -> {
|
||||
sendContentDirectlyByPhone(phone, smsPhoneSendVO.getContent());
|
||||
});
|
||||
}, 3, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
* 如果用户设置手机号,并且手机号格式正确,用手机号发送,如果格式错误,判断用户名
|
||||
|
Loading…
Reference in New Issue
Block a user