You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
871 B

package util
import (
"github.com/gin-gonic/gin"
"net/http"
)
/**
@author: sre
@date: 2022/8/19 0019
@desc: todo
**/
type Result struct {
Ctx *gin.Context
}
func NewResult(ctx *gin.Context) *Result {
return &Result{Ctx: ctx}
}
// ResultCont 返回的结果:
type ResultCont struct {
Code int `json:"code"` //提示代码
Msg string `json:"msg"` //提示信息
Data any `json:"data"` //出错
}
// 通用错误处理
func (r *Result) Error(code int, msg string) {
res := ResultCont{}
res.Code = code
res.Msg = msg
res.Data = gin.H{}
if gin.Mode() == gin.ReleaseMode {
res.Msg = "系统繁忙,请稍后再试"
}
r.Ctx.JSON(http.StatusOK, res)
}
// Success 成功
func (r *Result) Success(data any) {
if data == nil {
data = gin.H{}
}
res := ResultCont{}
res.Code = 0
res.Msg = ""
res.Data = data
r.Ctx.JSON(http.StatusOK, res)
}