This repository has been archived by the owner on Jun 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
department.go
144 lines (131 loc) · 3.99 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
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
package wecom
//
// 企业微信部门管理接口
// 参考连接 https://open.work.weixin.qq.com/api/doc/90000/90135/90204
import (
"encoding/json"
"fmt"
"github.com/go-laoji/wecom-app-sdk/internal"
)
type Department struct {
Id int32 `json:"id"`
Order int `json:"order,omitempty"`
ParentId int32 `json:"parentid" validate:"required"`
Name string `json:"name" validate:"required,min=1,max=32"`
NameEn string `json:"name_en,omitempty" validate:"omitempty,min=1,max=32"`
DepartmentLeader []string `json:"department_leader"`
}
type DepartmentCreateResponse struct {
internal.BizResponse
Id int32 `json:"id"`
}
// DepartmentCreate 创建部门
func (app weCom) DepartmentCreate(department Department) (resp DepartmentCreateResponse) {
if ok := validate.Struct(department); ok != nil {
resp.ErrCode = 500
resp.ErrorMsg = ok.Error()
return
}
queryParams := app.buildBasicTokenQuery(app.getContactsAccessToken())
body, err := internal.HttpPost(fmt.Sprintf("/cgi-bin/department/create?%s", queryParams.Encode()), department)
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
} else {
json.Unmarshal(body, &resp)
}
return
}
// DepartmentUpdate 更新部门
func (app weCom) DepartmentUpdate(department Department) (resp internal.BizResponse) {
if ok := validate.Struct(department); ok != nil {
resp.ErrCode = 500
resp.ErrorMsg = ok.Error()
return
}
queryParams := app.buildBasicTokenQuery(app.getContactsAccessToken())
body, err := internal.HttpPost(fmt.Sprintf("/cgi-bin/department/update?%s", queryParams.Encode()), department)
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
} else {
json.Unmarshal(body, &resp)
}
return
}
// DepartmentDelete 删除部门
func (app weCom) DepartmentDelete(id int32) (resp internal.BizResponse) {
queryParams := app.buildBasicTokenQuery(app.getContactsAccessToken())
queryParams.Add("id", fmt.Sprintf("%v", id))
body, err := internal.HttpGet(fmt.Sprintf("/cgi-bin/department/delete?%s", queryParams.Encode()))
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
} else {
json.Unmarshal(body, &resp)
}
return
}
type DepartmentListResponse struct {
internal.BizResponse
Department []Department `json:"department"`
}
// DepartmentList 获取部门列表
func (app weCom) DepartmentList(id int32) (resp DepartmentListResponse) {
queryParams := app.buildBasicTokenQuery(app.getAppAccessToken())
queryParams.Add("id", fmt.Sprintf("%v", id))
body, err := internal.HttpGet(fmt.Sprintf("/cgi-bin/department/list?%s", queryParams.Encode()))
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
} else {
json.Unmarshal(body, &resp)
}
return
}
type DepartmentSimpleListResponse struct {
internal.BizResponse
DepartmentId []struct {
Id int32 `json:"id"`
ParentId int32 `json:"parentid"`
Order int `json:"order"`
}
}
// DepartmentSimpleList 获取子部门ID列表
func (app weCom) DepartmentSimpleList(id int32) (resp DepartmentSimpleListResponse) {
queryParams := app.buildBasicTokenQuery(app.getAppAccessToken())
if id > 0 {
queryParams.Add("id", fmt.Sprintf("%v", id))
}
body, err := internal.HttpGet(fmt.Sprintf("/cgi-bin/department/simplelist?%s", queryParams.Encode()))
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
} else {
json.Unmarshal(body, &resp)
}
return
}
type DepartmentGetResponse struct {
internal.BizResponse
Department Department `json:"department"`
}
// DepartmentGet 获取单个部门详情
func (app weCom) DepartmentGet(id int32) (resp DepartmentGetResponse) {
queryParams := app.buildBasicTokenQuery(app.getAppAccessToken())
if id > 0 {
queryParams.Add("id", fmt.Sprintf("%v", id))
} else {
resp.ErrCode = 500
resp.ErrorMsg = "部门ID异常"
return
}
body, err := internal.HttpGet(fmt.Sprintf("/cgi-bin/department/get?%s", queryParams.Encode()))
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
} else {
json.Unmarshal(body, &resp)
}
return
}