Parcourir la source

feat: 验证请求

runningwater il y a 1 an
Parent
commit
6aa67558f7
5 fichiers modifiés avec 56 ajouts et 8 suppressions
  1. 14 6
      app/http/controllers/api/v1/auth/signup_controller.go
  2. 37 0
      app/requests/signup_request.go
  3. 1 0
      go.mod
  4. 2 0
      go.sum
  5. 2 2
      gohub.http

+ 14 - 6
app/http/controllers/api/v1/auth/signup_controller.go

@@ -2,10 +2,12 @@ package auth
 
 import (
 	"fmt"
+	"net/http"
 
 	"github.com/gin-gonic/gin"
 	v1 "github.com/runningwater/gohub/app/http/controllers/api/v1"
 	"github.com/runningwater/gohub/app/models/user"
+	"github.com/runningwater/gohub/app/requests"
 )
 
 // SignupController 处理用户注册相关的逻辑
@@ -15,15 +17,12 @@ type SignupController struct {
 
 func (controller *SignupController) IsPhoneExist(c *gin.Context) {
 
-	// 请求对象
-	type SignupPhoneExistRequest struct {
-		Phone string `json:"phone,omitempty" binding:"required,min=11"`
-	}
-	req := SignupPhoneExistRequest{}
+	// 初始化请求对象
+	req := requests.SignupPhoneExistRequest{}
 
 	// 解析 JSON 请求
 	if err := c.ShouldBindJSON(&req); err != nil {
-		c.AbortWithStatusJSON(422, gin.H{
+		c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{
 			"error": "请求参数错误"+ err.Error(),
 		})
 		// 打印错误信息
@@ -31,6 +30,15 @@ func (controller *SignupController) IsPhoneExist(c *gin.Context) {
 		return
 	}
 
+	// 表单验证
+	errs := requests.ValidateSignupPhoneExist(&req, c)
+	if len(errs) > 0 {
+		c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{
+			"errors": errs,
+		})
+		return
+	}
+
 	// 检查数据库并返回响应
 	c.JSON(200, gin.H{
 		"exist": user.IsPhoneExist(req.Phone),

+ 37 - 0
app/requests/signup_request.go

@@ -0,0 +1,37 @@
+// Package: requests 处理请求数据和表单验证逻辑
+package requests
+
+import (
+	"github.com/gin-gonic/gin"
+	"github.com/thedevsaddam/govalidator"
+)
+
+type SignupPhoneExistRequest struct {
+	Phone string `json:"phone,omitempty" valid:"phone"`
+}
+
+func ValidateSignupPhoneExist(data any, c *gin.Context) map[string][]string {
+
+		// 自定义验证规则
+		rules := govalidator.MapData{
+			"phone": []string{"required", "digits:11"},
+		}
+
+		// 自定义错误信息
+		messages := govalidator.MapData{
+			"phone": []string{
+				"required:手机号不能为空",
+				"digits:手机号必须是11位数字",
+			},
+		}
+
+		opts := govalidator.Options{
+			Data:         data,
+			Rules: 	 rules,
+			TagIdentifier: "valid", // 使用结构体中的valid标签
+			Messages:     messages,
+		}
+
+		// 执行验证
+		return govalidator.New(opts).ValidateStruct()
+}

+ 1 - 0
go.mod

@@ -6,6 +6,7 @@ require (
 	github.com/gin-gonic/gin v1.10.0
 	github.com/spf13/cast v1.7.1
 	github.com/spf13/viper v1.20.1
+	github.com/thedevsaddam/govalidator v1.9.10
 	gorm.io/driver/mysql v1.5.7
 	gorm.io/driver/sqlite v1.5.7
 	gorm.io/gorm v1.25.12

+ 2 - 0
go.sum

@@ -91,6 +91,8 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
 github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
 github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
 github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
+github.com/thedevsaddam/govalidator v1.9.10 h1:m3dLRbSZ5Hts3VUWYe+vxLMG+FdyQuWOjzTeQRiMCvU=
+github.com/thedevsaddam/govalidator v1.9.10/go.mod h1:Ilx8u7cg5g3LXbSS943cx5kczyNuUn7LH/cK5MYuE90=
 github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
 github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
 github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=

+ 2 - 2
gohub.http

@@ -1,9 +1,9 @@
 ### POST /signup/phone/exist 注册手机号是否已存在
-POST http://localhost:3000/v1/auth/signup/phone/exist HTTP/1.1
+POST {{base_url}}/v1/auth/signup/phone/exist HTTP/1.1
 Content-Type: application/json
 
 {
-  "phone": "15968875425"
+  "phone": "13661164483"
 }
 
 ### POST /signup/phone/verify