mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-02-23 23:32:15 +08:00
用户登录 简单实现
This commit is contained in:
parent
b06fedf311
commit
53642665ed
@ -2,7 +2,12 @@ package auth
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gofiber/fiber/v2"
|
"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/controller/types"
|
||||||
|
"gofiber.study.skcks.cn/dto"
|
||||||
|
"gofiber.study.skcks.cn/global"
|
||||||
|
"gofiber.study.skcks.cn/model/generic/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
@ -18,3 +23,44 @@ func NewController(app *fiber.App) *Controller {
|
|||||||
Controller: types.NewController(app, "/auth"),
|
Controller: types.NewController(app, "/auth"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Login 用户登录
|
||||||
|
//
|
||||||
|
// @Summary 用户登录
|
||||||
|
// @Description 用户登录
|
||||||
|
// @Tags Auth
|
||||||
|
// @Accept json
|
||||||
|
// @Produce json
|
||||||
|
// @Param vo body dto.Login true "用户登录"
|
||||||
|
// @Success 200 {object} response.Response{data=string}
|
||||||
|
// @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{}
|
||||||
|
err := ctx.BodyParser(login)
|
||||||
|
if err = errorx.ParseError(err); err != nil {
|
||||||
|
return ctx.JSON(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
user := &models.User{Account: login.Account, Password: login.Password}
|
||||||
|
exist, err := global.DataSources.Get(user)
|
||||||
|
if err = errorx.ParseError(err); err != nil {
|
||||||
|
return ctx.JSON(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !exist {
|
||||||
|
return ctx.JSON(errorx.NewDefaultError("账号或密码错误"))
|
||||||
|
}
|
||||||
|
|
||||||
|
token, err := global.GetToken(global.UserClaims{
|
||||||
|
Id: user.Id,
|
||||||
|
Account: user.Account,
|
||||||
|
})
|
||||||
|
if err = errorx.ParseError(err); err != nil {
|
||||||
|
return ctx.JSON(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx.JSON(response.NewResponse(token))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
65
docs/docs.go
65
docs/docs.go
@ -24,6 +24,58 @@ const docTemplate = `{
|
|||||||
"host": "{{.Host}}",
|
"host": "{{.Host}}",
|
||||||
"basePath": "{{.BasePath}}",
|
"basePath": "{{.BasePath}}",
|
||||||
"paths": {
|
"paths": {
|
||||||
|
"/auth/login": {
|
||||||
|
"post": {
|
||||||
|
"description": "用户登录",
|
||||||
|
"consumes": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"Auth"
|
||||||
|
],
|
||||||
|
"summary": "用户登录",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"description": "用户登录",
|
||||||
|
"name": "vo",
|
||||||
|
"in": "body",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/dto.Login"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "OK",
|
||||||
|
"schema": {
|
||||||
|
"allOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/definitions/response.Response"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"data": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"default": {
|
||||||
|
"description": "",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"/casbin/getUserRoles": {
|
"/casbin/getUserRoles": {
|
||||||
"get": {
|
"get": {
|
||||||
"description": "用户所有角色",
|
"description": "用户所有角色",
|
||||||
@ -495,6 +547,19 @@ const docTemplate = `{
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"definitions": {
|
"definitions": {
|
||||||
|
"dto.Login": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"account": {
|
||||||
|
"type": "string",
|
||||||
|
"example": "root"
|
||||||
|
},
|
||||||
|
"password": {
|
||||||
|
"type": "string",
|
||||||
|
"example": "12341234"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"errorx.CodeErrorResponse": {
|
"errorx.CodeErrorResponse": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
|
@ -16,6 +16,58 @@
|
|||||||
},
|
},
|
||||||
"basePath": "/",
|
"basePath": "/",
|
||||||
"paths": {
|
"paths": {
|
||||||
|
"/auth/login": {
|
||||||
|
"post": {
|
||||||
|
"description": "用户登录",
|
||||||
|
"consumes": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"Auth"
|
||||||
|
],
|
||||||
|
"summary": "用户登录",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"description": "用户登录",
|
||||||
|
"name": "vo",
|
||||||
|
"in": "body",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/dto.Login"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "OK",
|
||||||
|
"schema": {
|
||||||
|
"allOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/definitions/response.Response"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"data": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"default": {
|
||||||
|
"description": "",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"/casbin/getUserRoles": {
|
"/casbin/getUserRoles": {
|
||||||
"get": {
|
"get": {
|
||||||
"description": "用户所有角色",
|
"description": "用户所有角色",
|
||||||
@ -487,6 +539,19 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"definitions": {
|
"definitions": {
|
||||||
|
"dto.Login": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"account": {
|
||||||
|
"type": "string",
|
||||||
|
"example": "root"
|
||||||
|
},
|
||||||
|
"password": {
|
||||||
|
"type": "string",
|
||||||
|
"example": "12341234"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"errorx.CodeErrorResponse": {
|
"errorx.CodeErrorResponse": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
|
@ -1,5 +1,14 @@
|
|||||||
basePath: /
|
basePath: /
|
||||||
definitions:
|
definitions:
|
||||||
|
dto.Login:
|
||||||
|
properties:
|
||||||
|
account:
|
||||||
|
example: root
|
||||||
|
type: string
|
||||||
|
password:
|
||||||
|
example: "12341234"
|
||||||
|
type: string
|
||||||
|
type: object
|
||||||
errorx.CodeErrorResponse:
|
errorx.CodeErrorResponse:
|
||||||
properties:
|
properties:
|
||||||
code:
|
code:
|
||||||
@ -81,6 +90,37 @@ info:
|
|||||||
title: GoFiber Study API
|
title: GoFiber Study API
|
||||||
version: "1.0"
|
version: "1.0"
|
||||||
paths:
|
paths:
|
||||||
|
/auth/login:
|
||||||
|
post:
|
||||||
|
consumes:
|
||||||
|
- application/json
|
||||||
|
description: 用户登录
|
||||||
|
parameters:
|
||||||
|
- description: 用户登录
|
||||||
|
in: body
|
||||||
|
name: vo
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/dto.Login'
|
||||||
|
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'
|
||||||
|
summary: 用户登录
|
||||||
|
tags:
|
||||||
|
- Auth
|
||||||
/casbin/getUserRoles:
|
/casbin/getUserRoles:
|
||||||
get:
|
get:
|
||||||
consumes:
|
consumes:
|
||||||
|
6
dto/auth.go
Normal file
6
dto/auth.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package dto
|
||||||
|
|
||||||
|
type Login struct {
|
||||||
|
Account string `json:"account" example:"root"`
|
||||||
|
Password string `json:"password" example:"12341234"`
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user