forked from heroicyang/wechat-qy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
department.go
118 lines (94 loc) · 2.49 KB
/
department.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
package api
import (
"encoding/json"
"net/url"
"strconv"
)
const (
createDepartmentURI = "https://qyapi.weixin.qq.com/cgi-bin/department/create"
updateDepartmentURI = "https://qyapi.weixin.qq.com/cgi-bin/department/update"
deleteDepartmentURI = "https://qyapi.weixin.qq.com/cgi-bin/department/delete"
listDepartmentURI = "https://qyapi.weixin.qq.com/cgi-bin/department/list"
)
// Department 表示部门信息
type Department struct {
ID int64 `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Order int64 `json:"order,omitempty"`
ParentID int64 `json:"parentid,omitempty"`
}
// CreateDepartment 方法用于创建部门
func (a *API) CreateDepartment(department *Department) error {
token, err := a.Tokener.Token()
if err != nil {
return err
}
qs := make(url.Values)
qs.Add("access_token", token)
url := createDepartmentURI + "?" + qs.Encode()
data, err := json.Marshal(department)
if err != nil {
return err
}
body, err := a.Client.PostJSON(url, data)
if err != nil {
return err
}
d := &Department{}
if err := json.Unmarshal(body, d); err != nil {
return err
}
department.ID = d.ID
return nil
}
// UpdateDepartment 方法用于更新部门信息
func (a *API) UpdateDepartment(department *Department) error {
token, err := a.Tokener.Token()
if err != nil {
return err
}
qs := make(url.Values)
qs.Add("access_token", token)
url := updateDepartmentURI + "?" + qs.Encode()
data, err := json.Marshal(department)
if err != nil {
return err
}
_, err = a.Client.PostJSON(url, data)
return err
}
// DeleteDepartment 方法用于删除部门
func (a *API) DeleteDepartment(id int64) error {
token, err := a.Tokener.Token()
if err != nil {
return err
}
qs := make(url.Values)
qs.Add("access_token", token)
qs.Add("id", strconv.FormatInt(id, 10))
url := deleteDepartmentURI + "?" + qs.Encode()
_, err = a.Client.GetJSON(url)
return err
}
// ListDepartment 方法用于获取部门列表,获取根部门时 id 为 1
func (a *API) ListDepartment(id int64) ([]*Department, error) {
token, err := a.Tokener.Token()
if err != nil {
return nil, err
}
qs := make(url.Values)
qs.Add("access_token", token)
qs.Add("id", strconv.FormatInt(id, 10))
url := listDepartmentURI + "?" + qs.Encode()
body, err := a.Client.GetJSON(url)
if err != nil {
return nil, err
}
result := &struct {
Departments []*Department `json:"department"`
}{}
if err := json.Unmarshal(body, result); err != nil {
return nil, err
}
return result.Departments, nil
}