42 lines
800 B
Go
42 lines
800 B
Go
package app
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-manage-backend/pkg/errcode"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Response 统一响应包装
|
|
type Response struct {
|
|
Ctx *gin.Context
|
|
}
|
|
|
|
func NewResponse(c *gin.Context) *Response {
|
|
return &Response{Ctx: c}
|
|
}
|
|
|
|
// ToResponse 成功响应
|
|
func (r *Response) ToResponse(data interface{}) {
|
|
if data == nil {
|
|
data = gin.H{}
|
|
}
|
|
r.Ctx.JSON(http.StatusOK, gin.H{
|
|
"code": errcode.Success.Code(),
|
|
"msg": errcode.Success.Msg(),
|
|
"data": data,
|
|
})
|
|
}
|
|
|
|
// ToErrorResponse 错误响应
|
|
func (r *Response) ToErrorResponse(err *errcode.Error) {
|
|
response := gin.H{
|
|
"code": err.Code(),
|
|
"msg": err.Msg(),
|
|
}
|
|
if details := err.Details(); len(details) > 0 {
|
|
response["details"] = details
|
|
}
|
|
r.Ctx.JSON(err.StatusCode(), response)
|
|
}
|