您最多能選擇 25 個主題
主題必須以字母或數字為開頭,可包含連接號「-」且最長為 35 個字元。
67 行
1.2 KiB
67 行
1.2 KiB
3 年前
|
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
|
||
|
}
|