优化:使用枚举存储性别、状态等信息(采用 MyBatis Plus#通用枚举扩展),常量类则专注于存储全局变量,例如:缓存键、默认值等

This commit is contained in:
Charles7c 2023-01-02 00:19:56 +08:00
parent 1d21019813
commit 21f5aceccf
9 changed files with 125 additions and 26 deletions

View File

@ -172,6 +172,7 @@ continew-admin # 全局通用项目配置及依赖版本管理
│ │ ├─ threadpool # 线程池配置 │ │ ├─ threadpool # 线程池配置
│ │ └─ properties # 公共配置属性 │ │ └─ properties # 公共配置属性
│ ├─ consts # 公共常量 │ ├─ consts # 公共常量
│ ├─ enums # 公共枚举
│ ├─ exception # 公共异常 │ ├─ exception # 公共异常
│ ├─ handler # 公共处理器 │ ├─ handler # 公共处理器
│ ├─ model # 公共模型 │ ├─ model # 公共模型

View File

@ -16,6 +16,7 @@
package top.charles7c.cnadmin.common.config.jackson; package top.charles7c.cnadmin.common.config.jackson;
import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.time.LocalDate; import java.time.LocalDate;
@ -30,6 +31,10 @@ import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilde
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import com.baomidou.mybatisplus.annotation.IEnum;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
@ -59,6 +64,15 @@ public class JacksonConfiguration {
String timeFormatPattern = "HH:mm:ss"; String timeFormatPattern = "HH:mm:ss";
return builder -> { return builder -> {
// 针对通用枚举 IEnum 的转换
builder.serializerByType(IEnum.class, new JsonSerializer<IEnum<Integer>>() {
@Override
public void serialize(IEnum<Integer> value, JsonGenerator gen, SerializerProvider serializers)
throws IOException {
gen.writeNumber(value.getValue());
}
});
// 针对 LongBigIntegerBigDecimal 的转换 // 针对 LongBigIntegerBigDecimal 的转换
JavaTimeModule javaTimeModule = new JavaTimeModule(); JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(Long.class, BigNumberSerializer.SERIALIZER_INSTANCE); javaTimeModule.addSerializer(Long.class, BigNumberSerializer.SERIALIZER_INSTANCE);

View File

@ -16,22 +16,21 @@
package top.charles7c.cnadmin.common.consts; package top.charles7c.cnadmin.common.consts;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
/** /**
* 公共常量 * 缓存键常量
* *
* @author Charles7c * @author Charles7c
* @since 2022/12/22 19:30 * @since 2022/12/22 19:30
*/ */
public interface CommonConstants { @NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CacheConstants {
/** /**
* 状态-启用 * 登录用户缓存键
*/ */
Integer STATUS_ENABLE = 1; public static final String LOGIN_USER_CACHE_KEY = "LOGIN_USER";
/**
* 状态-禁用
*/
Integer STATUS_DISABLE = 2;
} }

View File

@ -0,0 +1,42 @@
/*
* 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.enums;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.annotation.IEnum;
/**
* 启用/禁用状态枚举
*
* @author Charles7c
* @since 2022/12/29 22:38
*/
@Getter
@RequiredArgsConstructor
public enum DisEnableStatusEnum implements IEnum<Integer> {
/** 启用 */
ENABLE(1, "启用"),
/** 禁用 */
DISABLE(2, "禁用"),;
private final Integer value;
private final String description;
}

View File

@ -0,0 +1,45 @@
/*
* 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.enums;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.annotation.IEnum;
/**
* 性别枚举
*
* @author Charles7c
* @since 2022/12/29 21:59
*/
@Getter
@RequiredArgsConstructor
public enum GenderEnum implements IEnum<Integer> {
/** 未知 */
UNKNOWN(0, "未知"),
/** 男 */
MALE(1, ""),
/** 女 */
FEMALE(2, ""),;
private final Integer value;
private final String description;
}

View File

@ -21,6 +21,8 @@ import java.time.LocalDateTime;
import lombok.Data; import lombok.Data;
import top.charles7c.cnadmin.common.enums.GenderEnum;
/** /**
* 登录用户信息 * 登录用户信息
* *
@ -50,7 +52,7 @@ public class LoginUser implements Serializable {
/** /**
* 性别0未知 1男 2女 * 性别0未知 1男 2女
*/ */
private Integer gender; private GenderEnum gender;
/** /**
* 手机号码 * 手机号码
@ -72,11 +74,6 @@ public class LoginUser implements Serializable {
*/ */
private String notes; private String notes;
/**
* 状态1启用 2禁用
*/
private Integer status;
/** /**
* 最后一次修改密码的时间 * 最后一次修改密码的时间
*/ */

View File

@ -22,6 +22,7 @@ import lombok.NoArgsConstructor;
import cn.dev33.satoken.context.SaHolder; import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.stp.StpUtil;
import top.charles7c.cnadmin.common.consts.CacheConstants;
import top.charles7c.cnadmin.common.model.dto.LoginUser; import top.charles7c.cnadmin.common.model.dto.LoginUser;
import top.charles7c.cnadmin.common.util.ExceptionUtils; import top.charles7c.cnadmin.common.util.ExceptionUtils;
@ -34,8 +35,6 @@ import top.charles7c.cnadmin.common.util.ExceptionUtils;
@NoArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor(access = AccessLevel.PRIVATE)
public class LoginHelper { public class LoginHelper {
private static final String LOGIN_USER_KEY = "LOGIN_USER";
/** /**
* 用户登录并缓存用户信息 * 用户登录并缓存用户信息
* *
@ -43,9 +42,9 @@ public class LoginHelper {
* 登录用户信息 * 登录用户信息
*/ */
public static void login(LoginUser loginUser) { public static void login(LoginUser loginUser) {
SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser); SaHolder.getStorage().set(CacheConstants.LOGIN_USER_CACHE_KEY, loginUser);
StpUtil.login(loginUser.getUserId()); StpUtil.login(loginUser.getUserId());
StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser); StpUtil.getTokenSession().set(CacheConstants.LOGIN_USER_CACHE_KEY, loginUser);
} }
/** /**
@ -54,13 +53,13 @@ public class LoginHelper {
* @return / * @return /
*/ */
public static LoginUser getLoginUser() { public static LoginUser getLoginUser() {
LoginUser loginUser = (LoginUser)SaHolder.getStorage().get(LOGIN_USER_KEY); LoginUser loginUser = (LoginUser)SaHolder.getStorage().get(CacheConstants.LOGIN_USER_CACHE_KEY);
if (loginUser != null) { if (loginUser != null) {
return loginUser; return loginUser;
} }
try { try {
loginUser = (LoginUser)StpUtil.getTokenSession().get(LOGIN_USER_KEY); loginUser = (LoginUser)StpUtil.getTokenSession().get(CacheConstants.LOGIN_USER_CACHE_KEY);
SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser); SaHolder.getStorage().set(CacheConstants.LOGIN_USER_CACHE_KEY, loginUser);
} catch (Exception ignored) { } catch (Exception ignored) {
} }
return loginUser; return loginUser;

View File

@ -24,7 +24,7 @@ import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import top.charles7c.cnadmin.auth.service.LoginService; import top.charles7c.cnadmin.auth.service.LoginService;
import top.charles7c.cnadmin.common.consts.CommonConstants; import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
import top.charles7c.cnadmin.common.model.dto.LoginUser; import top.charles7c.cnadmin.common.model.dto.LoginUser;
import top.charles7c.cnadmin.common.util.CheckUtils; import top.charles7c.cnadmin.common.util.CheckUtils;
import top.charles7c.cnadmin.common.util.SecureUtils; import top.charles7c.cnadmin.common.util.SecureUtils;
@ -53,7 +53,7 @@ public class LoginServiceImpl implements LoginService {
CheckUtils.exIfNull(sysUser, "用户名或密码错误"); CheckUtils.exIfNull(sysUser, "用户名或密码错误");
Long userId = sysUser.getUserId(); Long userId = sysUser.getUserId();
CheckUtils.exIfNotEqual(sysUser.getPassword(), SecureUtils.md5Salt(password, userId.toString()), "用户名或密码错误"); CheckUtils.exIfNotEqual(sysUser.getPassword(), SecureUtils.md5Salt(password, userId.toString()), "用户名或密码错误");
CheckUtils.exIfEqual(CommonConstants.STATUS_DISABLE, sysUser.getStatus(), "此账号已被禁用,如有疑问,请联系管理员"); CheckUtils.exIfEqual(DisEnableStatusEnum.DISABLE, sysUser.getStatus(), "此账号已被禁用,如有疑问,请联系管理员");
// 登录 // 登录
LoginUser loginUser = BeanUtil.copyProperties(sysUser, LoginUser.class); LoginUser loginUser = BeanUtil.copyProperties(sysUser, LoginUser.class);

View File

@ -23,6 +23,8 @@ import lombok.Data;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
import top.charles7c.cnadmin.common.enums.GenderEnum;
import top.charles7c.cnadmin.common.model.entity.BaseEntity; import top.charles7c.cnadmin.common.model.entity.BaseEntity;
/** /**
@ -61,7 +63,7 @@ public class SysUser extends BaseEntity {
/** /**
* 性别0未知 1男 2女 * 性别0未知 1男 2女
*/ */
private Integer gender; private GenderEnum gender;
/** /**
* 手机号码 * 手机号码
@ -86,7 +88,7 @@ public class SysUser extends BaseEntity {
/** /**
* 状态1启用 2禁用 * 状态1启用 2禁用
*/ */
private Integer status; private DisEnableStatusEnum status;
/** /**
* 最后一次修改密码的时间 * 最后一次修改密码的时间