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.
128 lines
3.4 KiB
128 lines
3.4 KiB
package response
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
)
|
|
|
|
// JsonRes 数据返回通用JSON数据结构
|
|
type JsonRes struct {
|
|
Code int `json:"code"` // 错误码((0:成功, 1:失败, >1:错误码))
|
|
Message string `json:"msg"` // 提示信息
|
|
Data interface{} `json:"data"` // 返回数据(业务接口定义具体数据结构)
|
|
// Redirect string `json:"redirect"` // 引导客户端跳转到指定路由
|
|
}
|
|
|
|
// 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()
|
|
}
|
|
|
|
func dataReturn(r *ghttp.Request, code int, req ...interface{}) *JsonRes {
|
|
var msg string
|
|
var data interface{}
|
|
if len(req) > 0 {
|
|
msg = gconv.String(req[0])
|
|
}
|
|
if len(req) > 1 {
|
|
data = req[1]
|
|
}
|
|
// msg = GetCodeMsg(code, msg)
|
|
if code != 1 && !gconv.Bool(r.GetCtxVar("api_code")) {
|
|
code = 0
|
|
}
|
|
response := &JsonRes{
|
|
// ID: r.GetCtxVar("RequestId").String(),
|
|
Code: code,
|
|
Message: msg,
|
|
Data: data,
|
|
}
|
|
r.SetParam("apiReturnRes", response)
|
|
return response
|
|
}
|
|
|
|
// Auth 认证失败
|
|
func Auth(r *ghttp.Request) {
|
|
res := dataReturn(r, 999, "请登录")
|
|
r.Response.WriteJsonExit(res)
|
|
}
|
|
|
|
// AuthBlack Auth 认证失败 被冻结拉黑
|
|
func AuthBlack(r *ghttp.Request) {
|
|
res := dataReturn(r, 888, "您的账号被冻结拉黑,请联系管理员")
|
|
r.Response.WriteJsonExit(res)
|
|
}
|
|
|
|
// JsonRedirect 返回标准JSON数据引导客户端跳转。
|
|
func JsonRedirect(r *ghttp.Request, code int, message, redirect string, data ...interface{}) {
|
|
responseData := interface{}(nil)
|
|
if len(data) > 0 {
|
|
responseData = data[0]
|
|
}
|
|
r.Response.WriteJson(JsonRes{
|
|
Code: code,
|
|
Message: message,
|
|
Data: responseData,
|
|
// Redirect: redirect,
|
|
})
|
|
}
|
|
|
|
// JsonRedirectExit 返回标准JSON数据引导客户端跳转,并退出当前HTTP执行函数。
|
|
func JsonRedirectExit(r *ghttp.Request, code int, message, redirect string, data ...interface{}) {
|
|
JsonRedirect(r, code, message, redirect, data...)
|
|
r.Exit()
|
|
}
|
|
|
|
func SuccessWithData(r *ghttp.Request, data interface{}) {
|
|
res := dataReturn(r, 1, "ok", data)
|
|
r.Response.WriteJsonExit(res)
|
|
}
|
|
|
|
// JsonResponse 数据返回通用JSON数据结构
|
|
type JsonResponse struct {
|
|
// ID string `json:"id"` //
|
|
Code int `json:"code"` // 错误码((1:成功, 0:失败, >1:错误码))
|
|
Message string `json:"message"` // 提示信息
|
|
Data interface{} `json:"data,omitempty"` // 返回数据(业务接口定义具体数据结构)
|
|
Redirect string `json:"redirect,omitempty"` // 引导客户端跳转到指定路由
|
|
}
|
|
|
|
/*错误,只有错误信息,错误码0*/
|
|
func Error(msg string) *JsonRes {
|
|
return &JsonRes{
|
|
Code: 400, // 错误码
|
|
Message: msg, // 错误信息
|
|
Data: nil,
|
|
}
|
|
}
|
|
|
|
/*成功,有数据*/
|
|
func SuccessWithDataNoReq(data interface{}) *JsonRes {
|
|
return &JsonRes{
|
|
Code: 200,
|
|
Message: "success",
|
|
Data: data,
|
|
}
|
|
}
|