68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"gitea.party/public-messag-service/common/response"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type _Default struct {
|
|
Code int `json:"error_code"`
|
|
Msg string `json:"msg"`
|
|
Data interface{} `json:"data"`
|
|
Meta interface{} `json:"meta,omitempty"`
|
|
Errors []string `json:"errors,omitempty"`
|
|
}
|
|
|
|
func (r *_Default) SetCustomError(err response.Error) response.Response {
|
|
response.SetCode(&r.Code, err.Code())
|
|
return r._SetMsg(err.Error())
|
|
}
|
|
|
|
func (r *_Default) SetErrCode(code int) response.Response {
|
|
response.SetErrCode(&r.Code, &r.Msg, code)
|
|
return r
|
|
}
|
|
|
|
func (r *_Default) SetErrMsg(code int, msg string, msgArgs ...interface{}) response.Response {
|
|
response.SetCode(&r.Code, code)
|
|
return r._SetMsg(msg, msgArgs...)
|
|
}
|
|
|
|
func (r *_Default) _SetMsg(msg string, msgArgs ...interface{}) response.Response {
|
|
response.SetMsg(&r.Msg, msg, msgArgs...)
|
|
return r
|
|
}
|
|
|
|
func (r *_Default) SetData(v interface{}) response.Response {
|
|
response.SetData(&r.Data, v)
|
|
return r
|
|
}
|
|
|
|
func (r *_Default) SetDataMsg(v interface{}, msg string, msgArgs ...interface{}) response.Response {
|
|
response.SetData(&r.Data, v)
|
|
response.SetMsg(&r.Msg, msg, msgArgs...)
|
|
return r
|
|
}
|
|
|
|
func (r *_Default) SetMeta(v interface{}) response.Response {
|
|
response.SetMeta(&r.Meta, v)
|
|
return r
|
|
}
|
|
|
|
func (r *_Default) AddError(errs ...error) response.Response {
|
|
response.SetError(&r.Errors, errs...)
|
|
return r
|
|
}
|
|
|
|
func NewResponse() response.Response {
|
|
return &_Default{}
|
|
}
|
|
|
|
func SendResponse(c *gin.Context, req response.Response) func() {
|
|
return func() {
|
|
c.JSON(http.StatusOK, req)
|
|
}
|
|
}
|