mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-24 20:57:21 +08:00
优化:基于 ESLint 和阿里编码规约插件对部分代码规范进行优化
This commit is contained in:
@@ -70,7 +70,7 @@ public class IpUtils {
|
||||
* @return 归属地信息
|
||||
*/
|
||||
public static String getHttpCityInfo(String ip) {
|
||||
if (isInnerIP(ip)) {
|
||||
if (isInnerIp(ip)) {
|
||||
return "内网IP";
|
||||
}
|
||||
String api = String.format(IP_URL, ip);
|
||||
@@ -86,7 +86,7 @@ public class IpUtils {
|
||||
* @return 归属地信息
|
||||
*/
|
||||
public static String getLocalCityInfo(String ip) {
|
||||
if (isInnerIP(ip)) {
|
||||
if (isInnerIp(ip)) {
|
||||
return "内网IP";
|
||||
}
|
||||
Ip2regionSearcher ip2regionSearcher = SpringUtil.getBean(Ip2regionSearcher.class);
|
||||
@@ -104,7 +104,7 @@ public class IpUtils {
|
||||
* IP 地址
|
||||
* @return 是否为内网 IP
|
||||
*/
|
||||
public static boolean isInnerIP(String ip) {
|
||||
public static boolean isInnerIp(String ip) {
|
||||
ip = "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : HtmlUtil.cleanHtmlTag(ip);
|
||||
return NetUtil.isInnerIP(ip);
|
||||
}
|
||||
|
@@ -19,6 +19,7 @@ package top.charles7c.cnadmin.common.util;
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
@@ -33,10 +34,10 @@ import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.consts.CharConstants;
|
||||
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
|
||||
|
||||
/**
|
||||
@@ -186,7 +187,7 @@ public class MailUtils {
|
||||
*/
|
||||
public static void send(Collection<String> tos, Collection<String> ccs, Collection<String> bccs, String subject,
|
||||
String content, boolean isHtml, File... files) throws MessagingException {
|
||||
CheckUtils.throwIf(() -> CollUtil.isEmpty(tos), "请至少指定一名收件人");
|
||||
CheckUtils.throwIfEmpty(tos, "请至少指定一名收件人");
|
||||
MimeMessage mimeMessage = MAIL_SENDER.createMimeMessage();
|
||||
MimeMessageHelper messageHelper =
|
||||
new MimeMessageHelper(mimeMessage, true, StandardCharsets.UTF_8.displayName());
|
||||
@@ -228,14 +229,14 @@ public class MailUtils {
|
||||
*/
|
||||
private static List<String> splitAddress(String addresses) {
|
||||
if (StrUtil.isBlank(addresses)) {
|
||||
return null;
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<String> result;
|
||||
if (StrUtil.contains(addresses, CharUtil.COMMA)) {
|
||||
result = StrUtil.splitTrim(addresses, CharUtil.COMMA);
|
||||
} else if (StrUtil.contains(addresses, ';')) {
|
||||
result = StrUtil.splitTrim(addresses, ';');
|
||||
if (StrUtil.contains(addresses, CharConstants.COMMA)) {
|
||||
result = StrUtil.splitTrim(addresses, CharConstants.COMMA);
|
||||
} else if (StrUtil.contains(addresses, CharConstants.SEMICOLON)) {
|
||||
result = StrUtil.splitTrim(addresses, CharConstants.SEMICOLON);
|
||||
} else {
|
||||
result = CollUtil.newArrayList(addresses);
|
||||
}
|
||||
|
@@ -205,7 +205,12 @@ public class RedisUtils {
|
||||
return String.join(":", subKeys);
|
||||
}
|
||||
|
||||
public static NameMapper getNameMapper() {
|
||||
/**
|
||||
* 根据集群或单机配置,获取名称映射器
|
||||
*
|
||||
* @return 名称映射器
|
||||
*/
|
||||
private static NameMapper getNameMapper() {
|
||||
Config config = REDISSON_CLIENT.getConfig();
|
||||
if (config.isClusterConfig()) {
|
||||
return config.useClusterServers().getNameMapper();
|
||||
|
@@ -35,6 +35,54 @@ public class CheckUtils extends Validator {
|
||||
|
||||
private static final Class<ServiceException> EXCEPTION_TYPE = ServiceException.class;
|
||||
|
||||
/**
|
||||
* 如果为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void throwIfNull(Object obj, String message) {
|
||||
throwIfNull(obj, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果不为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void throwIfNotNull(Object obj, String message) {
|
||||
throwIfNotNull(obj, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void throwIfEmpty(Object obj, String message) {
|
||||
throwIfEmpty(obj, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果不为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void throwIfNotEmpty(Object obj, String message) {
|
||||
throwIfNotEmpty(obj, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果为空,抛出异常
|
||||
*
|
||||
@@ -115,30 +163,6 @@ public class CheckUtils extends Validator {
|
||||
throwIfNotEqualIgnoreCase(str1, str2, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void throwIfNull(Object obj, String message) {
|
||||
throwIfNull(obj, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果不为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void throwIfNotNull(Object obj, String message) {
|
||||
throwIfNotNull(obj, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果条件成立,抛出异常
|
||||
*
|
||||
|
@@ -35,6 +35,54 @@ public class ValidationUtils extends Validator {
|
||||
|
||||
private static final Class<BadRequestException> EXCEPTION_TYPE = BadRequestException.class;
|
||||
|
||||
/**
|
||||
* 如果为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void throwIfNull(Object obj, String message) {
|
||||
throwIfNull(obj, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果不为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void throwIfNotNull(Object obj, String message) {
|
||||
throwIfNotNull(obj, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void throwIfEmpty(Object obj, String message) {
|
||||
throwIfEmpty(obj, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果不为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void throwIfNotEmpty(Object obj, String message) {
|
||||
throwIfNotEmpty(obj, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果为空,抛出异常
|
||||
*
|
||||
@@ -115,30 +163,6 @@ public class ValidationUtils extends Validator {
|
||||
throwIfNotEqualIgnoreCase(str1, str2, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void throwIfNull(Object obj, String message) {
|
||||
throwIfNull(obj, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果不为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
*/
|
||||
public static void throwIfNotNull(Object obj, String message) {
|
||||
throwIfNotNull(obj, message, EXCEPTION_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果条件成立,抛出异常
|
||||
*
|
||||
|
@@ -34,6 +34,62 @@ import cn.hutool.core.util.StrUtil;
|
||||
@NoArgsConstructor(access = AccessLevel.PROTECTED)
|
||||
public class Validator {
|
||||
|
||||
/**
|
||||
* 如果为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
* @param exceptionType
|
||||
* 异常类型
|
||||
*/
|
||||
protected static void throwIfNull(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
|
||||
throwIf(() -> obj == null, message, exceptionType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果不为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
* @param exceptionType
|
||||
* 异常类型
|
||||
*/
|
||||
protected static void throwIfNotNull(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
|
||||
throwIf(() -> obj != null, message, exceptionType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
* @param exceptionType
|
||||
* 异常类型
|
||||
*/
|
||||
protected static void throwIfEmpty(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
|
||||
throwIf(() -> ObjectUtil.isEmpty(obj), message, exceptionType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果不为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
* @param exceptionType
|
||||
* 异常类型
|
||||
*/
|
||||
protected static void throwIfNotEmpty(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
|
||||
throwIf(() -> ObjectUtil.isNotEmpty(obj), message, exceptionType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果为空,抛出异常
|
||||
*
|
||||
@@ -132,34 +188,6 @@ public class Validator {
|
||||
throwIf(() -> !StrUtil.equalsIgnoreCase(str1, str2), message, exceptionType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
* @param exceptionType
|
||||
* 异常类型
|
||||
*/
|
||||
protected static void throwIfNull(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
|
||||
throwIf(() -> obj == null, message, exceptionType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果不为空,抛出异常
|
||||
*
|
||||
* @param obj
|
||||
* 被检测的对象
|
||||
* @param message
|
||||
* 错误信息
|
||||
* @param exceptionType
|
||||
* 异常类型
|
||||
*/
|
||||
protected static void throwIfNotNull(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
|
||||
throwIf(() -> obj != null, message, exceptionType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果条件成立,抛出异常
|
||||
*
|
||||
|
Reference in New Issue
Block a user