fix: api ok nil response

This commit is contained in:
liwei committed 2023-02-08 14:59:59 +08:00
1 parent aa9577a2e0
commit 3523f5855a
1 file changed
+6 -4
+6 -4
View File
@@ -15,13 +15,15 @@ func ReadJson(w http.ResponseWriter, r *http.Request, v any) bool {
}
func WriteJsonOk(w http.ResponseWriter, v any) {
WriteJson(w, http.StatusOK, v)
if v == nil {
WriteJson(w, http.StatusOK, model.NewResultWithData[any](nil))
} else {
WriteJson(w, http.StatusOK, v)
}
}
func WriteJson(w http.ResponseWriter, code int, v any) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
if v != nil {
json.NewEncoder(w).Encode(v)
}
json.NewEncoder(w).Encode(v)
}