尝试登录逻辑
This commit is contained in:
@@ -1,50 +1,52 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"Crimson-Gatekeeper/internal/common"
|
||||
"Crimson-Gatekeeper/internal/ero"
|
||||
"Crimson-Gatekeeper/internal/query"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const version = "/api/v1"
|
||||
const group = "/user"
|
||||
|
||||
type Controller struct {
|
||||
client *gorm.DB
|
||||
query *query.Query
|
||||
}
|
||||
|
||||
type PostLoginParam struct {
|
||||
type postLoginParam struct {
|
||||
Account string `json:"account" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
|
||||
func (c *Controller) PostLogin(ctx *gin.Context) {
|
||||
param := &PostLoginParam{}
|
||||
func (c *Controller) postLogin(ctx *gin.Context) {
|
||||
param := &postLoginParam{}
|
||||
if err := ctx.ShouldBindJSON(¶m); err != nil {
|
||||
fmt.Println("参数解析失败,请确认参数是否为 JSON")
|
||||
fmt.Println(err)
|
||||
}
|
||||
user, err := gorm.G[User](c.client).Where("account = ?", param.Account).First(context.Background())
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
fmt.Println("没有找到用户")
|
||||
} else {
|
||||
u := c.query.User
|
||||
user, err := u.WithContext(context.Background()).Where(u.Account.Eq(param.Account)).Take()
|
||||
if err != nil {
|
||||
fmt.Println("查询错误")
|
||||
panic(err)
|
||||
common.SetData(ctx, ero.SimpleError("服务器繁忙,请稍后重试"))
|
||||
}
|
||||
if user == nil {
|
||||
fmt.Println("没有找到用户")
|
||||
return
|
||||
}
|
||||
if user.Passwd != param.Password {
|
||||
fmt.Println("用户密码错误")
|
||||
return
|
||||
}
|
||||
fmt.Println("接受参数")
|
||||
fmt.Println(user)
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"message": "success",
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Controller) RegisterRoute(gin *gin.Engine) {
|
||||
group := gin.Group(version).Group(group)
|
||||
group.POST("/login", c.PostLogin)
|
||||
group.POST("/login", c.postLogin)
|
||||
}
|
||||
|
||||
// New 获取用户模块的控制器
|
||||
@@ -54,6 +56,6 @@ func (c *Controller) RegisterRoute(gin *gin.Engine) {
|
||||
//
|
||||
// 返回值
|
||||
// - Controller 控制器的实例指针
|
||||
func New(client *gorm.DB) *Controller {
|
||||
return &Controller{client}
|
||||
func New(param *query.Query) *Controller {
|
||||
return &Controller{param}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package user_test
|
||||
|
||||
import (
|
||||
"Crimson-Gatekeeper/internal/common"
|
||||
"Crimson-Gatekeeper/internal/query"
|
||||
"Crimson-Gatekeeper/internal/user"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
@@ -14,16 +15,14 @@ import (
|
||||
)
|
||||
|
||||
func TestLogin(t *testing.T) {
|
||||
client, close := common.GetDataBaseClient()
|
||||
defer close()
|
||||
|
||||
client, clean := common.GetDataBaseClient()
|
||||
defer clean()
|
||||
gin.SetMode(gin.TestMode)
|
||||
app := gin.Default()
|
||||
user.New(client).RegisterRoute(app)
|
||||
|
||||
user.New(query.Use(client)).RegisterRoute(app)
|
||||
w := httptest.NewRecorder()
|
||||
param := gin.H{
|
||||
"account": "admin",
|
||||
"account": "admin1",
|
||||
"password": "123445",
|
||||
}
|
||||
data, ero := json.Marshal(param)
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Id int64
|
||||
CreateTime time.Time
|
||||
LastUpdateTime time.Time
|
||||
Name string
|
||||
Account string
|
||||
Passwd string
|
||||
}
|
||||
25
srv/internal/user/service.go
Normal file
25
srv/internal/user/service.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"Crimson-Gatekeeper/internal/model"
|
||||
"Crimson-Gatekeeper/internal/query"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// loginInfo 储存用户的登录状态
|
||||
type loginInfo struct {
|
||||
// user 信息指针
|
||||
user *model.User
|
||||
// invalidation 失效时间,此事件之后,该数据应该失效且被删除
|
||||
invalidation time.Time
|
||||
}
|
||||
|
||||
var loginIn = sync.Map{}
|
||||
|
||||
type Service struct {
|
||||
c *query.Query
|
||||
}
|
||||
|
||||
func (s *Service) login(user *model.User) error {
|
||||
}
|
||||
Reference in New Issue
Block a user