mirror of
https://github.com/wonli/aqi.git
synced 2024-06-28 09:57:05 +08:00
43 lines
625 B
Go
43 lines
625 B
Go
package ws
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Response struct {
|
|
Ctx *ApiData
|
|
|
|
g *gin.Context
|
|
httpCode int
|
|
}
|
|
|
|
func (r *Response) WithData(data any) *Response {
|
|
r.Ctx.Data = data
|
|
return r
|
|
}
|
|
|
|
func (r *Response) WithError(e Error, err error) *Response {
|
|
r.Ctx.Code = e.Code
|
|
if err != nil {
|
|
r.Ctx.Msg = fmt.Sprintf("%s,%s", e.Msg, err.Error())
|
|
} else {
|
|
r.Ctx.Msg = e.Msg
|
|
}
|
|
|
|
return r
|
|
}
|
|
|
|
func (r *Response) Send() {
|
|
r.g.JSON(r.httpCode, r.Ctx)
|
|
}
|
|
|
|
func NewResponse(g *gin.Context, httpCode int) *Response {
|
|
return &Response{
|
|
g: g,
|
|
httpCode: httpCode,
|
|
Ctx: &ApiData{},
|
|
}
|
|
}
|