Search in sources :

Example 1 with SmsResult

use of com.ruoyi.common.sms.entity.SmsResult in project RuoYi-Cloud-Plus by JavaLionLi.

the class SysSmsController method smsCaptcha.

/**
 * 短信验证码
 */
@ApiOperation("短信验证码")
@GetMapping("/code")
public R<Void> smsCaptcha(@ApiParam("用户手机号") @NotBlank(message = "{user.phonenumber.not.blank}") String phonenumber) {
    if (smsProperties.getEnabled()) {
        R.fail("当前系统没有开启短信功能!");
    }
    String key = Constants.CAPTCHA_CODE_KEY + phonenumber;
    String code = RandomUtil.randomNumbers(4);
    RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
    // 验证码模板id 自行处理 (查数据库或写死均可)
    String templateId = "";
    Map<String, String> map = new HashMap<>(1);
    map.put("code", code);
    SmsTemplate smsTemplate = SpringUtils.getBean(SmsTemplate.class);
    SmsResult result = smsTemplate.send(phonenumber, templateId, map);
    if (!result.getIsSuccess()) {
        log.error("验证码短信发送异常 => {}", result);
        return R.fail(result.getMessage());
    }
    return R.ok();
}
Also used : HashMap(java.util.HashMap) SmsResult(com.ruoyi.common.sms.entity.SmsResult) SmsTemplate(com.ruoyi.common.sms.core.SmsTemplate) GetMapping(org.springframework.web.bind.annotation.GetMapping) ApiOperation(io.swagger.annotations.ApiOperation)

Example 2 with SmsResult

use of com.ruoyi.common.sms.entity.SmsResult 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)

Example 3 with SmsResult

use of com.ruoyi.common.sms.entity.SmsResult in project RuoYi-Cloud-Plus by JavaLionLi.

the class RemoteSmsServiceImpl method send.

/**
 * 发送短信
 *
 * @param phones     电话号(多个逗号分割)
 * @param templateId 模板id
 * @param param      模板对应参数
 */
public SysSms send(String phones, String templateId, Map<String, String> param) throws ServiceException {
    if (smsProperties.getEnabled()) {
        R.fail("当前系统没有开启短信功能!");
    }
    SmsTemplate smsTemplate = SpringUtils.getBean(SmsTemplate.class);
    SmsResult smsResult = smsTemplate.send(phones, templateId, param);
    return BeanUtil.toBean(smsResult, SysSms.class);
}
Also used : SmsResult(com.ruoyi.common.sms.entity.SmsResult) SmsTemplate(com.ruoyi.common.sms.core.SmsTemplate)

Aggregations

SmsResult (com.ruoyi.common.sms.entity.SmsResult)3 SmsTemplate (com.ruoyi.common.sms.core.SmsTemplate)2 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 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 ApiOperation (io.swagger.annotations.ApiOperation)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1