mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-09 20:57:21 +08:00
优化:基于阿里巴巴 Java 开发手册(黄山版)优化常量及包命名
1.编程规约>常量定义>第4条: 【推荐】不要使用一个常量类维护所有常量,要按常量功能进行归类,分开维护。 说明:大而全的常量类,杂乱无章,使用查找功能才能定位到要修改的常量,不利于理解,也不利于维护。 正例:缓存相关常量放在类 CacheConsts 下;系统配置相关常量放在类 SystemConfigConsts 下。 2.编程规约>常量定义>第5条: 【推荐】常量的复用层次有五层:跨应用共享常量、应用内共享常量、子工程内共享常量、包内共享常 量、类内共享常量。 1)跨应用共享常量:放置在二方库中,通常是 client.jar 中的 constant 目录下。 2)应用内共享常量:放置在一方库中,通常是子模块中的 constant 目录下。 反例:易懂常量也要统一定义成应用内共享常量,两个程序员在两个类中分别定义了表示“是”的常量: 类 A 中:public static final String YES = "yes"; 类 B 中:public static final String YES = "y"; A.YES.equals(B.YES),预期是 true,但实际返回为 false,导致线上问题。 3)子工程内部共享常量:即在当前子工程的 constant 目录下。 4)包内共享常量:即在当前包下单独的 constant 目录下。 5)类内共享常量:直接在类内部 private static final 定义。
This commit is contained in:
@@ -14,19 +14,19 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.consts;
|
||||
package top.charles7c.cnadmin.common.constant;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 缓存键常量
|
||||
* 缓存相关常量
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2022/12/22 19:30
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class CacheConstants {
|
||||
public class CacheConsts {
|
||||
|
||||
/**
|
||||
* 登录用户缓存键
|
||||
@@ -42,5 +42,4 @@ public class CacheConstants {
|
||||
* 限流缓存键
|
||||
*/
|
||||
public static final String LIMIT_CACHE_KEY = "LIMIT";
|
||||
|
||||
}
|
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.consts;
|
||||
package top.charles7c.cnadmin.common.constant;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
@@ -22,13 +22,13 @@ 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 class CharConsts implements CharPool {
|
||||
|
||||
/**
|
||||
* 分号
|
@@ -14,23 +14,22 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.consts;
|
||||
package top.charles7c.cnadmin.common.constant;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 文件常量
|
||||
* 文件相关常量
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/1/2 21:19
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class FileConstants {
|
||||
public class FileConsts {
|
||||
|
||||
/**
|
||||
* 头像支持的图片类型
|
||||
*/
|
||||
public static final String[] AVATAR_SUPPORTED_IMG_TYPES = {"jpg", "png", "gif", "jpeg"};
|
||||
|
||||
}
|
@@ -14,17 +14,19 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.consts;
|
||||
package top.charles7c.cnadmin.common.constant;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 正则相关常量
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/1/10 20:06
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class RegExpConstants {
|
||||
public class RegExpConsts {
|
||||
|
||||
/**
|
||||
* 密码正则(必须包含字母和数字的组合,可以使用特殊字符,长度在6-32之间)
|
@@ -14,19 +14,19 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.common.consts;
|
||||
package top.charles7c.cnadmin.common.constant;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 系统常量
|
||||
* 系统相关常量
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/2/9 22:11
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class Constants {
|
||||
public class SysConsts {
|
||||
|
||||
/**
|
||||
* 超级管理员角色编码
|
@@ -30,7 +30,7 @@ import org.springframework.data.domain.Sort;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.consts.CharConstants;
|
||||
import top.charles7c.cnadmin.common.constant.CharConsts;
|
||||
|
||||
/**
|
||||
* 排序查询条件
|
||||
@@ -62,10 +62,10 @@ public class SortQuery implements Serializable {
|
||||
}
|
||||
|
||||
List<Sort.Order> orders = new ArrayList<>(sort.length);
|
||||
if (StrUtil.contains(sort[0], CharConstants.COMMA)) {
|
||||
if (StrUtil.contains(sort[0], CharConsts.COMMA)) {
|
||||
// e.g "sort=published,desc&sort=title,asc"
|
||||
for (String s : sort) {
|
||||
List<String> sortList = StrUtil.split(s, CharConstants.COMMA);
|
||||
List<String> sortList = StrUtil.split(s, CharConsts.COMMA);
|
||||
Sort.Order order =
|
||||
new Sort.Order(Sort.Direction.valueOf(sortList.get(1).toUpperCase()), sortList.get(0));
|
||||
orders.add(order);
|
||||
|
@@ -37,7 +37,7 @@ import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.consts.CharConstants;
|
||||
import top.charles7c.cnadmin.common.constant.CharConsts;
|
||||
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
|
||||
|
||||
/**
|
||||
@@ -233,10 +233,10 @@ public class MailUtils {
|
||||
}
|
||||
|
||||
List<String> result;
|
||||
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);
|
||||
if (StrUtil.contains(addresses, CharConsts.COMMA)) {
|
||||
result = StrUtil.splitTrim(addresses, CharConsts.COMMA);
|
||||
} else if (StrUtil.contains(addresses, CharConsts.SEMICOLON)) {
|
||||
result = StrUtil.splitTrim(addresses, CharConsts.SEMICOLON);
|
||||
} else {
|
||||
result = CollUtil.newArrayList(addresses);
|
||||
}
|
||||
|
@@ -27,7 +27,7 @@ import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.extra.servlet.ServletUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.consts.CacheConstants;
|
||||
import top.charles7c.cnadmin.common.constant.CacheConsts;
|
||||
import top.charles7c.cnadmin.common.model.dto.LogContext;
|
||||
import top.charles7c.cnadmin.common.model.dto.LoginUser;
|
||||
import top.charles7c.cnadmin.common.util.ExceptionUtils;
|
||||
@@ -66,8 +66,8 @@ public class LoginHelper {
|
||||
// 登录保存用户信息
|
||||
StpUtil.login(loginUser.getUserId());
|
||||
loginUser.setToken(StpUtil.getTokenValue());
|
||||
SaHolder.getStorage().set(CacheConstants.LOGIN_USER_CACHE_KEY, loginUser);
|
||||
StpUtil.getTokenSession().set(CacheConstants.LOGIN_USER_CACHE_KEY, loginUser);
|
||||
SaHolder.getStorage().set(CacheConsts.LOGIN_USER_CACHE_KEY, loginUser);
|
||||
StpUtil.getTokenSession().set(CacheConsts.LOGIN_USER_CACHE_KEY, loginUser);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,13 +76,13 @@ public class LoginHelper {
|
||||
* @return /
|
||||
*/
|
||||
public static LoginUser getLoginUser() {
|
||||
LoginUser loginUser = (LoginUser)SaHolder.getStorage().get(CacheConstants.LOGIN_USER_CACHE_KEY);
|
||||
LoginUser loginUser = (LoginUser)SaHolder.getStorage().get(CacheConsts.LOGIN_USER_CACHE_KEY);
|
||||
if (loginUser != null) {
|
||||
return loginUser;
|
||||
}
|
||||
try {
|
||||
loginUser = (LoginUser)StpUtil.getTokenSession().get(CacheConstants.LOGIN_USER_CACHE_KEY);
|
||||
SaHolder.getStorage().set(CacheConstants.LOGIN_USER_CACHE_KEY, loginUser);
|
||||
loginUser = (LoginUser)StpUtil.getTokenSession().get(CacheConsts.LOGIN_USER_CACHE_KEY);
|
||||
SaHolder.getStorage().set(CacheConsts.LOGIN_USER_CACHE_KEY, loginUser);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return loginUser;
|
||||
@@ -95,8 +95,8 @@ public class LoginHelper {
|
||||
* 登录用户信息
|
||||
*/
|
||||
public static void updateLoginUser(LoginUser loginUser) {
|
||||
SaHolder.getStorage().set(CacheConstants.LOGIN_USER_CACHE_KEY, loginUser);
|
||||
StpUtil.getTokenSession().set(CacheConstants.LOGIN_USER_CACHE_KEY, loginUser);
|
||||
SaHolder.getStorage().set(CacheConsts.LOGIN_USER_CACHE_KEY, loginUser);
|
||||
StpUtil.getTokenSession().set(CacheConsts.LOGIN_USER_CACHE_KEY, loginUser);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user