-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
164 lines (144 loc) · 3.35 KB
/
service.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package service
import (
"context"
"crypto/md5"
"encoding/hex"
"errors"
"github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/bilibili/kratos/pkg/net/rpc/warden"
"github.com/itswcg/micro/account/internal/dao"
authrpc "github.com/itswcg/micro/auth/api"
leafrpc "github.com/itswcg/micro/leaf/api"
"github.com/itswcg/micro/middleware/snowflake"
"github.com/prometheus/common/log"
"strconv"
)
// Service service.
type Service struct {
ac *paladin.Map
dao dao.Dao
lcSrv leafrpc.LeafClient
acSrv authrpc.AuthClient
}
// New new a service and return.
func New() (s *Service) {
type Gc struct {
LeafClient *warden.ClientConfig
AuthClient *warden.ClientConfig
}
var (
ac = new(paladin.TOML)
gc = new(Gc) // new 返回指针, slice,map,chan使用make
)
if err := paladin.Watch("application.toml", ac); err != nil {
panic(err)
}
if err := paladin.Get("grpc.toml").UnmarshalTOML(gc); err != nil {
panic(err)
}
lcSrv, err := leafrpc.NewClient(gc.LeafClient)
if err != nil {
panic(err)
}
acSrv, err := authrpc.NewClient(gc.AuthClient)
if err != nil {
panic(err)
}
s = &Service{
ac: ac,
dao: dao.New(),
lcSrv: lcSrv,
acSrv: acSrv,
}
return s
}
// Ping ping the resource.
func (s *Service) Ping(ctx context.Context) (err error) {
return s.dao.Ping(ctx)
}
// Close close the resource.
func (s *Service) Close() {
s.dao.Close()
}
// Rpc nextId
func (s *Service) NextID(ctx context.Context) (id int64, err error) {
leaf_tag, err := s.ac.Get("leaf_tag").String()
if err != nil {
return
}
LeafReq := &leafrpc.LeafReq{
BizTag: leaf_tag,
}
LeafReply, err := s.lcSrv.NextID(ctx, LeafReq)
if err != nil {
log.Error("s.lc NextID(%v) err(%v)", LeafReq, err)
return
}
return LeafReply.Id, nil
}
// Hash password
func (s *Service) GeneratePassword(ctx context.Context, password string) (hash_password string, err error) {
password_secret, err := s.ac.Get("password_secret").String()
if err != nil {
return
}
h := md5.New()
h.Write([]byte(password + password_secret))
return hex.EncodeToString(h.Sum(nil)), nil
}
// Check password
func (s *Service) CheckPassword(ctx context.Context, mid int64, password string) (pass bool, err error) {
hash_password, err := s.GeneratePassword(ctx, password)
if err != nil {
return
}
db_password, err := s.dao.GetPassword(ctx, mid)
if err != nil {
return
}
if hash_password == db_password {
pass = true
}
return
}
// Generate unique token
func (s *Service) GenerateToken(ctx context.Context) (token string, err error) {
// 实现
workId, err := s.ac.Get("snowflake_worker_id").Int64()
if err != nil {
return
}
work, err := snowflake.New(workId)
if err != nil {
return
}
ID := work.GetID()
return strconv.FormatInt(ID, 10), nil
}
// Set Token to auth
func (s *Service) SetToken(ctx context.Context, token string, mid int64) (err error) {
SetTokenReq := &authrpc.SetTokenReq{
Token: token,
Mid: mid,
}
SetTokenReply, err := s.acSrv.SetToken(ctx, SetTokenReq)
if err != nil {
return
}
if SetTokenReply.Success == true {
return nil
} else {
return errors.New("SetToken error")
}
}
// Get token by mid
func (s *Service) GetToken(ctx context.Context, mid int64) (token string, err error) {
GetTokenReq := &authrpc.GetTokenReq{
Mid: mid,
}
GetTokenReply, err := s.acSrv.GetToken(ctx, GetTokenReq)
if err != nil {
return
}
return GetTokenReply.Token, nil
}