mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-02-23 23:32:15 +08:00
login sha1 加密
主题创建测试
This commit is contained in:
parent
89f547a3bf
commit
7392e4c26a
@ -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)
|
||||
|
57
controller/topical/topical.go
Normal file
57
controller/topical/topical.go
Normal file
@ -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(""))
|
||||
})
|
||||
}
|
68
docs/docs.go
68
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": {
|
||||
|
@ -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": {
|
||||
|
@ -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:
|
||||
|
@ -1 +1,6 @@
|
||||
package topical
|
||||
|
||||
type CreateTopicalDTO struct {
|
||||
Title string
|
||||
Content string
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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),
|
||||
|
Loading…
Reference in New Issue
Block a user