41 Commits

Author SHA1 Message Date
8460b9da29 修改部分 jsr305 注解的使用。 2023-04-16 00:08:04 +08:00
19fc97362c 更改 Exception Handler 版本。 2023-04-16 00:06:36 +08:00
d91f818c96 Merge remote-tracking branch 'origin/main' into main 2023-04-15 22:06:58 +08:00
0e0d6f1808 plusone-commons 做了调整。 2023-04-15 05:24:03 +08:00
06ffc8d858 避免实例化。 2023-04-04 00:32:25 +08:00
60b4f18e8c 更改 hutool 版本。 2023-04-04 00:27:53 +08:00
2b282039ad 更新 plusone-validator 和 plusone-exception-handler。 2023-04-04 00:24:31 +08:00
ea7a8ee6a0 Merge pull request '整合多次更改' (#1) from dev into main
Reviewed-on: http://zhouxy.xyz:3000/ZhouXY108/plusone-admin/pulls/1
2023-03-29 18:54:03 +08:00
67313938e1 提交代码。 2023-03-28 15:41:15 +08:00
27be582bfb 升级 Spring Boot 版本。 2023-03-28 15:40:49 +08:00
288ce9689f postgresql:42.3.8 依赖 checker-qual:3.5.0,将 guava 的版本更改为 30.1-jre,避免依赖冲突。 2023-03-21 15:52:38 +08:00
71edaa60a4 整理依赖。 2023-03-17 18:46:39 +08:00
39cd57e675 使用原生哈希算法。使用自己实现的随机盐生成方法。 2023-03-17 18:46:06 +08:00
7a44c43402 使用原生 UUID。 2023-03-17 18:44:51 +08:00
55395ed327 自己实现随机字符串的生成。 2023-03-17 18:44:33 +08:00
956da350ed 更改 plusone-exception-handler 版本。 2023-03-17 18:43:24 +08:00
78cdded667 int 数组的值总和可能大于 int 的最大值,使用 long 类型的变量存储其结果。 2023-03-17 18:42:43 +08:00
cc3c4be8de 升级 hutool。 2023-03-14 18:10:02 +08:00
1e31e1f327 使用 Integer 接收前端的参数,而不是枚举类。 2023-03-14 10:51:11 +08:00
993ba7fa7b 枚举类不允许继承。 2023-03-12 13:44:28 +08:00
8ae0faa04d 优化正则的使用。 2023-03-12 13:42:08 +08:00
920971c640 Merge branch 'dev' of http://zhouxy.xyz:3000/ZhouXY108/plusone-admin into dev 2023-03-12 13:29:15 +08:00
5f1cb36235 AccountInfo 的构建使用 Builder 模式。 2023-03-12 13:29:07 +08:00
b4bccfe663 升级 Spring Boot 到 2.7.9。 2023-03-12 13:28:39 +08:00
dc09022919 update hutool to 5.8.14 2023-03-08 19:48:17 +08:00
f0b6014e77 整理代码。 2023-03-08 17:42:04 +08:00
19557734ed Update Spring Boot to 2.7.9. 2023-03-03 21:02:03 +08:00
ff54aca271 Merge branch 'dev' of http://zhouxy.xyz:3000/ZhouXY108/plusone-admin into dev 2023-02-26 13:49:09 +08:00
5d9e6ae5b3 修改 Optional.map 方法的错误使用。 2023-02-26 13:49:03 +08:00
5c3b31bb18 更新方法名。 2023-02-25 02:00:12 +08:00
33d3d86393 Merge branch 'dev' into main 2023-02-25 00:32:34 +08:00
a0b16afde2 plusone-commons 重构,代码随之重构调整。 2023-02-24 17:30:18 +08:00
9293ba6817 20230219 2023-02-19 04:21:39 +08:00
92dbc613d2 统一空格。 2023-02-18 11:00:40 +08:00
1b551eeb4c Merge branch 'dev' into main 2023-02-17 15:57:05 +08:00
5b80f70373 添加 guava 依赖。 2023-02-17 15:29:51 +08:00
cf068566a1 Merge branch 'dev' of http://zhouxy.xyz:3000/ZhouXY108/plusone-admin into dev 2023-02-17 11:25:07 +08:00
0212020dcf 修改领域模型。 2023-02-17 11:24:55 +08:00
1611bf38e8 Merge branch 'dev' into main 2023-01-28 17:37:12 +08:00
17273b0bc6 Merge pull request 'dev' (#3) from dev into master
Reviewed-on: http://zhouxy.xyz:3000/ZhouXY108/plusone-admin/pulls/3
2022-12-17 22:55:06 +08:00
2d02899372 Merge pull request 'dev' (#1) from dev into master
Reviewed-on: http://zhouxy.xyz:3000/ZhouXY108/plusone-admin/pulls/1
2022-12-10 03:46:53 +08:00
93 changed files with 604 additions and 353 deletions

View File

@@ -33,4 +33,4 @@ indent_size=4
indent_size=2
[*.java]
indent_size = 4
indent_size=4

View File

@@ -1,10 +1,13 @@
package xyz.zhouxy.plusone.exception.handler;
import javax.annotation.Nonnull;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler.ExceptionInfoHolder;
import xyz.zhouxy.plusone.commons.exception.handler.AllExceptionHandler;
import xyz.zhouxy.plusone.commons.exception.handler.BaseExceptionHandler.ExceptionInfoHolder;
/**
* AllExceptionHandlerConfig
@@ -14,7 +17,7 @@ import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler.ExceptionInfoHo
public class AllExceptionHandlerConfig {
@Bean
AllExceptionHandler getAllExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) {
AllExceptionHandler getAllExceptionHandler(@Nonnull ExceptionInfoHolder exceptionInfoHolder) {
return new AllExceptionHandler(exceptionInfoHolder);
}
}

View File

@@ -1,5 +1,7 @@
package xyz.zhouxy.plusone.exception.handler;
import javax.annotation.Nonnull;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
@@ -11,7 +13,8 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.util.RestfulResult;
import xyz.zhouxy.plusone.commons.exception.handler.BaseExceptionHandler;
import xyz.zhouxy.plusone.commons.exception.handler.RestfulResult;
/**
* 默认异常的处理器
@@ -40,7 +43,7 @@ import xyz.zhouxy.plusone.util.RestfulResult;
@Order(Ordered.LOWEST_PRECEDENCE - 1)
@Slf4j
public class DefaultExceptionHandler extends BaseExceptionHandler {
public DefaultExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) {
public DefaultExceptionHandler(@Nonnull ExceptionInfoHolder exceptionInfoHolder) {
super(exceptionInfoHolder);
set(IllegalArgumentException.class, 4010000, "格式错误", HttpStatus.FORBIDDEN);
set(DataAccessException.class, 6030000, "数据库错误", HttpStatus.INTERNAL_SERVER_ERROR, true);

View File

@@ -0,0 +1,29 @@
package xyz.zhouxy.plusone.exception.handler;
import javax.annotation.Nonnull;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.commons.exception.handler.BaseExceptionHandler;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
import xyz.zhouxy.plusone.exception.SysException;
@RestControllerAdvice
@Slf4j
public class SysExceptionHandler extends BaseExceptionHandler {
public SysExceptionHandler(@Nonnull ExceptionInfoHolder exceptionInfoHolder) {
super(exceptionInfoHolder);
}
@ExceptionHandler({ SysException.class })
public ResponseEntity<RestfulResult> handleException(@Nonnull Exception e) {
log.error(e.getMessage(), e);
HttpStatus httpStatus = getHttpStatus(e);
return new ResponseEntity<>(RestfulResult.error(getErrorCode(e), "系统错误"), httpStatus);
}
}

View File

@@ -39,17 +39,17 @@
<dependency>
<groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-validator</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-validator</artifactId>
<version>0.1.3-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-exception-handler</artifactId>
<version>0.0.6-SNAPSHOT</version>
<version>0.0.9-SNAPSHOT</version>
</dependency>
</dependencies>

View File

@@ -2,4 +2,8 @@ package xyz.zhouxy.plusone.constant;
public class ErrorCodeConsts {
public static final int DEFAULT_ERROR_CODE = 9999999;
private ErrorCodeConsts() {
throw new IllegalStateException("Utility class");
}
}

View File

@@ -0,0 +1,39 @@
package xyz.zhouxy.plusone.exception;
import xyz.zhouxy.plusone.commons.exception.BaseException;
/**
* 业务异常
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
public class BizException extends BaseException {
@java.io.Serial
private static final long serialVersionUID = -5524759033245815405L;
public static final int DEFAULT_ERROR_CODE = 4000000;
public BizException(int code, String msg) {
super(code, msg);
}
public BizException(int code, Throwable cause) {
super(code, cause);
}
public BizException(int code, String msg, Throwable cause) {
super(code, msg, cause);
}
public BizException(String msg) {
super(DEFAULT_ERROR_CODE, msg);
}
public BizException(Throwable cause) {
super(DEFAULT_ERROR_CODE, cause);
}
public BizException(String msg, Throwable cause) {
super(DEFAULT_ERROR_CODE, msg, cause);
}
}

View File

@@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
@ResponseStatus(HttpStatus.NOT_FOUND)
public class DataNotExistException extends PlusoneException {
public class DataNotExistException extends BizException {
@java.io.Serial
private static final long serialVersionUID = 6536955800679703111L;

View File

@@ -8,13 +8,12 @@ import org.springframework.web.bind.annotation.ResponseStatus;
*
* <p>
* 暂时先这样,后续完善异常体系时可能会更改。
* </p>
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
* @see xyz.zhouxy.plusone.util.AssertResult
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public class DataOperationResultException extends PlusoneException {
public class DataOperationResultException extends SysException {
@java.io.Serial
private static final long serialVersionUID = -9220765735990318186L;

View File

@@ -0,0 +1,34 @@
package xyz.zhouxy.plusone.exception;
import xyz.zhouxy.plusone.commons.exception.BaseException;
public class SysException extends BaseException {
@java.io.Serial
private static final long serialVersionUID = 8821240827443168118L;
public static final int DEFAULT_ERROR_CODE = 5000000;
public SysException(int code, String msg) {
super(code, msg);
}
public SysException(int code, Throwable cause) {
super(code, cause);
}
public SysException(int code, String msg, Throwable cause) {
super(code, msg, cause);
}
public SysException(String msg) {
super(DEFAULT_ERROR_CODE, msg);
}
public SysException(Throwable cause) {
super(DEFAULT_ERROR_CODE, cause);
}
public SysException(String msg, Throwable cause) {
super(DEFAULT_ERROR_CODE, msg, cause);
}
}

View File

@@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
* @author ZhouXY
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class UserOperationException extends PlusoneException {
public class UserOperationException extends BizException {
@java.io.Serial
private static final long serialVersionUID = 4371055414421991940L;

View File

@@ -0,0 +1,18 @@
package xyz.zhouxy.plusone.util;
import java.util.concurrent.ThreadLocalRandom;
public final class RandomUtil {
private RandomUtil() {
throw new IllegalStateException("Utility class");
}
public static String randomStr(char[] sourceCharacters, int length) {
ThreadLocalRandom random = ThreadLocalRandom.current();
char[] result = new char[length];
for (int i = 0; i < length; i++) {
result[i] = sourceCharacters[random.nextInt(sourceCharacters.length)];
}
return String.valueOf(result);
}
}

View File

@@ -0,0 +1,23 @@
package xyz.zhouxy.plusone.util;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
import lombok.extern.slf4j.Slf4j;
@Slf4j
class RandomUtilTests {
@Test
void testRandom() {
String[] s = new String[20];
for (int i = 0; i < 20; i++) {
s[i] = RandomUtil.randomStr(
"0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM~`!@#$%^&*()_-+={[\\|/:;\"',.<>?]}"
.toCharArray(),
28);
}
log.info("{}", Arrays.toString(s));
}
}

View File

@@ -15,11 +15,6 @@
<groupId>xyz.zhouxy</groupId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
@@ -28,12 +23,10 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.4</version>
</dependency>
</dependency>
</dependencies>
</project>

View File

@@ -1,32 +1,34 @@
package xyz.zhouxy.plusone.constant;
import xyz.zhouxy.plusone.util.Enumeration;
import xyz.zhouxy.plusone.util.EnumerationValuesHolder;
import java.util.Collection;
import javax.annotation.Nonnull;
import xyz.zhouxy.plusone.commons.util.Enumeration;
/**
* 实体状态
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
public class EntityStatus extends Enumeration<EntityStatus> {
public final class EntityStatus extends Enumeration<EntityStatus> {
private EntityStatus(int value, String name) {
super(value, name);
private EntityStatus(int id, @Nonnull String name) {
super(id, name);
}
// 常量
public static final EntityStatus AVAILABLE = new EntityStatus(0, "正常");
public static final EntityStatus DISABLED = new EntityStatus(1, "禁用");
private static final EnumerationValuesHolder<EntityStatus> ENUMERATION_VALUES = new EnumerationValuesHolder<>(
new EntityStatus[] { AVAILABLE, DISABLED });
private static final ValueSet<EntityStatus> VALUE_SET = new ValueSet<>(
AVAILABLE, DISABLED);
public static EntityStatus of(int value) {
return ENUMERATION_VALUES.get(value);
public static EntityStatus of(int id) {
return VALUE_SET.get(id);
}
@Override
public String toString() {
return "EntityStatus" + super.toString();
public static Collection<EntityStatus> constants() {
return VALUE_SET.getValues();
}
}

View File

@@ -1,27 +0,0 @@
package xyz.zhouxy.plusone.constant;
/**
* 正则表达式常量
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
public final class RegexConsts {
public static final String DATE = "^\\d{4}-\\d{2}-\\d{2}";
public static final String PASSWORD = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[\\w\\\\!#$%&'*\\+\\-/=?^`{|}~@\\(\\)\\[\\]\",\\.;':><]{8,32}$";
public static final String CAPTCHA = "^[0-9A-Za-z]{4,6}$";
public static final String EMAIL = "^\\w+([-+.]\\w+)*@[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})*(\\.(?![0-9]+$)[a-zA-Z0-9][-0-9A-Za-z]{0,62})$";
public static final String MOBILE_PHONE = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$";
public static final String USERNAME = "^[\\da-zA-Z_.@\\\\]{4,36}$";
public static final String NICKNAME = "^[\\da-zA-Z_.@\\\\]{4,36}$";
private RegexConsts() {
throw new IllegalStateException("Utility class");
}
}

View File

@@ -1,6 +1,7 @@
package xyz.zhouxy.plusone.domain;
import cn.hutool.core.lang.UUID;
import java.util.UUID;
import lombok.Getter;
/**
@@ -20,7 +21,7 @@ public abstract class DomainEvent {
private long happenedAt;
protected DomainEvent() {
this.identifier = UUID.randomUUID().toString(true);
this.identifier = UUID.randomUUID().toString();
this.happenedAt = System.currentTimeMillis();
}
}

View File

@@ -1,5 +1,8 @@
package xyz.zhouxy.plusone.domain;
import java.util.Optional;
import java.util.regex.Pattern;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonValue;
@@ -11,15 +14,15 @@ import com.fasterxml.jackson.annotation.JsonValue;
public abstract class ValidatableStringRecord implements IValueObject {
protected String value;
protected final String format;
protected final Pattern format;
protected ValidatableStringRecord(String format) {
protected ValidatableStringRecord(Pattern format) {
this.format = format;
}
@JsonIgnore
protected boolean isValid() {
return value.matches(format);
return format.matcher(value).matches();
}
@JsonValue
@@ -31,4 +34,8 @@ public abstract class ValidatableStringRecord implements IValueObject {
public String toString() {
return value;
}
public static String getValueOrNull(Optional<? extends ValidatableStringRecord> s) {
return s.isPresent() ? s.get().value() : null;
}
}

View File

@@ -1,36 +0,0 @@
package xyz.zhouxy.plusone.util;
import java.util.regex.Pattern;
public class RegexUtil {
private RegexUtil() {
throw new IllegalStateException("Utility class");
}
public static boolean matches(CharSequence input, String regex) {
return Pattern.matches(regex, input);
}
public static boolean matchesOr(CharSequence input, String... regexs) {
boolean isMatched;
for (var regex : regexs) {
isMatched = Pattern.matches(regex, input);
if (isMatched) {
return true;
}
}
return false;
}
public static boolean matchesAnd(CharSequence input, String... regexs) {
boolean isMatched;
for (var regex : regexs) {
isMatched = Pattern.matches(regex, input);
if (!isMatched) {
return false;
}
}
return true;
}
}

View File

@@ -5,7 +5,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xyz.zhouxy.plusone.constant.ErrorCodeConsts;
import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler.ExceptionInfoHolder;
import xyz.zhouxy.plusone.commons.exception.handler.BaseExceptionHandler.ExceptionInfoHolder;
@Configuration
public class PlusoneExceptionHandlerConfig {

View File

@@ -14,9 +14,10 @@ import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.util.CollectionUtils;
import xyz.zhouxy.plusone.commons.util.NumberUtil;
import xyz.zhouxy.plusone.exception.DataOperationResultException;
import xyz.zhouxy.plusone.util.NumberUtil;
public abstract class PlusoneJdbcDaoSupport {
protected final NamedParameterJdbcTemplate jdbc;
@@ -68,13 +69,13 @@ public abstract class PlusoneJdbcDaoSupport {
return queryForStream(sql, new MapSqlParameterSource(paramName, value), elementType);
}
protected final boolean queryExists(String sql, SqlParameterSource parameterSource) {
Boolean isExists = this.jdbc.queryForObject(sql, parameterSource, Boolean.TYPE);
return Boolean.TRUE.equals(isExists);
protected final boolean queryForBool(String sql, SqlParameterSource parameterSource) {
Boolean result = this.jdbc.queryForObject(sql, parameterSource, Boolean.TYPE);
return Boolean.TRUE.equals(result);
}
protected final boolean queryExists(String sql, String paramName, Object value) {
return queryExists(sql, new MapSqlParameterSource(paramName, value));
protected final boolean queryForBool(String sql, String paramName, Object value) {
return queryForBool(sql, new MapSqlParameterSource(paramName, value));
}
protected final int update(String sql, SqlParameterSource parameterSource) {
@@ -85,18 +86,18 @@ public abstract class PlusoneJdbcDaoSupport {
return update(sql, new MapSqlParameterSource(paramName, value));
}
protected final int batchUpdate(String sql, SqlParameterSource[] batchArgs) {
protected final long batchUpdate(String sql, SqlParameterSource[] batchArgs) {
int[] i = this.jdbc.batchUpdate(sql, batchArgs);
return NumberUtil.sum(i);
}
protected final <T> int batchUpdate(String sql, Stream<T> c,
protected final <T> long batchUpdate(String sql, Stream<T> c,
@Nonnull Function<T, SqlParameterSource> paramSourceBuilder) {
int[] i = this.jdbc.batchUpdate(sql, buildSqlParameterSourceArray(c, paramSourceBuilder));
return NumberUtil.sum(i);
}
protected final <T> int batchUpdate(String sql, Collection<T> c,
protected final <T> long batchUpdate(String sql, Collection<T> c,
@Nonnull Function<T, SqlParameterSource> paramSourceBuilder) {
int[] i = this.jdbc.batchUpdate(sql, buildSqlParameterSourceArray(c, paramSourceBuilder));
return NumberUtil.sum(i);
@@ -156,7 +157,7 @@ public abstract class PlusoneJdbcDaoSupport {
protected static final <T> SqlParameterSource[] buildSqlParameterSourceArray(
Collection<T> c,
@Nonnull Function<T, SqlParameterSource> paramSourceBuilder) {
if (c == null || c.isEmpty()) {
if (CollectionUtils.isEmpty(c)) {
return new SqlParameterSource[] {};
}
return buildSqlParameterSourceArray(c.stream(), paramSourceBuilder);

View File

@@ -6,6 +6,7 @@ package xyz.zhouxy.plusone.oss;
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
public class FastDFSException extends Exception {
private static final long serialVersionUID = 7871031982887742468L;
public FastDFSException() {
}

View File

@@ -12,7 +12,7 @@ import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.constant.ErrorCodeConsts;
import xyz.zhouxy.plusone.exception.PlusoneException;
import xyz.zhouxy.plusone.exception.BizException;
import xyz.zhouxy.plusone.sms.SmsProperties.SmsCredentialProperties;
import xyz.zhouxy.plusone.sms.SmsProperties.SmsHttpProperties;
import xyz.zhouxy.plusone.sms.SmsProperties.SmsProxyProperties;
@@ -69,7 +69,7 @@ public class TencentSmsServiceImpl implements SmsService {
} catch (TencentCloudSDKException e) {
log.error(e.getMessage(), e);
throw new PlusoneException(ErrorCodeConsts.DEFAULT_ERROR_CODE, e);
throw new BizException(ErrorCodeConsts.DEFAULT_ERROR_CODE, e);
}
}

View File

@@ -1,13 +1,15 @@
package xyz.zhouxy.plusone.validator;
import javax.annotation.Nonnull;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler;
import xyz.zhouxy.plusone.commons.exception.handler.BaseExceptionHandler;
@RestControllerAdvice
public class InvalidInputExceptionHandler extends BaseExceptionHandler {
public InvalidInputExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) {
public InvalidInputExceptionHandler(@Nonnull ExceptionInfoHolder exceptionInfoHolder) {
super(exceptionInfoHolder);
set(InvalidInputException.class, InvalidInputException.ERROR_CODE, "无效的用户输入");
}

View File

@@ -1,11 +1,13 @@
package xyz.zhouxy.plusone.validatortest;
import java.util.regex.Pattern;
import org.junit.jupiter.api.Test;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import xyz.zhouxy.plusone.constant.RegexConsts;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
import xyz.zhouxy.plusone.validator.BaseValidator;
class BaseValidatorTest {
@@ -34,11 +36,11 @@ class LoginCommandValidator extends BaseValidator<LoginCommand> {
private LoginCommandValidator() {
ruleForString(LoginCommand::getAccount)
.notNull("邮箱地址不能为空")
.matchesOr(new String[] { RegexConsts.EMAIL, RegexConsts.MOBILE_PHONE },
.matchesOr(new Pattern[] { PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE },
value -> new RuntimeException('"' + value + "\" 不是邮箱地址或手机号"));
ruleForString(LoginCommand::getPwd)
.notNull("密码不能为空")
.notEmpty("密码不能为空")
.matches(RegexConsts.PASSWORD, "密码格式错误");
.matches(PatternConsts.PASSWORD, "密码格式错误");
}
}

View File

@@ -7,9 +7,7 @@
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>xyz.zhouxy</groupId>
<artifactId>plusone-basic</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

View File

@@ -7,10 +7,7 @@
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>xyz.zhouxy</groupId>
<artifactId>plusone-start</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>plusone-start</name>
<description>参考 DDD 落地的脚手架</description>

View File

@@ -5,14 +5,14 @@ import java.io.ObjectStreamClass;
import org.junit.jupiter.api.Test;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.exception.PlusoneException;
import xyz.zhouxy.plusone.exception.*;
@Slf4j
class SerialTests {
@Test
void testSerialVersionUID() {
var cl = PlusoneException.class;
var cl = SysException.class;
var c = ObjectStreamClass.lookup(cl);
var uid = c.getSerialVersionUID();
log.info("\n @java.io.Serial" +

View File

@@ -1,5 +1,7 @@
package xyz.zhouxy.plusone;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import javax.annotation.Resource;
import org.junit.jupiter.api.Test;
@@ -25,6 +27,7 @@ class TestAop {
command.setPrincipal("Code108");
command.setPassword("2333");
command.setRememberMe(false);
assertNotNull(service);
LoginInfoViewObject loginInfo = service.loginByPassword(command);
System.err.println(loginInfo);
}

View File

@@ -0,0 +1,32 @@
package xyz.zhouxy.plusone.system.application.common.exception;
import xyz.zhouxy.plusone.exception.BizException;
/**
* 不支持的 Principal 类型出现时抛出的异常
*
* @author <a href="https://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public class UnsupportedPrincipalTypeException extends BizException {
private static final long serialVersionUID = 5207757290868470762L;
public static final int ERR_CODE = 4040201;
private static final String DEFAULT_ERROR_MSG = "不支持的 PrincipalType";
public UnsupportedPrincipalTypeException() {
super(ERR_CODE, DEFAULT_ERROR_MSG);
}
public UnsupportedPrincipalTypeException(String msg) {
super(ERR_CODE, msg);
}
public UnsupportedPrincipalTypeException(Throwable cause) {
super(ERR_CODE, cause);
}
public UnsupportedPrincipalTypeException(String msg, Throwable cause) {
super(ERR_CODE, msg, cause);
}
}

View File

@@ -1,18 +1,20 @@
package xyz.zhouxy.plusone.system.application.common.util;
import java.util.regex.Pattern;
import lombok.Getter;
import xyz.zhouxy.plusone.constant.RegexConsts;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
public enum PrincipalType {
EMAIL(RegexConsts.EMAIL),
MOBILE_PHONE(RegexConsts.MOBILE_PHONE),
USERNAME(RegexConsts.USERNAME)
EMAIL(PatternConsts.EMAIL),
MOBILE_PHONE(PatternConsts.MOBILE_PHONE),
USERNAME(PatternConsts.USERNAME)
;
@Getter
private final String regex;
private final Pattern regex;
PrincipalType(String regex) {
PrincipalType(Pattern regex) {
this.regex = regex;
}
}

View File

@@ -2,11 +2,11 @@ package xyz.zhouxy.plusone.system.application.common.util;
import javax.annotation.Nullable;
import xyz.zhouxy.plusone.system.application.common.exception.UnsupportedPrincipalTypeException;
import xyz.zhouxy.plusone.system.domain.model.account.Email;
import xyz.zhouxy.plusone.system.domain.model.account.MobilePhone;
import xyz.zhouxy.plusone.system.domain.model.account.Principal;
import xyz.zhouxy.plusone.system.domain.model.account.Username;
import xyz.zhouxy.plusone.validator.InvalidInputException;
/**
* 根据字面值,判断并生成 {@link Principal} 值对象。
@@ -16,7 +16,7 @@ import xyz.zhouxy.plusone.validator.InvalidInputException;
* @see Username
* @see Email
* @see MobilePhone
* @see InvalidInputException
* @see UnsupportedPrincipalTypeException
*/
public class PrincipalUtil {
@@ -30,11 +30,11 @@ public class PrincipalUtil {
}
PrincipalType[] principalTypes = PrincipalType.values();
for (var principalType : principalTypes) {
if (principal.matches(principalType.getRegex())) {
if (principalType.getRegex().matcher(principal).matches()) {
return principalType;
}
}
throw InvalidInputException.unsupportedPrincipalTypeException();
throw new UnsupportedPrincipalTypeException();
}
public static Principal getPrincipal(@Nullable String principal) {
@@ -48,7 +48,7 @@ public class PrincipalUtil {
if (principalType == PrincipalType.USERNAME) {
return Username.of(principal);
}
throw InvalidInputException.unsupportedPrincipalTypeException();
throw new UnsupportedPrincipalTypeException();
}
public static Principal getEmailOrMobilePhone(@Nullable String principal) {
@@ -59,6 +59,6 @@ public class PrincipalUtil {
if (principalType == PrincipalType.MOBILE_PHONE) {
return MobilePhone.of(principal);
}
throw InvalidInputException.unsupportedPrincipalTypeException("输入邮箱地址或手机号");
throw new UnsupportedPrincipalTypeException("输入邮箱地址或手机号");
}
}

View File

@@ -7,10 +7,10 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
import xyz.zhouxy.plusone.system.application.service.AccountContextService;
import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordCommand;
import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordWithoutLoginCommand;
import xyz.zhouxy.plusone.util.RestfulResult;
/**
* 账号查询本身相关信息

View File

@@ -1,7 +1,7 @@
package xyz.zhouxy.plusone.system.application.controller;
import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic;
import static xyz.zhouxy.plusone.util.RestfulResult.success;
import static xyz.zhouxy.plusone.commons.util.RestfulResult.success;
import java.util.List;
@@ -16,11 +16,11 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
import xyz.zhouxy.plusone.system.application.query.params.AccountQueryParams;
import xyz.zhouxy.plusone.system.application.service.AccountManagementService;
import xyz.zhouxy.plusone.system.application.service.command.CreateAccountCommand;
import xyz.zhouxy.plusone.system.application.service.command.UpdateAccountCommand;
import xyz.zhouxy.plusone.util.RestfulResult;
/**
* 账号管理

View File

@@ -1,6 +1,6 @@
package xyz.zhouxy.plusone.system.application.controller;
import static xyz.zhouxy.plusone.util.RestfulResult.success;
import static xyz.zhouxy.plusone.commons.util.RestfulResult.success;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.RestController;
import xyz.zhouxy.plusone.system.application.service.AdminLoginService;
import xyz.zhouxy.plusone.system.application.service.command.LoginByOtpCommand;
import xyz.zhouxy.plusone.system.application.service.command.LoginByPasswordCommand;
import xyz.zhouxy.plusone.util.RestfulResult;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
/**
* Admin 账号登录

View File

@@ -1,7 +1,7 @@
package xyz.zhouxy.plusone.system.application.controller;
import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic;
import static xyz.zhouxy.plusone.util.RestfulResult.success;
import static xyz.zhouxy.plusone.commons.util.RestfulResult.success;
import java.util.List;
@@ -20,7 +20,7 @@ import xyz.zhouxy.plusone.system.application.query.params.DictQueryParams;
import xyz.zhouxy.plusone.system.application.service.DictManagementService;
import xyz.zhouxy.plusone.system.application.service.command.CreateDictCommand;
import xyz.zhouxy.plusone.system.application.service.command.UpdateDictCommand;
import xyz.zhouxy.plusone.util.RestfulResult;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
/**
* 数据字典管理

View File

@@ -1,7 +1,7 @@
package xyz.zhouxy.plusone.system.application.controller;
import static xyz.zhouxy.plusone.system.constant.AuthLogic.adminAuthLogic;
import static xyz.zhouxy.plusone.util.RestfulResult.success;
import static xyz.zhouxy.plusone.commons.util.RestfulResult.success;
import javax.validation.Valid;
@@ -18,7 +18,7 @@ import org.springframework.web.bind.annotation.RestController;
import xyz.zhouxy.plusone.system.application.service.MenuManagementService;
import xyz.zhouxy.plusone.system.application.service.command.CreateMenuCommand;
import xyz.zhouxy.plusone.system.application.service.command.UpdateMenuCommand;
import xyz.zhouxy.plusone.util.RestfulResult;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
/**
* 菜单管理

View File

@@ -1,6 +1,6 @@
package xyz.zhouxy.plusone.system.application.controller;
import static xyz.zhouxy.plusone.util.RestfulResult.success;
import static xyz.zhouxy.plusone.commons.util.RestfulResult.success;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.RestController;
import xyz.zhouxy.plusone.system.application.service.RegisterAccountService;
import xyz.zhouxy.plusone.system.application.service.command.RegisterAccountCommand;
import xyz.zhouxy.plusone.util.RestfulResult;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
/**
* 注册账号服务

View File

@@ -17,7 +17,7 @@ import xyz.zhouxy.plusone.system.application.query.params.RoleQueryParams;
import xyz.zhouxy.plusone.system.application.service.RoleManagementService;
import xyz.zhouxy.plusone.system.application.service.command.CreateRoleCommand;
import xyz.zhouxy.plusone.system.application.service.command.UpdateRoleCommand;
import xyz.zhouxy.plusone.util.RestfulResult;
import xyz.zhouxy.plusone.commons.util.RestfulResult;
/**
* 角色管理服务

View File

@@ -3,10 +3,10 @@ package xyz.zhouxy.plusone.system.application.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
import xyz.zhouxy.plusone.exception.PlusoneException;
import xyz.zhouxy.plusone.exception.BizException;
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class AccountLoginException extends PlusoneException {
public class AccountLoginException extends BizException {
@java.io.Serial
private static final long serialVersionUID = -3040996790739138556L;

View File

@@ -3,10 +3,10 @@ package xyz.zhouxy.plusone.system.application.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
import xyz.zhouxy.plusone.exception.PlusoneException;
import xyz.zhouxy.plusone.exception.BizException;
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class AccountRegisterException extends PlusoneException {
public class AccountRegisterException extends BizException {
@java.io.Serial
private static final long serialVersionUID = 7580245181633370195L;

View File

@@ -6,14 +6,14 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler;
import xyz.zhouxy.plusone.system.application.exception.AccountLoginException;
import xyz.zhouxy.plusone.util.RestfulResult;
import xyz.zhouxy.plusone.commons.exception.handler.BaseExceptionHandler;
import xyz.zhouxy.plusone.commons.exception.handler.RestfulResult;
@RestControllerAdvice
public class AccountLoginExceptionHandler extends BaseExceptionHandler {
public AccountLoginExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) {
public AccountLoginExceptionHandler(@Nonnull ExceptionInfoHolder exceptionInfoHolder) {
super(exceptionInfoHolder);
}

View File

@@ -1,5 +1,7 @@
package xyz.zhouxy.plusone.system.application.exception.handler;
import javax.annotation.Nonnull;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
@@ -14,8 +16,8 @@ import cn.dev33.satoken.exception.NotSafeException;
import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.exception.SameTokenInvalidException;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler;
import xyz.zhouxy.plusone.util.RestfulResult;
import xyz.zhouxy.plusone.commons.exception.handler.BaseExceptionHandler;
import xyz.zhouxy.plusone.commons.exception.handler.RestfulResult;
/**
* Sa-Token 异常处理器
@@ -27,7 +29,7 @@ import xyz.zhouxy.plusone.util.RestfulResult;
public class SaTokenExceptionHandler extends BaseExceptionHandler {
public SaTokenExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) {
public SaTokenExceptionHandler(@Nonnull ExceptionInfoHolder exceptionInfoHolder) {
super(exceptionInfoHolder);
set(NotPermissionException.class, 4030103, "会话未能通过权限认证", HttpStatus.FORBIDDEN);
set(NotRoleException.class, 4030103, "会话未能通过角色认证", HttpStatus.FORBIDDEN);

View File

@@ -2,12 +2,14 @@ package xyz.zhouxy.plusone.system.application.query;
import java.util.List;
import javax.annotation.Nonnull;
import org.apache.ibatis.annotations.Mapper;
import xyz.zhouxy.plusone.commons.util.PageDTO;
import xyz.zhouxy.plusone.system.application.query.params.AccountQueryParams;
import xyz.zhouxy.plusone.system.application.query.result.AccountDetails;
import xyz.zhouxy.plusone.system.application.query.result.AccountOverview;
import xyz.zhouxy.plusone.util.PageDTO;
/**
* 账号信息查询器
@@ -23,6 +25,7 @@ public interface AccountQueries {
return PageDTO.of(content, total);
}
@Nonnull
List<AccountOverview> queryAccountOverview(AccountQueryParams queryParams);
long count(AccountQueryParams queryParams);

View File

@@ -5,7 +5,7 @@ import java.time.LocalDate;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import xyz.zhouxy.plusone.util.PagingAndSortingQueryParams;
import xyz.zhouxy.plusone.commons.util.PagingAndSortingQueryParams;
/**
* 账号信息查询参数

View File

@@ -4,7 +4,7 @@ import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import xyz.zhouxy.plusone.util.PagingAndSortingQueryParams;
import xyz.zhouxy.plusone.commons.util.PagingAndSortingQueryParams;
/**
* 数据字典查询参数

View File

@@ -5,7 +5,7 @@ import java.time.LocalDate;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import xyz.zhouxy.plusone.util.PagingAndSortingQueryParams;
import xyz.zhouxy.plusone.commons.util.PagingAndSortingQueryParams;
/**
* 角色信息查询参数

View File

@@ -109,7 +109,7 @@ public class MenuViewObject implements IWithOrderNumber {
viewObject.icon = menu.getIcon();
viewObject.hidden = menu.isHidden();
viewObject.orderNumber = menu.getOrderNumber();
viewObject.status = menu.getStatus().getValue();
viewObject.status = menu.getStatus().getId();
viewObject.remarks = menu.getRemarks();
if (viewObject.type == MenuType.MENU_ITEM.ordinal()) {
viewObject.component = menu.getComponent();

View File

@@ -1,7 +1,6 @@
package xyz.zhouxy.plusone.system.application.service;
import java.util.List;
import java.util.Optional;
import javax.annotation.Resource;
@@ -9,6 +8,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.dev33.satoken.stp.StpLogic;
import xyz.zhouxy.plusone.system.application.common.exception.UnsupportedPrincipalTypeException;
import xyz.zhouxy.plusone.system.application.common.util.PrincipalUtil;
import xyz.zhouxy.plusone.system.application.exception.AccountLoginException;
import xyz.zhouxy.plusone.system.application.query.AccountQueries;
@@ -23,7 +23,6 @@ import xyz.zhouxy.plusone.system.domain.model.account.AccountRepository;
import xyz.zhouxy.plusone.system.domain.model.account.Email;
import xyz.zhouxy.plusone.system.domain.model.account.MobilePhone;
import xyz.zhouxy.plusone.system.domain.model.account.Principal;
import xyz.zhouxy.plusone.validator.InvalidInputException;
/**
* 账号对当前帐号进行操作
@@ -91,13 +90,14 @@ public class AccountContextService {
public void changePasswordByOtp(ChangePasswordByOtpCommand command) {
var principal = command.getAccount();
Optional<Account> account = switch (command.getPrincipalType()) {
case EMAIL -> accountRepository.findByEmail(Email.of(principal));
case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal));
default -> throw InvalidInputException.unsupportedPrincipalTypeException("输入邮箱地址或手机号");
boolean accountExists = switch (command.getPrincipalType()) {
case EMAIL -> accountRepository.existsEmail(Email.of(principal));
case MOBILE_PHONE -> accountRepository.existsMobilePhone(MobilePhone.of(principal));
default -> throw new UnsupportedPrincipalTypeException("输入邮箱地址或手机号");
};
account.orElseThrow(AccountLoginException::accountNotExistException);
if (accountExists) {
throw AccountLoginException.accountNotExistException();
}
mailAndSmsVerifyService.checkOtp(principal, command.getOtp());
}
}

View File

@@ -14,6 +14,7 @@ import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import xyz.zhouxy.plusone.commons.util.PageDTO;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.system.application.exception.AccountRegisterException;
import xyz.zhouxy.plusone.system.application.query.AccountQueries;
@@ -25,11 +26,12 @@ import xyz.zhouxy.plusone.system.application.service.command.UpdateAccountComman
import xyz.zhouxy.plusone.system.domain.model.account.Account;
import xyz.zhouxy.plusone.system.domain.model.account.AccountInfo;
import xyz.zhouxy.plusone.system.domain.model.account.AccountRepository;
import xyz.zhouxy.plusone.system.domain.model.account.AccountStatus;
import xyz.zhouxy.plusone.system.domain.model.account.Email;
import xyz.zhouxy.plusone.system.domain.model.account.MobilePhone;
import xyz.zhouxy.plusone.system.domain.model.account.Sex;
import xyz.zhouxy.plusone.system.domain.model.account.Username;
import xyz.zhouxy.plusone.util.AssertResult;
import xyz.zhouxy.plusone.util.PageDTO;
/**
* 账号管理
@@ -67,9 +69,13 @@ public class AccountManagementService {
mobilePhone,
command.getPassword(),
command.getPasswordConfirmation(),
command.getStatus(),
AccountStatus.of(command.getStatus()),
command.getRoleRefs(),
AccountInfo.of(command.getNickname(), command.getAvatar(), command.getSex()),
AccountInfo.builder()
.nickname(command.getNickname())
.avatar(command.getAvatar())
.sex(Sex.of(command.getSex()))
.build(),
adminAuthLogic.getLoginIdAsLong());
accountRepository.save(account);
}
@@ -87,7 +93,7 @@ public class AccountManagementService {
Assert.isTrue(Objects.equals(id, command.getId()), "参数错误: id 不匹配");
Account account = accountRepository.find(id)
.orElseThrow(() -> new DataNotExistException("该账号不存在"));
account.setAccountInfo(command.getNickname(), command.getAvatar(), command.getSex());
account.setAccountInfo(command.getNickname(), command.getAvatar(), Sex.of(command.getSex()));
account.setUpdatedBy(adminAuthLogic.getLoginIdAsLong());
accountRepository.save(account);
}

View File

@@ -6,6 +6,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Assert;
import xyz.zhouxy.plusone.system.application.common.exception.UnsupportedPrincipalTypeException;
import xyz.zhouxy.plusone.system.application.common.util.PrincipalType;
import xyz.zhouxy.plusone.system.application.common.util.PrincipalUtil;
import xyz.zhouxy.plusone.system.application.exception.AccountLoginException;
@@ -18,7 +19,6 @@ import xyz.zhouxy.plusone.system.domain.model.account.AccountRepository;
import xyz.zhouxy.plusone.system.domain.model.account.Email;
import xyz.zhouxy.plusone.system.domain.model.account.MobilePhone;
import xyz.zhouxy.plusone.system.domain.model.account.Username;
import xyz.zhouxy.plusone.validator.InvalidInputException;
import xyz.zhouxy.plusone.validator.ValidateDto;
/**
@@ -63,7 +63,7 @@ public class AdminLoginService {
Account account = (switch (command.getPrincipalType()) {
case EMAIL -> accountRepository.findByEmail(Email.of(principal));
case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal));
default -> throw InvalidInputException.unsupportedPrincipalTypeException("输入邮箱地址或手机号");
default -> throw new UnsupportedPrincipalTypeException("输入邮箱地址或手机号");
}).orElseThrow(AccountLoginException::accountNotExistException);
mailAndSmsVerifyService.checkOtp(principal, command.getOtp());

View File

@@ -13,6 +13,7 @@ import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import xyz.zhouxy.plusone.constant.EntityStatus;
import xyz.zhouxy.plusone.domain.IWithOrderNumber;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.system.application.exception.UnsupportedMenuTypeException;
@@ -66,7 +67,7 @@ public class MenuManagementService {
command.getIcon(),
command.getHidden(),
command.getOrderNumber(),
command.getStatus(),
EntityStatus.of(command.getStatus()),
command.getRemarks());
}
@@ -79,7 +80,7 @@ public class MenuManagementService {
command.getIcon(),
command.getHidden(),
command.getOrderNumber(),
command.getStatus(),
EntityStatus.of(command.getStatus()),
command.getComponent(),
command.getResource(),
command.getCache(),
@@ -107,7 +108,7 @@ public class MenuManagementService {
command.getIcon(),
command.getHidden(),
command.getOrderNumber(),
command.getStatus(),
EntityStatus.of(command.getStatus()),
command.getComponent(),
command.getResource(),
command.getCache(),

View File

@@ -5,6 +5,7 @@ import java.util.Set;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouxy.plusone.system.application.common.exception.UnsupportedPrincipalTypeException;
import xyz.zhouxy.plusone.system.application.common.util.PrincipalType;
import xyz.zhouxy.plusone.system.application.common.util.PrincipalUtil;
import xyz.zhouxy.plusone.system.application.exception.AccountRegisterException;
@@ -16,9 +17,7 @@ import xyz.zhouxy.plusone.system.domain.model.account.AccountStatus;
import xyz.zhouxy.plusone.system.domain.model.account.Email;
import xyz.zhouxy.plusone.system.domain.model.account.MobilePhone;
import xyz.zhouxy.plusone.system.domain.model.account.Password;
import xyz.zhouxy.plusone.system.domain.model.account.Sex;
import xyz.zhouxy.plusone.system.domain.model.account.Username;
import xyz.zhouxy.plusone.validator.InvalidInputException;
/**
* 注册账号服务
@@ -70,7 +69,7 @@ public class RegisterAccountService {
throw AccountRegisterException.emailAlreadyExists(mobilePhone.value());
}
} else {
throw InvalidInputException.unsupportedPrincipalTypeException();
throw new UnsupportedPrincipalTypeException();
}
verifyService.checkCode(emailOrMobilePhone, command.getCode());
@@ -82,7 +81,9 @@ public class RegisterAccountService {
Password.newPassword(command.getPassword(), command.getReenteredPassword()),
AccountStatus.AVAILABLE,
Set.of(DEFAULT_ROLE_ID),
AccountInfo.of(command.getNickname(), null, Sex.UNSET));
AccountInfo.builder()
.nickname(command.getNickname())
.build());
accountRepository.save(accountToSave);
}
@@ -93,7 +94,7 @@ public class RegisterAccountService {
} else if (principalType == PrincipalType.MOBILE_PHONE) {
verifyService.sendCodeToMobilePhone(MobilePhone.of(principal));
} else {
throw InvalidInputException.unsupportedPrincipalTypeException();
throw new UnsupportedPrincipalTypeException();
}
}
}

View File

@@ -10,6 +10,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouxy.plusone.constant.EntityStatus;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.system.application.query.RoleQueries;
import xyz.zhouxy.plusone.system.application.query.params.RoleQueryParams;
@@ -54,7 +55,7 @@ public class RoleManagementService {
Role roleToCreate = Role.newInstance(
command.getName(),
command.getIdentifier(),
command.getStatus(),
EntityStatus.of(command.getStatus()),
command.getRemarks(),
menuRefs,
permissionRefs);
@@ -67,7 +68,7 @@ public class RoleManagementService {
roleToUpdate.update(
command.getName(),
command.getIdentifier(),
command.getStatus(),
EntityStatus.of(command.getStatus()),
command.getRemarks(),
Set.copyOf(_menuRepository.findByIdIn(command.getMenus())),
Set.copyOf(_menuRepository.findPermissionsByIdIn(command.getPermissions())));

View File

@@ -10,10 +10,8 @@ import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.URL;
import lombok.Data;
import xyz.zhouxy.plusone.constant.RegexConsts;
import xyz.zhouxy.plusone.commons.constant.RegexConsts;
import xyz.zhouxy.plusone.domain.ICommand;
import xyz.zhouxy.plusone.system.domain.model.account.AccountStatus;
import xyz.zhouxy.plusone.system.domain.model.account.Sex;
/**
* 创建账号命令
@@ -41,7 +39,7 @@ public class CreateAccountCommand implements ICommand {
String passwordConfirmation;
@NotNull
AccountStatus status;
Integer status;
Set<Long> roleRefs;
@@ -52,5 +50,5 @@ public class CreateAccountCommand implements ICommand {
@URL
String avatar;
Sex sex;
Integer sex;
}

View File

@@ -5,7 +5,6 @@ import javax.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import xyz.zhouxy.plusone.constant.EntityStatus;
import xyz.zhouxy.plusone.domain.ICommand;
import xyz.zhouxy.plusone.system.domain.model.menu.Menu.MenuType;
@@ -42,7 +41,7 @@ public class CreateMenuCommand implements ICommand {
private Integer orderNumber;
@NotNull
private EntityStatus status;
private Integer status;
private String component;

View File

@@ -6,7 +6,6 @@ import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import lombok.Data;
import xyz.zhouxy.plusone.constant.EntityStatus;
import xyz.zhouxy.plusone.domain.ICommand;
/**
@@ -23,7 +22,7 @@ public class CreateRoleCommand implements ICommand {
String identifier;
@NotNull
EntityStatus status;
Integer status;
String remarks;
Set<Long> menus;

View File

@@ -4,7 +4,7 @@ import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import lombok.Data;
import xyz.zhouxy.plusone.constant.RegexConsts;
import xyz.zhouxy.plusone.commons.constant.RegexConsts;
import xyz.zhouxy.plusone.domain.ICommand;
/**

View File

@@ -6,9 +6,8 @@ import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.URL;
import lombok.Data;
import xyz.zhouxy.plusone.constant.RegexConsts;
import xyz.zhouxy.plusone.commons.constant.RegexConsts;
import xyz.zhouxy.plusone.domain.ICommand;
import xyz.zhouxy.plusone.system.domain.model.account.Sex;
/**
* 更新账号信息命令
@@ -27,5 +26,5 @@ public class UpdateAccountCommand implements ICommand {
@URL
String avatar;
Sex sex;
Integer sex;
}

View File

@@ -5,7 +5,6 @@ import javax.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import xyz.zhouxy.plusone.constant.EntityStatus;
import xyz.zhouxy.plusone.domain.ICommand;
import xyz.zhouxy.plusone.system.domain.model.menu.Menu.MenuType;
@@ -46,7 +45,7 @@ public class UpdateMenuCommand implements ICommand {
private Integer orderNumber;
@NotNull
private EntityStatus status;
private Integer status;
private String component;

View File

@@ -6,7 +6,6 @@ import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import lombok.Data;
import xyz.zhouxy.plusone.constant.EntityStatus;
import xyz.zhouxy.plusone.domain.ICommand;
/**
@@ -27,7 +26,7 @@ public class UpdateRoleCommand implements ICommand {
String identifier;
@NotNull
EntityStatus status;
Integer status;
@NotBlank
String remarks;

View File

@@ -4,7 +4,7 @@ import java.util.List;
import org.springframework.stereotype.Component;
import xyz.zhouxy.plusone.constant.RegexConsts;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
import xyz.zhouxy.plusone.system.application.service.command.LoginByOtpCommand;
import xyz.zhouxy.plusone.validator.BaseValidator;
import xyz.zhouxy.plusone.validator.DtoValidator;
@@ -16,10 +16,10 @@ public class LoginByOtpCommandValidator extends BaseValidator<LoginByOtpCommand>
ruleForString(LoginByOtpCommand::getPrincipal)
.notNull("输入邮箱地址或手机号")
.notEmpty("输入邮箱地址或手机号")
.matchesOr(List.of(RegexConsts.EMAIL, RegexConsts.MOBILE_PHONE), "输入用户名、邮箱地址或手机号");
.matchesOr(List.of(PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE), "输入用户名、邮箱地址或手机号");
ruleForString(LoginByOtpCommand::getOtp)
.notNull("验证码不能为空")
.notEmpty("验证码不能为空")
.matches(RegexConsts.CAPTCHA, "验证码格式不正确");
.matches(PatternConsts.CAPTCHA, "验证码格式不正确");
}
}

View File

@@ -2,7 +2,8 @@ package xyz.zhouxy.plusone.system.application.service.command.validator;
import java.util.List;
import org.springframework.stereotype.Component;
import xyz.zhouxy.plusone.constant.RegexConsts;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
import xyz.zhouxy.plusone.system.application.service.command.LoginByPasswordCommand;
import xyz.zhouxy.plusone.validator.BaseValidator;
import xyz.zhouxy.plusone.validator.DtoValidator;
@@ -14,11 +15,11 @@ public class LoginByPasswordCommandValidator extends BaseValidator<LoginByPasswo
ruleForString(LoginByPasswordCommand::getPrincipal)
.notNull("输入用户名、邮箱地址或手机号")
.notEmpty("输入用户名、邮箱地址或手机号")
.matchesOr(List.of(RegexConsts.USERNAME, RegexConsts.EMAIL, RegexConsts.MOBILE_PHONE),
.matchesOr(List.of(PatternConsts.USERNAME, PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE),
"输入用户名、邮箱地址或手机号");
ruleForString(LoginByPasswordCommand::getPassword)
.notNull("密码不能为空")
.notEmpty("密码不能为空")
.matches(RegexConsts.PASSWORD, "密码格式不正确");
.matches(PatternConsts.PASSWORD, "密码格式不正确");
}
}

View File

@@ -14,10 +14,6 @@
<groupId>xyz.zhouxy</groupId>
<artifactId>plusone-basic-common</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-crypto</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -1,19 +1,26 @@
package xyz.zhouxy.plusone.system.util;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.annotation.Nonnull;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.DigestUtil;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.constant.ErrorCodeConsts;
import xyz.zhouxy.plusone.exception.PlusoneException;
import xyz.zhouxy.plusone.exception.BizException;
import xyz.zhouxy.plusone.util.RandomUtil;
/**
* 密码工具类
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
@Slf4j
public final class PasswordUtil {
private static final String SALT_BASE_STRING = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~`!@#$%^&*()_-+={}[]|\\:;\"',.<>?/";
private static final char[] SALT_BASE_CHAR_ARRAY = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~`!@#$%^&*()_-+={}[]|\\:;\"',.<>?/"
.toCharArray();
/**
* 将密码和随机盐混合,并进行哈希加密。
@@ -29,9 +36,9 @@ public final class PasswordUtil {
var passwordWithSalt = salt.substring(0, i)
+ password
+ salt.substring(1);
String sha512Hex = DigestUtil.sha512Hex(passwordWithSalt);
String sha512Hex = sha512Hex(passwordWithSalt);
if (sha512Hex == null) {
throw new PlusoneException(ErrorCodeConsts.DEFAULT_ERROR_CODE, "未知错误:哈希加密失败!");
throw new BizException(ErrorCodeConsts.DEFAULT_ERROR_CODE, "未知错误:哈希加密失败!");
}
return sha512Hex;
}
@@ -42,11 +49,23 @@ public final class PasswordUtil {
* @return 生成的随机盐
*/
public static String generateRandomSalt() {
return RandomUtil.randomString(SALT_BASE_STRING, 24);
return RandomUtil.randomStr(SALT_BASE_CHAR_ARRAY, 24);
}
private PasswordUtil() {
// 不允许实例化
throw new IllegalStateException("Utility class");
}
private static String sha512Hex(String data) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
messageDigest.update(data.getBytes(StandardCharsets.UTF_8));
byte[] result = messageDigest.digest();
return new BigInteger(1, result).toString(16);
} catch (NoSuchAlgorithmException e) {
log.error("{}", e);
}
return null;
}
}

View File

@@ -27,7 +27,7 @@ public class AccountCreated extends DomainEvent {
public AccountCreated(Account account) {
this.username = account.getUsername();
this.email = account.getEmail();
this.mobilePhone = account.getMobilePhone();
this.email = account.getEmail().orElse(null);
this.mobilePhone = account.getMobilePhone().orElse(null);
}
}

View File

@@ -27,7 +27,7 @@ public class AccountLocked extends DomainEvent {
public AccountLocked(Account account) {
this.username = account.getUsername();
this.email = account.getEmail();
this.mobilePhone = account.getMobilePhone();
this.email = account.getEmail().orElse(null);
this.mobilePhone = account.getMobilePhone().orElse(null);
}
}

View File

@@ -27,8 +27,8 @@ public class AccountPasswordChanged extends DomainEvent {
public AccountPasswordChanged(Account account) {
this.username = account.getUsername();
this.email = account.getEmail();
this.mobilePhone = account.getMobilePhone();
this.email = account.getEmail().orElse(null);
this.mobilePhone = account.getMobilePhone().orElse(null);
}
}

View File

@@ -25,6 +25,6 @@ public class EmailChanged extends DomainEvent {
public EmailChanged(Account account) {
this.username = account.getUsername();
this.email = account.getEmail();
this.email = account.getEmail().orElse(null);
}
}

View File

@@ -25,6 +25,6 @@ public class MobilePhoneChanged extends DomainEvent {
public MobilePhoneChanged(Account account) {
this.username = account.getUsername();
this.mobilePhone = account.getMobilePhone();
this.mobilePhone = account.getMobilePhone().orElse(null);
}
}

View File

@@ -27,7 +27,7 @@ public class UsernameChanged extends DomainEvent {
public UsernameChanged(Account account) {
this.username = account.getUsername();
this.email = account.getEmail();
this.mobilePhone = account.getMobilePhone();
this.email = account.getEmail().orElse(null);
this.mobilePhone = account.getMobilePhone().orElse(null);
}
}

View File

@@ -5,8 +5,6 @@ import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import xyz.zhouxy.plusone.domain.AggregateRoot;
import xyz.zhouxy.plusone.domain.IWithVersion;
@@ -30,17 +28,17 @@ public class Account extends AggregateRoot<Long> implements IWithVersion {
// ===================== 字段 ====================
private Long id;
private @Getter Username username;
private @Getter Email email;
private @Getter MobilePhone mobilePhone;
private Username username;
private Email email;
private MobilePhone mobilePhone;
private Password password;
private @Getter AccountStatus status;
private @Getter AccountInfo accountInfo;
private AccountStatus status;
private AccountInfo accountInfo;
private Set<Long> roleRefs = new HashSet<>();
private @Getter Long createdBy;
private @Getter @Setter Long updatedBy;
private @Getter long version;
private Long createdBy;
private Long updatedBy;
private long version;
public void setUsername(Username username) {
this.username = username;
@@ -95,11 +93,19 @@ public class Account extends AggregateRoot<Long> implements IWithVersion {
}
public void setAccountInfo(Nickname nickname, URL avatar, Sex sex) {
this.accountInfo = AccountInfo.of(nickname, avatar, sex);
this.accountInfo = AccountInfo.builder()
.nickname(nickname)
.avatar(avatar)
.sex(sex)
.build();
}
public void setAccountInfo(String nickname, String avatar, Sex sex) {
this.accountInfo = AccountInfo.of(nickname, avatar, sex);
this.accountInfo = AccountInfo.builder()
.nickname(nickname)
.avatar(avatar)
.sex(sex)
.build();
}
/**
@@ -223,11 +229,48 @@ public class Account extends AggregateRoot<Long> implements IWithVersion {
return Optional.ofNullable(id);
}
public Set<Long> getRoleIds() {
return Set.copyOf(this.roleRefs);
public Username getUsername() {
return username;
}
public Optional<Email> getEmail() {
return Optional.ofNullable(email);
}
public Optional<MobilePhone> getMobilePhone() {
return Optional.ofNullable(mobilePhone);
}
Password getPassword() {
return password;
}
public AccountStatus getStatus() {
return status;
}
public AccountInfo getAccountInfo() {
return accountInfo;
}
public Set<Long> getRoleIds() {
return Set.copyOf(roleRefs);
}
public Optional<Long> getCreatedBy() {
return Optional.ofNullable(createdBy);
}
public Optional<Long> getUpdatedBy() {
return Optional.ofNullable(updatedBy);
}
public void setUpdatedBy(long updatedBy) {
this.updatedBy = updatedBy;
}
@Override
public long getVersion() {
return this.version;
}
}

View File

@@ -3,8 +3,11 @@ package xyz.zhouxy.plusone.system.domain.model.account;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import lombok.Getter;
import lombok.ToString;
import xyz.zhouxy.plusone.domain.IValueObject;
@@ -13,31 +16,77 @@ import xyz.zhouxy.plusone.domain.IValueObject;
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
@Getter
@ToString
public class AccountInfo implements IValueObject {
@Nullable
private final Nickname nickname;
@Nullable
private final URL avatar;
@Nonnull
private final Sex sex;
private AccountInfo(Nickname nickname, URL avatar, Sex sex) {
private AccountInfo(@Nullable Nickname nickname, @Nullable URL avatar, @Nullable Sex sex) {
this.nickname = nickname;
this.avatar = avatar;
this.sex = Objects.nonNull(sex) ? sex : Sex.UNSET;
}
public static AccountInfo of(Nickname nickname, URL avatar, Sex sex) {
return new AccountInfo(nickname, avatar, sex);
public Optional<Nickname> getNickname() {
return Optional.ofNullable(nickname);
}
public static AccountInfo of(String nickname, String avatar, Sex sex) {
URL avatarURL;
try {
avatarURL = Objects.nonNull(avatar) ? new URL(avatar) : null;
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
public Optional<URL> getAvatar() {
return Optional.ofNullable(avatar);
}
@Nonnull
public Sex getSex() {
return this.sex;
}
// Builder
public static Builder builder() {
return new Builder();
}
public static class Builder {
private Nickname nickname;
private URL avatar;
private Sex sex;
public Builder nickname(@Nullable Nickname nickname) {
this.nickname = nickname;
return this;
}
public Builder nickname(@Nullable String nickname) {
return nickname(Nickname.ofNullable(nickname));
}
public Builder avatar(@Nullable URL avatar) {
this.avatar = avatar;
return this;
}
public Builder avatar(@Nullable String avatar) {
URL avatarURL;
try {
avatarURL = Objects.nonNull(avatar) ? new URL(avatar) : null;
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
return avatar(avatarURL);
}
public Builder sex(@Nullable Sex sex) {
this.sex = sex;
return this;
}
public AccountInfo build() {
return new AccountInfo(this.nickname, this.avatar, this.sex);
}
return new AccountInfo(Nickname.ofNullable(nickname), avatarURL, sex);
}
}

View File

@@ -1,9 +1,12 @@
package xyz.zhouxy.plusone.system.domain.model.account;
import java.util.Collection;
import javax.annotation.Nonnull;
import lombok.Getter;
import xyz.zhouxy.plusone.commons.util.Enumeration;
import xyz.zhouxy.plusone.domain.IValueObject;
import xyz.zhouxy.plusone.util.Enumeration;
import xyz.zhouxy.plusone.util.EnumerationValuesHolder;
/**
* 账号状态
@@ -11,19 +14,24 @@ import xyz.zhouxy.plusone.util.EnumerationValuesHolder;
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
@Getter
public class AccountStatus extends Enumeration<AccountStatus> implements IValueObject {
public final class AccountStatus extends Enumeration<AccountStatus> implements IValueObject {
private AccountStatus(int value, String name) {
super(value, name);
private AccountStatus(int id, @Nonnull String name) {
super(id, name);
}
public static final AccountStatus AVAILABLE = new AccountStatus(0, "账号正常");
public static final AccountStatus LOCKED = new AccountStatus(1, "账号被锁定");
private static final EnumerationValuesHolder<AccountStatus> ENUMERATION_VALUES = new EnumerationValuesHolder<>(
new AccountStatus[] { AVAILABLE, LOCKED });
private static final ValueSet<AccountStatus> VALUE_SET = new ValueSet<>(
AVAILABLE,
LOCKED);
public static AccountStatus of(int value) {
return ENUMERATION_VALUES.get(value);
public static AccountStatus of(int id) {
return VALUE_SET.get(id);
}
public static Collection<AccountStatus> constants() {
return VALUE_SET.getValues();
}
}

View File

@@ -1,9 +1,10 @@
package xyz.zhouxy.plusone.system.domain.model.account;
import java.util.Objects;
import java.util.regex.Pattern;
import cn.hutool.core.util.DesensitizedUtil;
import xyz.zhouxy.plusone.constant.RegexConsts;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
/**
* 值对象:电子邮箱地址
@@ -12,7 +13,7 @@ import xyz.zhouxy.plusone.constant.RegexConsts;
*/
public class Email extends Principal {
public static final String REGEX = RegexConsts.EMAIL;
public static final Pattern REGEX = PatternConsts.EMAIL;
private Email(String email) {
super(REGEX);

View File

@@ -1,9 +1,10 @@
package xyz.zhouxy.plusone.system.domain.model.account;
import java.util.Objects;
import java.util.regex.Pattern;
import cn.hutool.core.util.DesensitizedUtil;
import xyz.zhouxy.plusone.constant.RegexConsts;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
/**
* 值对象:手机号码
@@ -12,7 +13,7 @@ import xyz.zhouxy.plusone.constant.RegexConsts;
*/
public class MobilePhone extends Principal {
public static final String REGEX = RegexConsts.MOBILE_PHONE;
public static final Pattern REGEX = PatternConsts.MOBILE_PHONE;
private MobilePhone(String mobilePhone) {
super(REGEX);

View File

@@ -1,8 +1,9 @@
package xyz.zhouxy.plusone.system.domain.model.account;
import java.util.Objects;
import java.util.regex.Pattern;
import xyz.zhouxy.plusone.constant.RegexConsts;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
import xyz.zhouxy.plusone.domain.ValidatableStringRecord;
/**
@@ -12,7 +13,7 @@ import xyz.zhouxy.plusone.domain.ValidatableStringRecord;
*/
public class Nickname extends ValidatableStringRecord {
public static final String REGEX = RegexConsts.NICKNAME;
public static final Pattern REGEX = PatternConsts.NICKNAME;
private Nickname(String value) {
super(REGEX);

View File

@@ -7,10 +7,10 @@ import javax.annotation.Nonnull;
import org.springframework.util.Assert;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
import xyz.zhouxy.plusone.constant.ErrorCodeConsts;
import xyz.zhouxy.plusone.constant.RegexConsts;
import xyz.zhouxy.plusone.domain.IValueObject;
import xyz.zhouxy.plusone.exception.PlusoneException;
import xyz.zhouxy.plusone.exception.BizException;
import xyz.zhouxy.plusone.system.util.PasswordUtil;
/**
@@ -20,7 +20,7 @@ import xyz.zhouxy.plusone.system.util.PasswordUtil;
*/
public class Password implements IValueObject {
private static final Pattern PATTERN = Pattern.compile(RegexConsts.PASSWORD);
private static final Pattern PATTERN = PatternConsts.PASSWORD;
private static final String DEFAULT_PASSWORD = "A1b2C3d4";
@Nonnull
@@ -37,7 +37,7 @@ public class Password implements IValueObject {
}
var salt = PasswordUtil.generateRandomSalt();
if (salt == null) {
throw new PlusoneException(ErrorCodeConsts.DEFAULT_ERROR_CODE, "未知错误:生成随机盐失败");
throw new BizException(ErrorCodeConsts.DEFAULT_ERROR_CODE, "未知错误:生成随机盐失败");
}
this.saltVal = salt;
this.passwordVal = PasswordUtil.hashPassword(password, salt);

View File

@@ -1,5 +1,7 @@
package xyz.zhouxy.plusone.system.domain.model.account;
import java.util.regex.Pattern;
import xyz.zhouxy.plusone.domain.ValidatableStringRecord;
/**
@@ -8,7 +10,7 @@ import xyz.zhouxy.plusone.domain.ValidatableStringRecord;
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
public abstract class Principal extends ValidatableStringRecord {
protected Principal(String format) {
protected Principal(Pattern format) {
super(format);
}
}

View File

@@ -1,30 +1,36 @@
package xyz.zhouxy.plusone.system.domain.model.account;
import java.util.Collection;
import javax.annotation.Nonnull;
import xyz.zhouxy.plusone.commons.util.Enumeration;
import xyz.zhouxy.plusone.domain.IValueObject;
import xyz.zhouxy.plusone.util.Enumeration;
import xyz.zhouxy.plusone.util.EnumerationValuesHolder;
/**
* 值对象:性别
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
public class Sex extends Enumeration<Sex> implements IValueObject {
public final class Sex extends Enumeration<Sex> implements IValueObject {
@Nonnull
public static final Sex UNSET = new Sex(0, "未设置");
@Nonnull
public static final Sex MALE = new Sex(1, "男性");
@Nonnull
public static final Sex FEMALE = new Sex(2, "女性");
private Sex(int value, String name) {
super(value, name);
private Sex(int id, @Nonnull String name) {
super(id, name);
}
private static EnumerationValuesHolder<Sex> values = new EnumerationValuesHolder<>(new Sex[] {
UNSET,
MALE,
FEMALE
});
private static final ValueSet<Sex> VALUE_SET = new ValueSet<>(UNSET, MALE, FEMALE);
public static Sex of(int value) {
return values.get(value);
return VALUE_SET.get(value);
}
public static Collection<Sex> constants() {
return VALUE_SET.getValues();
}
}

View File

@@ -1,6 +1,8 @@
package xyz.zhouxy.plusone.system.domain.model.account;
import xyz.zhouxy.plusone.constant.RegexConsts;
import java.util.regex.Pattern;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
/**
* 值对象:用户名
@@ -9,7 +11,7 @@ import xyz.zhouxy.plusone.constant.RegexConsts;
*/
public class Username extends Principal {
public static final String REGEX = RegexConsts.USERNAME;
public static final Pattern REGEX = PatternConsts.USERNAME;
private Username(String username) {
super(REGEX);

View File

@@ -6,7 +6,6 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.ToString;
import xyz.zhouxy.plusone.domain.AggregateRoot;
@@ -115,7 +114,7 @@ public class Dict extends AggregateRoot<Long> implements IWithLabel, IWithVersio
}
public Set<DictValue> getValues() {
return this.values.values().stream().collect(Collectors.toSet());
return Set.copyOf(this.values.values());
}
@Override

View File

@@ -4,7 +4,6 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.Nonnull;
@@ -15,6 +14,7 @@ import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import cn.hutool.core.util.IdUtil;
import static xyz.zhouxy.plusone.domain.ValidatableStringRecord.getValueOrNull;
import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport;
/**
@@ -94,25 +94,27 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
@Override
public boolean exists(Long id) {
return queryExists("SELECT EXISTS (SELECT 1 FROM sys_account WHERE id = :id AND deleted = 0 LIMIT 1)",
return queryForBool("SELECT EXISTS (SELECT 1 FROM sys_account WHERE id = :id AND deleted = 0 LIMIT 1)",
"id", id);
}
@Override
public boolean existsUsername(Username username) {
return queryExists("SELECT EXISTS (SELECT 1 FROM sys_account WHERE username = :username AND deleted = 0 LIMIT 1)",
return queryForBool(
"SELECT EXISTS (SELECT 1 FROM sys_account WHERE username = :username AND deleted = 0 LIMIT 1)",
"username", username.value());
}
@Override
public boolean existsEmail(Email email) {
return queryExists("SELECT EXISTS (SELECT 1 FROM sys_account WHERE email = :email AND deleted = 0 LIMIT 1)",
return queryForBool("SELECT EXISTS (SELECT 1 FROM sys_account WHERE email = :email AND deleted = 0 LIMIT 1)",
"email", email.value());
}
@Override
public boolean existsMobilePhone(MobilePhone mobilePhone) {
return queryExists("SELECT EXISTS (SELECT 1 FROM sys_account WHERE mobile_phone = :mobile_phone AND deleted = 0 LIMIT 1)",
return queryForBool(
"SELECT EXISTS (SELECT 1 FROM sys_account WHERE mobile_phone = :mobile_phone AND deleted = 0 LIMIT 1)",
"mobile_phone", mobilePhone.value());
}
@@ -174,10 +176,6 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
@Override
protected final Account mapRow(ResultSet rs) throws SQLException {
long accountId = rs.getLong("id");
AccountInfo accountInfo = AccountInfo.of(
rs.getString("nickname"),
rs.getString("avatar"),
Sex.of(rs.getInt("sex")));
return new Account(
accountId,
rs.getString("username"),
@@ -185,7 +183,11 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
rs.getString("mobile_phone"),
Password.of(rs.getString("password"), rs.getString("salt")),
AccountStatus.of(rs.getInt("status")),
accountInfo,
AccountInfo.builder()
.nickname(rs.getString("nickname"))
.avatar(rs.getString("avatar"))
.sex(Sex.of(rs.getInt("sex")))
.build(),
this.accountRoleDAO.selectRoleIdsByAccountId(accountId),
rs.getLong("created_by"),
rs.getLong("updated_by"),
@@ -198,17 +200,15 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
AccountInfo accountInfo = entity.getAccountInfo();
return new MapSqlParameterSource()
.addValue("id", id)
.addValue("email", Objects.nonNull(entity.getEmail()) ? entity.getEmail().value() : null)
.addValue("mobilePhone",
Objects.nonNull(entity.getMobilePhone()) ? entity.getMobilePhone().value() : null)
.addValue("email", getValueOrNull(entity.getEmail()))
.addValue("mobilePhone", getValueOrNull(entity.getMobilePhone()))
.addValue("username", entity.getUsername().value())
.addValue("password", entity.getPassword().value())
.addValue("salt", entity.getPassword().getSalt())
.addValue("avatar", accountInfo.getAvatar().toString())
.addValue("sex", accountInfo.getSex().getValue())
.addValue("nickname",
Objects.nonNull(accountInfo.getNickname()) ? accountInfo.getNickname().value() : null)
.addValue("status", entity.getStatus().getValue())
.addValue("sex", accountInfo.getSex().getId())
.addValue("nickname", getValueOrNull(accountInfo.getNickname()))
.addValue("status", entity.getStatus().getId())
.addValue("createdBy", entity.getCreatedBy())
.addValue("createTime", now)
.addValue("updatedBy", entity.getUpdatedBy())

View File

@@ -31,7 +31,7 @@ class AccountRoleRefDAO extends PlusoneJdbcDaoSupport {
}
void insertAccountRoleRefs(Long accountId, Set<Long> roleRefs) {
int i = batchUpdate(
long i = batchUpdate(
"INSERT INTO sys_account_role (account_id, role_id) VALUES (:accountId, :roleId)",
roleRefs,
(Long roleId) -> new MapSqlParameterSource()

View File

@@ -83,7 +83,7 @@ public class DictRepositoryImpl extends JdbcRepositorySupport<Dict, Long> implem
@Override
public boolean exists(Long id) {
return queryExists("SELECT EXISTS (SELECT 1 FROM sys_dict_type WHERE id = :id AND deleted = 0 LIMIT 1)",
return queryForBool("SELECT EXISTS (SELECT 1 FROM sys_dict_type WHERE id = :id AND deleted = 0 LIMIT 1)",
new MapSqlParameterSource("id", id));
}

View File

@@ -21,11 +21,11 @@ class DictValueDAO extends PlusoneJdbcDaoSupport {
void updateDictValues(Dict entity) {
update("DELETE FROM sys_dict_value WHERE dict_type = :dictType",
"dictType", entity.getId().orElseThrow());
int i = insertDictValues(entity.getId().orElseThrow(), entity);
long i = insertDictValues(entity.getId().orElseThrow(), entity);
assertResultEquals(i, entity.count());
}
int insertDictValues(Long dictId, Dict entity) {
long insertDictValues(Long dictId, Dict entity) {
if (Objects.isNull(dictId) || Objects.isNull(entity) || CollectionUtils.isEmpty(entity.getValues())) {
return 0;
}

View File

@@ -8,7 +8,6 @@ import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
@@ -16,6 +15,7 @@ import javax.annotation.Nonnull;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.util.CollectionUtils;
import cn.hutool.core.util.IdUtil;
import xyz.zhouxy.plusone.jdbc.JdbcEntityDaoSupport;
@@ -80,7 +80,7 @@ class ActionDAO extends JdbcEntityDaoSupport<Action, Long> {
}
Collection<Action> selectActionsByIdIn(Collection<Long> actionIds) {
if (Objects.isNull(actionIds) || actionIds.isEmpty()) {
if (CollectionUtils.isEmpty(actionIds)) {
return Collections.emptyList();
}
return queryForList("""

View File

@@ -7,7 +7,6 @@ import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.Nonnull;
@@ -15,14 +14,15 @@ import javax.annotation.Nonnull;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import cn.hutool.core.util.IdUtil;
import xyz.zhouxy.plusone.commons.util.EnumUtil;
import xyz.zhouxy.plusone.constant.EntityStatus;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport;
import xyz.zhouxy.plusone.system.domain.model.menu.Menu.MenuType;
import xyz.zhouxy.plusone.util.AssertResult;
import xyz.zhouxy.plusone.util.EnumUtil;
/**
* MenuRepository 实现类
@@ -115,13 +115,13 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
@Override
public boolean exists(Long id) {
return queryExists("SELECT EXISTS (SELECT 1 FROM sys_menu WHERE id = :id AND deleted = 0 LIMIT 1)",
return queryForBool("SELECT EXISTS (SELECT 1 FROM sys_menu WHERE id = :id AND deleted = 0 LIMIT 1)",
"id", id);
}
@Override
public Collection<Menu> findByIdIn(Collection<Long> ids) {
if (Objects.isNull(ids) || ids.isEmpty()) {
if (CollectionUtils.isEmpty(ids)) {
return Collections.emptyList();
}
return queryForList("""
@@ -188,7 +188,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
.addValue("icon", entity.getIcon())
.addValue("hidden", entity.isHidden())
.addValue("orderNumber", entity.getOrderNumber())
.addValue("status", entity.getStatus().getValue())
.addValue("status", entity.getStatus().getId())
.addValue("remarks", entity.getRemarks())
.addValue("component", entity.getComponent())
.addValue("cache", entity.getCache())

View File

@@ -29,7 +29,7 @@ class RoleMenuRefDAO extends PlusoneJdbcDaoSupport {
}
void saveRoleMenuRefs(Long roleId, Role entity) {
int i = batchUpdate(
long i = batchUpdate(
"INSERT INTO sys_role_menu(role_id, menu_id) VALUES (:roleId, :menuId)",
entity.getMenus(),
menuRef -> new MapSqlParameterSource()

View File

@@ -30,7 +30,7 @@ class RolePermissionRefDAO extends PlusoneJdbcDaoSupport {
}
void saveRolePermissionRefs(Long roleId, Role entity) {
int i = batchUpdate(
long i = batchUpdate(
"INSERT INTO sys_role_permission(role_id, permission_id) VALUES (:roleId, :permissionId)",
entity.getPermissions(),
actionRef -> new MapSqlParameterSource()

View File

@@ -60,7 +60,7 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport<Role, Long> implem
@Override
public boolean exists(Long id) {
return queryExists("SELECT EXISTS (SELECT 1 FROM sys_role WHERE id = :id AND deleted = 0 LIMIT 1)",
return queryForBool("SELECT EXISTS (SELECT 1 FROM sys_role WHERE id = :id AND deleted = 0 LIMIT 1)",
"id", id);
}
@@ -135,7 +135,7 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport<Role, Long> implem
.addValue("id", id)
.addValue("name", entity.getName())
.addValue("identifier", entity.getIdentifier())
.addValue("status", entity.getStatus().getValue())
.addValue("status", entity.getStatus().getId())
.addValue("remarks", entity.getRemarks())
.addValue("createTime", now)
.addValue("createdBy", loginId)

View File

@@ -7,9 +7,7 @@
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>xyz.zhouxy</groupId>
<artifactId>plusone-system</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>plusone-system</name>
@@ -69,6 +67,10 @@
</dependencyManagement>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>

30
pom.xml
View File

@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
@@ -26,15 +25,15 @@
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<spring-boot.version>2.7.8</spring-boot.version>
<spring-boot.version>2.7.10</spring-boot.version>
<sa-token.version>1.34.0</sa-token.version>
<hutool.version>5.8.11</hutool.version>
<hutool.version>5.8.16</hutool.version>
<mybatis-starter.version>3.0.1</mybatis-starter.version>
<mybatis-plus.version>3.5.3.1</mybatis-plus.version>
<commons-io.version>2.11.0</commons-io.version>
<mica.version>2.7.5</mica.version>
<mapstruct.version>1.5.3.Final</mapstruct.version>
<guava.version>31.1-jre</guava.version>
<guava.version>30.1-jre</guava.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
<commons-collections4.version>4.4</commons-collections4.version>
<commons-math3.version>3.6.1</commons-math3.version>
@@ -87,26 +86,12 @@
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-crypto</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-captcha</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
@@ -193,9 +178,8 @@
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
</dependencies>