department.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, unicode_literals
  3. from itertools import chain
  4. from optionaldict import optionaldict
  5. from wechatpy.client.api.base import BaseWeChatAPI
  6. class WeChatDepartment(BaseWeChatAPI):
  7. """
  8. https://work.weixin.qq.com/api/doc#90000/90135/90204
  9. """
  10. def create(self, name, parent_id=1, order=None, id=None):
  11. """
  12. 创建部门
  13. https://work.weixin.qq.com/api/doc#90000/90135/90205
  14. :param name: 部门名称。长度限制为1~32个字符,字符不能包括\\:?”<>|
  15. :param parent_id: 父部门id,32位整型
  16. :param order: 在父部门中的次序值。order值大的排序靠前。有效的值范围是[0, 2^32)
  17. :param id: 部门id,32位整型,指定时必须大于1。若不填该参数,将自动生成id
  18. :return: 返回的 JSON 数据包
  19. """
  20. data = optionaldict(
  21. name=name,
  22. parentid=parent_id,
  23. order=order,
  24. id=id
  25. )
  26. return self._post('department/create', data=data)
  27. def update(self, id, name=None, parent_id=None, order=None):
  28. """
  29. 更新部门
  30. https://work.weixin.qq.com/api/doc#90000/90135/90206
  31. :param id: 部门 id
  32. :param name: 部门名称。长度限制为1~32个字符,字符不能包括\\:?”<>|
  33. :param parent_id: 父亲部门id
  34. :param order: 在父部门中的次序值。order值大的排序靠前。有效的值范围是[0, 2^32)
  35. :return: 返回的 JSON 数据包
  36. """
  37. data = optionaldict(
  38. id=id,
  39. name=name,
  40. parentid=parent_id,
  41. order=order
  42. )
  43. return self._post('department/update', data=data)
  44. def delete(self, id):
  45. """
  46. 删除部门
  47. https://work.weixin.qq.com/api/doc#90000/90135/90207
  48. :param id: 部门id。(注:不能删除根部门;不能删除含有子部门、成员的部门)
  49. :return: 返回的 JSON 数据包
  50. """
  51. return self._get('department/delete', params={'id': id})
  52. def get(self, id=None):
  53. """
  54. 获取指定部门列表
  55. https://work.weixin.qq.com/api/doc#90000/90135/90208
  56. 权限说明:
  57. 只能拉取token对应的应用的权限范围内的部门列表
  58. :param id: 部门id。获取指定部门及其下的子部门。 如果不填,默认获取全量组织架构
  59. :return: 部门列表
  60. """
  61. if id is None:
  62. res = self._get('department/list')
  63. else:
  64. res = self._get('department/list', params={'id': id})
  65. return res['department']
  66. def get_users(self, id, status=0, fetch_child=0, simple=True):
  67. """
  68. 获取部门成员:https://work.weixin.qq.com/api/doc#90000/90135/90200
  69. 获取部门成员详情:https://work.weixin.qq.com/api/doc#90000/90135/90201
  70. :param id: 部门 id
  71. :param status: 0 获取全部员工,1 获取已关注成员列表,
  72. 2 获取禁用成员列表,4 获取未关注成员列表。可叠加
  73. :param fetch_child: 1/0:是否递归获取子部门下面的成员
  74. :param simple: True 获取部门成员,False 获取部门成员详情
  75. :return: 部门成员列表
  76. """
  77. url = 'user/simplelist' if simple else 'user/list'
  78. res = self._get(
  79. url,
  80. params={
  81. 'department_id': id,
  82. 'status': status,
  83. 'fetch_child': 1 if fetch_child else 0
  84. }
  85. )
  86. return res['userlist']
  87. def get_map_users(self, id=None, key='name', status=0, fetch_child=0):
  88. """
  89. 映射员工某详细字段到 ``user_id``
  90. 企业微信许多对员工操作依赖于 ``user_id`` ,但没有提供直接查询员工对应 ``user_id`` 的结构,
  91. 这里是一个变通的方法,常用于储存员工 ``user_id`` ,并用于后续查询或对单人操作(如发送指定消息)
  92. :param id: 部门 id, 如果不填,默认获取有权限的所有部门
  93. :param key: 员工详细信息字段 key,所指向的值必须唯一
  94. :param status: 0 获取全部员工,1 获取已关注成员列表,
  95. 2 获取禁用成员列表,4 获取未关注成员列表。可叠加
  96. :param fetch_child: 1/0:是否递归获取子部门下面的成员
  97. :return: dict - 部门成员指定字段到 user_id 的 map ``{ key: user_id }``
  98. """
  99. ids = [id] if id is None else [item['id'] for item in self.get()]
  100. users_info = list(chain(*[
  101. self.get_users(department, status=status, fetch_child=fetch_child, simple=False)
  102. for department in ids
  103. ]))
  104. users_zip = [(user[key], user['userid']) for user in users_info]
  105. return dict(users_zip)