From 2281ebe00bd013aab624f59e1413486423e1b80e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Wed, 10 Aug 2022 15:26:59 +0800 Subject: [PATCH] =?UTF-8?q?enhance(response):=E6=9B=B4=E6=96=B0=E5=9F=BA?= =?UTF-8?q?=E6=9C=AC=E7=9A=84=E7=AE=80=E6=98=93=E5=93=8D=E5=BA=94=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=87=BD=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- response/base-response.go | 50 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) 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) +}