| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- package requests
- import (
- "github.com/gin-gonic/gin"
- "github.com/runningwater/gohub/app/requests/validators"
- "github.com/thedevsaddam/govalidator"
- )
- type LoginByPhoneRequest struct {
- Phone string `json:"phone,omitempty" valid:"phone"`
- VerifyCode string `json:"verify_code,omitempty" valid:"verify_code"`
- }
- // LoginByPhone 验证表单,返回长度等于零即通过
- func LoginByPhone(data any, c *gin.Context) map[string][]string {
- // 1. 定制认证规则
- rules := govalidator.MapData{
- "phone": []string{"required", "digits:11"},
- "verify_code": []string{"required", "digits:6"},
- }
- // 2. 定制错误消息
- messages := govalidator.MapData{
- "phone": []string{
- "required:手机号为必填项,参数名称 phone",
- "digits:手机号长度必须为 11 位的数字",
- },
- "verify_code": []string{
- "required:验证码答案必填",
- "digits:验证码长度必须为 6 位的数字",
- },
- }
- errs := validate(data, rules, messages)
- // 3. ... 增加其他业务逻辑
- _data := data.(*LoginByPhoneRequest)
- errs = validators.ValidateVerifyCode(_data.Phone, _data.VerifyCode, errs)
- // 4. 返回错误消息
- return errs
- }
|