package response import ( "net/http" "github.com/zeromicro/go-zero/rest/httpx" "photography-backend/pkg/errorx" ) type Body struct { Code int `json:"code"` Message string `json:"message"` Data interface{} `json:"data,omitempty"` } func Response(w http.ResponseWriter, resp interface{}, err error) { var body Body if err != nil { if e, ok := err.(*errorx.CodeError); ok { body.Code = e.Code body.Message = e.Msg httpx.WriteJson(w, errorx.GetHttpStatus(e.Code), body) } else { body.Code = errorx.ServerError body.Message = err.Error() httpx.WriteJson(w, http.StatusInternalServerError, body) } } else { body.Code = errorx.Success body.Message = "success" body.Data = resp httpx.OkJson(w, body) } } func Success(w http.ResponseWriter, data interface{}) { Response(w, data, nil) } func Error(w http.ResponseWriter, err error) { Response(w, nil, err) }