53 lines
1012 B
Go
53 lines
1012 B
Go
|
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,
|
|||
|
})
|
|||
|
}
|
|||
|
}
|