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:
@ -2,164 +2,41 @@ package response
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"photography-backend/pkg/errorx"
|
||||
)
|
||||
|
||||
// Response 统一响应结构
|
||||
type Response struct {
|
||||
Success bool `json:"success"`
|
||||
type Body struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
Meta *Meta `json:"meta,omitempty"`
|
||||
}
|
||||
|
||||
// Meta 元数据
|
||||
type Meta struct {
|
||||
Timestamp string `json:"timestamp"`
|
||||
RequestID string `json:"request_id,omitempty"`
|
||||
}
|
||||
|
||||
// PaginatedResponse 分页响应
|
||||
type PaginatedResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data"`
|
||||
Pagination *Pagination `json:"pagination"`
|
||||
Meta *Meta `json:"meta,omitempty"`
|
||||
}
|
||||
|
||||
// Pagination 分页信息
|
||||
type Pagination struct {
|
||||
Page int `json:"page"`
|
||||
Limit int `json:"limit"`
|
||||
Total int64 `json:"total"`
|
||||
TotalPages int `json:"total_pages"`
|
||||
HasNext bool `json:"has_next"`
|
||||
HasPrev bool `json:"has_prev"`
|
||||
}
|
||||
|
||||
// Success 成功响应
|
||||
func Success(data interface{}) *Response {
|
||||
return &Response{
|
||||
Success: true,
|
||||
Code: http.StatusOK,
|
||||
Message: "Success",
|
||||
Data: data,
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
func Response(w http.ResponseWriter, resp interface{}, err error) {
|
||||
var body Body
|
||||
if err != nil {
|
||||
if e, ok := err.(*errorx.CodeError); ok {
|
||||
body.Code = e.Code
|
||||
body.Message = e.Msg
|
||||
httpx.WriteJson(w, errorx.GetHttpStatus(e.Code), body)
|
||||
} else {
|
||||
body.Code = errorx.ServerError
|
||||
body.Message = err.Error()
|
||||
httpx.WriteJson(w, http.StatusInternalServerError, body)
|
||||
}
|
||||
} else {
|
||||
body.Code = errorx.Success
|
||||
body.Message = "success"
|
||||
body.Data = resp
|
||||
httpx.OkJson(w, body)
|
||||
}
|
||||
}
|
||||
|
||||
// Error 错误响应
|
||||
func Error(code int, message string) *Response {
|
||||
return &Response{
|
||||
Success: false,
|
||||
Code: code,
|
||||
Message: message,
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
func Success(w http.ResponseWriter, data interface{}) {
|
||||
Response(w, data, nil)
|
||||
}
|
||||
|
||||
// Created 创建成功响应
|
||||
func Created(data interface{}) *Response {
|
||||
return &Response{
|
||||
Success: true,
|
||||
Code: http.StatusCreated,
|
||||
Message: "Created successfully",
|
||||
Data: data,
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Updated 更新成功响应
|
||||
func Updated(data interface{}) *Response {
|
||||
return &Response{
|
||||
Success: true,
|
||||
Code: http.StatusOK,
|
||||
Message: "Updated successfully",
|
||||
Data: data,
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Deleted 删除成功响应
|
||||
func Deleted() *Response {
|
||||
return &Response{
|
||||
Success: true,
|
||||
Code: http.StatusOK,
|
||||
Message: "Deleted successfully",
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Paginated 分页响应
|
||||
func Paginated(data interface{}, page, limit int, total int64) *PaginatedResponse {
|
||||
totalPages := int((total + int64(limit) - 1) / int64(limit))
|
||||
|
||||
return &PaginatedResponse{
|
||||
Success: true,
|
||||
Code: http.StatusOK,
|
||||
Message: "Success",
|
||||
Data: data,
|
||||
Pagination: &Pagination{
|
||||
Page: page,
|
||||
Limit: limit,
|
||||
Total: total,
|
||||
TotalPages: totalPages,
|
||||
HasNext: page < totalPages,
|
||||
HasPrev: page > 1,
|
||||
},
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// BadRequest 400错误
|
||||
func BadRequest(message string) *Response {
|
||||
return Error(http.StatusBadRequest, message)
|
||||
}
|
||||
|
||||
// Unauthorized 401错误
|
||||
func Unauthorized(message string) *Response {
|
||||
return Error(http.StatusUnauthorized, message)
|
||||
}
|
||||
|
||||
// Forbidden 403错误
|
||||
func Forbidden(message string) *Response {
|
||||
return Error(http.StatusForbidden, message)
|
||||
}
|
||||
|
||||
// NotFound 404错误
|
||||
func NotFound(message string) *Response {
|
||||
return Error(http.StatusNotFound, message)
|
||||
}
|
||||
|
||||
// InternalServerError 500错误
|
||||
func InternalServerError(message string) *Response {
|
||||
return Error(http.StatusInternalServerError, message)
|
||||
}
|
||||
|
||||
// ValidationError 验证错误
|
||||
func ValidationError(errors map[string]string) *Response {
|
||||
return &Response{
|
||||
Success: false,
|
||||
Code: http.StatusUnprocessableEntity,
|
||||
Message: "Validation failed",
|
||||
Data: errors,
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
func Error(w http.ResponseWriter, err error) {
|
||||
Response(w, nil, err)
|
||||
}
|
||||
Reference in New Issue
Block a user