diff --git a/controller/controller.go b/controller/controller.go index 7573cc8..15cbb94 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -7,6 +7,7 @@ import ( "gofiber.study.skcks.cn/controller/auth" "gofiber.study.skcks.cn/controller/casbin" "gofiber.study.skcks.cn/controller/test" + "gofiber.study.skcks.cn/controller/topical" "gofiber.study.skcks.cn/controller/types" "gofiber.study.skcks.cn/controller/user" "reflect" @@ -18,6 +19,7 @@ func RegisterController(app *fiber.App) { user.NewController(app), casbin.NewController(app), test.NewController(app), + topical.NewController(app), } args := make([]reflect.Value, 0) diff --git a/controller/topical/topical.go b/controller/topical/topical.go new file mode 100644 index 0000000..0bcc6db --- /dev/null +++ b/controller/topical/topical.go @@ -0,0 +1,57 @@ +package topical + +import ( + "github.com/gofiber/fiber/v2" + "gofiber.study.skcks.cn/common/errorx" + + "gofiber.study.skcks.cn/common/response" + "gofiber.study.skcks.cn/controller/types" + "gofiber.study.skcks.cn/global" + "gofiber.study.skcks.cn/middleware" + topical2 "gofiber.study.skcks.cn/model/topical" + "gofiber.study.skcks.cn/services/topical" +) + +type Controller struct { + *types.Controller +} + +func (c *Controller) GetRouter() fiber.Router { + return c.Router +} + +func NewController(app *fiber.App) *Controller { + return &Controller{ + Controller: types.NewController(app, "/topical", middleware.CasbinMiddleWare), + } +} + +// CreateTopical 创建主题 +// +// @Summary 创建主题 +// @Description 创建主题 +// @Tags Topical +// @Accept json +// @Produce json +// @Param vo body topical.CreateTopicalDTO true "CreateTopicalDTO" +// @Success 200 {object} response.Response{data=string} +// @Failure default {object} errorx.CodeErrorResponse +// @Router /topical/create [post] +// @Security Token +func (c *Controller) CreateTopical() { + c.Router.Post("/create", func(ctx *fiber.Ctx) error { + token := ctx.GetReqHeaders()["Token"] + claim, _ := global.ParseToken(token) + id := claim.Id + + var dto = &topical2.CreateTopicalDTO{} + + _ = ctx.BodyParser(dto) + err := topical.Services.CreateTopical(*dto, id) + if err != nil { + return ctx.JSON(errorx.ParseError(err)) + } + + return ctx.JSON(response.NewResponse("")) + }) +} diff --git a/docs/docs.go b/docs/docs.go index 347ae43..338c348 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -808,6 +808,63 @@ const docTemplate = `{ } } }, + "/topical/create": { + "post": { + "security": [ + { + "Token": [] + } + ], + "description": "创建主题", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Topical" + ], + "summary": "创建主题", + "parameters": [ + { + "description": "CreateTopicalDTO", + "name": "vo", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/topical.CreateTopicalDTO" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/response.Response" + }, + { + "type": "object", + "properties": { + "data": { + "type": "string" + } + } + } + ] + } + }, + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + } + }, "/user/account": { "get": { "security": [ @@ -1036,6 +1093,17 @@ const docTemplate = `{ "example": "OK" } } + }, + "topical.CreateTopicalDTO": { + "type": "object", + "properties": { + "content": { + "type": "string" + }, + "title": { + "type": "string" + } + } } }, "securityDefinitions": { diff --git a/docs/swagger.json b/docs/swagger.json index 2335376..82c9179 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -800,6 +800,63 @@ } } }, + "/topical/create": { + "post": { + "security": [ + { + "Token": [] + } + ], + "description": "创建主题", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Topical" + ], + "summary": "创建主题", + "parameters": [ + { + "description": "CreateTopicalDTO", + "name": "vo", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/topical.CreateTopicalDTO" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/response.Response" + }, + { + "type": "object", + "properties": { + "data": { + "type": "string" + } + } + } + ] + } + }, + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + } + }, "/user/account": { "get": { "security": [ @@ -1028,6 +1085,17 @@ "example": "OK" } } + }, + "topical.CreateTopicalDTO": { + "type": "object", + "properties": { + "content": { + "type": "string" + }, + "title": { + "type": "string" + } + } } }, "securityDefinitions": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 2d64ad4..204cdad 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -126,6 +126,13 @@ definitions: example: OK type: string type: object + topical.CreateTopicalDTO: + properties: + content: + type: string + title: + type: string + type: object info: contact: email: 919411476@qq.com @@ -614,6 +621,39 @@ paths: summary: wol 测试 tags: - Test + /topical/create: + post: + consumes: + - application/json + description: 创建主题 + parameters: + - description: CreateTopicalDTO + in: body + name: vo + required: true + schema: + $ref: '#/definitions/topical.CreateTopicalDTO' + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/response.Response' + - properties: + data: + type: string + type: object + default: + description: "" + schema: + $ref: '#/definitions/errorx.CodeErrorResponse' + security: + - Token: [] + summary: 创建主题 + tags: + - Topical /user/account: get: consumes: diff --git a/model/topical/dto.go b/model/topical/dto.go index 44f434e..75a2f47 100644 --- a/model/topical/dto.go +++ b/model/topical/dto.go @@ -1 +1,6 @@ package topical + +type CreateTopicalDTO struct { + Title string + Content string +} diff --git a/services/auth/auth.go b/services/auth/auth.go index 4480fb4..77c8851 100644 --- a/services/auth/auth.go +++ b/services/auth/auth.go @@ -2,8 +2,10 @@ package auth import ( "context" + "crypto/sha256" "encoding/base64" "errors" + "fmt" "github.com/goccy/go-json" "gofiber.study.skcks.cn/common/logger" "gofiber.study.skcks.cn/common/utils" @@ -11,6 +13,7 @@ import ( "gofiber.study.skcks.cn/middleware" "gofiber.study.skcks.cn/model/auth" "gofiber.study.skcks.cn/model/generic/models" + "io" "time" ) @@ -96,7 +99,8 @@ func (s *Service) Login(login *auth.LoginDTO) (result *auth.LoginVO, err error) return nil, err } - user := &models.User{Account: login.Account, Password: login.Password} + user := &models.User{Account: login.Account} + exist, err := global.DataSources.Get(user) if err != nil { return @@ -106,6 +110,14 @@ func (s *Service) Login(login *auth.LoginDTO) (result *auth.LoginVO, err error) return nil, Failed } + hash := sha256.New() + _, _ = io.WriteString(hash, login.Password+user.Salt) + pwd := fmt.Sprintf("%x", hash.Sum(nil)) + logger.Log.Debugf("login pwd sha1: %s", pwd) + if pwd != user.Password { + return nil, Failed + } + token, err := global.GetToken(s.getUserClaims(user)) if err != nil { diff --git a/services/topical/topical.go b/services/topical/topical.go index 9d7414f..33ac4c8 100644 --- a/services/topical/topical.go +++ b/services/topical/topical.go @@ -3,6 +3,7 @@ package topical import ( "gofiber.study.skcks.cn/global" "gofiber.study.skcks.cn/model/generic/models" + "gofiber.study.skcks.cn/model/topical" "strconv" ) @@ -15,12 +16,7 @@ func InitService() { Services = &Service{} } -type CreateTopicalDTO struct { - Title string - Content string -} - -func (s *Service) CreateTopical(dto CreateTopicalDTO, userId string) error { +func (s *Service) CreateTopical(dto topical.CreateTopicalDTO, userId string) error { id, _ := global.SonyFlake.NextID() _, err := global.DataSources.Insert(&models.Topical{ Id: strconv.FormatUint(id, 10),