forked from flipped-aurora/gin-vue-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
liaoyongfu
committed
Jul 14, 2021
1 parent
5232830
commit d023905
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package captcha | ||
|
||
import ( | ||
"gin-vue-admin/global" | ||
"time" | ||
|
||
"github.com/mojocn/base64Captcha" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func NewDefaultRedisStore() base64Captcha.Store { | ||
return &RedisStore{ | ||
Expiration: time.Second * 180, | ||
PreKey: "CAPTCHA_", | ||
} | ||
} | ||
|
||
type RedisStore struct { | ||
Expiration time.Duration | ||
PreKey string | ||
} | ||
|
||
func (rs *RedisStore) Set(id string, value string) { | ||
err := global.GVA_REDIS.Set(rs.PreKey+id, value, rs.Expiration).Err() | ||
if err != nil { | ||
global.GVA_LOG.Error("RedisStoreSetError!", zap.Error(err)) | ||
} | ||
} | ||
|
||
func (rs *RedisStore) Get(key string, clear bool) string { | ||
val, err := global.GVA_REDIS.Get(key).Result() | ||
if err != nil { | ||
global.GVA_LOG.Error("RedisStoreGetError!", zap.Error(err)) | ||
return "" | ||
} | ||
if clear { | ||
err := global.GVA_REDIS.Del(key).Err() | ||
if err != nil { | ||
global.GVA_LOG.Error("RedisStoreClearError!", zap.Error(err)) | ||
return "" | ||
} | ||
} | ||
return val | ||
} | ||
|
||
func (rs *RedisStore) Verify(id, answer string, clear bool) bool { | ||
key := rs.PreKey + id | ||
v := rs.Get(key, clear) | ||
return v == answer | ||
} |