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:
48
backend/api/desc/auth.api
Normal file
48
backend/api/desc/auth.api
Normal file
@ -0,0 +1,48 @@
|
||||
syntax = "v1"
|
||||
|
||||
import "common.api"
|
||||
|
||||
// 登录请求
|
||||
type LoginRequest {
|
||||
Username string `json:"username" validate:"required"`
|
||||
Password string `json:"password" validate:"required"`
|
||||
}
|
||||
|
||||
// 登录响应
|
||||
type LoginResponse {
|
||||
BaseResponse
|
||||
Data LoginData `json:"data"`
|
||||
}
|
||||
|
||||
type LoginData {
|
||||
Token string `json:"token"`
|
||||
User User `json:"user"`
|
||||
}
|
||||
|
||||
// 注册请求
|
||||
type RegisterRequest {
|
||||
Username string `json:"username" validate:"required"`
|
||||
Email string `json:"email" validate:"required,email"`
|
||||
Password string `json:"password" validate:"required,min=6"`
|
||||
}
|
||||
|
||||
// 注册响应
|
||||
type RegisterResponse {
|
||||
BaseResponse
|
||||
Data User `json:"data"`
|
||||
}
|
||||
|
||||
// 认证接口
|
||||
@server(
|
||||
group: auth
|
||||
prefix: /api/v1/auth
|
||||
)
|
||||
service photography-api {
|
||||
@doc "用户登录"
|
||||
@handler login
|
||||
post /login (LoginRequest) returns (LoginResponse)
|
||||
|
||||
@doc "用户注册"
|
||||
@handler register
|
||||
post /register (RegisterRequest) returns (RegisterResponse)
|
||||
}
|
||||
96
backend/api/desc/category.api
Normal file
96
backend/api/desc/category.api
Normal file
@ -0,0 +1,96 @@
|
||||
syntax = "v1"
|
||||
|
||||
import "common.api"
|
||||
|
||||
// 分类管理接口
|
||||
|
||||
// 获取分类列表请求
|
||||
type GetCategoryListRequest {
|
||||
PageRequest
|
||||
Keyword string `form:"keyword,optional"`
|
||||
}
|
||||
|
||||
// 获取分类列表响应
|
||||
type GetCategoryListResponse {
|
||||
BaseResponse
|
||||
Data CategoryListData `json:"data"`
|
||||
}
|
||||
|
||||
type CategoryListData {
|
||||
PageResponse
|
||||
Categories []Category `json:"categories"`
|
||||
}
|
||||
|
||||
// 获取分类详情请求
|
||||
type GetCategoryRequest {
|
||||
Id int64 `path:"id"`
|
||||
}
|
||||
|
||||
// 获取分类详情响应
|
||||
type GetCategoryResponse {
|
||||
BaseResponse
|
||||
Data Category `json:"data"`
|
||||
}
|
||||
|
||||
// 创建分类请求
|
||||
type CreateCategoryRequest {
|
||||
Name string `json:"name" validate:"required"`
|
||||
Description string `json:"description,optional"`
|
||||
}
|
||||
|
||||
// 创建分类响应
|
||||
type CreateCategoryResponse {
|
||||
BaseResponse
|
||||
Data Category `json:"data"`
|
||||
}
|
||||
|
||||
// 更新分类请求
|
||||
type UpdateCategoryRequest {
|
||||
Id int64 `path:"id"`
|
||||
Name string `json:"name,optional"`
|
||||
Description string `json:"description,optional"`
|
||||
}
|
||||
|
||||
// 更新分类响应
|
||||
type UpdateCategoryResponse {
|
||||
BaseResponse
|
||||
Data Category `json:"data"`
|
||||
}
|
||||
|
||||
// 删除分类请求
|
||||
type DeleteCategoryRequest {
|
||||
Id int64 `path:"id"`
|
||||
}
|
||||
|
||||
// 删除分类响应
|
||||
type DeleteCategoryResponse {
|
||||
BaseResponse
|
||||
}
|
||||
|
||||
// 分类管理接口组
|
||||
@server(
|
||||
group: category
|
||||
prefix: /api/v1/categories
|
||||
jwt: Auth
|
||||
)
|
||||
service photography-api {
|
||||
@doc "获取分类列表"
|
||||
@handler getCategoryList
|
||||
get / (GetCategoryListRequest) returns (GetCategoryListResponse)
|
||||
|
||||
@doc "创建分类"
|
||||
@handler createCategory
|
||||
post / (CreateCategoryRequest) returns (CreateCategoryResponse)
|
||||
|
||||
@doc "获取分类详情"
|
||||
@handler getCategory
|
||||
get /:id (GetCategoryRequest) returns (GetCategoryResponse)
|
||||
|
||||
@doc "更新分类"
|
||||
@handler updateCategory
|
||||
put /:id (UpdateCategoryRequest) returns (UpdateCategoryResponse)
|
||||
|
||||
@doc "删除分类"
|
||||
@handler deleteCategory
|
||||
delete /:id (DeleteCategoryRequest) returns (DeleteCategoryResponse)
|
||||
}
|
||||
53
backend/api/desc/common.api
Normal file
53
backend/api/desc/common.api
Normal file
@ -0,0 +1,53 @@
|
||||
syntax = "v1"
|
||||
|
||||
// 公共响应结构
|
||||
type BaseResponse {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// 分页请求
|
||||
type PageRequest {
|
||||
Page int `form:"page,default=1"`
|
||||
PageSize int `form:"page_size,default=10"`
|
||||
}
|
||||
|
||||
// 分页响应
|
||||
type PageResponse {
|
||||
Total int64 `json:"total"`
|
||||
Page int `json:"page"`
|
||||
Size int `json:"size"`
|
||||
}
|
||||
|
||||
// 用户信息
|
||||
type User {
|
||||
Id int64 `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
Avatar string `json:"avatar"`
|
||||
Status int `json:"status"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
UpdatedAt int64 `json:"updated_at"`
|
||||
}
|
||||
|
||||
// 照片信息
|
||||
type Photo {
|
||||
Id int64 `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
FilePath string `json:"file_path"`
|
||||
ThumbnailPath string `json:"thumbnail_path"`
|
||||
UserId int64 `json:"user_id"`
|
||||
CategoryId int64 `json:"category_id"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
UpdatedAt int64 `json:"updated_at"`
|
||||
}
|
||||
|
||||
// 分类信息
|
||||
type Category {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
UpdatedAt int64 `json:"updated_at"`
|
||||
}
|
||||
100
backend/api/desc/photo.api
Normal file
100
backend/api/desc/photo.api
Normal file
@ -0,0 +1,100 @@
|
||||
syntax = "v1"
|
||||
|
||||
import "common.api"
|
||||
|
||||
// 照片管理接口
|
||||
|
||||
// 获取照片列表请求
|
||||
type GetPhotoListRequest {
|
||||
PageRequest
|
||||
CategoryId int64 `form:"category_id,optional"`
|
||||
UserId int64 `form:"user_id,optional"`
|
||||
Keyword string `form:"keyword,optional"`
|
||||
}
|
||||
|
||||
// 获取照片列表响应
|
||||
type GetPhotoListResponse {
|
||||
BaseResponse
|
||||
Data PhotoListData `json:"data"`
|
||||
}
|
||||
|
||||
type PhotoListData {
|
||||
PageResponse
|
||||
Photos []Photo `json:"photos"`
|
||||
}
|
||||
|
||||
// 获取照片详情请求
|
||||
type GetPhotoRequest {
|
||||
Id int64 `path:"id"`
|
||||
}
|
||||
|
||||
// 获取照片详情响应
|
||||
type GetPhotoResponse {
|
||||
BaseResponse
|
||||
Data Photo `json:"data"`
|
||||
}
|
||||
|
||||
// 上传照片请求
|
||||
type UploadPhotoRequest {
|
||||
Title string `json:"title" validate:"required"`
|
||||
Description string `json:"description,optional"`
|
||||
CategoryId int64 `json:"category_id" validate:"required"`
|
||||
}
|
||||
|
||||
// 上传照片响应
|
||||
type UploadPhotoResponse {
|
||||
BaseResponse
|
||||
Data Photo `json:"data"`
|
||||
}
|
||||
|
||||
// 更新照片请求
|
||||
type UpdatePhotoRequest {
|
||||
Id int64 `path:"id"`
|
||||
Title string `json:"title,optional"`
|
||||
Description string `json:"description,optional"`
|
||||
CategoryId int64 `json:"category_id,optional"`
|
||||
}
|
||||
|
||||
// 更新照片响应
|
||||
type UpdatePhotoResponse {
|
||||
BaseResponse
|
||||
Data Photo `json:"data"`
|
||||
}
|
||||
|
||||
// 删除照片请求
|
||||
type DeletePhotoRequest {
|
||||
Id int64 `path:"id"`
|
||||
}
|
||||
|
||||
// 删除照片响应
|
||||
type DeletePhotoResponse {
|
||||
BaseResponse
|
||||
}
|
||||
|
||||
// 照片管理接口组
|
||||
@server(
|
||||
group: photo
|
||||
prefix: /api/v1/photos
|
||||
jwt: Auth
|
||||
)
|
||||
service photography-api {
|
||||
@doc "获取照片列表"
|
||||
@handler getPhotoList
|
||||
get / (GetPhotoListRequest) returns (GetPhotoListResponse)
|
||||
|
||||
@doc "上传照片"
|
||||
@handler uploadPhoto
|
||||
post / (UploadPhotoRequest) returns (UploadPhotoResponse)
|
||||
|
||||
@doc "获取照片详情"
|
||||
@handler getPhoto
|
||||
get /:id (GetPhotoRequest) returns (GetPhotoResponse)
|
||||
|
||||
@doc "更新照片"
|
||||
@handler updatePhoto
|
||||
put /:id (UpdatePhotoRequest) returns (UpdatePhotoResponse)
|
||||
|
||||
@doc "删除照片"
|
||||
@handler deletePhoto
|
||||
delete /:id (DeletePhotoRequest) returns (DeletePhotoResponse)
|
||||
}
|
||||
33
backend/api/desc/photography.api
Normal file
33
backend/api/desc/photography.api
Normal file
@ -0,0 +1,33 @@
|
||||
syntax = "v1"
|
||||
|
||||
info (
|
||||
title: "Photography Portfolio API"
|
||||
desc: "摄影作品集 API 服务"
|
||||
author: "Photography Team"
|
||||
email: "team@photography.com"
|
||||
version: "v1.0.0"
|
||||
)
|
||||
|
||||
import "common.api"
|
||||
import "auth.api"
|
||||
import "user.api"
|
||||
import "photo.api"
|
||||
import "category.api"
|
||||
|
||||
// JWT 认证配置
|
||||
@server (
|
||||
jwt: Auth
|
||||
)
|
||||
service photography-api { // 健康检查接口 (无需认证)}
|
||||
|
||||
// 健康检查接口 (无需认证)
|
||||
@server (
|
||||
group: health
|
||||
prefix: /api/v1
|
||||
)
|
||||
service photography-api {
|
||||
@doc "健康检查"
|
||||
@handler health
|
||||
get /health returns (BaseResponse)
|
||||
}
|
||||
|
||||
99
backend/api/desc/user.api
Normal file
99
backend/api/desc/user.api
Normal file
@ -0,0 +1,99 @@
|
||||
syntax = "v1"
|
||||
|
||||
import "common.api"
|
||||
|
||||
// 用户管理接口
|
||||
|
||||
// 获取用户列表请求
|
||||
type GetUserListRequest {
|
||||
PageRequest
|
||||
Keyword string `form:"keyword,optional"`
|
||||
}
|
||||
|
||||
// 获取用户列表响应
|
||||
type GetUserListResponse {
|
||||
BaseResponse
|
||||
Data UserListData `json:"data"`
|
||||
}
|
||||
|
||||
type UserListData {
|
||||
PageResponse
|
||||
Users []User `json:"users"`
|
||||
}
|
||||
|
||||
// 获取用户详情请求
|
||||
type GetUserRequest {
|
||||
Id int64 `path:"id"`
|
||||
}
|
||||
|
||||
// 获取用户详情响应
|
||||
type GetUserResponse {
|
||||
BaseResponse
|
||||
Data User `json:"data"`
|
||||
}
|
||||
|
||||
// 创建用户请求
|
||||
type CreateUserRequest {
|
||||
Username string `json:"username" validate:"required"`
|
||||
Email string `json:"email" validate:"required,email"`
|
||||
Password string `json:"password" validate:"required,min=6"`
|
||||
}
|
||||
|
||||
// 创建用户响应
|
||||
type CreateUserResponse {
|
||||
BaseResponse
|
||||
Data User `json:"data"`
|
||||
}
|
||||
|
||||
// 更新用户请求
|
||||
type UpdateUserRequest {
|
||||
Id int64 `path:"id"`
|
||||
Username string `json:"username,optional"`
|
||||
Email string `json:"email,optional"`
|
||||
Avatar string `json:"avatar,optional"`
|
||||
Status int `json:"status,optional"`
|
||||
}
|
||||
|
||||
// 更新用户响应
|
||||
type UpdateUserResponse {
|
||||
BaseResponse
|
||||
Data User `json:"data"`
|
||||
}
|
||||
|
||||
// 删除用户请求
|
||||
type DeleteUserRequest {
|
||||
Id int64 `path:"id"`
|
||||
}
|
||||
|
||||
// 删除用户响应
|
||||
type DeleteUserResponse {
|
||||
BaseResponse
|
||||
}
|
||||
|
||||
// 用户管理接口组
|
||||
@server(
|
||||
group: user
|
||||
prefix: /api/v1/users
|
||||
jwt: Auth
|
||||
)
|
||||
service photography-api {
|
||||
@doc "获取用户列表"
|
||||
@handler getUserList
|
||||
get / (GetUserListRequest) returns (GetUserListResponse)
|
||||
|
||||
@doc "创建用户"
|
||||
@handler createUser
|
||||
post / (CreateUserRequest) returns (CreateUserResponse)
|
||||
|
||||
@doc "获取用户详情"
|
||||
@handler getUser
|
||||
get /:id (GetUserRequest) returns (GetUserResponse)
|
||||
|
||||
@doc "更新用户"
|
||||
@handler updateUser
|
||||
put /:id (UpdateUserRequest) returns (UpdateUserResponse)
|
||||
|
||||
@doc "删除用户"
|
||||
@handler deleteUser
|
||||
delete /:id (DeleteUserRequest) returns (DeleteUserResponse)
|
||||
}
|
||||
Reference in New Issue
Block a user