From b9b9a9c738a544ad40777e04143e3fb206961a41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Fri, 12 Aug 2022 13:39:58 +0800 Subject: [PATCH] =?UTF-8?q?enhance(response):=E8=B0=83=E6=95=B4=E5=9F=BA?= =?UTF-8?q?=E7=A1=80=E5=93=8D=E5=BA=94=E6=9E=84=E5=BB=BA=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/user.go | 12 ++++++++---- response/base_response.go | 8 +++++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/controller/user.go b/controller/user.go index 000a8cb..e56a97c 100644 --- a/controller/user.go +++ b/controller/user.go @@ -96,7 +96,7 @@ func (_UserController) InvalidUserPassword(c *gin.Context) { result.Error(500, err.Error()) return } - result.QuickJson(http.StatusOK, http.StatusAccepted, "用户密码已经失效", gin.H{"verify": verifyCode}) + result.Json(http.StatusOK, http.StatusAccepted, "用户密码已经失效", gin.H{"verify": verifyCode}) } type ResetPasswordFormData struct { @@ -110,14 +110,18 @@ func (_UserController) ResetUserPassword(c *gin.Context) { resetForm := new(ResetPasswordFormData) c.BindJSON(resetForm) verified, err := service.UserService.VerifyUserPassword(resetForm.Username, resetForm.VerifyCode) - if !verified { - result.Error(http.StatusUnauthorized, "验证码不正确。") + if errors.Is(err, &exceptions.NotFoundError{}) { + result.NotFound("指定的用户不存在。") return } if err != nil { result.Error(http.StatusInternalServerError, err.Error()) return } + if !verified { + result.Error(http.StatusUnauthorized, "验证码不正确。") + return + } completed, err := service.UserService.ResetUserPassword(resetForm.Username, resetForm.NewPassword) if err != nil { result.Error(http.StatusInternalServerError, err.Error()) @@ -127,5 +131,5 @@ func (_UserController) ResetUserPassword(c *gin.Context) { result.Success("用户凭据已更新。") return } - result.Error(http.StatusNotAcceptable, "用户凭据未能成功更新。") + result.NotAccept("用户凭据未能成功更新。") } diff --git a/response/base_response.go b/response/base_response.go index 58e8ed0..df479c7 100644 --- a/response/base_response.go +++ b/response/base_response.go @@ -15,6 +15,12 @@ type BaseResponse struct { Message string `json:"message"` } +type PagedResponse struct { + Page int `json:"page"` + Size int `json:"pageSize"` + Total int `json:"total"` +} + func NewResult(ctx *gin.Context) *Result { return &Result{Ctx: ctx} } @@ -77,7 +83,7 @@ func (r *Result) NotFound(msg string) { // 快速自由JSON格式响应 // ! 注意,给定的map中,同名的键会被覆盖。 -func (r *Result) QuickJson(status, code int, msg string, payloads ...map[string]interface{}) { +func (r *Result) Json(status, code int, msg string, payloads ...map[string]interface{}) { var finalPayload = make(map[string]interface{}, 0) finalPayload["code"] = code finalPayload["message"] = &msg