diff --git a/api/api.go b/api/api.go index e05a44c..8b37b7d 100644 --- a/api/api.go +++ b/api/api.go @@ -3,6 +3,7 @@ package api import ( "schisandra-cloud-album/api/captcha_api" "schisandra-cloud-album/api/client_api" + "schisandra-cloud-album/api/comment_api" "schisandra-cloud-album/api/oauth_api" "schisandra-cloud-album/api/permission_api" "schisandra-cloud-album/api/role_api" @@ -21,6 +22,7 @@ type Apis struct { RoleApi role_api.RoleAPI PermissionApi permission_api.PermissionAPI ClientApi client_api.ClientAPI + CommonApi comment_api.CommentAPI } // Api new函数实例化,实例化完成后会返回结构体地指针类型 diff --git a/api/comment_api/comment.go b/api/comment_api/comment.go index adebab8..3d9afa4 100644 --- a/api/comment_api/comment.go +++ b/api/comment_api/comment.go @@ -1,3 +1,7 @@ package comment_api +import "schisandra-cloud-album/service" + type CommentAPI struct{} + +var commentReplyService = service.Service.CommentReplyService diff --git a/api/comment_api/comment_api.go b/api/comment_api/comment_api.go index a30dc1b..cad31e9 100644 --- a/api/comment_api/comment_api.go +++ b/api/comment_api/comment_api.go @@ -1 +1,28 @@ package comment_api + +import ( + ginI18n "github.com/gin-contrib/i18n" + "github.com/gin-gonic/gin" + "schisandra-cloud-album/api/comment_api/dto" + "schisandra-cloud-album/common/result" +) + +// CommentSubmit 提交评论 +func (CommentAPI) CommentSubmit(c *gin.Context) { + commentRequest := dto.CommentRequest{} + err := c.ShouldBindJSON(&commentRequest) + if err != nil { + result.FailWithMessage(ginI18n.MustGetMessage(c, "ParamsError"), c) + return + } + content := commentRequest.Content + images := commentRequest.Images + if content == "" { + result.FailWithMessage(ginI18n.MustGetMessage(c, "RequestParamsNotEmpty"), c) + return + } + if len(images) > 5 { + result.FailWithMessage(ginI18n.MustGetMessage(c, "TooManyImages"), c) + return + } +} diff --git a/api/comment_api/dto/request_dto.go b/api/comment_api/dto/request_dto.go new file mode 100644 index 0000000..ff8aded --- /dev/null +++ b/api/comment_api/dto/request_dto.go @@ -0,0 +1,6 @@ +package dto + +type CommentRequest struct { + Content string `json:"content"` + Images []string `json:"images"` +} diff --git a/i18n/language/en.toml b/i18n/language/en.toml index e420464..aecaf3f 100644 --- a/i18n/language/en.toml +++ b/i18n/language/en.toml @@ -62,4 +62,6 @@ SystemError = "system error, please contact the administrator!" RequestLimit = "request limit!" 404NotFound = "404 not found!" PermissionVerifyFailed = "permission verify failed!" -IllegalRequests = "illegal requests!" \ No newline at end of file +IllegalRequests = "illegal requests!" +RequestParamsNotEmpty = "request params can not be empty!" +TooManyImages = "too many images!" \ No newline at end of file diff --git a/i18n/language/zh.toml b/i18n/language/zh.toml index 4e9473c..fca3c2a 100644 --- a/i18n/language/zh.toml +++ b/i18n/language/zh.toml @@ -63,3 +63,5 @@ RequestLimit = "请求频率过高!" 404NotFound = "未找到资源!" PermissionVerifyFailed = "权限验证失败!" IllegalRequests = "非法请求!" +RequestParamsNotEmpty = "请求参数不能为空!" +TooManyImages = "最多只能上传5张图片!" \ No newline at end of file diff --git a/router/modules/comment_router.go b/router/modules/comment_router.go new file mode 100644 index 0000000..d448257 --- /dev/null +++ b/router/modules/comment_router.go @@ -0,0 +1,12 @@ +package modules + +import ( + "github.com/gin-gonic/gin" + "schisandra-cloud-album/api" +) + +var commonApi = api.Api.CommonApi + +func CommentRouter(router *gin.RouterGroup) { + router.POST("/auth/comment/submit", commonApi.CommentSubmit) +} diff --git a/router/router.go b/router/router.go index 7f3140d..6d97de0 100644 --- a/router/router.go +++ b/router/router.go @@ -2,9 +2,7 @@ package router import ( "github.com/gin-contrib/cors" - ginI18n "github.com/gin-contrib/i18n" "github.com/gin-gonic/gin" - "schisandra-cloud-album/common/result" "schisandra-cloud-album/global" "schisandra-cloud-album/middleware" "schisandra-cloud-album/router/modules" @@ -14,8 +12,6 @@ import ( func InitRouter() *gin.Engine { gin.SetMode(global.CONFIG.System.Env) router := gin.Default() - router.NoRoute(HandleNotFound) - router.NoMethod(HandleNotFound) err := router.SetTrustedProxies([]string{global.CONFIG.System.Ip}) if err != nil { global.LOG.Error(err) @@ -33,7 +29,7 @@ func InitRouter() *gin.Engine { // 国际化设置 router.Use(middleware.I18n()) - noMiddlewareRouter := router.Group("api") + noMiddlewareRouter := router.Group("api") // 不需要中间件的路由组 { modules.WebsocketRouter(noMiddlewareRouter) // 注册websocket路由 modules.OauthRouter(noMiddlewareRouter) // 注册oauth路由 @@ -48,24 +44,19 @@ func InitRouter() *gin.Engine { modules.SmsRouter(publicGroup) // 注册短信验证码路由 modules.UserRouter(publicGroup) // 注册鉴权路由 } - authGroup := router.Group("api") // 需要鉴权的路由组 + authGroup := router.Group("api") // 需要鉴权的路由组 authGroup.Use( + middleware.SecurityHeaders(), middleware.JWTAuthMiddleware(), middleware.CasbinMiddleware(), - middleware.SecurityHeaders(), ) { modules.UserRouterAuth(authGroup) // 注册鉴权路由 modules.RoleRouter(authGroup) // 注册角色路由 modules.PermissionRouter(authGroup) // 注册权限路由 + modules.CommentRouter(authGroup) // 注册评论路由 } return router } - -// HandleNotFound 404处理 -func HandleNotFound(c *gin.Context) { - result.FailWithCodeAndMessage(404, ginI18n.MustGetMessage(c, "404NotFound"), c) - return -} diff --git a/service/comment_reply_service/comment_reply_service.go b/service/comment_reply_service/comment_reply_service.go index 12fc35d..f317cef 100644 --- a/service/comment_reply_service/comment_reply_service.go +++ b/service/comment_reply_service/comment_reply_service.go @@ -1 +1,14 @@ package comment_reply_service + +import ( + "schisandra-cloud-album/global" + "schisandra-cloud-album/model" +) + +// CreateCommentReply 创建评论 +func (CommentReplyService) CreateCommentReply(comment *model.ScaCommentReply) error { + if err := global.DB.Create(&comment).Error; err != nil { + return err + } + return nil +} diff --git a/service/service.go b/service/service.go index a8c587a..bdcc3ca 100644 --- a/service/service.go +++ b/service/service.go @@ -1,6 +1,7 @@ package service import ( + "schisandra-cloud-album/service/comment_reply_service" "schisandra-cloud-album/service/permission_service" "schisandra-cloud-album/service/role_service" "schisandra-cloud-album/service/user_device_service" @@ -10,11 +11,12 @@ import ( // Services 统一导出的service type Services struct { - UserService user_service.UserService - RoleService role_service.RoleService - PermissionService permission_service.PermissionService - UserSocialService user_social_service.UserSocialService - UserDeviceService user_device_service.UserDeviceService + UserService user_service.UserService + RoleService role_service.RoleService + PermissionService permission_service.PermissionService + UserSocialService user_social_service.UserSocialService + UserDeviceService user_device_service.UserDeviceService + CommentReplyService comment_reply_service.CommentReplyService } // Service new函数实例化,实例化完成后会返回结构体地指针类型 diff --git a/utils/jwt.go b/utils/jwt.go index 3c94913..b7a89d5 100644 --- a/utils/jwt.go +++ b/utils/jwt.go @@ -89,7 +89,6 @@ func ParseAccessToken(tokenString string) (*AccessJWTPayload, bool, error) { return MySecret, nil }) if err != nil { - global.LOG.Error(err) return nil, false, err } if claims, ok := token.Claims.(*AccessJWTClaims); ok && token.Valid {