-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathprovider.go
96 lines (90 loc) · 2.43 KB
/
provider.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
package wework
import (
"github.com/dgraph-io/badger/v3"
"github.com/go-laoji/wecom-go-sdk/v2/internal"
"time"
)
type providerAccessTokenResponse struct {
internal.BizResponse
ProviderAccessToken string `json:"provider_access_token"`
ExpiresIn int `json:"expires_in"`
}
func (ww *weWork) requestProviderToken() (resp providerAccessTokenResponse) {
apiUrl := "/cgi-bin/service/get_provider_token"
params := H{}
params["corpid"] = ww.corpId
params["provider_secret"] = ww.providerSecret
var err error
_, err = ww.httpClient.R().SetBody(params).SetResult(&resp).Post(apiUrl)
if err != nil {
resp.ErrCode = 400
resp.ErrorMsg = err.Error()
return
}
return resp
}
func (ww *weWork) getProviderToken() (token string) {
var err error
var item *badger.Item
err = ww.cache.View(func(txn *badger.Txn) error {
item, err = txn.Get([]byte("providerToken"))
if err == badger.ErrKeyNotFound {
return err
}
item.Value(func(val []byte) error {
token = string(val)
return nil
})
return err
})
if err == badger.ErrKeyNotFound {
if resp := ww.requestProviderToken(); resp.ErrCode != 0 {
panic(resp)
//logger.Error(err.Error())
} else {
token = resp.ProviderAccessToken
ww.cache.Update(func(txn *badger.Txn) error {
entry := badger.NewEntry([]byte("providerToken"), []byte(token)).
WithTTL(time.Second * time.Duration(resp.ExpiresIn))
err = txn.SetEntry(entry)
return err
})
}
}
return token
}
type GetLoginInfoResponse struct {
internal.BizResponse
UserType int `json:"usertype"`
UserInfo struct {
} `json:"user_info"`
CorpInfo struct {
CorpId string `json:"corpid"`
} `json:"corp_info"`
Agent []struct {
AgentId int `json:"agentid"`
AuthType int `json:"auth_type"`
} `json:"agent"`
AuthInfo []struct {
Department []struct {
Id int `json:"id"`
Writable bool `json:"writable"`
} `json:"department"`
} `json:"auth_info"`
}
// GetLoginInfo 获取登录用户信息
// https://open.work.weixin.qq.com/api/doc/90001/90143/91125
// Deprecated: 2023-06-10重构时发现找不到该接口了
func (ww *weWork) GetLoginInfo(authCode string) (resp GetLoginInfoResponse) {
h := H{}
h["auth_code"] = authCode
_, err := ww.httpClient.R().SetQueryParam("access_token", ww.getProviderToken()).
SetBody(h).SetResult(&resp).Post("/cgi-bin/service/get_login_info")
if err != nil {
logger.Sugar().Error(err)
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
return
}
return
}