Search in sources :

Example 1 with SmsException

use of com.ruoyi.common.sms.exception.SmsException in project RuoYi-Cloud-Plus by JavaLionLi.

the class TencentSmsTemplate method send.

public SmsResult send(String phones, String templateId, Map<String, String> param) {
    if (StringUtils.isBlank(phones)) {
        throw new SmsException("手机号不能为空");
    }
    if (StringUtils.isBlank(templateId)) {
        throw new SmsException("模板ID不能为空");
    }
    SendSmsRequest req = new SendSmsRequest();
    Set<String> set = Arrays.stream(phones.split(",")).map(p -> "+86" + p).collect(Collectors.toSet());
    req.setPhoneNumberSet(ArrayUtil.toArray(set, String.class));
    if (CollUtil.isNotEmpty(param)) {
        req.setTemplateParamSet(ArrayUtil.toArray(param.values(), String.class));
    }
    req.setTemplateID(templateId);
    req.setSign(properties.getSignName());
    req.setSmsSdkAppid(properties.getSdkAppId());
    try {
        SendSmsResponse resp = client.SendSms(req);
        SmsResult.SmsResultBuilder builder = SmsResult.builder().isSuccess(true).message("send success").response(resp);
        for (SendStatus sendStatus : resp.getSendStatusSet()) {
            if (!"Ok".equals(sendStatus.getCode())) {
                builder.isSuccess(false).message(sendStatus.getMessage());
                break;
            }
        }
        return builder.build();
    } catch (Exception e) {
        throw new SmsException(e.getMessage());
    }
}
Also used : SmsProperties(com.ruoyi.common.sms.config.properties.SmsProperties) Arrays(java.util.Arrays) SneakyThrows(lombok.SneakyThrows) Set(java.util.Set) SendStatus(com.tencentcloudapi.sms.v20190711.models.SendStatus) SendSmsRequest(com.tencentcloudapi.sms.v20190711.models.SendSmsRequest) StringUtils(com.ruoyi.common.core.utils.StringUtils) SmsResult(com.ruoyi.common.sms.entity.SmsResult) Collectors(java.util.stream.Collectors) SmsClient(com.tencentcloudapi.sms.v20190711.SmsClient) CollUtil(cn.hutool.core.collection.CollUtil) SmsException(com.ruoyi.common.sms.exception.SmsException) SendSmsResponse(com.tencentcloudapi.sms.v20190711.models.SendSmsResponse) Map(java.util.Map) ArrayUtil(cn.hutool.core.util.ArrayUtil) Credential(com.tencentcloudapi.common.Credential) ClientProfile(com.tencentcloudapi.common.profile.ClientProfile) HttpProfile(com.tencentcloudapi.common.profile.HttpProfile) SendSmsRequest(com.tencentcloudapi.sms.v20190711.models.SendSmsRequest) SendStatus(com.tencentcloudapi.sms.v20190711.models.SendStatus) SmsResult(com.ruoyi.common.sms.entity.SmsResult) SendSmsResponse(com.tencentcloudapi.sms.v20190711.models.SendSmsResponse) SmsException(com.ruoyi.common.sms.exception.SmsException) SmsException(com.ruoyi.common.sms.exception.SmsException)

Aggregations

CollUtil (cn.hutool.core.collection.CollUtil)1 ArrayUtil (cn.hutool.core.util.ArrayUtil)1 StringUtils (com.ruoyi.common.core.utils.StringUtils)1 SmsProperties (com.ruoyi.common.sms.config.properties.SmsProperties)1 SmsResult (com.ruoyi.common.sms.entity.SmsResult)1 SmsException (com.ruoyi.common.sms.exception.SmsException)1 Credential (com.tencentcloudapi.common.Credential)1 ClientProfile (com.tencentcloudapi.common.profile.ClientProfile)1 HttpProfile (com.tencentcloudapi.common.profile.HttpProfile)1 SmsClient (com.tencentcloudapi.sms.v20190711.SmsClient)1 SendSmsRequest (com.tencentcloudapi.sms.v20190711.models.SendSmsRequest)1 SendSmsResponse (com.tencentcloudapi.sms.v20190711.models.SendSmsResponse)1 SendStatus (com.tencentcloudapi.sms.v20190711.models.SendStatus)1 Arrays (java.util.Arrays)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 SneakyThrows (lombok.SneakyThrows)1