Files
Crimson-Gatekeeper/srv/internal/user/controller.go

62 lines
1.4 KiB
Go
Raw Normal View History

2026-01-13 00:08:49 +08:00
package user
import (
2026-01-19 00:07:20 +08:00
"Crimson-Gatekeeper/internal/common"
"Crimson-Gatekeeper/internal/ero"
"Crimson-Gatekeeper/internal/query"
2026-01-15 18:57:32 +08:00
"context"
2026-01-14 19:04:01 +08:00
"fmt"
2026-01-13 23:15:16 +08:00
"github.com/gin-gonic/gin"
2026-01-13 00:08:49 +08:00
)
2026-01-13 23:15:16 +08:00
const version = "/api/v1"
const group = "/user"
2026-01-13 13:02:27 +08:00
type Controller struct {
2026-01-19 00:07:20 +08:00
query *query.Query
2026-01-13 13:02:27 +08:00
}
2026-01-13 00:08:49 +08:00
2026-01-19 00:07:20 +08:00
type postLoginParam struct {
2026-01-15 18:57:32 +08:00
Account string `json:"account" binding:"required"`
Password string `json:"password" binding:"required"`
2026-01-13 00:08:49 +08:00
}
2026-01-19 00:07:20 +08:00
func (c *Controller) postLogin(ctx *gin.Context) {
param := &postLoginParam{}
2026-01-13 23:15:16 +08:00
if err := ctx.ShouldBindJSON(&param); err != nil {
2026-01-14 19:04:01 +08:00
fmt.Println("参数解析失败,请确认参数是否为 JSON")
fmt.Println(err)
2026-01-13 23:15:16 +08:00
}
2026-01-19 00:07:20 +08:00
u := c.query.User
user, err := u.WithContext(context.Background()).Where(u.Account.Eq(param.Account)).Take()
if err != nil {
2026-01-15 18:57:32 +08:00
fmt.Println("查询错误")
2026-01-19 00:07:20 +08:00
common.SetData(ctx, ero.SimpleError("服务器繁忙,请稍后重试"))
}
if user == nil {
fmt.Println("没有找到用户")
return
}
if user.Passwd != param.Password {
fmt.Println("用户密码错误")
return
2026-01-15 18:57:32 +08:00
}
2026-01-14 19:04:01 +08:00
}
2026-01-13 23:15:16 +08:00
2026-01-14 19:04:01 +08:00
func (c *Controller) RegisterRoute(gin *gin.Engine) {
group := gin.Group(version).Group(group)
2026-01-19 00:07:20 +08:00
group.POST("/login", c.postLogin)
2026-01-13 13:02:27 +08:00
}
// New 获取用户模块的控制器
//
// 参数
// - gorm.DB 该模块所使用的数据库链接
//
// 返回值
// - Controller 控制器的实例指针
2026-01-19 00:07:20 +08:00
func New(param *query.Query) *Controller {
return &Controller{param}
2026-01-13 00:08:49 +08:00
}