option: replace leveldb with redis in storing message code

This commit is contained in:
RockYang
2023-11-22 10:57:24 +08:00
parent 8901cf8e81
commit 8f63e348b3
12 changed files with 35 additions and 154 deletions

View File

@@ -4,23 +4,23 @@ import (
"chatplus/core"
"chatplus/core/types"
"chatplus/service"
"chatplus/store"
"chatplus/utils"
"chatplus/utils/resp"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
)
const CodeStorePrefix = "/verify/codes/"
type SmsHandler struct {
BaseHandler
leveldb *store.LevelDB
redis *redis.Client
sms *service.AliYunSmsService
captcha *service.CaptchaService
}
func NewSmsHandler(app *core.AppServer, db *store.LevelDB, sms *service.AliYunSmsService, captcha *service.CaptchaService) *SmsHandler {
handler := &SmsHandler{leveldb: db, sms: sms, captcha: captcha}
func NewSmsHandler(app *core.AppServer, client *redis.Client, sms *service.AliYunSmsService, captcha *service.CaptchaService) *SmsHandler {
handler := &SmsHandler{redis: client, sms: sms, captcha: captcha}
handler.App = app
return handler
}
@@ -50,7 +50,7 @@ func (h *SmsHandler) SendCode(c *gin.Context) {
}
// 存储验证码,等待后面注册验证
err = h.leveldb.Put(CodeStorePrefix+data.Mobile, code)
_, err = h.redis.Set(c, CodeStorePrefix+data.Mobile, code, 0).Result()
if err != nil {
resp.ERROR(c, "验证码保存失败")
return