Skip to content

Commit

Permalink
优化返回结果
Browse files Browse the repository at this point in the history
  • Loading branch information
hulutech-web committed Oct 29, 2024
1 parent cf5d7bc commit 68c5453
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion http_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,22 @@ func (h *HttpResult) Success(message string, data interface{}) http.Response {
message = facades.Config().GetString("http_result.Message")
}
//查询的结果有可能存在data为nil的情况,判断如果是nil则,给定一个[]string{}类型的数据,避免前端页面报错
//或者判断data的slice类型的长度为0
res := []string{}
if data == nil {
data = []string{}
return h.Context.Response().Success().Json(http.Json{
"message": message,
"data": res,
})
}
slice, ok := data.([]interface{})
if ok && len(slice) == 0 {
return h.Context.Response().Success().Json(http.Json{
"message": message,
"data": res,
})
}

return h.Context.Response().Success().Json(http.Json{
"message": message,
"data": data,
Expand Down

0 comments on commit 68c5453

Please sign in to comment.