Skip to content

Commit 6da8840

Browse files
committed
update
1 parent 1d6eea5 commit 6da8840

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

redis/verifycode.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package redis
33
import (
44
"context"
55
"errors"
6+
"strconv"
67

78
"github.com/chenjie199234/Corelib/util/common"
89

@@ -104,20 +105,25 @@ func (c *Client) CheckVerifyCode(ctx context.Context, target, action, code, must
104105
}
105106

106107
// return nil means has check times
107-
// return ErrVerifyCodeCheckTimesUsedup means no check times any more
108108
// return ErrVerifyCodeMissing means verify code doesn't exist
109-
func (c *Client) HasCheckTimes(ctx context.Context, target, action string) error {
109+
// return ErrVerifyCodeCheckTimesUsedup means no check times any more
110+
// return ErrVerifyCodeReceiverMissing means the must receiver doesn't exist
111+
func (c *Client) HasCheckTimes(ctx context.Context, target, action, mustreceiver string) error {
110112
key := "verify_code_{" + target + "}_" + action
111-
r, e := c.HGet(ctx, key, "_check").Int()
113+
rs, e := c.HMGet(ctx, key, "_check", mustreceiver).Result()
112114
if e != nil {
113-
if e == gredis.Nil {
114-
e = ErrVerifyCodeMissing
115-
}
116115
return e
117116
}
118-
if r >= 5 {
117+
if rs[0] == nil {
118+
return ErrVerifyCodeMissing
119+
} else if r, e := strconv.Atoi(rs[0].(string)); e != nil {
120+
return e
121+
} else if r >= 5 {
119122
return ErrVerifyCodeCheckTimesUsedup
120123
}
124+
if mustreceiver != "" && rs[1] == nil {
125+
return ErrVerifyCodeReceiverMissing
126+
}
121127
return nil
122128
}
123129

redis/verifycode_test.go

+39-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ func Test_VerifyCode(t *testing.T) {
2626
t.Fatal(e)
2727
return
2828
}
29+
30+
//0 empty
31+
if e := client.HasCheckTimes(context.Background(), "testuserid", "testaction", ""); e != ErrVerifyCodeMissing {
32+
t.Fatal("should be verify code missing")
33+
return
34+
}
2935
//1
3036
code, dup, e := client.MakeVerifyCode(context.Background(), "testuserid", "testaction", "", 300)
3137
if e != nil {
@@ -36,6 +42,10 @@ func Test_VerifyCode(t *testing.T) {
3642
t.Fatal("should not be dup")
3743
return
3844
}
45+
if e := client.HasCheckTimes(context.Background(), "testuserid", "testaction", "[email protected]"); e != ErrVerifyCodeReceiverMissing {
46+
t.Fatal("should be receiver missing")
47+
return
48+
}
3949
//1 dup check
4050
newcode, dup, e := client.MakeVerifyCode(context.Background(), "testuserid", "testaction", "[email protected]", 300)
4151
if e != nil {
@@ -50,6 +60,10 @@ func Test_VerifyCode(t *testing.T) {
5060
t.Fatal("should get same code")
5161
return
5262
}
63+
if e := client.HasCheckTimes(context.Background(), "testuserid", "testaction", "[email protected]"); e != nil {
64+
t.Fatal(e)
65+
return
66+
}
5367
newcode, dup, e = client.MakeVerifyCode(context.Background(), "testuserid", "testaction", "[email protected]", 300)
5468
if e != nil {
5569
t.Fatal(e)
@@ -76,6 +90,10 @@ func Test_VerifyCode(t *testing.T) {
7690
t.Fatal("should get same code")
7791
return
7892
}
93+
if e := client.HasCheckTimes(context.Background(), "testuserid", "testaction", "123123123"); e != nil {
94+
t.Fatal(e)
95+
return
96+
}
7997

8098
//1 wrong receiver check
8199
if e := client.CheckVerifyCode(context.Background(), "testuserid", "testaction", code, "abc"); e != ErrVerifyCodeReceiverMissing {
@@ -86,7 +104,7 @@ func Test_VerifyCode(t *testing.T) {
86104
}
87105
return
88106
}
89-
if e := client.HasCheckTimes(context.Background(), "testuserid", "testaction"); e != nil {
107+
if e := client.HasCheckTimes(context.Background(), "testuserid", "testaction", ""); e != nil {
90108
t.Fatal(e)
91109
return
92110
}
@@ -96,7 +114,7 @@ func Test_VerifyCode(t *testing.T) {
96114
t.Fatal(e)
97115
return
98116
}
99-
if e := client.HasCheckTimes(context.Background(), "testuserid", "testaction"); e != ErrVerifyCodeMissing {
117+
if e := client.HasCheckTimes(context.Background(), "testuserid", "testaction", ""); e != ErrVerifyCodeMissing {
100118
t.Fatal("should be verify code missing")
101119
return
102120
}
@@ -117,7 +135,7 @@ func Test_VerifyCode(t *testing.T) {
117135
t.Fatal(e)
118136
return
119137
}
120-
if e := client.HasCheckTimes(context.Background(), "testuserid", "testaction"); e != ErrVerifyCodeMissing {
138+
if e := client.HasCheckTimes(context.Background(), "testuserid", "testaction", ""); e != ErrVerifyCodeMissing {
121139
t.Fatal("should be verify code missing")
122140
return
123141
}
@@ -150,7 +168,23 @@ func Test_VerifyCode(t *testing.T) {
150168
}
151169
return
152170
}
153-
if e := client.HasCheckTimes(context.Background(), "testuserid", "testaction"); e != nil {
171+
if e := client.CheckVerifyCode(context.Background(), "testuserid", "testaction", "123", ""); e != ErrVerifyCodeWrong {
172+
if e == nil {
173+
t.Fatal("should not pass")
174+
} else {
175+
t.Fatal(e)
176+
}
177+
return
178+
}
179+
if e := client.CheckVerifyCode(context.Background(), "testuserid", "testaction", "123", ""); e != ErrVerifyCodeWrong {
180+
if e == nil {
181+
t.Fatal("should not pass")
182+
} else {
183+
t.Fatal(e)
184+
}
185+
return
186+
}
187+
if e := client.HasCheckTimes(context.Background(), "testuserid", "testaction", ""); e != nil {
154188
t.Fatal(e)
155189
return
156190
}
@@ -162,7 +196,7 @@ func Test_VerifyCode(t *testing.T) {
162196
}
163197
return
164198
}
165-
if e := client.HasCheckTimes(context.Background(), "testuserid", "testaction"); e != ErrVerifyCodeCheckTimesUsedup {
199+
if e := client.HasCheckTimes(context.Background(), "testuserid", "testaction", ""); e != ErrVerifyCodeCheckTimesUsedup {
166200
t.Fatal("should be check times used up")
167201
return
168202
}

0 commit comments

Comments
 (0)