package resutil import ( "encoding/json" "github.com/go-kratos/kratos/v2/log" "net/http" ) func HttpResponseEncoder(w http.ResponseWriter, r *http.Request, v interface{}) error { if v == nil { _, err := w.Write(nil) return err } data, err := json.Marshal(MakeHttpSuccess(v)) if err != nil { return err } w.Header().Set("Content-Type", "application/json") _, err = w.Write(data) if err != nil { return err } return nil } func HttpErrorEncoder(w http.ResponseWriter, r *http.Request, err error) { data, err := json.Marshal(MakeHttpError(err)) if err != nil { w.WriteHeader(http.StatusInternalServerError) log.Errorf("HttpErrorEncoder json编码失败: %s", err) return } w.Header().Set("Content-Type", "application/json") _, _ = w.Write(data) }