1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package webutils
- import (
- "net/http"
- "daxia.dev/internal/dxpay/kkerror"
- "github.com/gin-gonic/gin"
- ut "github.com/go-playground/universal-translator"
- "github.com/sirupsen/logrus"
- )
- const (
- CodeSuccess = 20000
- CodeFailed = -1
- CodeIllegalToken = 50008
- CodeTokenExpired = 50014
- CodeOther = 50012
- )
- var (
- trans ut.Translator
- )
- type Response struct {
- Code int `json:"code"`
- Msg string `json:"msg"`
- Data interface{} `json:"data"`
- }
- type ResponseGen[T interface{}] struct {
- Code int `json:"code"`
- Msg string `json:"msg"`
- Data T `json:"data"`
- }
- func FailedResponse(c *gin.Context, msg string) {
- if c == nil {
- return
- }
- c.JSON(http.StatusOK, &Response{
- Code: CodeFailed,
- Msg: msg,
- Data: nil,
- })
- panic(kkerror.NewRespError("failed"))
- }
- func FailedResponseErr(c *gin.Context, err error) {
- if c == nil {
- return
- }
- logrus.Error(err)
- FailedResponse(c, "内部错误")
- }
- func FailedResponseWithCode(c *gin.Context, code int, msg string) {
- c.JSON(http.StatusOK, &Response{
- Code: code,
- Msg: msg,
- Data: nil,
- })
- panic(kkerror.NewRespError("failed"))
- }
- func SuccessResponse(c *gin.Context, data interface{}) {
- c.JSON(http.StatusOK, &Response{
- Code: CodeSuccess,
- Msg: "",
- Data: data,
- })
- panic(kkerror.NewRespError("success"))
- }
|