-
Notifications
You must be signed in to change notification settings - Fork 0
/
department_test.go
103 lines (83 loc) · 2 KB
/
department_test.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
package dingtalk_test
import (
"encoding/csv"
"encoding/json"
"fmt"
"os"
"testing"
"github.com/gozelle/dingtalk"
"github.com/gozelle/dingtalk/test"
"github.com/stretchr/testify/require"
)
func TestDepartment(t *testing.T) {
// 测试
client := test.NewTestClient()
subs, err := ListAllSub(client, nil)
require.NoError(t, err)
subs = append([]*dingtalk.Department{{DeptID: 1}}, subs...) // 加入根部门
var users []*dingtalk.DepartmentUser
for _, sub := range subs {
r, e := client.DepartmentClient().ListUsers(dingtalk.ListUsersParams{
DeptId: sub.DeptID,
Cursor: 0,
Size: 100,
})
require.NoError(t, e)
users = append(users, r.List...)
}
var _users []*dingtalk.DepartmentUser
check := map[string]bool{}
for _, v := range users {
if _, ok := check[v.UserID]; !ok {
_users = append(_users, v)
check[v.UserID] = true
}
}
d, err := json.MarshalIndent(_users, "", "\t")
require.NoError(t, err)
//t.Log(string(d))
f, err := os.Create("users.json")
require.NoError(t, err)
defer func() {
_ = f.Close()
}()
_, err = f.Write(d)
require.NoError(t, err)
_ = writeCSV(_users)
}
func writeCSV(users []*dingtalk.DepartmentUser) (err error) {
f, err := os.Create("users.csv") //创建文件
if err != nil {
panic(err)
}
defer f.Close()
f.WriteString("\xEF\xBB\xBF") // 写入UTF-8 BOM
w := csv.NewWriter(f) //创建一个新的写入文件流
data := [][]string{}
for i, v := range users {
data = append(data, []string{fmt.Sprintf("%d", i+1), v.Name, v.Avatar})
}
w.WriteAll(data) //写入数据
w.Flush()
return
}
func ListAllSub(client *dingtalk.Client, params *dingtalk.DepartmentListSubParams) (r []*dingtalk.Department, err error) {
list, err := client.DepartmentClient().ListSub(params)
if err != nil {
return
}
for _, v := range list {
r = append(r, v)
rr, e := ListAllSub(client, &dingtalk.DepartmentListSubParams{
DeptID: v.DeptID,
})
if e != nil {
err = e
return
}
if len(rr) > 0 {
r = append(r, rr...)
}
}
return
}