update captcha

This commit is contained in:
aichy126
2023-07-17 18:46:09 +08:00
parent 3a12d46c37
commit b6a85b06ab
5 changed files with 25 additions and 12 deletions
+3 -2
View File
@@ -16,6 +16,7 @@ const (
)
type ActionRecordInfo struct {
LastTime int `json:"last_time"`
Num int `json:"num"`
LastTime int `json:"last_time"`
Num int `json:"num"`
Config string `json:"config"`
}
+1 -1
View File
@@ -27,7 +27,7 @@ func NewCaptchaRepo(data *data.Data) action.CaptchaRepo {
}
}
func (cr *captchaRepo) SetActionType(ctx context.Context, unit, actionType string, amount int) (err error) {
func (cr *captchaRepo) SetActionType(ctx context.Context, unit, actionType, config string, amount int) (err error) {
now := time.Now()
cacheKey := fmt.Sprintf("ActionRecord:%s@%s@%s", unit, actionType, now.Format("2006-1-02"))
value := &entity.ActionRecordInfo{}
+1 -1
View File
@@ -16,7 +16,7 @@ var (
func Test_captchaRepo_DelActionType(t *testing.T) {
captchaRepo := captcha.NewCaptchaRepo(testDataSource)
err := captchaRepo.SetActionType(context.TODO(), ip, actionType, amount)
err := captchaRepo.SetActionType(context.TODO(), ip, actionType, "", amount)
assert.NoError(t, err)
gotAmount, err := captchaRepo.GetActionType(context.TODO(), ip, actionType)
+8 -8
View File
@@ -18,7 +18,7 @@ type CaptchaRepo interface {
SetCaptcha(ctx context.Context, key, captcha string) (err error)
GetCaptcha(ctx context.Context, key string) (captcha string, err error)
DelCaptcha(ctx context.Context, key string) (err error)
SetActionType(ctx context.Context, unit, actionType string, amount int) (err error)
SetActionType(ctx context.Context, unit, actionType, config string, amount int) (err error)
GetActionType(ctx context.Context, unit, actionType string) (actioninfo *entity.ActionRecordInfo, err error)
DelActionType(ctx context.Context, unit, actionType string) (err error)
}
@@ -69,9 +69,9 @@ func (cs *CaptchaService) UserRegisterVerifyCaptcha(
// ActionRecordVerifyCaptcha
// Verify that you need to enter a CAPTCHA, and that the CAPTCHA is correct
func (cs *CaptchaService) ActionRecordVerifyCaptcha(
ctx context.Context, actionType string, ip string, id string, VerifyValue string,
ctx context.Context, actionType string, unit string, id string, VerifyValue string,
) bool {
verificationResult := cs.ValidationStrategy(ctx, ip, actionType)
verificationResult := cs.ValidationStrategy(ctx, unit, actionType)
if !verificationResult {
if id == "" || VerifyValue == "" {
return false
@@ -85,22 +85,22 @@ func (cs *CaptchaService) ActionRecordVerifyCaptcha(
return true
}
func (cs *CaptchaService) ActionRecordAdd(ctx context.Context, actionType string, ip string) (int, error) {
func (cs *CaptchaService) ActionRecordAdd(ctx context.Context, actionType string, unit string) (int, error) {
var err error
info, cahceErr := cs.captchaRepo.GetActionType(ctx, ip, actionType)
info, cahceErr := cs.captchaRepo.GetActionType(ctx, unit, actionType)
if cahceErr != nil {
log.Error(err)
}
info.Num++
err = cs.captchaRepo.SetActionType(ctx, ip, actionType, info.Num)
err = cs.captchaRepo.SetActionType(ctx, unit, actionType, "", info.Num)
if err != nil {
return 0, err
}
return info.Num, nil
}
func (cs *CaptchaService) ActionRecordDel(ctx context.Context, actionType string, ip string) {
err := cs.captchaRepo.DelActionType(ctx, ip, actionType)
func (cs *CaptchaService) ActionRecordDel(ctx context.Context, actionType string, unit string) {
err := cs.captchaRepo.DelActionType(ctx, unit, actionType)
if err != nil {
log.Error(err)
}
@@ -52,70 +52,82 @@ func (cs *CaptchaService) CaptchaActionEmail(ctx context.Context, actioninfo *en
// setNum := 0
// setTime := 0 //seconds
// You need a verification code every time
spew.Dump("[CaptchaActionEmail]", actioninfo)
return false
}
func (cs *CaptchaService) CaptchaActionPassword(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionPassword]", actioninfo)
// setNum := 0
// setTime := 0 //seconds
return false
}
func (cs *CaptchaService) CaptchaActionEditUserinfo(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionEditUserinfo]", actioninfo)
// setNum := 0
// setTime := 0 //seconds
return false
}
func (cs *CaptchaService) CaptchaActionQuestion(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionQuestion]", actioninfo)
// setNum := 0
// setTime := 0 //seconds
return false
}
func (cs *CaptchaService) CaptchaActionAnswer(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionAnswer]", actioninfo)
// setNum := 0
// setTime := 0 //seconds
return false
}
func (cs *CaptchaService) CaptchaActionComment(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionComment]", actioninfo)
// setNum := 0
// setTime := 0 //seconds
return false
}
func (cs *CaptchaService) CaptchaActionEdit(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionEdit]", actioninfo)
// setNum := 0
// setTime := 0 //seconds
return false
}
func (cs *CaptchaService) CaptchaActionInvitationAnswer(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionInvitationAnswer]", actioninfo)
// setNum := 0
// setTime := 0 //seconds
return false
}
func (cs *CaptchaService) CaptchaActionSearch(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionSearch]", actioninfo)
// setNum := 0
// setTime := 0 //seconds
return false
}
func (cs *CaptchaService) CaptchaActionReport(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionReport]", actioninfo)
// setNum := 0
// setTime := 0 //seconds
return false
}
func (cs *CaptchaService) CaptchaActionDelete(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionDelete]", actioninfo)
// setNum := 0
// setTime := 0 //seconds
return false
}
func (cs *CaptchaService) CaptchaActionVote(ctx context.Context, actioninfo *entity.ActionRecordInfo) bool {
spew.Dump("[CaptchaActionVote]", actioninfo)
// setNum := 0
// setTime := 0 //seconds
return false