diff --git a/response/base-response.go b/response/base-response.go index 756492f..0b04e0c 100644 --- a/response/base-response.go +++ b/response/base-response.go @@ -19,10 +19,58 @@ func NewResult(ctx *gin.Context) *Result { return &Result{Ctx: ctx} } -// 统一出错信息 +// 操作出错(未成功)响应 func (r *Result) Error(code int, msg string) { res := BaseResponse{} res.Code = code res.Message = msg + r.Ctx.JSON(http.StatusInternalServerError, res) +} + +// 简易操作成功信息 +func (r *Result) Success(msg string) { + res := BaseResponse{} + res.Code = http.StatusOK + res.Message = msg r.Ctx.JSON(http.StatusOK, res) } + +// 数据成功创建 +func (r *Result) Created(msg string) { + res := BaseResponse{} + res.Code = http.StatusCreated + res.Message = msg + r.Ctx.JSON(http.StatusCreated, res) +} + +// 数据成功更新 +func (r *Result) Updated(msg string) { + res := BaseResponse{} + res.Code = http.StatusAccepted + res.Message = msg + r.Ctx.JSON(http.StatusAccepted, res) +} + +// 数据已成功删除 +func (r *Result) Deleted(msg string) { + res := BaseResponse{} + res.Code = http.StatusNoContent + res.Message = msg + r.Ctx.JSON(http.StatusNoContent, res) +} + +// 指定操作未被接受 +func (r *Result) NotAccept(msg string) { + res := BaseResponse{} + res.Code = http.StatusNotAcceptable + res.Message = msg + r.Ctx.JSON(http.StatusNotAcceptable, res) +} + +// 数据未找到 +func (r *Result) NotFound(msg string) { + res := BaseResponse{} + res.Code = http.StatusNotFound + res.Message = msg + r.Ctx.JSON(http.StatusNotFound, res) +}