|
|
@@ -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),
|