User.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php namespace App\Servers\WechatWork;
  2. use Illuminate\Support\Carbon;
  3. /**
  4. * 通讯录 成员管理
  5. *
  6. */
  7. class User extends Work
  8. {
  9. /**
  10. * 获取列表
  11. *
  12. * @param int $departId 指定部门ID
  13. * @param bool $fetchChild 递归获取子部门下面的成员
  14. *
  15. */
  16. public function getList($departId,$fetchChild=false){
  17. try {
  18. // 获取部门列表
  19. $result = $this->work->user->getDetailedDepartmentUsers($departId,$fetchChild);
  20. // 返回列表
  21. if( $result['errcode'] == 0 ) return $result['userlist'];// [["userid"=>"zhangsan", "name"=>"张三", "open_userid"=>"","department"=>[1,2]]];
  22. // 如果请求失败,返回空数据
  23. return [];
  24. } catch (\Throwable $th) {
  25. return [];
  26. }
  27. }
  28. /**
  29. * 获取所有部门的成员列表
  30. *
  31. */
  32. public function getListDepart(){
  33. try {
  34. // 获取缓存数据
  35. $departList = cache('work:department:user:list',[]);
  36. // 有数据的返回
  37. if( $departList ) return $departList;
  38. // 获取部门数据
  39. $departList = $this->work->department->list();
  40. // 返回列表
  41. $departList = $departList['errcode'] == 0 ? $departList['department'] : [];
  42. // 循环部门
  43. foreach ($departList as $key => $value) {
  44. // 获取每个部门的成员
  45. $value['user_list'] = $this->getList($value['id']);
  46. // 重组
  47. $departList[$key] = $value;
  48. }
  49. // 时间处理
  50. cache(['work:department:user:list'=>$departList],Carbon::now()->addMinutes(30));
  51. // 返回结果
  52. return $departList;
  53. } catch (\Throwable $th) {
  54. return [];
  55. }
  56. }
  57. /**
  58. * 获取成员详情
  59. *
  60. * @param string $userid 成员ID
  61. *
  62. */
  63. public function getOne($userid){
  64. // 获取部门列表
  65. $result = $this->work->user->get($userid);
  66. // 返回列表
  67. if( $result['errcode'] == 0 ) return $result['department'];// ["userid"=>"zhangsan", "name"=>"张三", "open_userid"=>"","department"=>[1,2]]
  68. // 如果请求失败,返回空数据
  69. return [];
  70. }
  71. }