health-go/services/sms/ValidateHandler.go

53 lines
1012 B
Go
Raw Normal View History

2024-10-28 14:41:27 +08:00
package sms
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/redis/go-redis/v9"
"health-go/config"
"net/http"
)
func Validate(c *gin.Context) {
var user struct {
Tel string `json:"tel"`
Code string `json:"code"`
}
if err := c.ShouldBindJSON(&user); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"msg": "无效的请求参数",
"data": nil,
})
return
}
storageCode, err := config.RDC.Get(config.CTX, user.Tel).Result()
fmt.Println("storageCode:", storageCode)
if errors.Is(err, redis.Nil) {
c.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "验证码已过期",
"data": nil,
})
return
}
if user.Code != storageCode {
c.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "验证码错误",
"data": nil,
})
} else {
fmt.Println("验证码正确删除redis中的验证码")
config.RDC.Del(config.CTX, user.Tel)
c.JSON(http.StatusOK, gin.H{"code": 200,
"msg": "验证码正确",
"data": nil,
})
}
}