This commit is contained in:
toom1996
2026-08-26 10:45:21 +08:00
parent 3cf1368e68
commit 30c9f21da4
48 changed files with 3442 additions and 0 deletions

View File

@ -0,0 +1,34 @@
// Package service 业务逻辑层。
//
// 分层约定:
// - 只依赖 repository 接口,不感知 *gorm.DB,也不感知 *gin.Context;
// - 业务规则(摘要截断长度、精选品牌排名、密码强度、令牌签发)集中在这里,
// handler 只负责 HTTP 参数解析与响应写出。
package service
import "net/http"
// Error 携带 HTTP 状态码的业务错误。
//
// service 层不引入 gin 依赖,但错误的语义(是客户端参数错误还是服务端故障)
// 只有业务层清楚。用状态码 + 文案表达,handler 直接透传,
// 既保持了分层,又能让错误响应与原项目逐字一致。
type Error struct {
Status int
Message string
}
func (e *Error) Error() string { return e.Message }
// NewError 构造业务错误。
func NewError(status int, msg string) *Error {
return &Error{Status: status, Message: msg}
}
func badRequest(msg string) *Error { return NewError(http.StatusBadRequest, msg) }
func unauthorized(msg string) *Error { return NewError(http.StatusUnauthorized, msg) }
func conflict(msg string) *Error { return NewError(http.StatusConflict, msg) }
func internalErr(msg string) *Error { return NewError(http.StatusInternalServerError, msg) }
// ErrArticleNotFound 文章不存在,文案与原项目一致。
var ErrArticleNotFound = NewError(http.StatusNotFound, "文章不存在")