Skip to content

Commit 662f2bc

Browse files
bors[bot]Lyiw3xse7en
authored
Merge #49
49: feat: add jsApiTicket, jsApiTicketAgentConfig and refactor(token): accessToken r=xen0n a=w3xse7en Support jsApiTicket, jsApiTicketAgentConfig doc: https://open.work.weixin.qq.com/api/doc/90000/90136/90506 jsApiTicket, jsApiTicketAgentConfig similar with accessToken, they all needs cache in system so i reference the accesstoken design, refact token to support accessToken, jsApiTicket and jsApiTicketAgentConfig Co-authored-by: Lyi <zhanghong@zuodashi.com> Co-authored-by: mew <w3xse7en@outlook.com> Co-authored-by: w3xse7en <w3xse7en@outlook.com>
2 parents de4cf5f + f939964 commit 662f2bc

File tree

6 files changed

+234
-107
lines changed

6 files changed

+234
-107
lines changed

accesstoken.go

Lines changed: 0 additions & 80 deletions
This file was deleted.

apis.md.go

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client.go

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"mime/multipart"
88
"net/url"
99
"sync"
10-
"time"
1110
)
1211

1312
// Workwx 企业微信客户端
@@ -25,12 +24,10 @@ type WorkwxApp struct {
2524
// CorpSecret 应用的凭证密钥,必填
2625
CorpSecret string
2726
// AgentID 应用 ID,必填
28-
AgentID int64
29-
30-
tokenMu *sync.RWMutex
31-
accessToken string
32-
tokenExpiresIn time.Duration
33-
lastRefresh time.Time
27+
AgentID int64
28+
accessToken *token
29+
jsapiTicket *token
30+
jsapiTicketAgentConfig *token
3431
}
3532

3633
// New 构造一个 Workwx 客户端对象,需要提供企业 ID
@@ -50,22 +47,22 @@ func New(corpID string, opts ...CtorOption) *Workwx {
5047

5148
// WithApp 构造本企业下某自建 app 的客户端
5249
func (c *Workwx) WithApp(corpSecret string, agentID int64) *WorkwxApp {
53-
return &WorkwxApp{
50+
app := WorkwxApp{
5451
Workwx: c,
5552

5653
CorpSecret: corpSecret,
5754
AgentID: agentID,
5855

59-
tokenMu: &sync.RWMutex{},
60-
accessToken: "",
61-
lastRefresh: time.Time{},
56+
accessToken: &token{mutex: &sync.RWMutex{}},
57+
jsapiTicket: &token{mutex: &sync.RWMutex{}},
58+
jsapiTicketAgentConfig: &token{mutex: &sync.RWMutex{}},
6259
}
60+
app.accessToken.setGetTokenFunc(app.getAccessToken)
61+
app.jsapiTicket.setGetTokenFunc(app.getJSAPITicket)
62+
app.jsapiTicketAgentConfig.setGetTokenFunc(app.getJSAPITicketAgentConfig)
63+
return &app
6364
}
6465

65-
//
66-
// impl WorkwxApp
67-
//
68-
6966
func (c *WorkwxApp) composeQyapiURL(path string, req interface{}) *url.URL {
7067
values := url.Values{}
7168
if valuer, ok := req.(urlValuer); ok {
@@ -92,19 +89,8 @@ func (c *WorkwxApp) composeQyapiURLWithToken(path string, req interface{}, withA
9289
return url
9390
}
9491

95-
// intensive mutex juggling action
96-
c.tokenMu.RLock()
97-
if c.accessToken == "" {
98-
c.tokenMu.RUnlock() // RWMutex doesn't like recursive locking
99-
// TODO: what to do with the possible error?
100-
_ = c.syncAccessToken()
101-
c.tokenMu.RLock()
102-
}
103-
tokenToUse := c.accessToken
104-
c.tokenMu.RUnlock()
105-
10692
q := url.Query()
107-
q.Set("access_token", tokenToUse)
93+
q.Set("access_token", c.accessToken.getToken())
10894
url.RawQuery = q.Encode()
10995

11096
return url

docs/apis.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
Name|Request Type|Response Type|Access Token|URL|Doc
66
:---|------------|-------------|------------|:--|:--
77
`execGetAccessToken`|`reqAccessToken`|`respAccessToken`|-|`GET /cgi-bin/gettoken`|[获取access_token](https://work.weixin.qq.com/api/doc#90000/90135/91039)
8+
`execGetJSAPITicket`|`reqJSAPITicket`|`respJSAPITicket`|+|`GET /cgi-bin/get_jsapi_ticket`|[获取企业的jsapi_ticket](https://open.work.weixin.qq.com/api/doc/90000/90136/90506)
9+
`execGetJSAPITicketAgentConfig`|`reqJSAPITicketAgentConfig`|`respJSAPITicket`|+|`GET /cgi-bin/ticket/get`|[获取应用的jsapi_ticket](https://open.work.weixin.qq.com/api/doc/90000/90136/90506)
810

911
# 成员管理
1012

models.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,31 @@ type respAccessToken struct {
5555
ExpiresInSecs int64 `json:"expires_in"`
5656
}
5757

58+
type reqJSAPITicketAgentConfig struct{}
59+
60+
var _ urlValuer = reqJSAPITicketAgentConfig{}
61+
62+
func (x reqJSAPITicketAgentConfig) intoURLValues() url.Values {
63+
return url.Values{
64+
"type": {"agent_config"},
65+
}
66+
}
67+
68+
type reqJSAPITicket struct{}
69+
70+
var _ urlValuer = reqJSAPITicket{}
71+
72+
func (x reqJSAPITicket) intoURLValues() url.Values {
73+
return url.Values{}
74+
}
75+
76+
type respJSAPITicket struct {
77+
respCommon
78+
79+
Ticket string `json:"ticket"`
80+
ExpiresInSecs int64 `json:"expires_in"`
81+
}
82+
5883
// reqMessage 消息发送请求
5984
type reqMessage struct {
6085
ToUser []string

0 commit comments

Comments
 (0)