diff --git a/.cursor/rules/zh.mdc b/.cursor/rules/zh.mdc index dc88b4a..a76cef1 100644 --- a/.cursor/rules/zh.mdc +++ b/.cursor/rules/zh.mdc @@ -1,5 +1,4 @@ --- -description: -globs: 中文回答我 -alwaysApply: true +alwaysApply: false --- +中文回答我 \ No newline at end of file diff --git a/docs/API文档.md b/docs/API文档.md new file mode 100644 index 0000000..c4ca9d8 --- /dev/null +++ b/docs/API文档.md @@ -0,0 +1,199 @@ +# API 文档 + +## 用户管理接口 + +### 创建用户账户 + +#### 管理员创建用户账户 + +**接口地址:** `POST /api/admin/accounts` + +**请求头:** +``` +Content-Type: application/json +Authorization: Bearer {token} +``` + +**请求参数:** +```json +{ + "userType": "ADMIN", // 必填,用户类型:ADMIN 或 AGENT + "username": "newuser", // 必填,用户名,3-64字符,只能包含字母、数字、下划线 + "password": "123456", // 必填,密码,6-128字符 + "status": "ENABLED", // 可选,状态:ENABLED 或 DISABLED,默认ENABLED + "pointsBalance": 0 // 可选,积分余额(仅AGENT类型),默认0 +} +``` + +**成功响应(200):** +```json +{ + "id": 2, + "userType": "ADMIN", + "username": "newuser", + "status": "ENABLED", + "pointsBalance": 0, + "createdAt": "2025-08-24T18:30:00.000", + "updatedAt": "2025-08-24T18:30:00.000" +} +``` + +**错误响应:** + +**400 Bad Request - 参数验证失败:** +```json +{ + "timestamp": "2025-08-24T18:30:00.000", + "status": 400, + "error": "Bad Request", + "message": "Validation failed", + "details": [ + { + "field": "username", + "message": "用户名长度必须在3-64字符之间" + }, + { + "field": "password", + "message": "密码长度必须在6-128字符之间" + } + ] +} +``` + +**409 Conflict - 用户名已存在:** +```json +{ + "timestamp": "2025-08-24T18:30:00.000", + "status": 409, + "error": "Conflict", + "message": "用户名已存在" +} +``` + +**401 Unauthorized - 未授权:** +```json +{ + "timestamp": "2025-08-24T18:30:00.000", + "status": 401, + "error": "Unauthorized", + "message": "访问被拒绝" +} +``` + +**403 Forbidden - 权限不足:** +```json +{ + "timestamp": "2025-08-24T18:30:00.000", + "status": 403, + "error": "Forbidden", + "message": "权限不足,无法创建用户" +} +``` + +#### 用户自注册接口 + +**接口地址:** `POST /api/users` + +**请求头:** +``` +Content-Type: application/json +``` + +**请求参数:** +```json +{ + "userType": "AGENT", // 必填,用户类型:只能为 AGENT + "username": "newagent", // 必填,用户名,3-64字符 + "password": "123456", // 必填,密码,6-128字符 + "pointsBalance": 0 // 可选,积分余额,默认0 +} +``` + +**成功响应(201):** +```json +{ + "id": 3, + "userType": "AGENT", + "username": "newagent", + "status": "ENABLED", + "pointsBalance": 0, + "createdAt": "2025-08-24T18:30:00.000", + "updatedAt": "2025-08-24T18:30:00.000" +} +``` + +### 接口说明 + +#### 权限要求 +- **管理员接口** (`/api/admin/accounts`):需要管理员权限,可以创建ADMIN和AGENT类型用户 +- **用户接口** (`/api/users`):公开接口,只能创建AGENT类型用户 + +#### 参数说明 + +**userType(用户类型):** +- `ADMIN`:管理员用户 +- `AGENT`:代理用户 + +**username(用户名):** +- 长度:3-64字符 +- 格式:只能包含字母、数字、下划线 +- 唯一性:系统内必须唯一 + +**password(密码):** +- 长度:6-128字符 +- 存储:使用BCrypt加密存储 +- 安全:建议包含大小写字母、数字和特殊字符 + +**status(状态):** +- 可选值:`ENABLED`(启用)、`DISABLED`(禁用) +- 默认值:`ENABLED` + +**pointsBalance(积分余额):** +- 仅AGENT类型用户可以设置 +- 类型:整数,不能为负数 +- 默认值:0 + +#### 业务规则 +1. 用户名在系统内必须唯一 +2. 密码使用BCrypt加密存储,无法解密 +3. 创建成功后账户默认状态为ENABLED +4. 只有管理员可以创建ADMIN类型用户 +5. 简化的用户模型,去除了角色和显示名称等复杂字段 + +#### 使用示例 + +**创建管理员用户:** +```bash +curl -X POST http://localhost:8080/api/admin/accounts \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer your-token" \ + -d '{ + "userType": "ADMIN", + "username": "admin001", + "password": "Admin123!" + }' +``` + +**创建代理用户:** +```bash +curl -X POST http://localhost:8080/api/admin/accounts \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer your-token" \ + -d '{ + "userType": "AGENT", + "username": "agent001", + "password": "Agent123!", + "pointsBalance": 1000 + }' +``` + +**用户自注册:** +```bash +curl -X POST http://localhost:8080/api/users \ + -H "Content-Type: application/json" \ + -d '{ + "userType": "AGENT", + "username": "newuser", + "password": "User123!" + }' +``` diff --git a/src/main/java/com/gameplatform/server/model/dto/account/AccountCreateRequest.java b/src/main/java/com/gameplatform/server/model/dto/account/AccountCreateRequest.java index 5faf5ed..f889b36 100644 --- a/src/main/java/com/gameplatform/server/model/dto/account/AccountCreateRequest.java +++ b/src/main/java/com/gameplatform/server/model/dto/account/AccountCreateRequest.java @@ -8,13 +8,11 @@ public class AccountCreateRequest { @NotBlank @Size(min = 3, max = 64) private String username; - @Size(max = 100) - private String displayName; - private String role; // for ADMIN: SUPER | ADMIN + private String status = "ENABLED"; // ENABLED | DISABLED @NotBlank @Size(min = 6, max = 128) - private String initialPassword; + private String password; @Min(0) private Long pointsBalance = 0L; // for AGENT @@ -22,14 +20,11 @@ public class AccountCreateRequest { public void setUserType(String userType) { this.userType = userType; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } - public String getDisplayName() { return displayName; } - public void setDisplayName(String displayName) { this.displayName = displayName; } - public String getRole() { return role; } - public void setRole(String role) { this.role = role; } + public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } - public String getInitialPassword() { return initialPassword; } - public void setInitialPassword(String initialPassword) { this.initialPassword = initialPassword; } + public String getPassword() { return password; } + public void setPassword(String password) { this.password = password; } public Long getPointsBalance() { return pointsBalance; } public void setPointsBalance(Long pointsBalance) { this.pointsBalance = pointsBalance; } } diff --git a/src/main/java/com/gameplatform/server/model/dto/account/AccountResponse.java b/src/main/java/com/gameplatform/server/model/dto/account/AccountResponse.java index 5696a99..5969097 100644 --- a/src/main/java/com/gameplatform/server/model/dto/account/AccountResponse.java +++ b/src/main/java/com/gameplatform/server/model/dto/account/AccountResponse.java @@ -6,8 +6,7 @@ public class AccountResponse { private Long id; private String userType; private String username; - private String displayName; - private String role; + private String status; private Long pointsBalance; private LocalDateTime createdAt; @@ -19,10 +18,7 @@ public class AccountResponse { public void setUserType(String userType) { this.userType = userType; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } - public String getDisplayName() { return displayName; } - public void setDisplayName(String displayName) { this.displayName = displayName; } - public String getRole() { return role; } - public void setRole(String role) { this.role = role; } + public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public Long getPointsBalance() { return pointsBalance; } diff --git a/src/main/java/com/gameplatform/server/model/dto/account/AccountUpdateRequest.java b/src/main/java/com/gameplatform/server/model/dto/account/AccountUpdateRequest.java index 8176cc0..f5defd6 100644 --- a/src/main/java/com/gameplatform/server/model/dto/account/AccountUpdateRequest.java +++ b/src/main/java/com/gameplatform/server/model/dto/account/AccountUpdateRequest.java @@ -3,15 +3,10 @@ package com.gameplatform.server.model.dto.account; import jakarta.validation.constraints.Size; public class AccountUpdateRequest { - @Size(max = 100) - private String displayName; - private String role; // SUPER | ADMIN (only for ADMIN) + private String status; // ENABLED | DISABLED - public String getDisplayName() { return displayName; } - public void setDisplayName(String displayName) { this.displayName = displayName; } - public String getRole() { return role; } - public void setRole(String role) { this.role = role; } + public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } } diff --git a/src/main/java/com/gameplatform/server/model/entity/account/UserAccount.java b/src/main/java/com/gameplatform/server/model/entity/account/UserAccount.java index d5fa3ff..2323624 100644 --- a/src/main/java/com/gameplatform/server/model/entity/account/UserAccount.java +++ b/src/main/java/com/gameplatform/server/model/entity/account/UserAccount.java @@ -6,9 +6,9 @@ public class UserAccount { private Long id; private String userType; // ADMIN | AGENT private String username; // 登录名(admin/agent 共用) - private String displayName; // 显示名称(agent 可用) + private String passwordHash; // BCrypt 或 PLAIN:xxx(初始化用) - private String role; // 仅 ADMIN 使用:SUPER / ADMIN + private String status; // ENABLED / DISABLED private Long pointsBalance; // 仅 AGENT 使用 private LocalDateTime createdAt; @@ -20,12 +20,10 @@ public class UserAccount { public void setUserType(String userType) { this.userType = userType; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } - public String getDisplayName() { return displayName; } - public void setDisplayName(String displayName) { this.displayName = displayName; } + public String getPasswordHash() { return passwordHash; } public void setPasswordHash(String passwordHash) { this.passwordHash = passwordHash; } - public String getRole() { return role; } - public void setRole(String role) { this.role = role; } + public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public Long getPointsBalance() { return pointsBalance; } diff --git a/src/main/java/com/gameplatform/server/service/account/AccountService.java b/src/main/java/com/gameplatform/server/service/account/AccountService.java index b80f322..bd9eb7d 100644 --- a/src/main/java/com/gameplatform/server/service/account/AccountService.java +++ b/src/main/java/com/gameplatform/server/service/account/AccountService.java @@ -25,14 +25,14 @@ public class AccountService { this.passwordEncoder = passwordEncoder; } - public Mono> list(String userType, String status, String role, String keyword, + public Mono> list(String userType, String status, String keyword, Integer page, Integer size) { int p = (page == null || page < 1) ? 1 : page; int s = (size == null || size < 1 || size > 200) ? 20 : size; int offset = (p - 1) * s; return Mono.fromCallable(() -> { - long total = mapper.countByFilter(userType, status, role, keyword); - List list = mapper.listByFilter(userType, status, role, keyword, s, offset); + long total = mapper.countByFilter(userType, status, null, keyword); + List list = mapper.listByFilter(userType, status, null, keyword, s, offset); List items = list.stream().map(this::toResp).collect(Collectors.toList()); return new PageResult<>(items, total, p, s); }) @@ -54,16 +54,13 @@ public class AccountService { UserAccount acc = new UserAccount(); acc.setUserType(type); acc.setUsername(req.getUsername()); - acc.setDisplayName(req.getDisplayName()); acc.setStatus(req.getStatus() == null ? "ENABLED" : req.getStatus()); if ("ADMIN".equals(type)) { - acc.setRole(req.getRole() == null ? "ADMIN" : req.getRole()); acc.setPointsBalance(0L); } else { - acc.setRole(null); acc.setPointsBalance(req.getPointsBalance() == null ? 0L : req.getPointsBalance()); } - acc.setPasswordHash(passwordEncoder.encode(req.getInitialPassword())); + acc.setPasswordHash(passwordEncoder.encode(req.getPassword())); mapper.insert(acc); return toResp(acc); }) @@ -83,11 +80,7 @@ public class AccountService { if (db == null) return null; UserAccount patch = new UserAccount(); patch.setId(id); - patch.setDisplayName(req.getDisplayName()); - // Only ADMIN account may set role; AGENT's role must remain null - if ("ADMIN".equalsIgnoreCase(db.getUserType())) { - patch.setRole(req.getRole()); - } + patch.setStatus(req.getStatus()); mapper.update(patch); return mapper.findById(id); @@ -119,8 +112,7 @@ public class AccountService { r.setId(a.getId()); r.setUserType(a.getUserType()); r.setUsername(a.getUsername()); - r.setDisplayName(a.getDisplayName()); - r.setRole(a.getRole()); + r.setStatus(a.getStatus()); r.setPointsBalance(a.getPointsBalance()); r.setCreatedAt(a.getCreatedAt()); diff --git a/target/classes/com/gameplatform/server/GamePlatformServerApplication.class b/target/classes/com/gameplatform/server/GamePlatformServerApplication.class index 48d8854..91b7441 100644 Binary files a/target/classes/com/gameplatform/server/GamePlatformServerApplication.class and b/target/classes/com/gameplatform/server/GamePlatformServerApplication.class differ diff --git a/target/classes/com/gameplatform/server/config/CorsConfig.class b/target/classes/com/gameplatform/server/config/CorsConfig.class index 6faec7c..9b5d160 100644 Binary files a/target/classes/com/gameplatform/server/config/CorsConfig.class and b/target/classes/com/gameplatform/server/config/CorsConfig.class differ diff --git a/target/classes/com/gameplatform/server/controller/UserController.class b/target/classes/com/gameplatform/server/controller/UserController.class index d7d9d2e..d4181f3 100644 Binary files a/target/classes/com/gameplatform/server/controller/UserController.class and b/target/classes/com/gameplatform/server/controller/UserController.class differ diff --git a/target/classes/com/gameplatform/server/controller/admin/AccountController.class b/target/classes/com/gameplatform/server/controller/admin/AccountController.class index ce66e17..c14b1fe 100644 Binary files a/target/classes/com/gameplatform/server/controller/admin/AccountController.class and b/target/classes/com/gameplatform/server/controller/admin/AccountController.class differ diff --git a/target/classes/com/gameplatform/server/controller/auth/AuthController$1.class b/target/classes/com/gameplatform/server/controller/auth/AuthController$1.class index f1dcb43..542d1f9 100644 Binary files a/target/classes/com/gameplatform/server/controller/auth/AuthController$1.class and b/target/classes/com/gameplatform/server/controller/auth/AuthController$1.class differ diff --git a/target/classes/com/gameplatform/server/controller/auth/AuthController.class b/target/classes/com/gameplatform/server/controller/auth/AuthController.class index bde12b3..da79dfb 100644 Binary files a/target/classes/com/gameplatform/server/controller/auth/AuthController.class and b/target/classes/com/gameplatform/server/controller/auth/AuthController.class differ diff --git a/target/classes/com/gameplatform/server/exception/GlobalExceptionHandler$1.class b/target/classes/com/gameplatform/server/exception/GlobalExceptionHandler$1.class index 35b664e..315d986 100644 Binary files a/target/classes/com/gameplatform/server/exception/GlobalExceptionHandler$1.class and b/target/classes/com/gameplatform/server/exception/GlobalExceptionHandler$1.class differ diff --git a/target/classes/com/gameplatform/server/exception/GlobalExceptionHandler$2.class b/target/classes/com/gameplatform/server/exception/GlobalExceptionHandler$2.class index 3d9a1c2..88214a0 100644 Binary files a/target/classes/com/gameplatform/server/exception/GlobalExceptionHandler$2.class and b/target/classes/com/gameplatform/server/exception/GlobalExceptionHandler$2.class differ diff --git a/target/classes/com/gameplatform/server/exception/GlobalExceptionHandler.class b/target/classes/com/gameplatform/server/exception/GlobalExceptionHandler.class index 3b797d3..3ae9d57 100644 Binary files a/target/classes/com/gameplatform/server/exception/GlobalExceptionHandler.class and b/target/classes/com/gameplatform/server/exception/GlobalExceptionHandler.class differ diff --git a/target/classes/com/gameplatform/server/model/dto/account/AccountCreateRequest.class b/target/classes/com/gameplatform/server/model/dto/account/AccountCreateRequest.class index 0ab009d..dbdd913 100644 Binary files a/target/classes/com/gameplatform/server/model/dto/account/AccountCreateRequest.class and b/target/classes/com/gameplatform/server/model/dto/account/AccountCreateRequest.class differ diff --git a/target/classes/com/gameplatform/server/model/dto/account/AccountResponse.class b/target/classes/com/gameplatform/server/model/dto/account/AccountResponse.class index ff99eb5..a479957 100644 Binary files a/target/classes/com/gameplatform/server/model/dto/account/AccountResponse.class and b/target/classes/com/gameplatform/server/model/dto/account/AccountResponse.class differ diff --git a/target/classes/com/gameplatform/server/model/dto/account/AccountUpdateRequest.class b/target/classes/com/gameplatform/server/model/dto/account/AccountUpdateRequest.class index e5f613d..390773e 100644 Binary files a/target/classes/com/gameplatform/server/model/dto/account/AccountUpdateRequest.class and b/target/classes/com/gameplatform/server/model/dto/account/AccountUpdateRequest.class differ diff --git a/target/classes/com/gameplatform/server/model/dto/account/ResetPasswordRequest.class b/target/classes/com/gameplatform/server/model/dto/account/ResetPasswordRequest.class index 695cdef..4572e92 100644 Binary files a/target/classes/com/gameplatform/server/model/dto/account/ResetPasswordRequest.class and b/target/classes/com/gameplatform/server/model/dto/account/ResetPasswordRequest.class differ diff --git a/target/classes/com/gameplatform/server/model/dto/auth/LoginRequest.class b/target/classes/com/gameplatform/server/model/dto/auth/LoginRequest.class index 416d507..bf762f2 100644 Binary files a/target/classes/com/gameplatform/server/model/dto/auth/LoginRequest.class and b/target/classes/com/gameplatform/server/model/dto/auth/LoginRequest.class differ diff --git a/target/classes/com/gameplatform/server/model/dto/auth/LoginResponse.class b/target/classes/com/gameplatform/server/model/dto/auth/LoginResponse.class index 2cb34ad..2557f1e 100644 Binary files a/target/classes/com/gameplatform/server/model/dto/auth/LoginResponse.class and b/target/classes/com/gameplatform/server/model/dto/auth/LoginResponse.class differ diff --git a/target/classes/com/gameplatform/server/model/dto/common/PageResult.class b/target/classes/com/gameplatform/server/model/dto/common/PageResult.class index 5a381ba..e70f530 100644 Binary files a/target/classes/com/gameplatform/server/model/dto/common/PageResult.class and b/target/classes/com/gameplatform/server/model/dto/common/PageResult.class differ diff --git a/target/classes/com/gameplatform/server/model/entity/account/UserAccount.class b/target/classes/com/gameplatform/server/model/entity/account/UserAccount.class index 0ecf4eb..3a5f264 100644 Binary files a/target/classes/com/gameplatform/server/model/entity/account/UserAccount.class and b/target/classes/com/gameplatform/server/model/entity/account/UserAccount.class differ diff --git a/target/classes/com/gameplatform/server/security/JwtService.class b/target/classes/com/gameplatform/server/security/JwtService.class index b6a4e1c..eadb048 100644 Binary files a/target/classes/com/gameplatform/server/security/JwtService.class and b/target/classes/com/gameplatform/server/security/JwtService.class differ diff --git a/target/classes/com/gameplatform/server/security/SecurityConfig.class b/target/classes/com/gameplatform/server/security/SecurityConfig.class index b167e2b..6242344 100644 Binary files a/target/classes/com/gameplatform/server/security/SecurityConfig.class and b/target/classes/com/gameplatform/server/security/SecurityConfig.class differ diff --git a/target/classes/com/gameplatform/server/service/UserService.class b/target/classes/com/gameplatform/server/service/UserService.class index 9daad0a..13db20a 100644 Binary files a/target/classes/com/gameplatform/server/service/UserService.class and b/target/classes/com/gameplatform/server/service/UserService.class differ diff --git a/target/classes/com/gameplatform/server/service/account/AccountService.class b/target/classes/com/gameplatform/server/service/account/AccountService.class index 2a0b690..bccc27d 100644 Binary files a/target/classes/com/gameplatform/server/service/account/AccountService.class and b/target/classes/com/gameplatform/server/service/account/AccountService.class differ diff --git a/target/classes/com/gameplatform/server/service/auth/AuthService.class b/target/classes/com/gameplatform/server/service/auth/AuthService.class index fddf891..c72d3a1 100644 Binary files a/target/classes/com/gameplatform/server/service/auth/AuthService.class and b/target/classes/com/gameplatform/server/service/auth/AuthService.class differ diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst new file mode 100644 index 0000000..e69de29 diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 0000000..7916b02 --- /dev/null +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1,20 @@ +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\GamePlatformServerApplication.java +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\security\SecurityConfig.java +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\model\dto\account\ResetPasswordRequest.java +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\service\account\AccountService.java +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\service\auth\AuthService.java +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\model\dto\account\AccountUpdateRequest.java +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\model\dto\auth\LoginRequest.java +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\controller\admin\AccountController.java +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\model\dto\auth\LoginResponse.java +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\controller\UserController.java +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\model\dto\account\AccountResponse.java +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\service\UserService.java +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\controller\auth\AuthController.java +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\security\JwtService.java +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\config\CorsConfig.java +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\exception\GlobalExceptionHandler.java +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\model\dto\common\PageResult.java +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\mapper\account\UserAccountMapper.java +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\model\dto\account\AccountCreateRequest.java +D:\project\gamePlatform\server\src\main\java\com\gameplatform\server\model\entity\account\UserAccount.java