-
Notifications
You must be signed in to change notification settings - Fork 94
/
accesstoken.go
134 lines (121 loc) · 3.18 KB
/
accesstoken.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
package wechat
import (
"fmt"
"log"
"time"
"github.com/esap/wechat/util"
)
// FetchDelay 默认5分钟同步一次
var FetchDelay time.Duration = 5 * time.Minute
// AccessToken 回复体
type AccessToken struct {
AccessToken string `json:"access_token"`
ExpiresIn int64 `json:"expires_in"`
WxErr
}
// GetAccessToken 读取AccessToken
func (s *Server) GetAccessToken() string {
s.Lock()
defer s.Unlock()
var err error
if s.accessToken == nil || s.accessToken.ExpiresIn < time.Now().Unix() {
for i := 0; i < 3; i++ {
err = s.getAccessToken()
if err == nil {
break
}
log.Printf("GetAccessToken[%v] %v", s.AgentId, err)
time.Sleep(time.Second)
}
if err != nil {
return ""
}
}
return s.accessToken.AccessToken
}
// GetUserAccessToken 获取企业微信通讯录AccessToken
func (s *Server) GetUserAccessToken() string {
if us, ok := UserServerMap[s.AppId]; ok {
return us.GetAccessToken()
}
return s.GetAccessToken()
}
func (s *Server) getAccessToken() (err error) {
if s.ExternalTokenHandler != nil {
s.accessToken = s.ExternalTokenHandler(s.AppId, s.AppName)
Printf("***%v[%v]远程获取token:%v", util.Substr(s.AppId, 14, 30), s.AgentId, s.accessToken)
return
}
url := fmt.Sprintf(s.TokenUrl, s.AppId, s.Secret)
at := new(AccessToken)
if err = util.GetJson(url, at); err != nil {
return
}
if at.ErrCode > 0 {
return at.Error()
}
at.ExpiresIn = time.Now().Unix() + at.ExpiresIn - 5
s.accessToken = at
Printf("***%v[%v]本地获取token:%v", util.Substr(s.AppId, 14, 30), s.AgentId, s.accessToken)
return
}
// Ticket JS-SDK
type Ticket struct {
Ticket string `json:"ticket"`
ExpiresIn int64 `json:"expires_in"`
WxErr
}
// GetTicket 读取获取Ticket
func (s *Server) GetTicket() string {
if s.ticket == nil || s.ticket.ExpiresIn < time.Now().Unix() {
for i := 0; i < 3; i++ {
err := s.getTicket()
if err != nil {
log.Printf("getTicket[%v] err:%v", s.AgentId, err)
time.Sleep(time.Second)
continue
}
break
}
}
return s.ticket.Ticket
}
func (s *Server) getTicket() (err error) {
url := s.JsApi + s.GetAccessToken()
at := new(Ticket)
if err = util.GetJson(url, at); err != nil {
return
}
if at.ErrCode > 0 {
return at.Error()
}
Printf("[%v::%v-JsApi] >>> %+v", s.AppId, s.AgentId, *at)
at.ExpiresIn = time.Now().Unix() + 500
s.ticket = at
return
}
// JsConfig Jssdk配置
type JsConfig struct {
Beta bool `json:"beta"`
Debug bool `json:"debug"`
AppId string `json:"appId"`
Timestamp int64 `json:"timestamp"`
Nonsestr string `json:"nonceStr"`
Signature string `json:"signature"`
JsApiList []string `json:"jsApiList"`
Url string `json:"jsurl"`
App int `json:"jsapp"`
}
// GetJsConfig 获取Jssdk配置
func (s *Server) GetJsConfig(Url string) *JsConfig {
jc := &JsConfig{Beta: true, Debug: Debug, AppId: s.AppId}
jc.Timestamp = time.Now().Unix()
jc.Nonsestr = "esap"
jc.Signature = util.SortSha1(fmt.Sprintf("jsapi_ticket=%v&noncestr=%v×tamp=%v&url=%v", s.GetTicket(), jc.Nonsestr, jc.Timestamp, Url))
// TODO:可加入其他apilist
jc.JsApiList = []string{"scanQRCode"}
jc.Url = Url
jc.App = s.AgentId
Println("jsconfig:", jc) // Debug
return jc
}