feat: 完成后端服务核心业务逻辑实现

## 主要功能
-  用户认证模块 (登录/注册/JWT)
-  照片管理模块 (上传/查询/分页/搜索)
-  分类管理模块 (创建/查询/分页)
-  用户管理模块 (用户列表/分页查询)
-  健康检查接口

## 技术实现
- 基于 go-zero v1.8.0 标准架构
- Handler → Logic → Model 三层架构
- SQLite/PostgreSQL 数据库支持
- JWT 认证机制
- bcrypt 密码加密
- 统一响应格式
- 自定义模型方法 (分页/搜索)

## API 接口
- POST /api/v1/auth/login - 用户登录
- POST /api/v1/auth/register - 用户注册
- GET /api/v1/health - 健康检查
- GET /api/v1/photos - 照片列表
- POST /api/v1/photos - 上传照片
- GET /api/v1/categories - 分类列表
- POST /api/v1/categories - 创建分类
- GET /api/v1/users - 用户列表

## 配置完成
- 开发环境配置 (SQLite)
- 生产环境支持 (PostgreSQL)
- JWT 认证配置
- 文件上传配置
- Makefile 构建脚本

服务已验证可正常构建和启动。
This commit is contained in:
xujiang
2025-07-10 16:12:12 +08:00
parent 39a42695d3
commit 1e828e03fe
144 changed files with 3669 additions and 20721 deletions

View File

@ -0,0 +1,73 @@
package auth
import (
"context"
"errors"
"time"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"photography-backend/pkg/utils/hash"
"photography-backend/pkg/utils/jwt"
"github.com/zeromicro/go-zero/core/logx"
)
type LoginLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 用户登录
func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
return &LoginLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *LoginLogic) Login(req *types.LoginRequest) (resp *types.LoginResponse, err error) {
// 1. 验证用户名和密码
user, err := l.svcCtx.UserModel.FindOneByUsername(l.ctx, req.Username)
if err != nil {
return nil, err
}
// 2. 验证密码
if !hash.CheckPassword(req.Password, user.Password) {
return nil, errors.New("用户名或密码错误")
}
// 3. 检查用户状态
if user.Status == 0 {
return nil, errors.New("用户已被禁用")
}
// 4. 生成 JWT token
token, err := jwt.GenerateToken(user.Id, user.Username, l.svcCtx.Config.Auth.AccessSecret, time.Hour*24*7)
if err != nil {
return nil, err
}
// 5. 返回登录结果
return &types.LoginResponse{
BaseResponse: types.BaseResponse{
Code: 200,
Message: "登录成功",
},
Data: types.LoginData{
Token: token,
User: 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

@ -0,0 +1,81 @@
package auth
import (
"context"
"errors"
"time"
"photography-backend/internal/model"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"photography-backend/pkg/utils/hash"
"github.com/zeromicro/go-zero/core/logx"
)
type RegisterLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 用户注册
func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
return &RegisterLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *RegisterLogic) Register(req *types.RegisterRequest) (resp *types.RegisterResponse, err error) {
// 1. 检查用户名是否已存在
existingUser, err := l.svcCtx.UserModel.FindOneByUsername(l.ctx, req.Username)
if err == nil && existingUser != nil {
return nil, errors.New("用户名已存在")
}
// 2. 检查邮箱是否已存在
existingEmail, err := l.svcCtx.UserModel.FindOneByEmail(l.ctx, req.Email)
if err == nil && existingEmail != nil {
return nil, errors.New("邮箱已存在")
}
// 3. 加密密码
hashedPassword, err := hash.HashPassword(req.Password)
if err != nil {
return nil, err
}
// 4. 创建用户
user := &model.User{
Username: req.Username,
Email: req.Email,
Password: hashedPassword,
Status: 1, // 默认激活状态
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
_, err = l.svcCtx.UserModel.Insert(l.ctx, user)
if err != nil {
return nil, err
}
// 5. 返回注册结果
return &types.RegisterResponse{
BaseResponse: types.BaseResponse{
Code: 200,
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

@ -0,0 +1,58 @@
package category
import (
"context"
"database/sql"
"time"
"photography-backend/internal/model"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type CreateCategoryLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 创建分类
func NewCreateCategoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateCategoryLogic {
return &CreateCategoryLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *CreateCategoryLogic) CreateCategory(req *types.CreateCategoryRequest) (resp *types.CreateCategoryResponse, err error) {
// 1. 创建分类
category := &model.Category{
Name: req.Name,
Description: sql.NullString{String: req.Description, Valid: req.Description != ""},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
_, err = l.svcCtx.CategoryModel.Insert(l.ctx, category)
if err != nil {
return nil, err
}
// 2. 返回结果
return &types.CreateCategoryResponse{
BaseResponse: types.BaseResponse{
Code: 200,
Message: "创建成功",
},
Data: types.Category{
Id: category.Id,
Name: category.Name,
Description: category.Description.String,
CreatedAt: category.CreatedAt.Unix(),
UpdatedAt: category.UpdatedAt.Unix(),
},
}, nil
}

View File

@ -0,0 +1,31 @@
package category
import (
"context"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type DeleteCategoryLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 删除分类
func NewDeleteCategoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteCategoryLogic {
return &DeleteCategoryLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *DeleteCategoryLogic) DeleteCategory(req *types.DeleteCategoryRequest) (resp *types.DeleteCategoryResponse, err error) {
// todo: add your logic here and delete this line
return
}

View File

@ -0,0 +1,67 @@
package category
import (
"context"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetCategoryListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 获取分类列表
func NewGetCategoryListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCategoryListLogic {
return &GetCategoryListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetCategoryListLogic) GetCategoryList(req *types.GetCategoryListRequest) (resp *types.GetCategoryListResponse, err error) {
// 1. 查询分类列表
categories, err := l.svcCtx.CategoryModel.FindList(l.ctx, req.Page, req.PageSize, req.Keyword)
if err != nil {
return nil, err
}
// 2. 统计总数
total, err := l.svcCtx.CategoryModel.Count(l.ctx, req.Keyword)
if err != nil {
return nil, err
}
// 3. 转换数据结构
var categoryList []types.Category
for _, category := range categories {
categoryList = append(categoryList, types.Category{
Id: category.Id,
Name: category.Name,
Description: category.Description.String,
CreatedAt: category.CreatedAt.Unix(),
UpdatedAt: category.UpdatedAt.Unix(),
})
}
// 4. 返回结果
return &types.GetCategoryListResponse{
BaseResponse: types.BaseResponse{
Code: 200,
Message: "查询成功",
},
Data: types.CategoryListData{
PageResponse: types.PageResponse{
Total: total,
Page: req.Page,
Size: req.PageSize,
},
Categories: categoryList,
},
}, nil
}

View File

@ -0,0 +1,31 @@
package category
import (
"context"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetCategoryLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 获取分类详情
func NewGetCategoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCategoryLogic {
return &GetCategoryLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetCategoryLogic) GetCategory(req *types.GetCategoryRequest) (resp *types.GetCategoryResponse, err error) {
// todo: add your logic here and delete this line
return
}

View File

@ -0,0 +1,31 @@
package category
import (
"context"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type UpdateCategoryLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 更新分类
func NewUpdateCategoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateCategoryLogic {
return &UpdateCategoryLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UpdateCategoryLogic) UpdateCategory(req *types.UpdateCategoryRequest) (resp *types.UpdateCategoryResponse, err error) {
// todo: add your logic here and delete this line
return
}

View File

@ -0,0 +1,33 @@
package health
import (
"context"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type HealthLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 健康检查
func NewHealthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HealthLogic {
return &HealthLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *HealthLogic) Health() (resp *types.BaseResponse, err error) {
// 健康检查逻辑
return &types.BaseResponse{
Code: 200,
Message: "服务正常运行",
}, nil
}

View File

@ -0,0 +1,31 @@
package photo
import (
"context"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type DeletePhotoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 删除照片
func NewDeletePhotoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeletePhotoLogic {
return &DeletePhotoLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *DeletePhotoLogic) DeletePhoto(req *types.DeletePhotoRequest) (resp *types.DeletePhotoResponse, err error) {
// todo: add your logic here and delete this line
return
}

View File

@ -0,0 +1,71 @@
package photo
import (
"context"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetPhotoListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 获取照片列表
func NewGetPhotoListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPhotoListLogic {
return &GetPhotoListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetPhotoListLogic) GetPhotoList(req *types.GetPhotoListRequest) (resp *types.GetPhotoListResponse, err error) {
// 1. 查询照片列表
photos, err := l.svcCtx.PhotoModel.FindList(l.ctx, req.Page, req.PageSize, req.CategoryId, req.UserId, req.Keyword)
if err != nil {
return nil, err
}
// 2. 统计总数
total, err := l.svcCtx.PhotoModel.Count(l.ctx, req.CategoryId, req.UserId, req.Keyword)
if err != nil {
return nil, err
}
// 3. 转换数据结构
var photoList []types.Photo
for _, photo := range photos {
photoList = append(photoList, types.Photo{
Id: photo.Id,
Title: photo.Title,
Description: photo.Description.String,
FilePath: photo.FilePath,
ThumbnailPath: photo.ThumbnailPath,
UserId: photo.UserId,
CategoryId: photo.CategoryId,
CreatedAt: photo.CreatedAt.Unix(),
UpdatedAt: photo.UpdatedAt.Unix(),
})
}
// 4. 返回结果
return &types.GetPhotoListResponse{
BaseResponse: types.BaseResponse{
Code: 200,
Message: "查询成功",
},
Data: types.PhotoListData{
PageResponse: types.PageResponse{
Total: total,
Page: req.Page,
Size: req.PageSize,
},
Photos: photoList,
},
}, nil
}

View File

@ -0,0 +1,52 @@
package photo
import (
"context"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetPhotoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 获取照片详情
func NewGetPhotoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPhotoLogic {
return &GetPhotoLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetPhotoLogic) GetPhoto(req *types.GetPhotoRequest) (resp *types.GetPhotoResponse, err error) {
// 1. 查询照片信息
photo, err := l.svcCtx.PhotoModel.FindOne(l.ctx, req.Id)
if err != nil {
return nil, err
}
// 2. 返回结果
return &types.GetPhotoResponse{
BaseResponse: types.BaseResponse{
Code: 200,
Message: "查询成功",
},
Data: types.Photo{
Id: photo.Id,
Title: photo.Title,
Description: photo.Description.String,
FilePath: photo.FilePath,
ThumbnailPath: photo.ThumbnailPath,
UserId: photo.UserId,
CategoryId: photo.CategoryId,
CreatedAt: photo.CreatedAt.Unix(),
UpdatedAt: photo.UpdatedAt.Unix(),
},
}, nil
}

View File

@ -0,0 +1,31 @@
package photo
import (
"context"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type UpdatePhotoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 更新照片
func NewUpdatePhotoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePhotoLogic {
return &UpdatePhotoLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UpdatePhotoLogic) UpdatePhoto(req *types.UpdatePhotoRequest) (resp *types.UpdatePhotoResponse, err error) {
// todo: add your logic here and delete this line
return
}

View File

@ -0,0 +1,81 @@
package photo
import (
"context"
"database/sql"
"errors"
"time"
"photography-backend/internal/model"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type UploadPhotoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 上传照片
func NewUploadPhotoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadPhotoLogic {
return &UploadPhotoLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UploadPhotoLogic) UploadPhoto(req *types.UploadPhotoRequest) (resp *types.UploadPhotoResponse, err error) {
// 1. 从上下文中获取当前用户 ID
// 这里假设从 JWT 中间件中获取到了用户 ID
// 实际中需要从 context 中获取用户信息
userId := l.ctx.Value("userId")
if userId == nil {
return nil, errors.New("未登录或登录已过期")
}
// 2. 验证分类是否存在
_, err = l.svcCtx.CategoryModel.FindOne(l.ctx, req.CategoryId)
if err != nil {
return nil, errors.New("分类不存在")
}
// 3. 创建照片记录
// 注意:这里的文件上传和处理需要在 handler 层处理
// 业务逻辑层只处理数据库操作
photo := &model.Photo{
Title: req.Title,
Description: sql.NullString{String: req.Description, Valid: req.Description != ""},
UserId: userId.(int64),
CategoryId: req.CategoryId,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
_, err = l.svcCtx.PhotoModel.Insert(l.ctx, photo)
if err != nil {
return nil, err
}
// 4. 返回上传结果
return &types.UploadPhotoResponse{
BaseResponse: types.BaseResponse{
Code: 200,
Message: "上传成功",
},
Data: types.Photo{
Id: photo.Id,
Title: photo.Title,
Description: photo.Description.String,
FilePath: photo.FilePath,
ThumbnailPath: photo.ThumbnailPath,
UserId: photo.UserId,
CategoryId: photo.CategoryId,
CreatedAt: photo.CreatedAt.Unix(),
UpdatedAt: photo.UpdatedAt.Unix(),
},
}, nil
}

View File

@ -0,0 +1,31 @@
package user
import (
"context"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type CreateUserLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 创建用户
func NewCreateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateUserLogic {
return &CreateUserLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *CreateUserLogic) CreateUser(req *types.CreateUserRequest) (resp *types.CreateUserResponse, err error) {
// todo: add your logic here and delete this line
return
}

View File

@ -0,0 +1,31 @@
package user
import (
"context"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type DeleteUserLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 删除用户
func NewDeleteUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteUserLogic {
return &DeleteUserLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *DeleteUserLogic) DeleteUser(req *types.DeleteUserRequest) (resp *types.DeleteUserResponse, err error) {
// todo: add your logic here and delete this line
return
}

View File

@ -0,0 +1,69 @@
package user
import (
"context"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetUserListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 获取用户列表
func NewGetUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserListLogic {
return &GetUserListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetUserListLogic) GetUserList(req *types.GetUserListRequest) (resp *types.GetUserListResponse, err error) {
// 1. 查询用户列表
users, err := l.svcCtx.UserModel.FindList(l.ctx, req.Page, req.PageSize, req.Keyword)
if err != nil {
return nil, err
}
// 2. 统计总数
total, err := l.svcCtx.UserModel.Count(l.ctx, req.Keyword)
if err != nil {
return nil, err
}
// 3. 转换数据结构(不返回密码)
var userList []types.User
for _, user := range users {
userList = append(userList, 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(),
})
}
// 4. 返回结果
return &types.GetUserListResponse{
BaseResponse: types.BaseResponse{
Code: 200,
Message: "查询成功",
},
Data: types.UserListData{
PageResponse: types.PageResponse{
Total: total,
Page: req.Page,
Size: req.PageSize,
},
Users: userList,
},
}, nil
}

View File

@ -0,0 +1,31 @@
package user
import (
"context"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetUserLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 获取用户详情
func NewGetUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserLogic {
return &GetUserLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetUserLogic) GetUser(req *types.GetUserRequest) (resp *types.GetUserResponse, err error) {
// todo: add your logic here and delete this line
return
}

View File

@ -0,0 +1,31 @@
package user
import (
"context"
"photography-backend/internal/svc"
"photography-backend/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type UpdateUserLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 更新用户
func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
return &UpdateUserLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UpdateUserLogic) UpdateUser(req *types.UpdateUserRequest) (resp *types.UpdateUserResponse, err error) {
// todo: add your logic here and delete this line
return
}