2024-03-06 17:13:29 +08:00
|
|
|
package compoment
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-03-07 11:03:46 +08:00
|
|
|
"os"
|
2024-03-06 17:31:39 +08:00
|
|
|
"strings"
|
2024-03-06 17:13:29 +08:00
|
|
|
"time"
|
|
|
|
"wireguard-dashboard/client"
|
|
|
|
"wireguard-dashboard/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CaptchaStore struct{}
|
|
|
|
|
|
|
|
// Set
|
|
|
|
// @description: 验证码放入指定存储
|
|
|
|
// @receiver CaptchaStore
|
|
|
|
// @param id
|
|
|
|
// @param value
|
|
|
|
// @return error
|
|
|
|
func (CaptchaStore) Set(id string, value string) error {
|
2024-03-07 11:03:46 +08:00
|
|
|
return client.Redis.Set(context.Background(), fmt.Sprintf("%s:%s", constant.Captcha, id), value, 2*time.Minute).Err()
|
2024-03-06 17:13:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get
|
|
|
|
// @description: 获取验证码信息
|
|
|
|
// @receiver CaptchaStore
|
|
|
|
// @param id
|
|
|
|
// @param clear
|
|
|
|
// @return string
|
|
|
|
func (CaptchaStore) Get(id string, clear bool) string {
|
2024-03-06 17:31:39 +08:00
|
|
|
val, err := client.Redis.Get(context.Background(), fmt.Sprintf("%s:%s", constant.Captcha, id)).Result()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
2024-03-06 17:13:29 +08:00
|
|
|
|
2024-03-06 17:31:39 +08:00
|
|
|
if clear {
|
|
|
|
client.Redis.Del(context.Background(), fmt.Sprintf("%s:%s", constant.Captcha, id))
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
return val
|
2024-03-06 17:13:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
// @description: 校验
|
|
|
|
// @receiver CaptchaStore
|
|
|
|
// @param id
|
|
|
|
// @param answer
|
|
|
|
// @param clear
|
|
|
|
// @return bool
|
2024-03-06 17:31:39 +08:00
|
|
|
func (c CaptchaStore) Verify(id, answer string, clear bool) bool {
|
2024-03-07 11:03:46 +08:00
|
|
|
if os.Getenv("GIN_MODE") != "release" {
|
|
|
|
return true
|
|
|
|
}
|
2024-03-06 17:31:39 +08:00
|
|
|
storeAnswer := c.Get(id, clear)
|
|
|
|
return strings.ToUpper(answer) == strings.ToUpper(storeAnswer)
|
2024-03-06 17:13:29 +08:00
|
|
|
}
|