signup_controller.go 1021 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package auth
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. v1 "github.com/runningwater/gohub/app/http/controllers/api/v1"
  7. "github.com/runningwater/gohub/app/models/user"
  8. "github.com/runningwater/gohub/app/requests"
  9. )
  10. // SignupController 处理用户注册相关的逻辑
  11. type SignupController struct {
  12. v1.BaseApiController
  13. }
  14. func (controller *SignupController) IsPhoneExist(c *gin.Context) {
  15. // 初始化请求对象
  16. req := requests.SignupPhoneExistRequest{}
  17. // 解析 JSON 请求
  18. if err := c.ShouldBindJSON(&req); err != nil {
  19. c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{
  20. "error": "请求参数错误"+ err.Error(),
  21. })
  22. // 打印错误信息
  23. fmt.Println(err.Error())
  24. return
  25. }
  26. // 表单验证
  27. errs := requests.ValidateSignupPhoneExist(&req, c)
  28. if len(errs) > 0 {
  29. c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{
  30. "errors": errs,
  31. })
  32. return
  33. }
  34. // 检查数据库并返回响应
  35. c.JSON(200, gin.H{
  36. "exist": user.IsPhoneExist(req.Phone),
  37. })
  38. }