结构调整

This commit is contained in:
Shikong 2022-11-13 18:14:50 +08:00
parent d12286939f
commit 89f547a3bf
17 changed files with 244 additions and 183 deletions

View File

@ -6,7 +6,7 @@ import (
"gofiber.study.skcks.cn/common/response"
"gofiber.study.skcks.cn/controller/types"
"gofiber.study.skcks.cn/global"
"gofiber.study.skcks.cn/model/dto"
auth2 "gofiber.study.skcks.cn/model/auth"
"gofiber.study.skcks.cn/services/auth"
)
@ -31,13 +31,13 @@ func NewController(app *fiber.App) *Controller {
// @Tags Auth
// @Accept json
// @Produce json
// @Param vo body dto.Login true "用户登录"
// @Success 200 {object} response.Response{data=vo.Login}
// @Param vo body auth.LoginDTO true "用户登录"
// @Success 200 {object} response.Response{data=auth.LoginVO}
// @Failure default {object} errorx.CodeErrorResponse
// @Router /auth/login [post]
func (c *Controller) Login() {
c.Router.Post("login", func(ctx *fiber.Ctx) error {
login := &dto.Login{}
login := &auth2.LoginDTO{}
err := ctx.BodyParser(login)
if err = errorx.ParseError(err); err != nil {
return ctx.JSON(err)
@ -59,13 +59,13 @@ func (c *Controller) Login() {
// @Tags Auth
// @Accept json
// @Produce json
// @Param vo body dto.RefreshToken true "刷新令牌"
// @Success 200 {object} response.Response{data=vo.Login}
// @Param vo body auth.RefreshToken true "刷新令牌"
// @Success 200 {object} response.Response{data=auth.LoginVO}
// @Failure default {object} errorx.CodeErrorResponse
// @Router /auth/refreshToken [post]
func (c *Controller) RefreshToken() {
c.Router.Post("refreshToken", func(ctx *fiber.Ctx) error {
refresh := &dto.RefreshToken{}
refresh := &auth2.RefreshToken{}
err := ctx.BodyParser(refresh)
if err = errorx.ParseErrorWithCode(fiber.StatusInternalServerError, err); err != nil {
return ctx.JSON(err)

View File

@ -8,8 +8,7 @@ import (
"gofiber.study.skcks.cn/common/response"
"gofiber.study.skcks.cn/controller/types"
"gofiber.study.skcks.cn/global"
"gofiber.study.skcks.cn/model/dto"
"gofiber.study.skcks.cn/model/vo"
"gofiber.study.skcks.cn/model/captcha"
"gofiber.study.skcks.cn/services/waf"
"gofiber.study.skcks.cn/services/wol"
"strconv"
@ -135,7 +134,7 @@ func (c *Controller) NanoIdTest() {
// @Tags Test
// @Accept json
// @Produce json
// @Success 200 {object} response.Response{data=vo.Captcha}
// @Success 200 {object} response.Response{data=captcha.Captcha}
// @Failure default {object} errorx.CodeErrorResponse
// @Router /test/captcha/get [get]
func (c *Controller) GetCaptchaTest() {
@ -147,7 +146,7 @@ func (c *Controller) GetCaptchaTest() {
return ctx.JSON(errorx.NewDefaultError("验证码生成失败"))
}
return ctx.JSON(response.NewResponse(vo.Captcha{
return ctx.JSON(response.NewResponse(captcha.Captcha{
Id: id,
Base64: item.EncodeB64string(),
Expire: time.Now().Add(time.Duration(global.Config.Captcha.Expire) * time.Second).Unix(),
@ -162,13 +161,13 @@ func (c *Controller) GetCaptchaTest() {
// @Tags Test
// @Accept json
// @Produce json
// @Param vo body dto.VerifyCaptcha true "验证码验证"
// @Param vo body captcha.VerifyCaptcha true "验证码验证"
// @Success 200 {object} response.Response{data=string}
// @Failure default {object} errorx.CodeErrorResponse
// @Router /test/captcha/verify [post]
func (c *Controller) VerifyCaptchaTest() {
c.Router.Add(fiber.MethodPost, "/captcha/verify", func(ctx *fiber.Ctx) error {
verifyCaptcha := &dto.VerifyCaptcha{}
verifyCaptcha := &captcha.VerifyCaptcha{}
if err := errorx.ParseError(ctx.BodyParser(verifyCaptcha)); err != nil {
return ctx.JSON(err)

View File

@ -5,6 +5,7 @@ import (
"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"
"gofiber.study.skcks.cn/services/user"
)
@ -25,19 +26,20 @@ func NewController(app *fiber.App) *Controller {
// GetByAccount 根据 账号 获取用户信息
//
// @Summary 根据 账号 获取用户信息
// @Description 根据 账号 获取用户信息
// @Summary 获取当前用户信息
// @Description 获取当前用户信息
// @Tags User
// @Accept json
// @Produce json
// @Param account query string true "账号名称"
// @Success 200 {object} response.Response{data=models.User}
// @Failure default {object} errorx.CodeErrorResponse
// @Router /user/account [get]
// @Security Token
func (c *Controller) GetByAccount() {
c.Router.Get("/account", func(ctx *fiber.Ctx) error {
account := ctx.Query("account")
token := ctx.GetReqHeaders()["Token"]
claim, _ := global.ParseToken(token)
account := claim.Account
u, err := user.Services.GetUserByAccount(account)
if err != nil {

View File

@ -44,7 +44,7 @@ const docTemplate = `{
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/dto.Login"
"$ref": "#/definitions/auth.LoginDTO"
}
}
],
@ -60,7 +60,7 @@ const docTemplate = `{
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/vo.Login"
"$ref": "#/definitions/auth.LoginVO"
}
}
}
@ -96,7 +96,7 @@ const docTemplate = `{
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/dto.RefreshToken"
"$ref": "#/definitions/auth.RefreshToken"
}
}
],
@ -112,7 +112,7 @@ const docTemplate = `{
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/vo.Login"
"$ref": "#/definitions/auth.LoginVO"
}
}
}
@ -467,7 +467,7 @@ const docTemplate = `{
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/vo.Captcha"
"$ref": "#/definitions/captcha.Captcha"
}
}
}
@ -503,7 +503,7 @@ const docTemplate = `{
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/dto.VerifyCaptcha"
"$ref": "#/definitions/captcha.VerifyCaptcha"
}
}
],
@ -815,7 +815,7 @@ const docTemplate = `{
"Token": []
}
],
"description": "根据 账号 获取用户信息",
"description": "获取当前用户信息",
"consumes": [
"application/json"
],
@ -825,16 +825,7 @@ const docTemplate = `{
"tags": [
"User"
],
"summary": "根据 账号 获取用户信息",
"parameters": [
{
"type": "string",
"description": "账号名称",
"name": "account",
"in": "query",
"required": true
}
],
"summary": "获取当前用户信息",
"responses": {
"200": {
"description": "OK",
@ -865,7 +856,7 @@ const docTemplate = `{
}
},
"definitions": {
"dto.Login": {
"auth.LoginDTO": {
"type": "object",
"required": [
"account",
@ -882,7 +873,22 @@ const docTemplate = `{
}
}
},
"dto.RefreshToken": {
"auth.LoginVO": {
"type": "object",
"properties": {
"refreshToken": {
"description": "refreshToken 刷新令牌",
"type": "string",
"example": "0123456789ABCDEFG"
},
"token": {
"description": "token 用户令牌",
"type": "string",
"example": "0123456789ABCDEFG"
}
}
},
"auth.RefreshToken": {
"type": "object",
"required": [
"refreshToken"
@ -894,7 +900,27 @@ const docTemplate = `{
}
}
},
"dto.VerifyCaptcha": {
"captcha.Captcha": {
"type": "object",
"properties": {
"base64": {
"description": "验证码图片 base64",
"type": "string",
"example": "data:image/png;base64,iVBO..."
},
"expire": {
"description": "过期时间 unix",
"type": "integer",
"example": 10000000
},
"id": {
"description": "验证码 id",
"type": "string",
"example": "abcdefg123456"
}
}
},
"captcha.VerifyCaptcha": {
"type": "object",
"properties": {
"captcha": {
@ -1010,41 +1036,6 @@ const docTemplate = `{
"example": "OK"
}
}
},
"vo.Captcha": {
"type": "object",
"properties": {
"base64": {
"description": "验证码图片 base64",
"type": "string",
"example": "data:image/png;base64,iVBO..."
},
"expire": {
"description": "过期时间 unix",
"type": "integer",
"example": 10000000
},
"id": {
"description": "验证码 id",
"type": "string",
"example": "abcdefg123456"
}
}
},
"vo.Login": {
"type": "object",
"properties": {
"refreshToken": {
"description": "refreshToken 刷新令牌",
"type": "string",
"example": "0123456789ABCDEFG"
},
"token": {
"description": "token 用户令牌",
"type": "string",
"example": "0123456789ABCDEFG"
}
}
}
},
"securityDefinitions": {

View File

@ -36,7 +36,7 @@
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/dto.Login"
"$ref": "#/definitions/auth.LoginDTO"
}
}
],
@ -52,7 +52,7 @@
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/vo.Login"
"$ref": "#/definitions/auth.LoginVO"
}
}
}
@ -88,7 +88,7 @@
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/dto.RefreshToken"
"$ref": "#/definitions/auth.RefreshToken"
}
}
],
@ -104,7 +104,7 @@
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/vo.Login"
"$ref": "#/definitions/auth.LoginVO"
}
}
}
@ -459,7 +459,7 @@
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/vo.Captcha"
"$ref": "#/definitions/captcha.Captcha"
}
}
}
@ -495,7 +495,7 @@
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/dto.VerifyCaptcha"
"$ref": "#/definitions/captcha.VerifyCaptcha"
}
}
],
@ -807,7 +807,7 @@
"Token": []
}
],
"description": "根据 账号 获取用户信息",
"description": "获取当前用户信息",
"consumes": [
"application/json"
],
@ -817,16 +817,7 @@
"tags": [
"User"
],
"summary": "根据 账号 获取用户信息",
"parameters": [
{
"type": "string",
"description": "账号名称",
"name": "account",
"in": "query",
"required": true
}
],
"summary": "获取当前用户信息",
"responses": {
"200": {
"description": "OK",
@ -857,7 +848,7 @@
}
},
"definitions": {
"dto.Login": {
"auth.LoginDTO": {
"type": "object",
"required": [
"account",
@ -874,7 +865,22 @@
}
}
},
"dto.RefreshToken": {
"auth.LoginVO": {
"type": "object",
"properties": {
"refreshToken": {
"description": "refreshToken 刷新令牌",
"type": "string",
"example": "0123456789ABCDEFG"
},
"token": {
"description": "token 用户令牌",
"type": "string",
"example": "0123456789ABCDEFG"
}
}
},
"auth.RefreshToken": {
"type": "object",
"required": [
"refreshToken"
@ -886,7 +892,27 @@
}
}
},
"dto.VerifyCaptcha": {
"captcha.Captcha": {
"type": "object",
"properties": {
"base64": {
"description": "验证码图片 base64",
"type": "string",
"example": "data:image/png;base64,iVBO..."
},
"expire": {
"description": "过期时间 unix",
"type": "integer",
"example": 10000000
},
"id": {
"description": "验证码 id",
"type": "string",
"example": "abcdefg123456"
}
}
},
"captcha.VerifyCaptcha": {
"type": "object",
"properties": {
"captcha": {
@ -1002,41 +1028,6 @@
"example": "OK"
}
}
},
"vo.Captcha": {
"type": "object",
"properties": {
"base64": {
"description": "验证码图片 base64",
"type": "string",
"example": "data:image/png;base64,iVBO..."
},
"expire": {
"description": "过期时间 unix",
"type": "integer",
"example": 10000000
},
"id": {
"description": "验证码 id",
"type": "string",
"example": "abcdefg123456"
}
}
},
"vo.Login": {
"type": "object",
"properties": {
"refreshToken": {
"description": "refreshToken 刷新令牌",
"type": "string",
"example": "0123456789ABCDEFG"
},
"token": {
"description": "token 用户令牌",
"type": "string",
"example": "0123456789ABCDEFG"
}
}
}
},
"securityDefinitions": {

View File

@ -1,6 +1,6 @@
basePath: /
definitions:
dto.Login:
auth.LoginDTO:
properties:
account:
example: root
@ -12,7 +12,18 @@ definitions:
- account
- password
type: object
dto.RefreshToken:
auth.LoginVO:
properties:
refreshToken:
description: refreshToken 刷新令牌
example: 0123456789ABCDEFG
type: string
token:
description: token 用户令牌
example: 0123456789ABCDEFG
type: string
type: object
auth.RefreshToken:
properties:
refreshToken:
example: 0123456789ABCDEFG
@ -20,7 +31,22 @@ definitions:
required:
- refreshToken
type: object
dto.VerifyCaptcha:
captcha.Captcha:
properties:
base64:
description: 验证码图片 base64
example: data:image/png;base64,iVBO...
type: string
expire:
description: 过期时间 unix
example: 10000000
type: integer
id:
description: 验证码 id
example: abcdefg123456
type: string
type: object
captcha.VerifyCaptcha:
properties:
captcha:
example: abcde
@ -100,32 +126,6 @@ definitions:
example: OK
type: string
type: object
vo.Captcha:
properties:
base64:
description: 验证码图片 base64
example: data:image/png;base64,iVBO...
type: string
expire:
description: 过期时间 unix
example: 10000000
type: integer
id:
description: 验证码 id
example: abcdefg123456
type: string
type: object
vo.Login:
properties:
refreshToken:
description: refreshToken 刷新令牌
example: 0123456789ABCDEFG
type: string
token:
description: token 用户令牌
example: 0123456789ABCDEFG
type: string
type: object
info:
contact:
email: 919411476@qq.com
@ -149,7 +149,7 @@ paths:
name: vo
required: true
schema:
$ref: '#/definitions/dto.Login'
$ref: '#/definitions/auth.LoginDTO'
produces:
- application/json
responses:
@ -160,7 +160,7 @@ paths:
- $ref: '#/definitions/response.Response'
- properties:
data:
$ref: '#/definitions/vo.Login'
$ref: '#/definitions/auth.LoginVO'
type: object
default:
description: ""
@ -180,7 +180,7 @@ paths:
name: vo
required: true
schema:
$ref: '#/definitions/dto.RefreshToken'
$ref: '#/definitions/auth.RefreshToken'
produces:
- application/json
responses:
@ -191,7 +191,7 @@ paths:
- $ref: '#/definitions/response.Response'
- properties:
data:
$ref: '#/definitions/vo.Login'
$ref: '#/definitions/auth.LoginVO'
type: object
default:
description: ""
@ -412,7 +412,7 @@ paths:
- $ref: '#/definitions/response.Response'
- properties:
data:
$ref: '#/definitions/vo.Captcha'
$ref: '#/definitions/captcha.Captcha'
type: object
default:
description: ""
@ -432,7 +432,7 @@ paths:
name: vo
required: true
schema:
$ref: '#/definitions/dto.VerifyCaptcha'
$ref: '#/definitions/captcha.VerifyCaptcha'
produces:
- application/json
responses:
@ -618,13 +618,7 @@ paths:
get:
consumes:
- application/json
description: 根据 账号 获取用户信息
parameters:
- description: 账号名称
in: query
name: account
required: true
type: string
description: 获取当前用户信息
produces:
- application/json
responses:
@ -643,7 +637,7 @@ paths:
$ref: '#/definitions/errorx.CodeErrorResponse'
security:
- Token: []
summary: 根据 账号 获取用户信息
summary: 获取当前用户信息
tags:
- User
securityDefinitions:

View File

@ -1,9 +1,9 @@
package dto
package auth
// Login
// LoginDTO
// @Param account body string true "用户账号(account)"
// @Param password body string true "用户密码"
type Login struct {
type LoginDTO struct {
Account string `json:"account" example:"root" validate:"required"`
Password string `json:"password" example:"12341234" validate:"required"`
}

View File

@ -1,6 +1,6 @@
package vo
package auth
type Login struct {
type LoginVO struct {
// token 用户令牌
Token string `json:"token" example:"0123456789ABCDEFG"`
// refreshToken 刷新令牌

View File

@ -1,4 +1,4 @@
package dto
package captcha
// VerifyCaptcha
// @Param id body string true "验证码 id"

View File

@ -1,4 +1,4 @@
package vo
package captcha
type Captcha struct {
// 验证码 id

View File

@ -12,6 +12,8 @@ targets:
include_tables: # tables included, you can use **
- user
- waf
- topical
- reply
table_prefix: ""
multiple_files: true # generate multiple files or one
template: | # template for code file, it has higher perior than template_path

View File

@ -0,0 +1,21 @@
package models
import (
"gofiber.study.skcks.cn/common/time"
)
type Reply struct {
Id string `xorm:"not null pk VARCHAR(32)" json:"id"`
Active bool `xorm:"not null index BIT(1)" json:"active"`
Content string `xorm:"not null index LONGTEXT(4294967295)" json:"content"`
CreateTime time.Time `xorm:"not null index DATETIME(6) created" json:"CreateTime"`
ModifyTime time.Time `xorm:"not null index DATETIME(6) updated" json:"modifyTime"`
ModifyTimes int64 `xorm:"not null index BIGINT" json:"modifyTimes"`
Top int `xorm:"not null index BIT(1)" json:"top"`
UserId string `xorm:"not null index VARCHAR(32)" json:"userId"`
TopicalId string `xorm:"not null index VARCHAR(32)" json:"topicalId"`
}
func (m *Reply) TableName() string {
return "reply"
}

View File

@ -0,0 +1,20 @@
package models
import (
"gofiber.study.skcks.cn/common/time"
)
type Topical struct {
Id string `xorm:"not null pk VARCHAR(32)" json:"id"`
Active bool `xorm:"not null index BIT(1)" json:"active"`
Content string `xorm:"not null index LONGTEXT(4294967295)" json:"content"`
CreateTime time.Time `xorm:"not null index DATETIME(6) created" json:"createTime"`
ModifyTime time.Time `xorm:"not null index DATETIME(6) updated" json:"modifyTime"`
ModifyTimes int64 `xorm:"not null index BIGINT" json:"modifyTimes"`
Title string `xorm:"not null VARCHAR(255)" json:"title"`
UserId string `xorm:"not null index VARCHAR(32)" json:"userId"`
}
func (m *Topical) TableName() string {
return "topical"
}

1
model/topical/dto.go Normal file
View File

@ -0,0 +1 @@
package topical

View File

@ -9,9 +9,8 @@ import (
"gofiber.study.skcks.cn/common/utils"
"gofiber.study.skcks.cn/global"
"gofiber.study.skcks.cn/middleware"
"gofiber.study.skcks.cn/model/dto"
"gofiber.study.skcks.cn/model/auth"
"gofiber.study.skcks.cn/model/generic/models"
"gofiber.study.skcks.cn/model/vo"
"time"
)
@ -91,7 +90,7 @@ func (s *Service) getUserClaims(user *models.User) global.UserClaims {
}
}
func (s *Service) Login(login *dto.Login) (result *vo.Login, err error) {
func (s *Service) Login(login *auth.LoginDTO) (result *auth.LoginVO, err error) {
err = global.ValidateStruct(login)
if err != nil {
return nil, err
@ -117,13 +116,13 @@ func (s *Service) Login(login *dto.Login) (result *vo.Login, err error) {
_, _ = global.DataSources.Update(user)
return &vo.Login{
return &auth.LoginVO{
Token: token,
RefreshToken: refreshToken,
}, err
}
func (s *Service) RefreshToken(refreshTokenB64 string) (result *vo.Login, err error) {
func (s *Service) RefreshToken(refreshTokenB64 string) (result *auth.LoginVO, err error) {
user, err := s.fromRefreshTokenGetUserCache(refreshTokenB64)
if err != nil {
return
@ -137,7 +136,7 @@ func (s *Service) RefreshToken(refreshTokenB64 string) (result *vo.Login, err er
refreshToken, err := s.generateAndSaveRefreshToken(user)
return &vo.Login{
return &auth.LoginVO{
Token: token,
RefreshToken: refreshToken,
}, err

View File

@ -2,6 +2,7 @@ package services
import (
"gofiber.study.skcks.cn/services/auth"
"gofiber.study.skcks.cn/services/topical"
"gofiber.study.skcks.cn/services/user"
"gofiber.study.skcks.cn/services/waf"
"gofiber.study.skcks.cn/services/wol"
@ -12,4 +13,5 @@ func Init() {
user.InitService()
waf.InitService()
wol.InitService()
topical.InitService()
}

View File

@ -0,0 +1,39 @@
package topical
import (
"gofiber.study.skcks.cn/global"
"gofiber.study.skcks.cn/model/generic/models"
"strconv"
)
type Service struct {
}
var Services *Service
func InitService() {
Services = &Service{}
}
type CreateTopicalDTO struct {
Title string
Content string
}
func (s *Service) CreateTopical(dto CreateTopicalDTO, userId string) error {
id, _ := global.SonyFlake.NextID()
_, err := global.DataSources.Insert(&models.Topical{
Id: strconv.FormatUint(id, 10),
Active: true,
Content: dto.Content,
ModifyTimes: 0,
Title: dto.Title,
UserId: userId,
})
if err != nil {
return err
}
return nil
}