package result import ( "github.com/gin-gonic/gin" "mc-client-updater-server/pkg/common" "net/http" ) type Result struct { ctx *gin.Context } type Root struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data"` } func NewResult(c *gin.Context) *Result { return &Result{ctx: c} } func (r *Result) Success(data interface{}) { if data == nil { data = gin.H{} } res := Root{ Code: http.StatusOK, Msg: "", Data: data, } r.ctx.JSON(http.StatusOK, res) r.ctx.Next() } func (r *Result) Fail(code int, msg string) { res := Root{ Code: code, Msg: msg, Data: gin.H{}, } r.ctx.JSON(http.StatusOK, res) r.ctx.Abort() } //func (r *Result) Redirect302(location string) { // r.ctx.Header("Location", location) // r.ctx.Status(http.StatusFound) // r.ctx.Next() //} func (r *Result) InternalServerError(msg string) { r.Fail(http.StatusInternalServerError, msg) } func (r *Result) BadRequest() { r.Fail(http.StatusBadRequest, "请求参数错误") } func (r *Result) LoginError() { r.Fail(common.LoginErrorCode, "账号或密码错误") } func (r *Result) Unauthorized() { r.Fail(http.StatusUnauthorized, "未登录") } func (r *Result) NoPermission() { r.Fail(common.NoPermission, "权限不足") } func (r *Result) LoginExpired() { r.Fail(common.LoginExpired, "登录过期") }