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();
}
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());
}
}
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);
}
Aggregations