希曼日记

登录

简单校验

type Person struct {
	Username string `form:"username"` // ShouldBind的方法需要用form绑定参数
	Password string `form:"password"` // ShouldBind的方法需要用form绑定参数
}

func main() {
	server := gin.Default()

	var lisa Person

	server.POST("/login", func(context *gin.Context) {
		body := context.ShouldBind(&lisa) // 结构体绑定JSON
		fmt.Println("body:", body)
		// 校验
		if lisa.Username == "123" && lisa.Password == "123" {
			context.JSON(http.StatusOK, gin.H{
				"person": lisa,
			})
		} else {
			context.JSON(http.StatusBadRequest, gin.H{
				"msg": "账号密码错误",
			})
		}
	})

	err := server.Run(":4000")
	if err != nil {
		return
	}
}