尝试登录逻辑

This commit is contained in:
des
2026-01-19 00:07:20 +08:00
parent c61fb947e7
commit 2d65afdf33
16 changed files with 713 additions and 66 deletions

View File

@@ -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(&param); 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}
}