health-go/services/sms/ValidateHandler.go

53 lines
1012 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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