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:
96
backend/pkg/errorx/errorx.go
Normal file
96
backend/pkg/errorx/errorx.go
Normal file
@ -0,0 +1,96 @@
|
||||
package errorx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const (
|
||||
// 通用错误代码
|
||||
Success = 0
|
||||
ServerError = 500
|
||||
ParamError = 400
|
||||
AuthError = 401
|
||||
NotFound = 404
|
||||
Forbidden = 403
|
||||
|
||||
// 业务错误代码
|
||||
UserNotFound = 1001
|
||||
UserExists = 1002
|
||||
InvalidPassword = 1003
|
||||
TokenExpired = 1004
|
||||
TokenInvalid = 1005
|
||||
|
||||
PhotoNotFound = 2001
|
||||
PhotoUploadFail = 2002
|
||||
|
||||
CategoryNotFound = 3001
|
||||
CategoryExists = 3002
|
||||
)
|
||||
|
||||
var codeText = map[int]string{
|
||||
Success: "Success",
|
||||
ServerError: "Server Error",
|
||||
ParamError: "Parameter Error",
|
||||
AuthError: "Authentication Error",
|
||||
NotFound: "Not Found",
|
||||
Forbidden: "Forbidden",
|
||||
|
||||
UserNotFound: "User Not Found",
|
||||
UserExists: "User Already Exists",
|
||||
InvalidPassword: "Invalid Password",
|
||||
TokenExpired: "Token Expired",
|
||||
TokenInvalid: "Token Invalid",
|
||||
|
||||
PhotoNotFound: "Photo Not Found",
|
||||
PhotoUploadFail: "Photo Upload Failed",
|
||||
|
||||
CategoryNotFound: "Category Not Found",
|
||||
CategoryExists: "Category Already Exists",
|
||||
}
|
||||
|
||||
type CodeError struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
func (e *CodeError) Error() string {
|
||||
return fmt.Sprintf("Code: %d, Msg: %s", e.Code, e.Msg)
|
||||
}
|
||||
|
||||
func New(code int, msg string) *CodeError {
|
||||
return &CodeError{
|
||||
Code: code,
|
||||
Msg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
func NewWithCode(code int) *CodeError {
|
||||
msg, ok := codeText[code]
|
||||
if !ok {
|
||||
msg = codeText[ServerError]
|
||||
}
|
||||
return &CodeError{
|
||||
Code: code,
|
||||
Msg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
func GetHttpStatus(code int) int {
|
||||
switch code {
|
||||
case Success:
|
||||
return http.StatusOK
|
||||
case ParamError:
|
||||
return http.StatusBadRequest
|
||||
case AuthError, TokenExpired, TokenInvalid:
|
||||
return http.StatusUnauthorized
|
||||
case NotFound, UserNotFound, PhotoNotFound, CategoryNotFound:
|
||||
return http.StatusNotFound
|
||||
case Forbidden:
|
||||
return http.StatusForbidden
|
||||
case UserExists, CategoryExists:
|
||||
return http.StatusConflict
|
||||
default:
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user