healthapp
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

41 lines
821 B

package response
import (
"net/http"
"healthapi/pkg/errorx"
"github.com/zeromicro/go-zero/rest/httpx"
)
// Response 统一响应结构
type Response struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
// Success 成功响应
func Success(w http.ResponseWriter, data interface{}) {
httpx.OkJson(w, Response{
Code: 0,
Message: "success",
Data: data,
})
}
// Error 错误响应
func Error(w http.ResponseWriter, err error) {
if codeErr, ok := err.(*errorx.CodeError); ok {
httpx.WriteJson(w, http.StatusBadRequest, Response{
Code: codeErr.Code,
Message: codeErr.Msg,
})
return
}
httpx.WriteJson(w, http.StatusInternalServerError, Response{
Code: errorx.CodeServerError,
Message: err.Error(),
})
}