-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathuser.go
320 lines (295 loc) · 9.68 KB
/
user.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package wework
import (
"encoding/json"
"fmt"
"github.com/go-laoji/wecom-go-sdk/v2/internal"
"strings"
)
type User struct {
OpenUserId string `json:"open_userid,omitempty"` // 仅在查询时返回
Userid string `json:"userid" validate:"required"`
Name string `json:"name,omitempty" validate:"required"`
Alias string `json:"alias,omitempty"`
Mobile string `json:"mobile,omitempty" validate:"required_without=Email,omitempty"`
Department []int32 `json:"department,omitempty" validate:"required,max=100"`
Order []int32 `json:"order,omitempty"`
Position string `json:"position,omitempty"`
Gender string `json:"gender,omitempty" validate:"omitempty,oneof=1 2"`
Email string `json:"email,omitempty" validate:"required_without=Mobile,omitempty,email"`
BizEmail string `json:"biz_email,omitempty"`
IsLeaderInDept []int `json:"is_leader_in_dept,omitempty"`
DirectLeader []string `json:"direct_leader,omitempty"`
Enable json.Number `json:"enable,omitempty"`
Avatar string `json:"avatar,omitempty"`
ThumbAvatar string `json:"thumb_avatar,omitempty"`
Telephone string `json:"telephone,omitempty"`
Address string `json:"address,omitempty"`
MainDepartment int32 `json:"main_department,omitempty"`
Status int `json:"status,omitempty"`
QrCode string `json:"qr_code,omitempty"`
Extattr *struct {
Attrs []Attrs `json:"attrs,omitempty"`
} `json:"extattr,omitempty"`
ToInvite bool `json:"to_invite,omitempty"`
ExternalPosition string `json:"external_position,omitempty"`
ExternalProfile *struct {
ExternalCorpName string `json:"external_corp_name,omitempty"`
WechatChannels struct {
Nickname string `json:"nickname,omitempty"`
} `json:"wechat_channels,omitempty"`
ExternalAttr []ExternalAttr `json:"external_attr,omitempty"`
} `json:"external_profile,omitempty"`
}
type Attrs struct {
Type int `json:"type" validate:"required,oneof= 0 1 2"`
Name string `json:"name" validate:"required"`
Text struct {
Value string `json:"value"`
} `json:"text,omitempty"`
Web struct {
URL string `json:"url" validate:"required"`
Title string `json:"title" validate:"required"`
} `json:"web,omitempty"`
}
type ExternalAttr struct {
Type int `json:"type"`
Name string `json:"name"`
Text struct {
Value string `json:"value"`
} `json:"text,omitempty"`
Web struct {
URL string `json:"url"`
Title string `json:"title"`
} `json:"web,omitempty"`
Miniprogram struct {
Appid string `json:"appid"`
Pagepath string `json:"pagepath"`
Title string `json:"title"`
} `json:"miniprogram,omitempty"`
}
// UserCreate 创建成员
func (ww *weWork) UserCreate(corpId uint, user User) (resp internal.BizResponse) {
if ok := validate.Struct(user); ok != nil {
resp.ErrCode = 500
resp.ErrorMsg = ok.Error()
return
}
_, err := ww.getRequest(corpId).SetBody(user).SetResult(&resp).
Post("/cgi-bin/user/create")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
type UserGetResponse struct {
internal.BizResponse
User
}
// UserGet 读取成员
func (ww *weWork) UserGet(corpId uint, userId string) (resp UserGetResponse) {
_, err := ww.getRequest(corpId).SetQueryParam("userid", userId).SetResult(&resp).
Get("/cgi-bin/user/get")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
// UserUpdate 更新成员
func (ww *weWork) UserUpdate(corpId uint, user User) (resp internal.BizResponse) {
if strings.TrimSpace(user.Userid) == "" {
resp.ErrCode = 500
resp.ErrorMsg = "userid can not empty"
return
}
_, err := ww.getRequest(corpId).SetBody(user).SetResult(&resp).
Post("/cgi-bin/user/update")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
// UserDelete 删除成员
func (ww *weWork) UserDelete(corpId uint, userId string) (resp internal.BizResponse) {
_, err := ww.getRequest(corpId).SetQueryParam("userid", userId).SetResult(&resp).
Get("/cgi-bin/user/delete")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
type UserSimpleListResponse struct {
internal.Error
UserList []struct {
UserId string `json:"userid"`
Name string `json:"name"`
Department []int `json:"department"`
OpenUserId string `json:"open_userid"`
} `json:"userlist"`
}
// UserSimpleList 获取部门成员
// https://open.work.weixin.qq.com/api/doc/90001/90143/90332
func (ww *weWork) UserSimpleList(corpId uint, departId int32, fetchChild int) (resp UserSimpleListResponse) {
if departId <= 0 {
return UserSimpleListResponse{internal.Error{ErrorMsg: "部门ID必需大于0", ErrCode: 403}, nil}
}
_, err := ww.getRequest(corpId).
SetQueryParam("department_id", fmt.Sprintf("%v", departId)).
SetQueryParam("fetch_child", fmt.Sprintf("%v", fetchChild)).
SetResult(&resp).
Get("/cgi-bin/user/simplelist")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
type UserListResponse struct {
internal.BizResponse
UserList []User `json:"userlist"`
}
// UserList 获取部门成员详情
// https://open.work.weixin.qq.com/api/doc/90001/90143/90337
func (ww *weWork) UserList(corpId uint, departId int32, fetchChild int) (resp UserListResponse) {
if departId <= 0 {
resp.ErrCode = 403
resp.ErrorMsg = "部门ID必需大于0"
return
}
_, err := ww.getRequest(corpId).
SetQueryParam("department_id", fmt.Sprintf("%v", departId)).
SetQueryParam("fetch_child", fmt.Sprintf("%v", fetchChild)).
SetResult(&resp).
Get("/cgi-bin/user/list")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
type UserId2OpenIdResponse struct {
internal.BizResponse
OpenId string `json:"openid"`
}
// UserId2OpenId userid与openid互换
// https://open.work.weixin.qq.com/api/doc/90001/90143/90338
func (ww *weWork) UserId2OpenId(corpId uint, userId string) (resp UserId2OpenIdResponse) {
p := H{"userid": userId}
_, err := ww.getRequest(corpId).SetBody(p).SetResult(&resp).
Post("/cgi-bin/user/convert_to_openid")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
type OpenId2UserIdResponse struct {
internal.BizResponse
UserId string `json:"userid"`
}
// OpenId2UserId openid转userid
// https://open.work.weixin.qq.com/api/doc/90001/90143/90338
func (ww *weWork) OpenId2UserId(corpId uint, openId string) (resp OpenId2UserIdResponse) {
p := H{"openid": openId}
_, err := ww.getRequest(corpId).SetBody(p).SetResult(&resp).
Post("/cgi-bin/user/convert_to_userid")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
type ListMemberAuthResponse struct {
internal.BizResponse
NextCursor string `json:"next_cursor"`
MemberAuthList []struct {
OpenUserId string `json:"open_userid"`
} `json:"member_auth_list"`
}
// ListMemberAuth 获取成员授权列表
// https://open.work.weixin.qq.com/api/doc/90001/90143/94513
func (ww *weWork) ListMemberAuth(corpId uint, cursor string, limit int) (resp ListMemberAuthResponse) {
p := H{"cursor": cursor, "limit": limit}
_, err := ww.getRequest(corpId).SetBody(p).SetResult(&resp).
Post("/cgi-bin/user/list_member_auth")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
type CheckMemberAuthResponse struct {
internal.BizResponse
IsMemberAuth bool `json:"is_member_auth"`
}
// CheckMemberAuth 查询成员用户是否已授权
// https://open.work.weixin.qq.com/api/doc/90001/90143/94514
func (ww *weWork) CheckMemberAuth(corpId uint, openUserId string) (resp CheckMemberAuthResponse) {
p := H{"open_userid": openUserId}
_, err := ww.getRequest(corpId).SetBody(p).SetResult(&resp).
Post("/cgi-bin/user/check_member_auth")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
type GetUserIdResponse struct {
internal.BizResponse
UserId string `json:"userid"`
}
// GetUserId 手机号获取userid
// https://open.work.weixin.qq.com/api/doc/90001/90143/91693
func (ww *weWork) GetUserId(corpId uint, mobile string) (resp GetUserIdResponse) {
p := H{"mobile": mobile}
_, err := ww.getRequest(corpId).SetBody(p).SetResult(&resp).
Post("/cgi-bin/user/getuserid")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
type ListSelectedTicketUserResponse struct {
internal.BizResponse
OperatorOpenUserId string `json:"operator_open_userid"`
OpenUserIdList []string `json:"open_userid_list"`
UnAuthOpenUserIdList []string `json:"unauth_open_userid_list"`
Total int `json:"total"`
}
// ListSelectedTicketUser 获取选人ticket对应的用户
// https://open.work.weixin.qq.com/api/doc/90001/90143/94894
func (ww *weWork) ListSelectedTicketUser(corpId uint, ticket string) (resp ListSelectedTicketUserResponse) {
p := H{"selected_ticket": ticket}
_, err := ww.getRequest(corpId).SetBody(p).SetResult(&resp).
Post("/cgi-bin/user/list_selected_ticket_user")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
type UserListIdResponse struct {
internal.BizResponse
NextCursor string `json:"next_cursor"`
DeptUser []struct {
UserId string `json:"userid"`
OpenUserId string `json:"open_userid"`
Department int `json:"department"`
} `json:"dept_user"`
}
// UserListId 获取成员ID列表 仅支持通过“通讯录同步secret”调用。
// https://developer.work.weixin.qq.com/document/40856
func (ww *weWork) UserListId(corpId uint, cursor string, limit int) (resp UserListIdResponse) {
p := H{"cursor": cursor, "limit": limit}
_, err := ww.getRequest(corpId).SetBody(p).SetResult(&resp).
Post("/cgi-bin/user/list_id")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}