-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
87 lines (79 loc) · 1.78 KB
/
main.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
package main
import (
"fmt"
"os"
"strconv"
"github.com/wenerme/go-wecom/wecom"
)
const (
CorpID = "xxxxxx"
AgentID = 1000003
CorpSecret = "xxxxxxxxxxxxx"
)
func WeComClient() *wecom.Client {
// token store - 默认内存 Map - 可以使用数据库实现
store := &wecom.SyncMapStore{}
// 加载缓存 - 复用之前的 Token
if bytes, err := os.ReadFile("wecom-cache.json"); err == nil {
_ = store.Restore(bytes)
}
// 当 Token 变化时生成缓存文件
store.OnChange = func(s *wecom.SyncMapStore) {
_ = os.WriteFile("wecom-cache.json", s.Dump(), 0o600)
}
client := wecom.NewClient(wecom.Conf{
CorpID: CorpID,
AgentID: AgentID,
CorpSecret: CorpSecret,
// 不配置默认使用 内存缓存
TokenProvider: &wecom.TokenCache{
Store: store,
},
})
return client
}
func main() {
groups, err := GetDepts()
if err != nil {
fmt.Printf("get all group failed: %v\n", err)
}
for _, group := range groups {
fmt.Println("分组信息:", group)
}
users, err := GetUsers()
if err != nil {
fmt.Printf("get all user failed: %v\n", err)
}
for _, user := range users {
fmt.Println("用户信息:", user.Department)
}
}
func GetDepts() ([]wecom.ListDepartmentResponseItem, error) {
depts, err := WeComClient().ListDepartment(
&wecom.ListDepartmentRequest{},
)
if err != nil {
return nil, err
}
return depts.Department, nil
}
func GetUsers() ([]wecom.ListUserResponseItem, error) {
depts, err := GetDepts()
if err != nil {
return nil, err
}
var us []wecom.ListUserResponseItem
for _, dept := range depts {
users, err := WeComClient().ListUser(
&wecom.ListUserRequest{
DepartmentID: strconv.Itoa(dept.ID),
FetchChild: "1",
},
)
if err != nil {
return nil, err
}
us = append(us, users.UserList...)
}
return us, nil
}