From b6b69896cbde480654491ae4d9f1e41615a2542c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 23 Aug 2022 21:37:24 +0800 Subject: [PATCH] =?UTF-8?q?refactor(response):=E5=AE=8C=E5=85=A8=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E5=BF=AB=E9=80=9F=E5=93=8D=E5=BA=94=E5=87=BD=E6=95=B0?= =?UTF-8?q?=EF=BC=8C=E4=BD=BF=E4=B9=8B=E5=8F=AF=E4=BB=A5=E6=96=B9=E4=BE=BF?= =?UTF-8?q?=E7=9A=84=E6=89=A9=E5=B1=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- response/base_response.go | 157 ++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 93 deletions(-) diff --git a/response/base_response.go b/response/base_response.go index 28826b6..cf2d3f8 100644 --- a/response/base_response.go +++ b/response/base_response.go @@ -26,100 +26,10 @@ func NewResult(ctx *gin.Context) *Result { return &Result{Ctx: ctx} } -// 操作出错(未成功)响应 -func (r *Result) Failure(code int, msg string) { - res := BaseResponse{} - res.Code = code - res.Message = msg - r.Ctx.JSON(http.StatusInternalServerError, res) -} - -// 操作出错(未成功)响应 -func (r *Result) Error(code int, msg string) { - res := BaseResponse{} - res.Code = code - res.Message = msg - r.Ctx.JSON(http.StatusOK, res) -} - -// 用户未获得授权)响应 -func (r *Result) Unauthorized(msg string) { - res := BaseResponse{} - res.Code = http.StatusUnauthorized - res.Message = msg - r.Ctx.JSON(http.StatusOK, 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.StatusOK, res) -} - -// 数据成功更新 -func (r *Result) Updated(msg string) { - res := BaseResponse{} - res.Code = http.StatusAccepted - res.Message = msg - r.Ctx.JSON(http.StatusOK, res) -} - -// 数据已成功删除 -func (r *Result) Deleted(msg string) { - res := BaseResponse{} - res.Code = http.StatusNoContent - res.Message = msg - r.Ctx.JSON(http.StatusOK, res) -} - -// 指定操作未被接受 -func (r *Result) BadRequest(msg string) { - res := BaseResponse{} - res.Code = http.StatusBadRequest - res.Message = msg - r.Ctx.JSON(http.StatusOK, res) -} - -// 指定操作未被接受 -func (r *Result) NotAccept(msg string) { - res := BaseResponse{} - res.Code = http.StatusNotAcceptable - res.Message = msg - r.Ctx.JSON(http.StatusOK, res) -} - -// 数据未找到 -func (r *Result) NotFound(msg string) { - res := BaseResponse{} - res.Code = http.StatusNotFound - res.Message = msg - r.Ctx.JSON(http.StatusOK, res) -} - -// 数据存在冲突 -func (r *Result) Conflict(msg string) { - res := BaseResponse{} - res.Code = http.StatusConflict - res.Message = msg - r.Ctx.JSON(http.StatusOK, res) -} - -// 快速自由JSON格式响应 -// ! 注意,给定的map中,同名的键会被覆盖。 -func (r *Result) Json(code int, msg string, payloads ...map[string]interface{}) { +func (r *Result) response(status, code int, msg string, payloads ...map[string]interface{}) { var finalPayload = make(map[string]interface{}, 0) finalPayload["code"] = code - finalPayload["message"] = &msg + finalPayload["message"] = msg for _, m := range payloads { for k, v := range m { @@ -127,7 +37,68 @@ func (r *Result) Json(code int, msg string, payloads ...map[string]interface{}) } } - r.Ctx.JSON(http.StatusOK, finalPayload) + r.Ctx.JSON(status, finalPayload) +} + +// 操作出错(未成功)响应 +func (r *Result) Failure(code int, msg string) { + r.response(http.StatusInternalServerError, code, msg) +} + +// 操作出错(未成功)响应 +func (r *Result) Error(code int, msg string) { + r.response(http.StatusOK, code, msg) +} + +// 用户未获得授权)响应 +func (r *Result) Unauthorized(msg string) { + r.response(http.StatusOK, http.StatusUnauthorized, msg) +} + +// 简易操作成功信息 +func (r *Result) Success(msg string) { + r.response(http.StatusOK, http.StatusOK, msg) +} + +// 数据成功创建 +func (r *Result) Created(msg string, payloads ...map[string]interface{}) { + r.response(http.StatusOK, http.StatusCreated, msg, payloads...) +} + +// 数据成功更新 +func (r *Result) Updated(msg string) { + r.response(http.StatusOK, http.StatusAccepted, msg) +} + +// 数据已成功删除 +func (r *Result) Deleted(msg string) { + r.response(http.StatusOK, http.StatusNoContent, msg) +} + +// 指定操作未被接受 +func (r *Result) BadRequest(msg string) { + r.response(http.StatusOK, http.StatusBadRequest, msg) +} + +// 指定操作未被接受 +func (r *Result) NotAccept(msg string) { + r.response(http.StatusOK, http.StatusNotAcceptable, msg) +} + +// 数据未找到 +func (r *Result) NotFound(msg string) { + r.response(http.StatusOK, http.StatusNotFound, msg) +} + +// 数据存在冲突 +func (r *Result) Conflict(msg string) { + r.response(http.StatusOK, http.StatusConflict, msg) +} + +// 快速自由JSON格式响应 +// ! 注意,给定的map中,同名的键会被覆盖。 +func (r *Result) Json(code int, msg string, payloads ...map[string]interface{}) { + r.response(http.StatusOK, code, msg, payloads...) } func NewPagedResponse(page int, total int64) *PagedResponse {