重置密码支持短信验证码

This commit is contained in:
2024-01-05 16:10:33 +08:00
parent 5fde0d6587
commit c07caba7e5

View File

@@ -12,10 +12,22 @@ import cn.lihongjie.coal.resetPwd.dto.UpdateResetPwdDto;
import cn.lihongjie.coal.resetPwd.entity.ResetPwdEntity;
import cn.lihongjie.coal.resetPwd.mapper.ResetPwdMapper;
import cn.lihongjie.coal.resetPwd.repository.ResetPwdRepository;
import cn.lihongjie.coal.sms.service.SmsService;
import cn.lihongjie.coal.smsTemplate.entity.SmsTemplateEntity;
import cn.lihongjie.coal.smsTemplate.service.SmsTemplateService;
import cn.lihongjie.coal.sysconfig.service.SysConfigService;
import cn.lihongjie.coal.user.entity.UserEntity;
import cn.lihongjie.coal.user.service.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@@ -24,10 +36,12 @@ import org.springframework.core.convert.ConversionService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
@@ -79,6 +93,13 @@ public class ResetPwdService extends BaseService<ResetPwdEntity, ResetPwdReposit
return page.map(this.mapper::toDto);
}
@Autowired ObjectMapper objectMapper;
@Autowired
private SmsTemplateService smsTemplateService;
@Autowired
private SmsService smsService;
@SneakyThrows
public String startReset(CreateResetPwdDto dto) {
@@ -111,6 +132,7 @@ public class ResetPwdService extends BaseService<ResetPwdEntity, ResetPwdReposit
throw new RuntimeException("手机号不正确");
}
String organizationId = user.get().getOrganizationId();
ResetPwdEntity resetPwdEntity = new ResetPwdEntity();
resetPwdEntity.setUser(user.get());
resetPwdEntity.setPhone(dto.getPhone());
@@ -131,6 +153,65 @@ public class ResetPwdService extends BaseService<ResetPwdEntity, ResetPwdReposit
this.save(resetPwdEntity);
log.info("重置密码会话创建成功: {}", resetPwdEntity);
if (!StringUtils.isEmpty(dto.getPhone())) {
Long orgTemplateCount =
smsTemplateService.count(
new Specification<SmsTemplateEntity>() {
@Override
public Predicate toPredicate(
Root<SmsTemplateEntity> root,
CriteriaQuery<?> query,
CriteriaBuilder criteriaBuilder) {
return criteriaBuilder.and(
criteriaBuilder.equal(root.get("code"), "login"),
criteriaBuilder.equal(
root.get("organizationId"), organizationId));
}
});
ObjectNode objectNode = objectMapper.createObjectNode();
objectNode.put("code", resetPwdEntity.getCode());
if (orgTemplateCount > 0) {
smsService.sendSmsMessage(
user.get().getPhone(),
objectMapper.writeValueAsString(objectNode),
"login",
organizationId);
} else {
List<SmsTemplateEntity> all =
smsTemplateService.findAll(
new Specification<SmsTemplateEntity>() {
@Override
public Predicate toPredicate(
Root<SmsTemplateEntity> root,
CriteriaQuery<?> query,
CriteriaBuilder criteriaBuilder) {
return criteriaBuilder.and(
criteriaBuilder.equal(
root.get("code"), "login_global"));
}
});
if (all.isEmpty()) {
throw new BizException("没有找到对应的短信模板 login_global");
}
smsService.sendSmsMessage(
user.get().getPhone(),
objectMapper.writeValueAsString(objectNode),
all.get(0),
organizationId);
}
}
return resetPwdEntity.getId();
}