25개 이상의 토픽을 선택하실 수 없습니다.
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.2 KiB
66 lines
1.2 KiB
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/mojocn/base64Captcha"
|
|
"time"
|
|
)
|
|
|
|
const CAPTCHA = "captcha:"
|
|
|
|
type RedisStore struct {
|
|
base64Captcha.Store
|
|
}
|
|
|
|
var redisObj = new(RedisClient)
|
|
|
|
//set a capt
|
|
func (r RedisStore) Set(id string, value string) error {
|
|
key := CAPTCHA + id
|
|
client := redisObj.GetClient()
|
|
_, err := client.Set(key, value, time.Minute*2).Result()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
redisObj.CloseClient(client)
|
|
return nil
|
|
}
|
|
|
|
//get a capt
|
|
func (r RedisStore) Get(id string, clear bool) string {
|
|
key := CAPTCHA + id
|
|
client := redisObj.GetClient()
|
|
val, err := client.Get(key).Result()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return ""
|
|
}
|
|
if clear {
|
|
client.Del(key)
|
|
}
|
|
redisObj.CloseClient(client)
|
|
return val
|
|
}
|
|
|
|
//verify a capt
|
|
func (r RedisStore) Verify(id, answer string, clear bool) bool {
|
|
v := RedisStore{}.Get(id, clear)
|
|
fmt.Println("key:" + id + ";value:" + v + ";answer:" + answer)
|
|
return v == answer
|
|
}
|
|
|
|
//get a capt
|
|
func (r RedisStore) Has(id string) int {
|
|
key := CAPTCHA + id
|
|
client := redisObj.GetClient()
|
|
val, err := client.Get(key).Result()
|
|
redisObj.CloseClient(client)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return 0
|
|
}
|
|
if val == "" {
|
|
return 0
|
|
}
|
|
return 1
|
|
}
|
|
|