Skip to content

Commit

Permalink
refactor: 优化字符串模板方法 API 使用
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Feb 18, 2024
1 parent 370f9cf commit 0f39384
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private boolean isNameExists(String name, Long parentId, Long id) {
*/
private String getAncestors(Long parentId) {
DeptDO parentDept = this.getByParentId(parentId);
return String.format("%s,%s", parentDept.getAncestors(), parentId);
return "%s,%s".formatted(parentDept.getAncestors(), parentId);
}

/**
Expand All @@ -155,7 +155,7 @@ private DeptDO getByParentId(Long parentId) {
* @return 子部门列表
*/
private List<DeptDO> listChildren(Long id) {
return baseMapper.lambdaQuery().apply(String.format("find_in_set(%s, ancestors)", id)).list();
return baseMapper.lambdaQuery().apply("find_in_set(%s, ancestors)".formatted(id)).list();
}

/**
Expand All @@ -170,7 +170,7 @@ private Long countChildren(List<Long> ids) {
return 0L;
}
return ids.stream()
.mapToLong(id -> baseMapper.lambdaQuery().apply(String.format("find_in_set(%s, ancestors)", id)).count())
.mapToLong(id -> baseMapper.lambdaQuery().apply("find_in_set(%s, ancestors)".formatted(id)).count())
.sum();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public static void main(String[] args) {
@SaIgnore
@GetMapping("/")
public String index() {
return String.format("%s service started successfully.", projectProperties.getName());
return "%s service started successfully.".formatted(projectProperties.getName());
}

@Override
public void run(ApplicationArguments args) throws Exception {
String hostAddress = InetAddress.getLocalHost().getHostAddress();
Integer port = serverProperties.getPort();
String contextPath = serverProperties.getServlet().getContextPath();
String baseUrl = URLUtil.normalize(String.format("%s:%s%s", hostAddress, port, contextPath));
String baseUrl = URLUtil.normalize("%s:%s%s".formatted(hostAddress, port, contextPath));
log.info("----------------------------------------------");
log.info("{} service started successfully.", projectProperties.getName());
log.info("API 地址:{}", baseUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private AuthRequest getAuthRequest(String source) {
try {
return authRequestFactory.get(source);
} catch (Exception e) {
throw new BadRequestException(String.format("暂不支持 [%s] 平台账号登录", source));
throw new BadRequestException("暂不支持 [%s] 平台账号登录".formatted(source));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ public R<Void> getMailCaptcha(@NotBlank(message = "邮箱不能为空") @Pattern
String content = TemplateUtils.render(captchaMail.getTemplatePath(), Dict.create()
.set("captcha", captcha)
.set("expiration", expirationInMinutes));
MailUtils.sendHtml(email, String.format("【%s】邮箱验证码", projectProperties.getName()), content);
MailUtils.sendHtml(email, "【%s】邮箱验证码".formatted(projectProperties.getName()), content);
// 保存验证码
String captchaKey = captchaKeyPrefix + email;
RedisUtils.set(captchaKey, captcha, Duration.ofMinutes(expirationInMinutes));
RedisUtils.set(limitCaptchaKey, captcha, Duration.ofSeconds(captchaMail.getLimitInSeconds()));
return R.ok(String.format("发送成功,验证码有效期 %s 分钟", expirationInMinutes));
return R.ok("发送成功,验证码有效期 %s 分钟".formatted(expirationInMinutes));
}

@Operation(summary = "获取短信验证码", description = "发送验证码到指定手机号")
Expand Down Expand Up @@ -173,6 +173,6 @@ public R<Void> getSmsCaptcha(@NotBlank(message = "手机号不能为空") @Patte
// 保存验证码
String captchaKey = captchaKeyPrefix + phone;
RedisUtils.set(captchaKey, captcha, Duration.ofMinutes(expirationInMinutes));
return R.ok(String.format("发送成功,验证码有效期 %s 分钟", expirationInMinutes));
return R.ok("发送成功,验证码有效期 %s 分钟".formatted(expirationInMinutes));
}
}

0 comments on commit 0f39384

Please sign in to comment.