feat: response
This commit is contained in:
parent
31692fa9ca
commit
909e962a5c
|
|
@ -0,0 +1,72 @@
|
||||||
|
package response
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"twin-api/base/config"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Button struct {
|
||||||
|
Color string `json:"-"`
|
||||||
|
Text string `json:"text"`
|
||||||
|
Url string `json:"url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Window struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
Close bool `json:"close"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Data struct {
|
||||||
|
Buttons []Button `json:"buttons"`
|
||||||
|
Window Window `json:"window"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Response struct {
|
||||||
|
ctx *gin.Context
|
||||||
|
Code int `json:"code"`
|
||||||
|
Msg string `json:"message"`
|
||||||
|
Trace string `json:"trace"`
|
||||||
|
Data Data `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(ctx *gin.Context, code int, message string) *Response {
|
||||||
|
var trace string
|
||||||
|
var ok bool
|
||||||
|
if ctx.Keys != nil {
|
||||||
|
if trace, ok = ctx.Keys["req_id"].(string); !ok {
|
||||||
|
trace = fmt.Sprintf("%d", time.Now().UnixMicro())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
response := new(Response)
|
||||||
|
response.Code = code
|
||||||
|
response.Msg = message
|
||||||
|
response.Trace = trace
|
||||||
|
response.Data.Buttons = make([]Button, 0)
|
||||||
|
response.Data.Window = Window{Title: "温馨提示", Close: true}
|
||||||
|
return response
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Response) Success(ctx *gin.Context) *Response {
|
||||||
|
resp := New(ctx, config.Success, "success")
|
||||||
|
resp.Window(&Window{Title: "", Close: true})
|
||||||
|
resp.Data.Buttons = make([]Button, 0)
|
||||||
|
return resp
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Response) Botton(b *Button) *Response {
|
||||||
|
r.Data.Buttons = append(r.Data.Buttons, *b)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Response) Window(win *Window) *Response {
|
||||||
|
if win.Title != "" {
|
||||||
|
r.Data.Window.Title = win.Title
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Data.Window.Close = win.Close
|
||||||
|
return r
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue