user_credential.go
1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package userAuth
import (
"github.com/tiptok/gocomm/pkg/redis"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
)
//Redis用户权限
type RedisUserCredential struct {
phone string
Options *Options
}
func NewRedisUserCredential(phone string, options ...Option) *RedisUserCredential {
rua := &RedisUserCredential{
Options: NewOptions(options...),
phone: phone,
}
return rua
}
func (auth RedisUserCredential) AddAuth(credential string) error {
err := redis.Hset(
auth.redisKey(),
auth.field(),
credential, 0,
)
return err
}
func (auth RedisUserCredential) RemoveAuth() error {
if !auth.Exist() {
return nil
}
return redis.Hdel(auth.redisKey(), auth.field())
}
func (auth RedisUserCredential) GetAuth() (string, error) {
if !auth.Exist() {
return "", errNotFound(auth.field())
}
data, err := redis.Hget(auth.redisKey(), auth.field())
if err != nil {
return "", err
}
return string(data), nil
}
func (auth RedisUserCredential) Exist() bool {
return redis.Hexists(auth.redisKey(), auth.field())
}
func (auth RedisUserCredential) redisKey() string {
if len(auth.phone) == 0 {
return ""
}
return utils.RedisKey("user_credential")
}
func (auth RedisUserCredential) field() string {
return auth.phone
}