Search in sources :

Example 1 with SmsTemplate

use of com.ruoyi.common.sms.core.SmsTemplate 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 SmsTemplate

use of com.ruoyi.common.sms.core.SmsTemplate 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

SmsTemplate (com.ruoyi.common.sms.core.SmsTemplate)2 SmsResult (com.ruoyi.common.sms.entity.SmsResult)2 ApiOperation (io.swagger.annotations.ApiOperation)1 HashMap (java.util.HashMap)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1