用户登录 简单实现

This commit is contained in:
Shikong 2022-10-06 17:23:22 +08:00
parent b06fedf311
commit 53642665ed
5 changed files with 222 additions and 0 deletions

View File

@ -2,7 +2,12 @@ package auth
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/dto"
"gofiber.study.skcks.cn/global"
"gofiber.study.skcks.cn/model/generic/models"
)
type Controller struct {
@ -18,3 +23,44 @@ func NewController(app *fiber.App) *Controller {
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))
})
}

View File

@ -24,6 +24,58 @@ const docTemplate = `{
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"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": {
"get": {
"description": "用户所有角色",
@ -495,6 +547,19 @@ const docTemplate = `{
}
},
"definitions": {
"dto.Login": {
"type": "object",
"properties": {
"account": {
"type": "string",
"example": "root"
},
"password": {
"type": "string",
"example": "12341234"
}
}
},
"errorx.CodeErrorResponse": {
"type": "object",
"properties": {

View File

@ -16,6 +16,58 @@
},
"basePath": "/",
"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": {
"get": {
"description": "用户所有角色",
@ -487,6 +539,19 @@
}
},
"definitions": {
"dto.Login": {
"type": "object",
"properties": {
"account": {
"type": "string",
"example": "root"
},
"password": {
"type": "string",
"example": "12341234"
}
}
},
"errorx.CodeErrorResponse": {
"type": "object",
"properties": {

View File

@ -1,5 +1,14 @@
basePath: /
definitions:
dto.Login:
properties:
account:
example: root
type: string
password:
example: "12341234"
type: string
type: object
errorx.CodeErrorResponse:
properties:
code:
@ -81,6 +90,37 @@ info:
title: GoFiber Study API
version: "1.0"
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:
get:
consumes:

6
dto/auth.go Normal file
View File

@ -0,0 +1,6 @@
package dto
type Login struct {
Account string `json:"account" example:"root"`
Password string `json:"password" example:"12341234"`
}