优化:基于 ESLint 和阿里编码规约插件对部分代码规范进行优化

This commit is contained in:
2023-02-10 20:45:15 +08:00
parent 5251a484f2
commit ebc7c2b3b0
29 changed files with 417 additions and 224 deletions

View File

@@ -34,6 +34,8 @@ public interface BaseEnum<V extends Serializable, D extends Serializable> extend
/**
* 枚举描述
*
* @return 枚举描述
*/
D getDescription();
}

View File

@@ -81,6 +81,13 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C ext
return BeanUtil.copyProperties(entity, detailVoClass);
}
/**
* 新增
*
* @param request
* 创建信息
* @return 自增 ID
*/
@Override
@Transactional(rollbackFor = Exception.class)
public abstract Long create(C request);

View File

@@ -16,9 +16,6 @@
package top.charles7c.cnadmin.common.config;
import java.util.HashMap;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import io.swagger.v3.oas.models.OpenAPI;
@@ -29,6 +26,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import top.charles7c.cnadmin.common.config.properties.ContiNewAdminProperties;
@@ -50,7 +48,7 @@ public class SwaggerConfiguration {
* 接口文档配置
*/
@Bean
public OpenAPI openAPI() {
public OpenAPI openApi() {
return new OpenAPI().info(
new Info().title(continewAdminProperties.getName() + " 接口文档").version(continewAdminProperties.getVersion())
.description(continewAdminProperties.getDescription()).termsOfService(continewAdminProperties.getUrl())
@@ -66,11 +64,8 @@ public class SwaggerConfiguration {
public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
return openApi -> {
if (openApi.getTags() != null) {
openApi.getTags().forEach(tag -> {
Map<String, Object> map = new HashMap<>();
map.put("x-order", RandomUtil.randomInt(0, 100));
tag.setExtensions(map);
});
openApi.getTags()
.forEach(tag -> tag.setExtensions(MapUtil.of("x-order", RandomUtil.randomInt(0, 100))));
}
};
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.consts;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import cn.hutool.core.text.CharPool;
/**
* 字符常量
*
* @author Charles7c
* @since 2023/2/10 20:14
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CharConstants implements CharPool {
/**
* 分号
*/
public static final String SEMICOLON = ";";
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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();

View File

@@ -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);
}
/**
* 如果条件成立,抛出异常
*

View File

@@ -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);
}
/**
* 如果条件成立,抛出异常
*

View File

@@ -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);
}
/**
* 如果条件成立,抛出异常
*