Dingtalk.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\constant\CommonConstant;
  4. use app\common\model\Department;
  5. use app\common\model\User;
  6. use app\common\service\DingtalkService;
  7. use hg\apidoc\annotation as Apidoc;
  8. /**
  9. * @Apidoc\Title("Dingtalk")
  10. * @Apidoc\Group("api")
  11. * @Apidoc\Sort("5")
  12. */
  13. class Dingtalk extends Base
  14. {
  15. /**
  16. * 获取部门列表
  17. *
  18. * @Apidoc\Method("POST")
  19. * @Apidoc\Param("dept_id", type="integer",require=true, desc="父部门ID")
  20. */
  21. public function department_listsub()
  22. {
  23. $dept_id = input('dept_id');
  24. $resp = DingtalkService::department_listsub($dept_id);
  25. $data = [];
  26. foreach ($resp as $value) {
  27. $data[] = [
  28. 'dept_id' => $value->dept_id,
  29. 'name' => $value->name,
  30. 'parent_id' => $value->parent_id,
  31. ];
  32. }
  33. if ($data) {
  34. Department::insertAll($data);
  35. }
  36. $this->success('获取部门列表', $resp);
  37. }
  38. /**
  39. * 获取部门详情
  40. *
  41. * @Apidoc\Method("POST")
  42. * @Apidoc\Param("dept_id", type="integer",require=true, desc="部门ID")
  43. */
  44. public function department_get()
  45. {
  46. $dept_id = input('dept_id');
  47. $resp = DingtalkService::department_get($dept_id);
  48. $this->success('获取部门详情', $resp);
  49. }
  50. /**
  51. * 获取部门用户详情
  52. *
  53. * @Apidoc\Method("POST")
  54. * @Apidoc\Param("dept_id", type="integer",require=true, desc="部门ID")
  55. */
  56. public function user_list()
  57. {
  58. $dept_id = input('dept_id');
  59. $resp = DingtalkService::user_list($dept_id);
  60. if($resp->list){
  61. $data = [];
  62. foreach ($resp->list as $value) {
  63. $data[] = [
  64. 'userid' => $value->userid,
  65. 'unionid' => $value->unionid,
  66. 'name' => $value->name,
  67. 'avatar' => $value->avatar,
  68. 'mobile' => isset($value->mobile) ? $value->mobile : '',
  69. 'email' => isset($value->email) ? $value->email : '',
  70. 'title' => isset($value->title) ? $value->title : '',
  71. 'manager_userid' => isset($value->manager_userid) ? $value->manager_userid : '',
  72. 'department' => implode(',', $value->dept_id_list),
  73. ];
  74. }
  75. if ($data) {
  76. User::insertAll($data);
  77. }
  78. }
  79. $this->success('获取部门用户详情', $resp);
  80. }
  81. /**
  82. * 查询用户详情
  83. *
  84. * @Apidoc\Method("POST")
  85. * @Apidoc\Param("userid", type="string",require=true, desc="用户的userId")
  86. */
  87. public function get_users()
  88. {
  89. $userid = input('userid');
  90. $resp = DingtalkService::user_get($userid);
  91. $this->success('查询用户详情', $resp);
  92. }
  93. //获取用户列表
  94. public function get_member()
  95. {
  96. $list = User::select();
  97. // $data = array_column($list->toArray(), null, 'userid');
  98. $no_user_list = [];
  99. $user_list = [];
  100. foreach ($list as $value) {
  101. $resp = DingtalkService::user_get($value['userid']);
  102. if ($resp->errcode == 0) {
  103. $user_list[$resp->result->userid] = [
  104. 'userid' => $resp->result->userid,
  105. 'manager_userid' => isset($resp->result->manager_userid) ? $resp->result->manager_userid : '',
  106. ];
  107. }
  108. if ($resp->errcode == 60121) {
  109. $no_user_list[$value['userid']] = [
  110. 'userid' => $value['userid'],
  111. ];
  112. }
  113. }
  114. foreach ($list as $val) {
  115. if (array_key_exists($val['userid'], $user_list)) {
  116. $val->manager_userid = $user_list[$val['userid']]['manager_userid'];
  117. $val->save();
  118. }
  119. }
  120. if ($no_user_list) {
  121. User::where('userid', 'in', array_keys($no_user_list))->update(['is_deleted' => 1]);
  122. }
  123. }
  124. /**
  125. * 获取鉴权需要的参数
  126. *
  127. * @Apidoc\Desc("设置企业内部应用H5微应用鉴权")
  128. * @Apidoc\Method("POST")
  129. * @Apidoc\Param("url", type="string", require=true, desc="应用URL")
  130. **/
  131. public function get_auth(){
  132. $url = input('url') ?: '';
  133. $this->success('获取鉴权需要的参数',DingtalkService::generateAuthSignature($url));
  134. }
  135. // 更新员工和部门
  136. public function renew(){
  137. DingtalkService::renew();
  138. $this->success('更新员工和部门成功');
  139. }
  140. }