Przeglądaj źródła

发送短信验证码

runningwater 10 miesięcy temu
rodzic
commit
b77334c01c

+ 4 - 4
.air.toml

@@ -4,8 +4,8 @@ tmp_dir = "tmp"
 
 [build]
   args_bin = []
-  bin = "./tmp/main"
-  cmd = "go build -o ./tmp/main ."
+  bin = "./tmp/gohub"
+  cmd = "go build -o ./tmp/gohub ."
   delay = 1000
   exclude_dir = ["assets", "tmp", "vendor", "testdata"]
   exclude_file = []
@@ -28,7 +28,7 @@ tmp_dir = "tmp"
   stop_on_error = false
 
 [color]
-  app = ""
+  app = "green"
   build = "yellow"
   main = "magenta"
   runner = "green"
@@ -48,5 +48,5 @@ tmp_dir = "tmp"
   proxy_port = 0
 
 [screen]
-  clear_on_rebuild = false
+  clear_on_rebuild = true
   keep_scroll = true

+ 20 - 0
app/http/controllers/api/v1/auth/verify_code_controller.go

@@ -4,6 +4,7 @@ import (
 	"github.com/gin-gonic/gin"
 
 	v1 "github.com/runningwater/gohub/app/http/controllers/api/v1"
+	"github.com/runningwater/gohub/app/requests"
 	"github.com/runningwater/gohub/pkg/captcha"
 	"github.com/runningwater/gohub/pkg/logger"
 	"github.com/runningwater/gohub/pkg/response"
@@ -25,3 +26,22 @@ func (vc *VerifyCodeController) ShowCaptcha(c *gin.Context) {
 		"captcha_image": b64s,
 	})
 }
+
+// SendUsingPhone 发送手机验证码
+func (vc *VerifyCodeController) SendUsingPhone(c *gin.Context) {
+
+	// 1. 验证表单
+	request := requests.VerifyCodeRequest{}
+	if ok := requests.Validate(c, &request, requests.VerifyCodePhone); !ok {
+		return
+	}
+
+	// 2. 发送手机验证码
+	//  if ok := verifycode.NewVerifyCode().SendSMS(request.Phone); !ok {
+	// 	response.Abort500(c, "发送短信失败~~")
+	// }else {
+	// 	response.Success(c)
+	// }
+
+	response.Success(c)
+}

+ 48 - 0
app/requests/verify_code_request.go

@@ -0,0 +1,48 @@
+package requests
+
+import (
+	"github.com/gin-gonic/gin"
+	"github.com/runningwater/gohub/pkg/captcha"
+	"github.com/thedevsaddam/govalidator"
+)
+
+type VerifyCodeRequest struct {
+	Phone string `json:"phone,omitempy" valid:"phone"`
+
+	CaptchaID     string `json:"captcha_id,omitempy" valid:"captcha_id"`
+	CaptchaAnswer string `json:"captcha_answer,omitempy" valid:"captcha_answer"`
+}
+
+func VerifyCodePhone(data any, c *gin.Context) map[string][]string {
+
+	// 1. 定制认证规则
+	rules := govalidator.MapData{
+		"phone":          []string{"required", "digits:11"},
+		"captcha_id":     []string{"required"},
+		"captcha_answer": []string{"required", "digits:6"},
+	}
+	// 2. 定制错误信息
+	messages := govalidator.MapData{
+		"phone": []string{
+			"required:手机号不能为空",
+			"digits:手机号格式不正确,长度必须为11位",
+		},
+		"captcha_id": []string{
+			"required:验证码ID不能为空",
+		},
+		"captcha_answer": []string{
+			"required:验证码不能为空",
+			"digits:验证码格式不正确,长度必须为6位",
+		},
+	}
+
+	errs := validate(data, rules, messages)
+
+	// Captcha 图片验证
+	_data := data.(*VerifyCodeRequest)
+	if ok := captcha.NewCaptcha().VerifyCaptcha(_data.CaptchaID, _data.CaptchaAnswer); !ok {
+		errs["captcha_answer"] = append(errs["captcha_answer"], "验证码错误")
+	}
+	// 3. 返回错误信息
+	return errs
+}

+ 11 - 1
gohub.http

@@ -16,4 +16,14 @@ Content-Type: application/json
 
 ### POST /verify_code/captcha 图片验证码
 POST {{base_url}}/v1/auth/verify_code/captcha HTTP/1.1
-Content-Type: application/json
+Content-Type: application/json
+
+### POST /verify_code/phone 发送手机验证码
+POST {{base_url}}/v1/auth/verify_code/phone HTTP/1.1
+Content-Type: application/json
+
+{
+  "phone": "15968875425",
+  "captcha_id": "captcha_skip_test",
+  "captcha_answer": "123456"
+}

+ 4 - 1
routes/api.go

@@ -14,12 +14,15 @@ func RegisterAPIRoutes(router *gin.Engine) {
 		authGroup := v1.Group("/auth")
 		{
 			suc := new(auth.SignupController)
+			vcc := new(auth.VerifyCodeController)
 			// 注册手机号是否已存在
 			authGroup.POST("/signup/phone/exist", suc.IsPhoneExist)
 			// 注册邮箱是否已存在
 			authGroup.POST("/signup/email/exist", suc.IsEmailExist)
 			// 显示图片验证码
-			authGroup.POST("/verify_code/captcha", new(auth.VerifyCodeController).ShowCaptcha)
+			authGroup.POST("/verify_code/captcha", vcc.ShowCaptcha)
+			// 发送手机验证码
+			authGroup.POST("/verify_code/phone", vcc.SendUsingPhone)
 		}
 	}
 }