interview-one-stop-server/util/gin_util/gin.go

31 lines
661 B
Go

package gin_util
import (
"context"
"github.com/gin-gonic/gin"
"interview-one-stop-server/util/ierrors"
"interview-one-stop-server/util/log"
)
func GenGinResponse(c *gin.Context, err ierrors.OneStopErr, data interface{}) {
if err.ErrNumber.Code != ierrors.Success.Code {
ctx, _ := c.Get("ctx")
funcName, _ := c.Get("func_name")
log.Error(ctx.(context.Context), funcName.(string), err.Error())
}
c.JSON(200, gin.H{
"code": err.Code,
"message": err.Message,
"data": data,
})
}
func GetContextFromGin(c *gin.Context) context.Context {
ctx, ok := c.Get("ctx")
if ok {
return ctx.(context.Context)
}
return context.Background()
}