1 Star 0 Fork 0

伍凯歌/GinFrame

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
response.go 1.25 KB
一键复制 编辑 原始数据 按行查看 历史
wukaige 提交于 2022-04-21 10:25 +08:00 . init
package gf
import (
"encoding/json"
"net/http"
)
var ResponseDataNil = struct {
}{}
type Response struct {
Code string `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
func (res Response) GetBytes() []byte {
b, _ := json.Marshal(res)
return b
}
func (res Response) GetString() string {
b, _ := json.Marshal(res)
return string(b[:])
}
func getDefaultErrorResponse(err IError) Response {
var data interface{}
//I18N TODO
if err.Error() == "操作失败" {
data = err.GetDetail()
}
if data == nil {
data = ResponseDataNil
}
return Response{
err.GetCode(), err.GetMsg(), data,
}
}
func getResponseWithCode(code string, data ...interface{}) Response {
if code == "" {
code = SuccessCode
}
msg := DefaultCodeMapping.GetCodeInfo(code)
var r = Response{
Code: code,
Msg: msg,
Data: nil,
}
if data == nil {
return r
}
l := len(data)
if l > 0 {
if l == 1 {
r.Data = data[0]
} else {
r.Data = data
}
}
return r
}
func ResponseStr(c *Context, str string) {
rStr(c, str)
}
func ResponseJson(c *Context, data interface{}) {
rJson(c, data)
}
func rStr(c *Context, str string) {
c.String(http.StatusOK, str)
}
func rJson(c *Context, data interface{}) {
c.JSON(http.StatusOK, data)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kingsingnal/go-gin-frame.git
git@gitee.com:kingsingnal/go-gin-frame.git
kingsingnal
go-gin-frame
GinFrame
0592110cac6f

搜索帮助