Dingtalk.php 4.3 KB

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