## 后端架构 (Go + Gin + GORM) - ✅ 完整的分层架构 (API/Service/Repository) - ✅ PostgreSQL数据库设计和迁移脚本 - ✅ JWT认证系统和权限控制 - ✅ 用户、照片、分类、标签等核心模型 - ✅ 中间件系统 (认证、CORS、日志) - ✅ 配置管理和环境变量支持 - ✅ 结构化日志和错误处理 - ✅ Makefile构建和部署脚本 ## 管理后台架构 (React + TypeScript) - ✅ Vite + React 18 + TypeScript现代化架构 - ✅ 路由系统和状态管理 (Zustand + TanStack Query) - ✅ 基于Radix UI的组件库基础 - ✅ 认证流程和权限控制 - ✅ 响应式设计和主题系统 ## 数据库设计 - ✅ 用户表 (角色权限、认证信息) - ✅ 照片表 (元数据、EXIF、状态管理) - ✅ 分类表 (层级结构、封面图片) - ✅ 标签表 (使用统计、标签云) - ✅ 关联表 (照片-标签多对多) ## 技术特点 - 🚀 高性能: Gin框架 + GORM ORM - 🔐 安全: JWT认证 + 密码加密 + 权限控制 - 📊 监控: 结构化日志 + 健康检查 - 🎨 现代化: React 18 + TypeScript + Vite - 📱 响应式: Tailwind CSS + Radix UI 参考文档: docs/development/saved-docs/
165 lines
3.6 KiB
Go
165 lines
3.6 KiB
Go
package response
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
// Response 统一响应结构
|
|
type Response struct {
|
|
Success bool `json:"success"`
|
|
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),
|
|
},
|
|
}
|
|
}
|
|
|
|
// Error 错误响应
|
|
func Error(code int, message string) *Response {
|
|
return &Response{
|
|
Success: false,
|
|
Code: code,
|
|
Message: message,
|
|
Meta: &Meta{
|
|
Timestamp: time.Now().Format(time.RFC3339),
|
|
},
|
|
}
|
|
}
|
|
|
|
// 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),
|
|
},
|
|
}
|
|
} |