From 66f17ef709b05e80b2eaef6f2b8190109de96937 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Mon, 20 Oct 2025 20:24:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8B=A6=E6=88=AA=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E4=BF=AE=E6=94=B9=E5=8F=91=E9=80=81=E9=82=AE?= =?UTF-8?q?=E4=BB=B6=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/tool.go | 38 +++++++++++++++----------------------- main.go | 2 +- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/handler/tool.go b/handler/tool.go index 390bc3b..6131452 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -35,9 +35,9 @@ type GetFileListReq struct { } type SendMailReq struct { - Title string `json:"title" form:"title"` - Content string `json:"content" form:"content"` - To string `json:"to" form:"to"` + Title string `json:"title" form:"title" binding:"required"` + Content string `json:"content" form:"content" binding:"required"` + To string `json:"to" form:"to" binding:"required"` } func SetUpToolGroup(router *gin.Engine) { @@ -457,37 +457,29 @@ func SendMail(title, content string) { } } -func SendMailTool(c *gin.Context) { +func RequestGetUserInfo(c *gin.Context) *dao.User { id, _ := c.Get("id") - id1 := int(id.(float64)) + userId := int(id.(float64)) + user := service.GetUserByIDWithCache(userId) + return &user +} +func SendMailTool(c *gin.Context) { + user := RequestGetUserInfo(c) + var resp proto.GeneralResp var req SendMailReq if err := c.ShouldBind(&req); err == nil { - user := dao.FindUserByUserID(id1) - if user.ID == 0 { - c.JSON(http.StatusOK, gin.H{"error": "user not found", "code": proto.ParameterError, "message": "failed"}) - return - } - //目标邮箱地址是否合法 - if !service.CheckEmail(req.To) { - c.JSON(http.StatusOK, gin.H{"error": "email address is invalid", "code": proto.ParameterError, "message": "failed"}) - return - } - if req.Title == "" || req.Content == "" { - c.JSON(http.StatusOK, gin.H{"error": "title or content is empty", "code": proto.ParameterError, "message": "failed"}) - return - } //发送邮件 if user.Role == "admin" { go service.SendEmail(req.To, req.Title, req.Content) - c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": "mail will be sent"}) + resp.Code, resp.Data, resp.Message = proto.SuccessCode, "mail will be sent", "success" } else { - c.JSON(http.StatusOK, gin.H{"error": "no send mail permission", "code": proto.PermissionDenied, "message": "failed"}) + resp.Code, resp.Data, resp.Message = proto.PermissionDenied, "mail will be sent", "error" } } else { - c.JSON(http.StatusOK, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"}) + resp.Data, resp.Data, resp.Message = proto.ParameterError, "toEmail or title or content request param error", "success" } - + c.JSON(http.StatusOK, resp) } // DownloadProxyHandle 处理下载代理请求 diff --git a/main.go b/main.go index d54913d..177110f 100644 --- a/main.go +++ b/main.go @@ -163,7 +163,7 @@ func JWTAuthMiddleware() gin.HandlerFunc { } if tokenString == "" { //c.AbortWithStatus(200) - c.JSON(http.StatusOK, gin.H{"message": "Unauthorized", "error": "token is empty", "code": proto.TokenIsNull}) + c.AbortWithStatusJSON(http.StatusOK, gin.H{"message": "Unauthorized", "error": "token is empty", "code": proto.TokenIsNull}) return } if proto.Config.TOKEN_USE_REDIS {