54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
)
|
|
|
|
// JsonRes 统一 JSON 响应格式
|
|
type JsonRes struct {
|
|
Code int `json:"code"` // 0=成功, -1=失败, 401=未登录
|
|
Message string `json:"message"` // 提示信息
|
|
Data interface{} `json:"data,omitempty"` // 数据(可选)
|
|
Count int `json:"count,omitempty"` // 分页总数(列表专用)
|
|
}
|
|
|
|
// jsonRes 返回统一 JSON 响应
|
|
func jsonRes(r *ghttp.Request, code int, message string, data interface{}) {
|
|
r.Response.WriteJson(JsonRes{
|
|
Code: code,
|
|
Message: message,
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
// jsonList 返回统一分页列表 JSON
|
|
func jsonList(r *ghttp.Request, code int, message string, count int, data interface{}) {
|
|
r.Response.WriteJson(JsonRes{
|
|
Code: code,
|
|
Message: message,
|
|
Count: count,
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
// jsonSuccess 操作成功快捷响应
|
|
func jsonSuccess(r *ghttp.Request, data interface{}) {
|
|
jsonRes(r, 0, "操作成功", data)
|
|
}
|
|
|
|
// jsonSuccessMsg 操作成功自定义消息
|
|
func jsonSuccessMsg(r *ghttp.Request, msg string) {
|
|
jsonRes(r, 0, msg, nil)
|
|
}
|
|
|
|
// jsonError 操作失败响应
|
|
func jsonError(r *ghttp.Request, msg string) {
|
|
jsonRes(r, -1, msg, nil)
|
|
}
|
|
|
|
// jsonUnauthorized 未登录/未授权响应
|
|
func jsonUnauthorized(r *ghttp.Request) {
|
|
jsonRes(r, 401, "请先登录", nil)
|
|
r.Exit()
|
|
}
|