1
|
|
|
# -*- coding: utf-8 -*- |
2
|
10 |
|
from __future__ import absolute_import, unicode_literals |
3
|
|
|
|
4
|
10 |
|
from optionaldict import optionaldict |
5
|
|
|
|
6
|
10 |
|
from wechatpy.client.api.base import BaseWeChatAPI |
7
|
|
|
|
8
|
|
|
|
9
|
10 |
|
class WeChatUser(BaseWeChatAPI): |
10
|
|
|
""" |
11
|
|
|
成员管理 |
12
|
|
|
|
13
|
|
|
https://work.weixin.qq.com/api/doc#90000/90135/90194 |
14
|
|
|
|
15
|
|
|
邀请成员接口位于 `WeChatBatch.invite` |
16
|
|
|
""" |
17
|
|
|
|
18
|
10 |
View Code Duplication |
def create(self, user_id, name, department=None, position=None, |
|
|
|
|
19
|
|
|
mobile=None, gender=0, tel=None, email=None, |
20
|
|
|
weixin_id=None, extattr=None, **kwargs): |
21
|
|
|
""" |
22
|
|
|
创建成员 |
23
|
|
|
|
24
|
|
|
https://work.weixin.qq.com/api/doc#90000/90135/90195 |
25
|
|
|
""" |
26
|
|
|
user_data = optionaldict() |
27
|
|
|
user_data['userid'] = user_id |
28
|
|
|
user_data['name'] = name |
29
|
|
|
user_data['gender'] = gender |
30
|
|
|
user_data['department'] = department |
31
|
|
|
user_data['position'] = position |
32
|
|
|
user_data['mobile'] = mobile |
33
|
|
|
user_data['tel'] = tel |
34
|
|
|
user_data['email'] = email |
35
|
|
|
user_data['weixinid'] = weixin_id |
36
|
|
|
user_data['extattr'] = extattr |
37
|
|
|
user_data.update(kwargs) |
38
|
|
|
|
39
|
|
|
return self._post( |
40
|
|
|
'user/create', |
41
|
|
|
data=user_data |
42
|
|
|
) |
43
|
|
|
|
44
|
10 |
|
def get(self, user_id): |
45
|
|
|
""" |
46
|
|
|
读取成员 |
47
|
|
|
|
48
|
|
|
https://work.weixin.qq.com/api/doc#90000/90135/90196 |
49
|
|
|
""" |
50
|
|
|
return self._get( |
51
|
|
|
'user/get', |
52
|
|
|
params={ |
53
|
|
|
'userid': user_id |
54
|
|
|
} |
55
|
|
|
) |
56
|
|
|
|
57
|
10 |
View Code Duplication |
def update(self, user_id, name=None, department=None, position=None, |
|
|
|
|
58
|
|
|
mobile=None, gender=None, tel=None, email=None, |
59
|
|
|
weixin_id=None, enable=None, extattr=None, **kwargs): |
60
|
|
|
""" |
61
|
|
|
更新成员 |
62
|
|
|
|
63
|
|
|
https://work.weixin.qq.com/api/doc#90000/90135/90197 |
64
|
|
|
""" |
65
|
|
|
user_data = optionaldict() |
66
|
|
|
user_data['userid'] = user_id |
67
|
|
|
user_data['name'] = name |
68
|
|
|
user_data['gender'] = gender |
69
|
|
|
user_data['department'] = department |
70
|
|
|
user_data['position'] = position |
71
|
|
|
user_data['mobile'] = mobile |
72
|
|
|
user_data['tel'] = tel |
73
|
|
|
user_data['email'] = email |
74
|
|
|
user_data['weixinid'] = weixin_id |
75
|
|
|
user_data['extattr'] = extattr |
76
|
|
|
user_data['enable'] = enable |
77
|
|
|
user_data.update(kwargs) |
78
|
|
|
|
79
|
|
|
return self._post( |
80
|
|
|
'user/update', |
81
|
|
|
data=user_data |
82
|
|
|
) |
83
|
|
|
|
84
|
10 |
|
def delete(self, user_id): |
85
|
|
|
""" |
86
|
|
|
删除成员 |
87
|
|
|
|
88
|
|
|
https://work.weixin.qq.com/api/doc#90000/90135/90198 |
89
|
|
|
""" |
90
|
|
|
return self._get( |
91
|
|
|
'user/delete', |
92
|
|
|
params={ |
93
|
|
|
'userid': user_id |
94
|
|
|
} |
95
|
|
|
) |
96
|
|
|
|
97
|
10 |
|
def batch_delete(self, user_ids): |
98
|
|
|
""" |
99
|
|
|
批量删除成员 |
100
|
|
|
|
101
|
|
|
https://work.weixin.qq.com/api/doc#90000/90135/90199 |
102
|
|
|
""" |
103
|
|
|
return self._post( |
104
|
|
|
'user/batchdelete', |
105
|
|
|
data={ |
106
|
|
|
'useridlist': user_ids |
107
|
|
|
} |
108
|
|
|
) |
109
|
|
|
|
110
|
10 |
|
def list(self, department_id, fetch_child=False, status=0, simple=False): |
111
|
|
|
""" |
112
|
|
|
批量获取部门成员 / 批量获取部门成员详情 |
113
|
|
|
|
114
|
|
|
https://work.weixin.qq.com/api/doc#90000/90135/90200 |
115
|
|
|
https://work.weixin.qq.com/api/doc#90000/90135/90201 |
116
|
|
|
|
117
|
|
|
此接口和 `WeChatDepartment.get_users` 是同一个接口,区别为 simple 的默认值不同。 |
118
|
|
|
""" |
119
|
|
|
url = 'user/simplelist' if simple else 'user/list' |
120
|
|
|
res = self._get( |
121
|
|
|
url, |
122
|
|
|
params={ |
123
|
|
|
'department_id': department_id, |
124
|
|
|
'fetch_child': 1 if fetch_child else 0, |
125
|
|
|
'status': status |
126
|
|
|
} |
127
|
|
|
) |
128
|
|
|
return res['userlist'] |
129
|
|
|
|
130
|
10 |
|
def convert_to_openid(self, user_id, agent_id=None): |
131
|
|
|
""" |
132
|
|
|
user_id 转成 openid |
133
|
|
|
|
134
|
|
|
https://work.weixin.qq.com/api/doc#90000/90135/90202 |
135
|
|
|
|
136
|
|
|
:param user_id: 企业微信内的成员 ID |
137
|
|
|
:param agent_id: 可选,需要发送红包的应用ID,若只是使用微信支付和企业转账,则无需该参数 |
138
|
|
|
:return: 返回的 JSON 数据包 |
139
|
|
|
""" |
140
|
10 |
|
data = optionaldict( |
141
|
|
|
userid=user_id, |
142
|
|
|
agentid=agent_id |
143
|
|
|
) |
144
|
10 |
|
return self._post('user/convert_to_openid', data=data) |
145
|
|
|
|
146
|
10 |
|
def convert_to_user_id(self, openid): |
147
|
|
|
""" |
148
|
|
|
openid 转成 user_id |
149
|
|
|
|
150
|
|
|
https://work.weixin.qq.com/api/doc#90000/90135/90202 |
151
|
|
|
|
152
|
|
|
:param openid: 在使用微信支付、微信红包和企业转账之后,返回结果的openid |
153
|
|
|
:return: 该 openid 在企业微信中对应的成员 user_id |
154
|
|
|
""" |
155
|
10 |
|
res = self._post('user/convert_to_userid', data={'openid': openid}) |
156
|
10 |
|
return res['userid'] |
157
|
|
|
|
158
|
10 |
|
def verify(self, user_id): |
159
|
|
|
""" |
160
|
|
|
二次验证 |
161
|
|
|
|
162
|
|
|
https://work.weixin.qq.com/api/doc#90000/90135/90203 |
163
|
|
|
|
164
|
|
|
:param user_id: 成员UserID。对应管理端的帐号 |
165
|
|
|
""" |
166
|
|
|
return self._get( |
167
|
|
|
'user/authsucc', |
168
|
|
|
params={ |
169
|
|
|
'userid': user_id |
170
|
|
|
} |
171
|
|
|
) |
172
|
|
|
|
173
|
10 |
|
def get_info(self, agent_id, code): |
174
|
|
|
return self._get( |
175
|
|
|
'user/getuserinfo', |
176
|
|
|
params={ |
177
|
|
|
'agentid': agent_id, |
178
|
|
|
'code': code |
179
|
|
|
} |
180
|
|
|
) |
181
|
|
|
|