58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package component
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
"wireguard-ui/global/client"
|
|
"wireguard-ui/global/constant"
|
|
)
|
|
|
|
type Captcha struct{}
|
|
|
|
// Set
|
|
// @description: 验证码放入指定存储
|
|
// @receiver Captcha
|
|
// @param id
|
|
// @param value
|
|
// @return error
|
|
func (Captcha) Set(id string, value string) error {
|
|
return client.Redis.Set(context.Background(), fmt.Sprintf("%s:%s", constant.Captcha, id), value, 2*time.Minute).Err()
|
|
}
|
|
|
|
// Get
|
|
// @description: 获取验证码信息
|
|
// @receiver Captcha
|
|
// @param id
|
|
// @param clear
|
|
// @return string
|
|
func (Captcha) Get(id string, clear bool) string {
|
|
val, err := client.Redis.Get(context.Background(), fmt.Sprintf("%s:%s", constant.Captcha, id)).Result()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
|
|
if clear {
|
|
client.Redis.Del(context.Background(), fmt.Sprintf("%s:%s", constant.Captcha, id))
|
|
return val
|
|
}
|
|
|
|
return val
|
|
}
|
|
|
|
// Verify
|
|
// @description: 校验
|
|
// @receiver Captcha
|
|
// @param id
|
|
// @param answer
|
|
// @param clear
|
|
// @return bool
|
|
func (c Captcha) Verify(id, answer string, clear bool) bool {
|
|
if os.Getenv("GIN_MODE") != "release" {
|
|
return true
|
|
}
|
|
return strings.ToUpper(answer) == strings.ToUpper(c.Get(id, clear))
|
|
}
|