-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
68 lines (55 loc) · 1.35 KB
/
client.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
package wechat
import (
"fmt"
"time"
"github.com/qiniu/x/rpc.v7"
)
var (
ApiHost string = "https://qyapi.weixin.qq.com"
)
type WechatWork struct {
agentId int
corpId string
corpSecret string
token AccessToken
Client rpc.Client
}
type AccessToken struct {
AccessToken string `json:"access_token"`
ExpiresIn int64 `json:"expires_in"`
ExpiresAt int64 `json:"expires_at"`
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
}
func NewWechatWork(corpId string, secretKey string, agentId int) (w *WechatWork, err error) {
w = &WechatWork{
corpId: corpId,
corpSecret: secretKey,
agentId: agentId,
Client: rpc.DefaultClient,
}
w.token, err = w.GetAccessToken()
return
}
//提前一分钟判断为token失效
func (t *AccessToken) expired() bool {
return t.ExpiresAt <= time.Now().Unix()+60
}
func (w *WechatWork) GetAccessToken() (token AccessToken, err error) {
token = w.token
if !token.expired() {
return
}
token, err = w.refreshToken()
return
}
func (w *WechatWork) refreshToken() (token AccessToken, err error) {
url1 := fmt.Sprintf("%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s", ApiHost, w.corpId, w.corpSecret)
err = w.Client.CallWithJson(nil, &token, "GET", url1, nil)
if err != nil {
return
}
token.ExpiresAt = time.Now().Unix() + token.ExpiresIn
w.token = token
return
}