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 {