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.
39 lines
1008 B
39 lines
1008 B
package response
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
)
|
|
|
|
// JsonRes 数据返回通用JSON数据结构
|
|
type JsonRes struct {
|
|
Code int `json:"code"` // 错误码((0:成功, 1:失败, >1:错误码))
|
|
Message string `json:"msg"` // 提示信息
|
|
Data interface{} `json:"data"` // 返回数据(业务接口定义具体数据结构)
|
|
}
|
|
|
|
// Json 返回标准JSON数据。
|
|
func Json(r *ghttp.Request, code int, message string, data ...interface{}) {
|
|
var responseData interface{}
|
|
if len(data) > 0 {
|
|
responseData = data[0]
|
|
} else {
|
|
responseData = g.Map{}
|
|
}
|
|
r.Response.WriteJson(JsonRes{
|
|
Code: code,
|
|
Message: message,
|
|
Data: responseData,
|
|
})
|
|
}
|
|
|
|
// JsonExit 返回标准JSON数据并退出当前HTTP执行函数。
|
|
func JsonExit(r *ghttp.Request, code int, message string, data ...interface{}) {
|
|
if code == http.StatusBadRequest {
|
|
g.Log().Error(r.Context(), code, message)
|
|
}
|
|
Json(r, code, message, data...)
|
|
r.Exit()
|
|
}
|