password_request.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package requests
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/runningwater/gohub/app/requests/validators"
  5. "github.com/thedevsaddam/govalidator"
  6. )
  7. type ResetByPhoneRequest struct {
  8. Phone string `json:"phone,omitempty" valid:"phone"`
  9. VerifyCode string `json:"verify_code,omitempty" valid:"verify_code"`
  10. Password string `json:"password,omitempty" valid:"password"`
  11. }
  12. // ResetByPhone 验证表单,返回长度等于零即通过
  13. func ResetByPhone(data any, c *gin.Context) map[string][]string {
  14. rules := govalidator.MapData{
  15. "phone": []string{"required", "digits:11"},
  16. "verify_code": []string{"required", "digits:6"},
  17. "password": []string{"required", "min:6"},
  18. }
  19. messages := govalidator.MapData{
  20. "phone": []string{
  21. "required:手机号为必填项,参数名称 phone",
  22. "digits:手机号长度必须为 11 位的数字",
  23. },
  24. "verify_code": []string{
  25. "required:验证码答案必填",
  26. "digits:验证码长度必须为 6 位的数字",
  27. },
  28. "password": []string{
  29. "required:密码为必填项",
  30. "min:密码长度需大于 6",
  31. },
  32. }
  33. errs := validate(data, rules, messages)
  34. // 验证码是否正确
  35. _data := data.(*ResetByPhoneRequest)
  36. errs = validators.ValidateVerifyCode(_data.Phone, _data.VerifyCode, errs)
  37. return errs
  38. }