2022-11-24 23:37:34 +08:00
|
|
|
|
// Package response
|
|
|
|
|
// @Link https://github.com/bufanyun/hotgo
|
2023-02-23 17:53:04 +08:00
|
|
|
|
// @Copyright Copyright (c) 2023 HotGo CLI
|
2022-11-24 23:37:34 +08:00
|
|
|
|
// @Author Ms <133814250@qq.com>
|
|
|
|
|
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
|
|
|
|
package response
|
|
|
|
|
|
|
|
|
|
import (
|
2023-01-18 16:23:39 +08:00
|
|
|
|
"github.com/gogf/gf/v2/errors/gcode"
|
2022-11-24 23:37:34 +08:00
|
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
|
|
|
"github.com/gogf/gf/v2/os/gctx"
|
2023-06-16 19:58:29 +08:00
|
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
2023-07-03 20:31:29 +08:00
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2022-11-24 23:37:34 +08:00
|
|
|
|
"hotgo/internal/library/contexts"
|
|
|
|
|
"hotgo/internal/model"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// JsonExit 返回JSON数据并退出当前HTTP执行函数
|
|
|
|
|
func JsonExit(r *ghttp.Request, code int, message string, data ...interface{}) {
|
|
|
|
|
RJson(r, code, message, data...)
|
|
|
|
|
r.Exit()
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 20:31:29 +08:00
|
|
|
|
// RXml xml
|
|
|
|
|
func RXml(r *ghttp.Request, code int, message string, data ...interface{}) {
|
|
|
|
|
responseData := interface{}(nil)
|
|
|
|
|
if len(data) > 0 {
|
|
|
|
|
responseData = data[0]
|
|
|
|
|
}
|
|
|
|
|
res := &model.Response{
|
|
|
|
|
Code: code,
|
|
|
|
|
Message: message,
|
|
|
|
|
Timestamp: gtime.Timestamp(),
|
|
|
|
|
TraceID: gctx.CtxId(r.Context()),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果不是正常的返回,则将data转为error
|
|
|
|
|
if gcode.CodeOK.Code() == code {
|
|
|
|
|
res.Data = responseData
|
|
|
|
|
} else {
|
|
|
|
|
res.Error = responseData
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 清空响应
|
|
|
|
|
r.Response.ClearBuffer()
|
|
|
|
|
|
|
|
|
|
// 写入响应
|
|
|
|
|
r.Response.WriteXml(gconv.Map(res))
|
|
|
|
|
|
|
|
|
|
// 加入到上下文
|
|
|
|
|
contexts.SetResponse(r.Context(), res)
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 23:37:34 +08:00
|
|
|
|
// RJson 标准返回结果数据结构封装
|
|
|
|
|
func RJson(r *ghttp.Request, code int, message string, data ...interface{}) {
|
|
|
|
|
responseData := interface{}(nil)
|
|
|
|
|
if len(data) > 0 {
|
|
|
|
|
responseData = data[0]
|
|
|
|
|
}
|
2023-01-18 16:23:39 +08:00
|
|
|
|
res := &model.Response{
|
2022-11-24 23:37:34 +08:00
|
|
|
|
Code: code,
|
|
|
|
|
Message: message,
|
2023-07-03 20:31:29 +08:00
|
|
|
|
Timestamp: gtime.Timestamp(),
|
2022-11-24 23:37:34 +08:00
|
|
|
|
TraceID: gctx.CtxId(r.Context()),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果不是正常的返回,则将data转为error
|
2023-01-18 16:23:39 +08:00
|
|
|
|
if gcode.CodeOK.Code() == code {
|
|
|
|
|
res.Data = responseData
|
2022-11-24 23:37:34 +08:00
|
|
|
|
} else {
|
2023-01-18 16:23:39 +08:00
|
|
|
|
res.Error = responseData
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 清空响应
|
|
|
|
|
r.Response.ClearBuffer()
|
|
|
|
|
|
|
|
|
|
// 写入响应
|
2023-01-18 16:23:39 +08:00
|
|
|
|
r.Response.WriteJson(res)
|
2022-11-24 23:37:34 +08:00
|
|
|
|
|
|
|
|
|
// 加入到上下文
|
2023-01-18 16:23:39 +08:00
|
|
|
|
contexts.SetResponse(r.Context(), res)
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 19:58:29 +08:00
|
|
|
|
// CustomJson 自定义JSON
|
|
|
|
|
func CustomJson(r *ghttp.Request, content interface{}) {
|
|
|
|
|
// 清空响应
|
|
|
|
|
r.Response.ClearBuffer()
|
2022-11-24 23:37:34 +08:00
|
|
|
|
|
2023-06-16 19:58:29 +08:00
|
|
|
|
// 写入响应
|
|
|
|
|
r.Response.WriteJson(content)
|
|
|
|
|
|
|
|
|
|
// 加入到上下文
|
|
|
|
|
contexts.SetResponse(r.Context(), &model.Response{
|
|
|
|
|
Code: 0,
|
|
|
|
|
Message: "",
|
|
|
|
|
Data: content,
|
|
|
|
|
Error: nil,
|
|
|
|
|
Timestamp: gtime.Timestamp(),
|
|
|
|
|
TraceID: gctx.CtxId(r.Context()),
|
|
|
|
|
})
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Redirect 重定向
|
|
|
|
|
func Redirect(r *ghttp.Request, location string, code ...int) {
|
|
|
|
|
r.Response.RedirectTo(location, code...)
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 23:54:50 +08:00
|
|
|
|
// RText 返回成功文本
|
|
|
|
|
func RText(r *ghttp.Request, message string) {
|
|
|
|
|
// 清空响应
|
|
|
|
|
r.Response.ClearBuffer()
|
|
|
|
|
|
|
|
|
|
// 写入响应
|
|
|
|
|
r.Response.Write(message)
|
|
|
|
|
}
|