47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
|
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) {
|
|||
|
return corelib.AdminCaptchaKey + id
|
|||
|
}
|