package response import ( "fmt" "time" "twin-api/base/config" "github.com/gin-gonic/gin" ) const defaultTitle = "温馨提示" type Button struct { Text string `json:"text"` Url string `json:"url"` Type string `json:"type"` } 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 { 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 { trace, ok := ctx.Keys[config.TraceId].(string) if !ok { trace = fmt.Sprintf("%d", time.Now().UnixMicro()) } return &Response{ Code: code, Msg: message, Trace: trace, Data: Data{ Buttons: make([]*Button, 0), Window: Window{Title: defaultTitle, Close: true}, }, } } // Button 用传入的一组按钮替换掉原有的全部按钮 func (r *Response) Button(b ...*Button) *Response { r.Data.Buttons = append(r.Data.Buttons[:0], b...) return r } func (r *Response) Message(message string) *Response { r.Msg = message return r } func (r *Response) Title(t string) *Response { r.Data.Window.Title = t return r } func (r *Response) Closable(b bool) *Response { r.Data.Window.Close = b return r }