修复Reply结构体中的字段名称,更新成功和错误响应方法以使用新的字段

This commit is contained in:
yanweidong 2025-10-14 14:48:44 +08:00
parent 8aafcbd91c
commit 179157f49e
1 changed files with 8 additions and 4 deletions

View File

@ -3,6 +3,8 @@
package infra package infra
import ( import (
"time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
) )
@ -13,7 +15,8 @@ var Response Reply
type Reply struct { type Reply struct {
Code int32 `json:"code"` // 响应码 Code int32 `json:"code"` // 响应码
Message string `json:"message"` // 响应消息 Message string `json:"message"` // 响应消息
Result any `json:"result"` // 响应数据 Details any `json:"details"` // 响应数据
Timeseq int64 `json:"timeseq"` // 时间戳序列
} }
// Success 返回成功响应 // Success 返回成功响应
@ -21,10 +24,11 @@ type Reply struct {
// data: 响应数据 // data: 响应数据
func (reply *Reply) Success(ctx *gin.Context, data any) { func (reply *Reply) Success(ctx *gin.Context, data any) {
reply.Code = 0 reply.Code = 0
reply.Result = data reply.Details = data
reply.Message = "" reply.Message = ""
reply.Timeseq = time.Now().UnixMilli()
if data == nil { if data == nil {
reply.Result = "" reply.Details = ""
} }
ctx.JSON(200, reply) ctx.JSON(200, reply)
} }
@ -34,7 +38,7 @@ func (reply *Reply) Success(ctx *gin.Context, data any) {
// err: 错误对象 // err: 错误对象
func (reply *Reply) Error(ctx *gin.Context, err error) { func (reply *Reply) Error(ctx *gin.Context, err error) {
reply.Code = 500 reply.Code = 500
reply.Result = "" reply.Details = ""
// 默认状态码为500 // 默认状态码为500
e, ok := status.FromError(err) e, ok := status.FromError(err)
if ok { if ok {