2022-09-27 17:59:05 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/translator"
|
2022-09-27 17:59:05 +08:00
|
|
|
"github.com/segmentfault/pacman/errors"
|
|
|
|
"github.com/segmentfault/pacman/i18n"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RespBody response body.
|
|
|
|
type RespBody struct {
|
|
|
|
// http code
|
|
|
|
Code int `json:"code"`
|
|
|
|
// reason key
|
|
|
|
Reason string `json:"reason"`
|
|
|
|
// response message
|
|
|
|
Message string `json:"msg"`
|
|
|
|
// response data
|
|
|
|
Data interface{} `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrMsg translate the reason cause as a message
|
|
|
|
func (r *RespBody) TrMsg(lang i18n.Language) *RespBody {
|
|
|
|
if len(r.Message) == 0 {
|
2023-01-06 18:00:51 +08:00
|
|
|
r.Message = translator.Tr(lang, r.Reason)
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRespBody new response body
|
|
|
|
func NewRespBody(code int, reason string) *RespBody {
|
|
|
|
return &RespBody{
|
|
|
|
Code: code,
|
|
|
|
Reason: reason,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRespBodyFromError new response body from error
|
|
|
|
func NewRespBodyFromError(e *errors.Error) *RespBody {
|
|
|
|
return &RespBody{
|
|
|
|
Code: e.Code,
|
|
|
|
Reason: e.Reason,
|
|
|
|
Message: e.Message,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRespBodyData new response body with data
|
|
|
|
func NewRespBodyData(code int, reason string, data interface{}) *RespBody {
|
|
|
|
return &RespBody{
|
|
|
|
Code: code,
|
|
|
|
Reason: reason,
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
}
|