forked from chanxuehong/wechat
-
Notifications
You must be signed in to change notification settings - Fork 3
/
department.go
119 lines (99 loc) · 3.44 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
119
// @description wechat 是腾讯微信公众平台 api 的 golang 语言封装
// @link https://gopkg.in/chanxuehong/wechat.v1 for the canonical source repository
// @license https://github.com/chanxuehong/wechat/blob/v1/LICENSE
// @authors chanxuehong(chanxuehong@gmail.com)
package addresslist
import (
"errors"
"strconv"
"gopkg.in/chanxuehong/wechat.v1/corp"
)
// 创建部门参数
type DepartmentCreateParameters struct {
DepartmentName string `json:"name,omitempty"` // 必须, 部门名称. 长度限制为1~64个字符
ParentId int64 `json:"parentid"` // 必须, 父亲部门id. 根部门id为1
Order *int `json:"order,omitempty"` // 可选, 在父部门中的次序. 从1开始, 数字越大排序越靠后
DepartmentId *int64 `json:"id,omitempty"` // 可选, 部门ID. 用指定部门ID新建部门, 不指定此参数时, 则自动生成
}
// 创建部门
func (clt *Client) DepartmentCreate(para *DepartmentCreateParameters) (id int64, err error) {
if para == nil {
err = errors.New("nil parameters")
return
}
var result struct {
corp.Error
Id int64 `json:"id"`
}
incompleteURL := "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token="
if err = ((*corp.Client)(clt)).PostJSON(incompleteURL, para, &result); err != nil {
return
}
if result.ErrCode != corp.ErrCodeOK {
err = &result.Error
return
}
id = result.Id
return
}
// 更新部门参数
type DepartmentUpdateParameters struct {
DepartmentId int64 `json:"id"` // 必须, 部门id
DepartmentName string `json:"name,omitempty"` // 可选, 更新的部门名称. 长度限制为1~64个字符. 修改部门名称时指定该参数
ParentId *int64 `json:"parentid,omitempty"` // 可选, 父亲部门id. 根部门id为1
Order *int `json:"order,omitempty"` // 可选, 在父部门中的次序. 从1开始, 数字越大排序越靠后, 当数字大于该层部门数时表示移动到最末尾.
}
// 更新部门
func (clt *Client) DepartmentUpdate(para *DepartmentUpdateParameters) (err error) {
if para == nil {
err = errors.New("nil parameters")
return
}
var result corp.Error
incompleteURL := "https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token="
if err = ((*corp.Client)(clt)).PostJSON(incompleteURL, para, &result); err != nil {
return
}
if result.ErrCode != corp.ErrCodeOK {
err = &result
return
}
return
}
// 删除部门
func (clt *Client) DepartmentDelete(id int64) (err error) {
var result corp.Error
incompleteURL := "https://qyapi.weixin.qq.com/cgi-bin/department/delete?id=" +
strconv.FormatInt(id, 10) + "&access_token="
if err = ((*corp.Client)(clt)).GetJSON(incompleteURL, &result); err != nil {
return
}
if result.ErrCode != corp.ErrCodeOK {
err = &result
return
}
return
}
type Department struct {
Id int64 `json:"id"`
Name string `json:"name"`
ParentId int64 `json:"parentid"`
}
// 获取 rootId 部门的子部门
func (clt *Client) DepartmentList(rootId int64) (departments []Department, err error) {
var result struct {
corp.Error
Departments []Department `json:"department"`
}
incompleteURL := "https://qyapi.weixin.qq.com/cgi-bin/department/list?id=" +
strconv.FormatInt(rootId, 10) + "&access_token="
if err = ((*corp.Client)(clt)).GetJSON(incompleteURL, &result); err != nil {
return
}
if result.ErrCode != corp.ErrCodeOK {
err = &result.Error
return
}
departments = result.Departments
return
}