diff --git a/open-isle-cli/src/views/SettingsPageView.vue b/open-isle-cli/src/views/SettingsPageView.vue
index 0839725f4..5a6357151 100644
--- a/open-isle-cli/src/views/SettingsPageView.vue
+++ b/open-isle-cli/src/views/SettingsPageView.vue
@@ -12,8 +12,9 @@
@@ -50,6 +51,7 @@ export default {
return {
username: '',
introduction: '',
+ usernameError: '',
avatar: '',
avatarFile: null,
role: '',
@@ -103,6 +105,16 @@ export default {
},
async save() {
const token = getToken()
+ this.usernameError = ''
+ if (!this.username) {
+ this.usernameError = '用户名不能为空'
+ } else if (this.username.length < 6) {
+ this.usernameError = '用户名至少6位'
+ }
+ if (this.usernameError) {
+ toast.error(this.usernameError)
+ return
+ }
if (this.avatarFile) {
const form = new FormData()
form.append('file', this.avatarFile)
@@ -221,4 +233,12 @@ export default {
.avatar-container:hover .avatar-overlay {
opacity: 1;
}
+
+.error-message {
+ color: red;
+ font-size: 14px;
+ width: calc(100% - 40px);
+ margin-top: -10px;
+ margin-bottom: 10px;
+}
diff --git a/open-isle-cli/src/views/SignupPageView.vue b/open-isle-cli/src/views/SignupPageView.vue
index beee9842a..c0afa8036 100644
--- a/open-isle-cli/src/views/SignupPageView.vue
+++ b/open-isle-cli/src/views/SignupPageView.vue
@@ -120,6 +120,8 @@ export default {
}
if (!this.username) {
this.usernameError = '用户名不能为空'
+ } else if (this.username.length < 6) {
+ this.usernameError = '用户名至少6位'
}
if (this.emailError || this.passwordError || this.usernameError) {
return
diff --git a/src/main/java/com/openisle/service/UserService.java b/src/main/java/com/openisle/service/UserService.java
index 06ff4294e..e874ec647 100644
--- a/src/main/java/com/openisle/service/UserService.java
+++ b/src/main/java/com/openisle/service/UserService.java
@@ -3,6 +3,7 @@ package com.openisle.service;
import com.openisle.model.User;
import com.openisle.model.Role;
import com.openisle.service.PasswordValidator;
+import com.openisle.service.UsernameValidator;
import com.openisle.exception.FieldException;
import com.openisle.repository.UserRepository;
import lombok.RequiredArgsConstructor;
@@ -18,9 +19,11 @@ import java.util.Random;
public class UserService {
private final UserRepository userRepository;
private final PasswordValidator passwordValidator;
+ private final UsernameValidator usernameValidator;
private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
public User register(String username, String email, String password) {
+ usernameValidator.validate(username);
passwordValidator.validate(password);
// ── 先按用户名查 ──────────────────────────────────────────
Optional byUsername = userRepository.findByUsername(username);
@@ -99,6 +102,7 @@ public class UserService {
User user = userRepository.findByUsername(currentUsername)
.orElseThrow(() -> new IllegalArgumentException("User not found"));
if (newUsername != null && !newUsername.equals(currentUsername)) {
+ usernameValidator.validate(newUsername);
userRepository.findByUsername(newUsername).ifPresent(u -> {
throw new FieldException("username", "User name already exists");
});
diff --git a/src/main/java/com/openisle/service/UsernameValidator.java b/src/main/java/com/openisle/service/UsernameValidator.java
new file mode 100644
index 000000000..5db4c5c6c
--- /dev/null
+++ b/src/main/java/com/openisle/service/UsernameValidator.java
@@ -0,0 +1,22 @@
+package com.openisle.service;
+
+import com.openisle.exception.FieldException;
+import org.springframework.stereotype.Service;
+
+/**
+ * Simple validator for usernames.
+ */
+@Service
+public class UsernameValidator {
+ /**
+ * Validate the username string.
+ *
+ * @param username the username to validate
+ */
+ public void validate(String username) {
+ if (username == null || username.length() < 6) {
+ throw new FieldException("username", "Username must be at least 6 characters long");
+ }
+ }
+}
+