refactor: 优化字符串模板方法 API 使用

This commit is contained in:
Charles7c 2024-02-18 22:47:18 +08:00
parent 370f9cf796
commit 0f393845a1
4 changed files with 9 additions and 9 deletions

View File

@ -133,7 +133,7 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptRes
*/ */
private String getAncestors(Long parentId) { private String getAncestors(Long parentId) {
DeptDO parentDept = this.getByParentId(parentId); DeptDO parentDept = this.getByParentId(parentId);
return String.format("%s,%s", parentDept.getAncestors(), parentId); return "%s,%s".formatted(parentDept.getAncestors(), parentId);
} }
/** /**
@ -155,7 +155,7 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptRes
* @return 子部门列表 * @return 子部门列表
*/ */
private List<DeptDO> listChildren(Long id) { 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();
} }
/** /**
@ -170,7 +170,7 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptRes
return 0L; return 0L;
} }
return ids.stream() 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(); .sum();
} }

View File

@ -71,7 +71,7 @@ public class ContiNewAdminApplication implements ApplicationRunner {
@SaIgnore @SaIgnore
@GetMapping("/") @GetMapping("/")
public String index() { public String index() {
return String.format("%s service started successfully.", projectProperties.getName()); return "%s service started successfully.".formatted(projectProperties.getName());
} }
@Override @Override
@ -79,7 +79,7 @@ public class ContiNewAdminApplication implements ApplicationRunner {
String hostAddress = InetAddress.getLocalHost().getHostAddress(); String hostAddress = InetAddress.getLocalHost().getHostAddress();
Integer port = serverProperties.getPort(); Integer port = serverProperties.getPort();
String contextPath = serverProperties.getServlet().getContextPath(); 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("----------------------------------------------");
log.info("{} service started successfully.", projectProperties.getName()); log.info("{} service started successfully.", projectProperties.getName());
log.info("API 地址:{}", baseUrl); log.info("API 地址:{}", baseUrl);

View File

@ -87,7 +87,7 @@ public class SocialAuthController {
try { try {
return authRequestFactory.get(source); return authRequestFactory.get(source);
} catch (Exception e) { } catch (Exception e) {
throw new BadRequestException(String.format("暂不支持 [%s] 平台账号登录", source)); throw new BadRequestException("暂不支持 [%s] 平台账号登录".formatted(source));
} }
} }
} }

View File

@ -121,12 +121,12 @@ public class CaptchaController {
String content = TemplateUtils.render(captchaMail.getTemplatePath(), Dict.create() String content = TemplateUtils.render(captchaMail.getTemplatePath(), Dict.create()
.set("captcha", captcha) .set("captcha", captcha)
.set("expiration", expirationInMinutes)); .set("expiration", expirationInMinutes));
MailUtils.sendHtml(email, String.format("【%s】邮箱验证码", projectProperties.getName()), content); MailUtils.sendHtml(email, "【%s】邮箱验证码".formatted(projectProperties.getName()), content);
// 保存验证码 // 保存验证码
String captchaKey = captchaKeyPrefix + email; String captchaKey = captchaKeyPrefix + email;
RedisUtils.set(captchaKey, captcha, Duration.ofMinutes(expirationInMinutes)); RedisUtils.set(captchaKey, captcha, Duration.ofMinutes(expirationInMinutes));
RedisUtils.set(limitCaptchaKey, captcha, Duration.ofSeconds(captchaMail.getLimitInSeconds())); RedisUtils.set(limitCaptchaKey, captcha, Duration.ofSeconds(captchaMail.getLimitInSeconds()));
return R.ok(String.format("发送成功,验证码有效期 %s 分钟", expirationInMinutes)); return R.ok("发送成功,验证码有效期 %s 分钟".formatted(expirationInMinutes));
} }
@Operation(summary = "获取短信验证码", description = "发送验证码到指定手机号") @Operation(summary = "获取短信验证码", description = "发送验证码到指定手机号")
@ -173,6 +173,6 @@ public class CaptchaController {
// 保存验证码 // 保存验证码
String captchaKey = captchaKeyPrefix + phone; String captchaKey = captchaKeyPrefix + phone;
RedisUtils.set(captchaKey, captcha, Duration.ofMinutes(expirationInMinutes)); RedisUtils.set(captchaKey, captcha, Duration.ofMinutes(expirationInMinutes));
return R.ok(String.format("发送成功,验证码有效期 %s 分钟", expirationInMinutes)); return R.ok("发送成功,验证码有效期 %s 分钟".formatted(expirationInMinutes));
} }
} }