feat: 完成用户管理接口核心CRUD功能

- 实现完整的用户创建逻辑,包含唯一性验证和密码加密
- 实现用户详情查询,安全过滤密码字段
- 实现用户信息更新,支持部分字段更新和唯一性验证
- 实现用户删除功能,包含存在性检查和日志记录
- 创建完整的API测试用例,覆盖正常和错误场景
- 更新任务进度文档,标记第10个任务为已完成
- 提升项目整体完成率至35%,中优先级任务完成率至25%
This commit is contained in:
xujiang
2025-07-11 13:01:50 +08:00
parent 494d98bee5
commit d47e55d5fb
6 changed files with 413 additions and 18 deletions

View File

@ -6,14 +6,14 @@
## 📊 总体进度概览
- **总任务数**: 40 (细化拆分后)
- **已完成**: 13
- **已完成**: 14
- **进行中**: 0 🔄
- **待开始**: 27
- **完成率**: 32.5%
- **待开始**: 26
- **完成率**: 35.0%
### 📈 任务分布
- **高优先级**: 9/9 (100% 完成) ✅
- **中优先级**: 4/20 (20% 完成) 📈
- **中优先级**: 5/20 (25% 完成) 📈
- **低优先级**: 0/11 (等待开始) ⏳
---
@ -161,10 +161,33 @@
## 📋 中优先级任务 (20/26) - 细化拆分
### 🔧 后端功能完善 (8项)
#### 10. 完善用户管理接口
**优先级**: 中 🔥
**预估工作量**: 0.5天
**具体任务**: 实现用户列表查询、用户信息更新、用户状态管理接口
#### 10. 完善用户管理接口
**状态**: 已完成 ✅
**完成时间**: 2025-07-11
**完成内容**:
- 实现完整的用户创建逻辑 (`createUserLogic.go`)
- 用户名和邮箱唯一性验证
- bcrypt 密码加密
- 默认状态设置和时间戳
- 安全的用户信息返回 (不含密码)
- 实现用户详情查询逻辑 (`getUserLogic.go`)
- 根据ID查询用户信息
- 用户不存在的错误处理
- 密码字段过滤保护
- 实现用户信息更新逻辑 (`updateUserLogic.go`)
- 支持部分字段更新 (用户名、邮箱、头像、状态)
- 更新时的唯一性验证 (排除当前用户)
- 字段验证和清理
- 时间戳自动更新
- 实现用户删除逻辑 (`deleteUserLogic.go`)
- 用户存在性检查
- 硬删除实现
- 删除日志记录
- 关联数据处理预留
- 创建完整的API测试用例 (`test_user_crud.http`)
- 正常场景测试 (创建、查询、更新、删除)
- 错误场景测试 (重复数据、不存在资源、格式错误)
- 边界情况验证 (密码长度、邮箱格式等)
#### 11. 实现用户头像上传功能
**优先级**: 中 🔥
@ -459,9 +482,13 @@
-`DELETE /api/v1/photos/:id` - 删除照片 (同时删除文件)
-`GET /api/v1/categories` - 分类列表
-`POST /api/v1/categories` - 创建分类
-`GET /api/v1/users` - 用户列表
-`GET /uploads/*` - 静态文件访问
-`PUT /api/v1/categories/:id` - 更新分类 (代码完善)
-`GET /api/v1/users` - 用户列表
-`POST /api/v1/users` - 创建用户 (完整业务逻辑)
-`GET /api/v1/users/:id` - 获取用户详情 (完整业务逻辑)
-`PUT /api/v1/users/:id` - 更新用户 (完整业务逻辑)
-`DELETE /api/v1/users/:id` - 删除用户 (完整业务逻辑)
-`GET /uploads/*` - 静态文件访问
-`DELETE /api/v1/categories/:id` - 删除分类
### 🛠️ 技术栈

View File

@ -2,9 +2,13 @@ package user
import (
"context"
"time"
"photography-backend/internal/model"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"photography-backend/pkg/errorx"
"photography-backend/pkg/utils/hash"
"github.com/zeromicro/go-zero/core/logx"
)
@ -25,7 +29,88 @@ func NewCreateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Create
}
func (l *CreateUserLogic) CreateUser(req *types.CreateUserRequest) (resp *types.CreateUserResponse, err error) {
// todo: add your logic here and delete this line
return
// 检查用户名是否已存在
existingUser, err := l.svcCtx.UserModel.FindOneByUsername(l.ctx, req.Username)
if err == nil && existingUser != nil {
return &types.CreateUserResponse{
BaseResponse: types.BaseResponse{
Code: errorx.UserExists,
Message: "用户名已存在",
},
}, nil
}
// 检查邮箱是否已存在
existingUser, err = l.svcCtx.UserModel.FindOneByEmail(l.ctx, req.Email)
if err == nil && existingUser != nil {
return &types.CreateUserResponse{
BaseResponse: types.BaseResponse{
Code: errorx.UserExists,
Message: "邮箱已存在",
},
}, nil
}
// 密码加密
hashedPassword, err := hash.HashPassword(req.Password)
if err != nil {
logx.WithContext(l.ctx).Error("密码加密失败: ", err)
return &types.CreateUserResponse{
BaseResponse: types.BaseResponse{
Code: errorx.ServerError,
Message: "密码加密失败",
},
}, nil
}
// 创建用户
user := &model.User{
Username: req.Username,
Email: req.Email,
Password: hashedPassword,
Avatar: "", // 默认空头像
Status: 1, // 默认启用状态
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
result, err := l.svcCtx.UserModel.Insert(l.ctx, user)
if err != nil {
logx.WithContext(l.ctx).Error("创建用户失败: ", err)
return &types.CreateUserResponse{
BaseResponse: types.BaseResponse{
Code: errorx.ServerError,
Message: "创建用户失败",
},
}, nil
}
// 获取新创建用户的ID
userId, err := result.LastInsertId()
if err != nil {
logx.WithContext(l.ctx).Error("获取用户ID失败: ", err)
return &types.CreateUserResponse{
BaseResponse: types.BaseResponse{
Code: errorx.ServerError,
Message: "获取用户ID失败",
},
}, nil
}
// 返回用户信息(不包含密码)
return &types.CreateUserResponse{
BaseResponse: types.BaseResponse{
Code: errorx.Success,
Message: "创建用户成功",
},
Data: types.User{
Id: userId,
Username: user.Username,
Email: user.Email,
Avatar: user.Avatar,
Status: int(user.Status),
CreatedAt: user.CreatedAt.Unix(),
UpdatedAt: user.UpdatedAt.Unix(),
},
}, nil
}

View File

@ -5,8 +5,10 @@ import (
"photography-backend/internal/svc"
"photography-backend/internal/types"
"photography-backend/pkg/errorx"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
type DeleteUserLogic struct {
@ -25,7 +27,48 @@ func NewDeleteUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Delete
}
func (l *DeleteUserLogic) DeleteUser(req *types.DeleteUserRequest) (resp *types.DeleteUserResponse, err error) {
// todo: add your logic here and delete this line
return
// 检查用户是否存在
user, err := l.svcCtx.UserModel.FindOne(l.ctx, req.Id)
if err != nil {
if err == sqlx.ErrNotFound {
return &types.DeleteUserResponse{
BaseResponse: types.BaseResponse{
Code: errorx.UserNotFound,
Message: "用户不存在",
},
}, nil
}
logx.WithContext(l.ctx).Error("查询用户失败: ", err)
return &types.DeleteUserResponse{
BaseResponse: types.BaseResponse{
Code: errorx.ServerError,
Message: "查询用户失败",
},
}, nil
}
// 检查用户是否有关联的照片
// 这里可以添加业务逻辑来决定是否允许删除有照片的用户
// 如果要严格控制,可以先检查用户是否有照片,如果有则不允许删除
// 删除用户
err = l.svcCtx.UserModel.Delete(l.ctx, req.Id)
if err != nil {
logx.WithContext(l.ctx).Error("删除用户失败: ", err)
return &types.DeleteUserResponse{
BaseResponse: types.BaseResponse{
Code: errorx.ServerError,
Message: "删除用户失败",
},
}, nil
}
logx.WithContext(l.ctx).Infof("用户 %s (ID: %d) 已被删除", user.Username, user.Id)
return &types.DeleteUserResponse{
BaseResponse: types.BaseResponse{
Code: errorx.Success,
Message: "删除用户成功",
},
}, nil
}

View File

@ -5,8 +5,10 @@ import (
"photography-backend/internal/svc"
"photography-backend/internal/types"
"photography-backend/pkg/errorx"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
type GetUserLogic struct {
@ -25,7 +27,40 @@ func NewGetUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserLo
}
func (l *GetUserLogic) GetUser(req *types.GetUserRequest) (resp *types.GetUserResponse, err error) {
// todo: add your logic here and delete this line
return
// 查询用户
user, err := l.svcCtx.UserModel.FindOne(l.ctx, req.Id)
if err != nil {
if err == sqlx.ErrNotFound {
return &types.GetUserResponse{
BaseResponse: types.BaseResponse{
Code: errorx.UserNotFound,
Message: "用户不存在",
},
}, nil
}
logx.WithContext(l.ctx).Error("查询用户失败: ", err)
return &types.GetUserResponse{
BaseResponse: types.BaseResponse{
Code: errorx.ServerError,
Message: "查询用户失败",
},
}, nil
}
// 返回用户信息(不包含密码)
return &types.GetUserResponse{
BaseResponse: types.BaseResponse{
Code: errorx.Success,
Message: "查询成功",
},
Data: types.User{
Id: user.Id,
Username: user.Username,
Email: user.Email,
Avatar: user.Avatar,
Status: int(user.Status),
CreatedAt: user.CreatedAt.Unix(),
UpdatedAt: user.UpdatedAt.Unix(),
},
}, nil
}

View File

@ -2,11 +2,15 @@ package user
import (
"context"
"strings"
"time"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"photography-backend/pkg/errorx"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
type UpdateUserLogic struct {
@ -25,7 +29,92 @@ func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Update
}
func (l *UpdateUserLogic) UpdateUser(req *types.UpdateUserRequest) (resp *types.UpdateUserResponse, err error) {
// todo: add your logic here and delete this line
return
// 检查用户是否存在
user, err := l.svcCtx.UserModel.FindOne(l.ctx, req.Id)
if err != nil {
if err == sqlx.ErrNotFound {
return &types.UpdateUserResponse{
BaseResponse: types.BaseResponse{
Code: errorx.UserNotFound,
Message: "用户不存在",
},
}, nil
}
logx.WithContext(l.ctx).Error("查询用户失败: ", err)
return &types.UpdateUserResponse{
BaseResponse: types.BaseResponse{
Code: errorx.ServerError,
Message: "查询用户失败",
},
}, nil
}
// 检查用户名唯一性(如果要更新用户名)
if req.Username != "" && strings.TrimSpace(req.Username) != user.Username {
existingUser, err := l.svcCtx.UserModel.FindOneByUsername(l.ctx, req.Username)
if err == nil && existingUser != nil {
return &types.UpdateUserResponse{
BaseResponse: types.BaseResponse{
Code: errorx.UserExists,
Message: "用户名已存在",
},
}, nil
}
user.Username = strings.TrimSpace(req.Username)
}
// 检查邮箱唯一性(如果要更新邮箱)
if req.Email != "" && strings.TrimSpace(req.Email) != user.Email {
existingUser, err := l.svcCtx.UserModel.FindOneByEmail(l.ctx, req.Email)
if err == nil && existingUser != nil {
return &types.UpdateUserResponse{
BaseResponse: types.BaseResponse{
Code: errorx.UserExists,
Message: "邮箱已存在",
},
}, nil
}
user.Email = strings.TrimSpace(req.Email)
}
// 更新其他字段
if req.Avatar != "" {
user.Avatar = req.Avatar
}
if req.Status >= 0 {
user.Status = int64(req.Status)
}
// 更新时间
user.UpdatedAt = time.Now()
// 执行更新
err = l.svcCtx.UserModel.Update(l.ctx, user)
if err != nil {
logx.WithContext(l.ctx).Error("更新用户失败: ", err)
return &types.UpdateUserResponse{
BaseResponse: types.BaseResponse{
Code: errorx.ServerError,
Message: "更新用户失败",
},
}, nil
}
// 返回更新后的用户信息(不包含密码)
return &types.UpdateUserResponse{
BaseResponse: types.BaseResponse{
Code: errorx.Success,
Message: "更新用户成功",
},
Data: types.User{
Id: user.Id,
Username: user.Username,
Email: user.Email,
Avatar: user.Avatar,
Status: int(user.Status),
CreatedAt: user.CreatedAt.Unix(),
UpdatedAt: user.UpdatedAt.Unix(),
},
}, nil
}

116
backend/test_user_crud.http Normal file
View File

@ -0,0 +1,116 @@
### User Management API 测试
### 基础URL
@baseUrl = http://localhost:8080/api/v1
### 1. 创建用户
POST {{baseUrl}}/users
Content-Type: application/json
{
"username": "testuser",
"email": "test@example.com",
"password": "123456"
}
### 2. 创建第二个用户
POST {{baseUrl}}/users
Content-Type: application/json
{
"username": "admin2",
"email": "admin2@example.com",
"password": "123456"
}
### 3. 获取用户列表 (已有功能)
GET {{baseUrl}}/users?page=1&page_size=10
### 4. 获取用户详情
GET {{baseUrl}}/users/1
### 5. 更新用户信息
PUT {{baseUrl}}/users/1
Content-Type: application/json
{
"username": "updateduser",
"email": "updated@example.com",
"avatar": "http://example.com/avatar.jpg",
"status": 1
}
### 6. 更新用户状态 (禁用)
PUT {{baseUrl}}/users/1
Content-Type: application/json
{
"status": 0
}
### 7. 删除用户
DELETE {{baseUrl}}/users/2
### 错误测试场景
### 8. 创建重复用户名
POST {{baseUrl}}/users
Content-Type: application/json
{
"username": "testuser",
"email": "another@example.com",
"password": "123456"
}
### 9. 创建重复邮箱
POST {{baseUrl}}/users
Content-Type: application/json
{
"username": "anotheruser",
"email": "test@example.com",
"password": "123456"
}
### 10. 获取不存在的用户
GET {{baseUrl}}/users/999
### 11. 更新不存在的用户
PUT {{baseUrl}}/users/999
Content-Type: application/json
{
"username": "notexist",
"status": 1
}
### 12. 删除不存在的用户
DELETE {{baseUrl}}/users/999
### 13. 创建用户 - 密码太短
POST {{baseUrl}}/users
Content-Type: application/json
{
"username": "shortpwd",
"email": "short@example.com",
"password": "123"
}
### 14. 创建用户 - 邮箱格式错误
POST {{baseUrl}}/users
Content-Type: application/json
{
"username": "bademail",
"email": "not-an-email",
"password": "123456"
}
### 15. 更新用户 - 用户名改为已存在的
PUT {{baseUrl}}/users/1
Content-Type: application/json
{
"username": "admin"
}