login sha1 加密

主题创建测试
This commit is contained in:
Shikong 2022-11-13 19:34:48 +08:00
parent 89f547a3bf
commit 7392e4c26a
8 changed files with 255 additions and 7 deletions

View File

@ -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)

View 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(""))
})
}

View File

@ -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": {

View File

@ -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": {

View File

@ -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:

View File

@ -1 +1,6 @@
package topical
type CreateTopicalDTO struct {
Title string
Content string
}

View File

@ -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 {

View File

@ -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),