错误处理中间件

This commit is contained in:
des
2026-01-15 18:57:32 +08:00
parent 69dad45d6a
commit c61fb947e7
4 changed files with 55 additions and 3 deletions

View File

@@ -1,6 +1,8 @@
package user
import (
"context"
"errors"
"fmt"
"net/http"
@@ -16,8 +18,8 @@ type Controller struct {
}
type PostLoginParam struct {
account string `binding:"required"`
password string `binding:"required"`
Account string `json:"account" binding:"required"`
Password string `json:"password" binding:"required"`
}
func (c *Controller) PostLogin(ctx *gin.Context) {
@@ -26,6 +28,15 @@ func (c *Controller) PostLogin(ctx *gin.Context) {
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 {
fmt.Println("查询错误")
panic(err)
}
fmt.Println("接受参数")
fmt.Println(user)
ctx.JSON(http.StatusOK, gin.H{
"message": "success",
})