-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsuite.go
475 lines (441 loc) · 14.1 KB
/
suite.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package wework
import (
"fmt"
"net/url"
"os"
"time"
"github.com/dgraph-io/badger/v3"
"github.com/go-laoji/wecom-go-sdk/v2/internal"
"github.com/go-laoji/wecom-go-sdk/v2/pkg/svr/models"
)
// UpdateSuiteTicket 用于接收回调后更新sdk实例的suite ticket
func (ww *weWork) UpdateSuiteTicket(ticket string) {
ww.suiteTicket = ticket
ww.cache.Update(func(txn *badger.Txn) error {
entry := badger.NewEntry([]byte("suiteTicket"), []byte(ticket)).WithTTL(time.Second * 600)
err := txn.SetEntry(entry)
return err
})
ww.requestSuiteToken()
}
type suiteTokenResponse struct {
internal.BizResponse
SuiteAccessToken string `json:"suite_access_token"`
ExpiresIn int `json:"expires_in"`
}
func (ww *weWork) requestSuiteToken() (resp suiteTokenResponse) {
if ww.suiteTicket == "" {
resp.ErrCode = 400
resp.ErrorMsg = "suite ticket 未推送"
logger.Sugar().Error("suite ticket 未推送")
return
}
apiUrl := "/cgi-bin/service/get_suite_token"
h := H{}
h["suite_id"] = ww.suiteId
h["suite_secret"] = ww.suiteSecret
h["suite_ticket"] = ww.suiteTicket
_, err := ww.httpClient.R().SetBody(h).SetResult(&resp).Post(apiUrl)
if err != nil {
resp.ErrCode = 400
resp.ErrorMsg = err.Error()
logger.Sugar().Error(err)
return
}
return
}
func (ww *weWork) getSuiteAccessToken() (token string) {
var err error
var item *badger.Item
err = ww.cache.View(func(txn *badger.Txn) error {
item, err = txn.Get([]byte("suiteToken"))
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.requestSuiteToken(); resp.ErrCode != 0 {
logger.Sugar().Error(resp.ErrorMsg)
return ""
} else {
token = resp.SuiteAccessToken
ww.cache.Update(func(txn *badger.Txn) error {
entry := badger.NewEntry([]byte("suiteToken"), []byte(token)).
WithTTL(time.Second * time.Duration(resp.ExpiresIn))
err = txn.SetEntry(entry)
return err
})
}
}
return token
}
type GetPreAuthCodeResponse struct {
internal.BizResponse
PreAuthCode string `json:"pre_auth_code"`
ExpiresIn int `json:"expires_in"`
}
// GetPreAuthCode 获取预授权码
// https://open.work.weixin.qq.com/api/doc/90001/90143/90601
func (ww *weWork) GetPreAuthCode() (resp GetPreAuthCodeResponse) {
_, err := ww.httpClient.R().SetQueryParam("suite_access_token", ww.getSuiteAccessToken()).
SetResult(&resp).Get("/cgi-bin/service/get_pre_auth_code")
if err != nil {
logger.Sugar().Error(err)
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
return
}
return
}
type DealerCorpInfo struct {
Corpid string `json:"corpid"`
CorpName string `json:"corp_name"`
}
type AuthCorpInfo struct {
CorpId string `json:"corpid"`
CorpName string `json:"corp_name"`
CorpType string `json:"corp_type"`
CorpSquareLogoURL string `json:"corp_square_logo_url"`
CorpUserMax int `json:"corp_user_max"`
CorpAgentMax int `json:"corp_agent_max"`
CorpFullName string `json:"corp_full_name"`
VerifiedEndTime int `json:"verified_end_time"`
SubjectType int `json:"subject_type"`
CorpWxqrcode string `json:"corp_wxqrcode"`
CorpScale string `json:"corp_scale"`
CorpIndustry string `json:"corp_industry"`
CorpSubIndustry string `json:"corp_sub_industry"`
}
type AuthUserInfo struct {
UserId string `json:"userid"`
OpenUserId string `json:"open_userid"`
Name string `json:"name"`
Avatar string `json:"avatar"`
}
type RegisterCodeInfo struct {
RegisterCode string `json:"register_code"`
TemplateID string `json:"template_id"`
State string `json:"state"`
}
type Agent struct {
AgentId int `json:"agentid"`
Name string `json:"name"`
RoundLogoURL string `json:"round_logo_url"`
SquareLogoURL string `json:"square_logo_url"`
Appid int `json:"appid"`
AuthMode int `json:"auth_mode,omitempty"`
IsCustomizedApp bool `json:"is_customized_app,omitempty"`
Privilege struct {
Level int `json:"level"`
AllowParty []int `json:"allow_party"`
AllowUser []string `json:"allow_user"`
AllowTag []int `json:"allow_tag"`
ExtraParty []int `json:"extra_party"`
ExtraUser []string `json:"extra_user"`
ExtraTag []int `json:"extra_tag"`
} `json:"privilege,omitempty"`
SharedFrom struct {
Corpid string `json:"corpid"`
} `json:"shared_from"`
}
type GetPermanentCodeResponse struct {
internal.BizResponse
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
PermanentCode string `json:"permanent_code"`
DealerCorpInfo DealerCorpInfo `json:"dealer_corp_info"`
AuthCorpInfo AuthCorpInfo `json:"auth_corp_info"`
AuthInfo struct {
Agent []Agent `json:"agent"`
} `json:"auth_info"`
AuthUserInfo AuthUserInfo `json:"auth_user_info"`
RegisterCodeInfo RegisterCodeInfo `json:"register_code_info"`
State string `json:"state"`
}
// GetPermanentCode 获取企业永久授权码
// https://open.work.weixin.qq.com/api/doc/90001/90143/90603
func (ww *weWork) GetPermanentCode(authCode string) (resp GetPermanentCodeResponse) {
h := H{}
h["auth_code"] = authCode
_, err := ww.httpClient.R().SetQueryParam("suite_access_token", ww.getSuiteAccessToken()).
SetBody(h).SetResult(&resp).Post("/cgi-bin/service/get_permanent_code")
if err != nil {
resp.ErrCode = 400
resp.ErrorMsg = err.Error()
logger.Sugar().Error(err)
}
return
}
type GetAuthInfoResponse struct {
internal.BizResponse
DealerCorpInfo DealerCorpInfo `json:"dealer_corp_info"`
AuthCorpInfo AuthCorpInfo `json:"auth_corp_info"`
AuthInfo struct {
Agent []Agent `json:"agent"`
} `json:"auth_info"`
}
// GetAuthInfo 获取企业授权信息
// https://open.work.weixin.qq.com/api/doc/90001/90143/90604
func (ww *weWork) GetAuthInfo(authCorpId, permanentCode string) (resp GetAuthInfoResponse) {
h := H{}
h["auth_corpid"] = authCorpId
h["permanent_code"] = permanentCode
_, err := ww.httpClient.R().SetQueryParam("suite_access_token", ww.getSuiteAccessToken()).
SetBody(h).SetResult(&resp).Post("/cgi-bin/service/get_auth_info")
if err != nil {
resp.ErrCode = 400
resp.ErrorMsg = err.Error()
logger.Sugar().Error(err)
}
return
}
type getCorpTokenResponse struct {
internal.BizResponse
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
}
// 默认从数据库获取应用secret配置信息
// 同一corpid(企业微信主体ID号)可以配置多个应用
func (ww *weWork) defaultAppSecretFunc(corpId uint) (corpid string, secret string, customizedApp bool) {
var authAgent models.Agent
ww.engine.Model(models.Agent{}).
Where(models.Agent{CorpId: corpId}).
First(&authAgent)
return authAgent.AuthCorpId, authAgent.PermanentCode, authAgent.IsCustomizedApp
}
// 默认从数据库获取应用的agentid
func (ww *weWork) defaultAgentIdFunc(corpId uint) (appId int) {
var authAgent models.Agent
ww.engine.Model(models.Agent{}).
Where(models.Agent{CorpId: corpId}).
First(&authAgent)
return authAgent.AgentId
}
// 从数据库查询永久授权码和授权企业的企业微信id,获取对应的access token
func (ww *weWork) requestCorpToken(corpId uint) (resp getCorpTokenResponse) {
var err error
var corpid, secret string
var customizedApp bool
if ww.getAppSecretFunc != nil {
corpid, secret, customizedApp = ww.getAppSecretFunc(corpId)
} else {
corpid, secret, customizedApp = ww.defaultAppSecretFunc(corpId)
}
// 兼容代开发应用/自建应用/三方应用的token获取
if !customizedApp {
h := H{}
h["auth_corpid"] = corpid
h["permanent_code"] = secret
_, err = ww.httpClient.R().SetQueryParam("suite_access_token", ww.getSuiteAccessToken()).
SetBody(h).SetResult(&resp).Post("/cgi-bin/service/get_corp_token")
} else {
_, err = ww.httpClient.R().SetQueryParam("corpid", corpid).
SetQueryParam("corpsecret", secret).
SetResult(&resp).Get("/cgi-bin/gettoken")
}
if err != nil {
logger.Sugar().Error(err)
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
func (ww *weWork) SetAppSecretFunc(f func(corpId uint) (corpid string, secret string, customizedApp bool)) {
ww.getAppSecretFunc = f
}
func (ww *weWork) SetAgentIdFunc(f func(corpId uint) (agentId int)) {
ww.getAgentIdFunc = f
}
func (ww *weWork) getCorpToken(corpId uint) (token string) {
var err error
var item *badger.Item
err = ww.cache.View(func(txn *badger.Txn) error {
item, err = txn.Get([]byte(fmt.Sprintf("corpToken-%v", corpId)))
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.requestCorpToken(corpId); resp.ErrCode != 0 {
logger.Sugar().Error(resp.ErrorMsg)
return ""
} else {
token = resp.AccessToken
ww.cache.Update(func(txn *badger.Txn) error {
entry := badger.NewEntry([]byte(fmt.Sprintf("corpToken-%v", corpId)), []byte(token)).
WithTTL(time.Second * time.Duration(resp.ExpiresIn))
err = txn.SetEntry(entry)
return err
})
}
}
return token
}
type GetUserInfo3rdResponse struct {
internal.BizResponse
CorpId string `json:"CorpId"`
UserId string `json:"UserId"`
DeviceId string `json:"DeviceId"`
UserTicket string `json:"user_ticket"`
ExpiresIn int `json:"expires_in"`
OpenUserId string `json:"open_userid"`
}
// GetUserInfo3rd 获取访问用户身份
// https://open.work.weixin.qq.com/api/doc/90001/90143/91121
func (ww *weWork) GetUserInfo3rd(code string) (resp GetUserInfo3rdResponse) {
_, err := ww.httpClient.R().
SetQueryParam("suite_access_token", ww.getSuiteAccessToken()).
SetQueryParam("code", code).
SetResult(&resp).Get("/cgi-bin/service/auth/getuserinfo3rd")
if err != nil {
logger.Sugar().Error(err)
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
type GetUserInfoDetail3rdResponse struct {
internal.BizResponse
CorpId string `json:"corpid"`
UserId string `json:"userid"`
Name string `json:"name"`
Gender string `json:"gender"`
Avatar string `json:"avatar"`
QrCode string `json:"qr_code"`
}
// GetUserInfoDetail3rd 获取访问用户敏感信息
// https://open.work.weixin.qq.com/api/doc/90001/90143/91122
func (ww *weWork) GetUserInfoDetail3rd(userTicket string) (resp GetUserInfoDetail3rdResponse) {
h := H{}
h["user_ticket"] = userTicket
_, err := ww.httpClient.R().
SetQueryParam("suite_access_token", ww.getSuiteAccessToken()).
SetBody(h).
SetResult(&resp).Post("/cgi-bin/service/auth/getuserdetail3rd")
if err != nil {
logger.Sugar().Error(err)
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
type GetUserInfoResponse struct {
internal.BizResponse
UserId string `json:"UserId,omitempty"`
DeviceId string `json:"DeviceId,omitempty"`
UserTicket string `json:"user_ticket,omitempty"`
OpenId string `json:"OpenId,omitempty"`
ExternalUserId string `json:"external_userid,omitempty"`
}
// GetUserInfo
// https://developer.work.weixin.qq.com/document/path/91023
func (ww *weWork) GetUserInfo(corpId uint, code string) (resp GetUserInfoResponse) {
_, err := ww.getRequest(corpId).SetQueryParam("code", code).
SetResult(&resp).Get("/cgi-bin/auth/getuserinfo")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
type GetUserDetailResponse struct {
internal.BizResponse
Userid string `json:"userid"`
Gender string `json:"gender"`
Avatar string `json:"avatar"`
QrCode string `json:"qr_code"`
Mobile string `json:"mobile"`
Email string `json:"email"`
BizMail string `json:"biz_mail"`
Address string `json:"address"`
}
func (ww *weWork) GetUserDetail(corpId uint, userTicket string) (resp GetUserDetailResponse) {
p := H{"user_ticket": userTicket}
_, err := ww.getRequest(corpId).SetBody(p).
SetResult(&resp).Post("/cgi-bin/auth/getuserdetail")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
type GetAppQrCodeRequest struct {
SuiteID string `json:"suite_id"`
Appid int `json:"appid,omitempty"`
State string `json:"state,omitempty"`
Style int `json:"style,omitempty" validate:"omitempty,oneof=0 1 2 3 4"`
ResultType int `json:"result_type" validate:"required,oneof=2"`
}
type GetAppQrCodeResponse struct {
internal.BizResponse
QrCode string `json:"qrcode"`
}
// GetAppQrCode 获取应用二维码 仅支持二维码地址返回
// https://developer.work.weixin.qq.com/document/path/95430#36592
func (ww *weWork) GetAppQrCode(request GetAppQrCodeRequest) (resp GetAppQrCodeResponse) {
if ok := validate.Struct(request); ok != nil {
resp.ErrCode = 500
resp.ErrorMsg = ok.Error()
return
}
_, err := ww.httpClient.R().SetQueryParam("suite_access_token", ww.GetSuiteToken()).
SetBody(request).SetResult(&resp).
Post("/cgi-bin/service/get_app_qrcode")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
type GetAdminListRequest struct {
AuthCorpId string `json:"auth_corpid" validate:"required"`
AgentId uint `json:"agentid" validate:"required"`
}
type GetAdminListResponse struct {
internal.BizResponse
Admin []struct {
Userid string `json:"userid"`
OpenUserid string `json:"open_userid"`
AuthType int `json:"auth_type"`
} `json:"admin"`
}
func (ww *weWork) GetAdminList(request GetAdminListRequest) (resp GetAdminListResponse) {
if ok := validate.Struct(request); ok != nil {
resp.ErrCode = 500
resp.ErrorMsg = ok.Error()
return
}
_, err := ww.httpClient.R().SetQueryParam("suite_access_token", ww.getSuiteAccessToken()).
SetBody(request).SetResult(&resp).
Post("/cgi-bin/service/get_admin_list")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
// ExecuteCorpApi
// apiUrl 需要带有 /cgi-bin
// GET请求时data传入nil即可
// Deprecated:
func (ww *weWork) ExecuteCorpApi(corpId uint, apiUrl string, query url.Values, data H) (body []byte, err error) {
query.Add("access_token", ww.getCorpToken(corpId))
if os.Getenv("debug") != "" {
query.Add("debug", "1")
}
if len(data) != 0 {
return internal.HttpPost(fmt.Sprintf("%s?%s", apiUrl, query.Encode()), data)
} else {
return internal.HttpGet(fmt.Sprintf("%s?%s", apiUrl, query.Encode()))
}
}