2024-07-12 20:34:06 +08:00
|
|
|
|
package captcha
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"picgo/corelib"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Store 存储验证码接口
|
|
|
|
|
type Store interface {
|
|
|
|
|
// SetCode cid 当前id
|
|
|
|
|
// oid 旧id
|
|
|
|
|
// code 验证码
|
|
|
|
|
// expired 过期时间(毫秒)
|
|
|
|
|
// return nil/error
|
|
|
|
|
SetCode(cid string, oid string, code string, expired int)
|
|
|
|
|
// GetCode cid 当前id
|
|
|
|
|
// return code
|
|
|
|
|
GetCode(cid string) string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetCode 验证码保存到redis
|
|
|
|
|
func SetCode(cid string, code string, expired int64) (err error) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
cid = genRedisKey(cid)
|
|
|
|
|
if err = corelib.RdbClient.Set(ctx, cid, code, time.Duration(expired)*time.Second).Err(); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetCode 从redis中查询验证码,查询之后删除
|
|
|
|
|
func GetCode(cid string) (code string, err error) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
cid = genRedisKey(cid)
|
|
|
|
|
if code, err = corelib.RdbClient.Get(ctx, cid).Result(); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
//rdb.Del(ctx, cid)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// captcha缓存key拼接
|
|
|
|
|
func genRedisKey(id string) (res string) {
|
2024-07-13 20:33:20 +08:00
|
|
|
|
return corelib.CaptchaKey + id
|
2024-07-12 20:34:06 +08:00
|
|
|
|
}
|