调整封装

This commit is contained in:
Yun
2026-07-22 01:17:07 +08:00
parent d3786198df
commit 69f90115dc
10 changed files with 1024 additions and 107 deletions
+43 -20
View File
@@ -18,11 +18,11 @@ type response struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
Pagination *pagination `json:"pagination,omitempty"`
Pagination *Pagination `json:"pagination,omitempty"`
TraceId string `json:"trace_id,omitempty"`
}
type pagination struct {
type Pagination struct {
Page int64 `json:"page"`
Size int64 `json:"size"`
TotalPage int64 `json:"total_page"`
@@ -44,8 +44,8 @@ func GetOffset(page, size int) (offset int, limit int) {
}
// 计算响应分页
func GetPage(page, size, totalCount int64) pagination {
pa := pagination{}
func GetPage(page, size, totalCount int64) Pagination {
pa := Pagination{}
totalPage := 0
if size != 0 {
totalPage = int(math.Ceil(float64(totalCount) / float64(size)))
@@ -58,47 +58,58 @@ func GetPage(page, size, totalCount int64) pagination {
}
// Msg格式化响应
func FormatMessage(ctx context.Context, w http.ResponseWriter, msgKey string, format map[string]string, data interface{}) {
func FormatMessage(ctx context.Context, w http.ResponseWriter, msgKey string, format map[string]string, data interface{}, ops ...Option) {
code, msg := langx.GetTransFormatCtx(ctx, msgKey, format)
ResponseCtx(ctx, w, SetCode(code), SetMessage(msg), SetData(data))
baseOps := []Option{SetCode(code), SetMessage(msg), SetData(data)}
baseOps = append(baseOps, ops...)
ResponseCtx(ctx, w, baseOps...)
}
// 成功的响应
func Success(ctx context.Context, w http.ResponseWriter, data any) {
ResponseCtx(ctx, w, SetCode(op.defaultSuccessCode), SetMessage("请求成功"), SetData(data))
func Success(ctx context.Context, w http.ResponseWriter, data any, ops ...Option) {
baseOps := []Option{SetCode(op.defaultSuccessCode), SetMessage("请求成功"), SetData(data)}
baseOps = append(baseOps, ops...)
ResponseCtx(ctx, w, baseOps...)
}
func SuccessWithPage(ctx context.Context, w http.ResponseWriter, data any, page *pagination) {
ResponseCtx(ctx, w, SetCode(op.defaultSuccessCode), SetMessage("请求成功"), SetData(data), SetPage(page))
func SuccessWithPage(ctx context.Context, w http.ResponseWriter, data any, page *Pagination, ops ...Option) {
baseOps := []Option{SetCode(op.defaultSuccessCode), SetMessage("请求成功"), SetData(data), SetPage(page)}
baseOps = append(baseOps, ops...)
ResponseCtx(ctx, w, baseOps...)
}
// 失败的响应
func Error(ctx context.Context, w http.ResponseWriter, err error) {
ErrorWithData(ctx, w, err, "")
func Error(ctx context.Context, w http.ResponseWriter, err error, ops ...Option) {
ErrorWithData(ctx, w, err, "", ops...)
}
func ErrorWithData(ctx context.Context, w http.ResponseWriter, err error, data interface{}) {
func ErrorWithData(ctx context.Context, w http.ResponseWriter, err error, data interface{}, ops ...Option) {
code := op.defaultErrorCode
msg := "请求失败"
if err != nil {
val, ok := err.(errorx.ErrorInterface)
val,ok := errorx.As(err)
if ok {
if langx.GetDefaultCode() != val.GetCode() {
code = val.GetCode()
if langx.GetDefaultCode() != val.Code() {
code = val.Code()
}
}
msg = err.Error()
}
ResponseCtx(ctx, w, SetCode(code), SetMessage(msg), SetData(data))
baseOps := []Option{SetCode(code), SetMessage(msg), SetData(data)}
baseOps = append(baseOps, ops...)
ResponseCtx(ctx, w, baseOps...)
}
func ErrorStr(ctx context.Context, w http.ResponseWriter, msg string) {
func ErrorStr(ctx context.Context, w http.ResponseWriter, msg string, ops ...Option) {
msg = langx.GetMsgCtx(ctx, msg)
code := langx.GetCode(msg)
if code == langx.GetDefaultCode() {
code = op.defaultErrorCode
}
ResponseCtx(ctx, w, SetCode(code), SetMessage(msg))
baseOps := []Option{SetCode(code), SetMessage(msg)}
baseOps = append(baseOps, ops...)
ResponseCtx(ctx, w, baseOps...)
}
// 基础的响应
@@ -136,8 +147,20 @@ func ResponseCtx(ctx context.Context, w http.ResponseWriter, ops ...Option) {
def.logger.Info(ctx, "response:", string(bs))
}
// 写入自定义响应头
for k, v := range def.headers {
w.Header().Set(k, v)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
// 确定HTTP状态码
httpStatus := def.httpStatusCode
if httpStatus == 0 {
httpStatus = 200
}
w.WriteHeader(httpStatus)
n, err := w.Write(bs)
if err != nil || n != len(bs) {
op.logger.Error(ctx, "write response failed err:%+v", err)