Dingtalk.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use hg\apidoc\annotation as Apidoc;
  5. class Dingtalk extends Base
  6. {
  7. //获取部门信息
  8. public function get_department(){
  9. $c = $this->get_obj()['obj'];
  10. $req = new \OapiV2DepartmentListsubRequest;
  11. $resp = $c->execute($req, $this->get_obj()['access_token'], "https://oapi.dingtalk.com/topapi/v2/department/listsub");
  12. if(isset($resp->result) && $resp->result){
  13. foreach ($resp->result as $value){
  14. $id = Db::name('department')->where('dept_id',$value->dept_id)->value('id');
  15. if(empty($id)){
  16. $data = array(
  17. 'dept_id' => $value->dept_id,
  18. 'name' => $value->name,
  19. 'parent_id' => $value->parent_id,
  20. );
  21. Db::name('department')->insert($data);
  22. }
  23. //获取子部门信息
  24. $dept_id_list = $this->get_son_department($value->dept_id);
  25. if($dept_id_list){
  26. foreach ($dept_id_list as $v){
  27. $id = Db::name('department')->where('dept_id',$v)->value('id');
  28. if(empty($id)){
  29. $data = array(
  30. 'dept_id' => $v,
  31. 'name' => $this->get_department_name($v),
  32. 'parent_id' => $value->dept_id,
  33. );
  34. Db::name('department')->insert($data);
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }
  41. //获取子部门列表
  42. public function get_son_department($parent_id){
  43. $c = $this->get_obj()['obj'];
  44. $req = new \OapiV2DepartmentListsubRequest;
  45. $req->setDeptId($parent_id);
  46. $resp = $c->execute($req, $this->get_obj()['access_token'], "https://oapi.dingtalk.com/topapi/v2/department/listsubid");
  47. $dept_id_list = array();
  48. if(isset($resp->result) && $resp->result){
  49. $dept_id_list = $resp->result->dept_id_list;
  50. }
  51. return $dept_id_list;
  52. }
  53. //获取部门名称
  54. public function get_department_name($dept_id){
  55. $c = $this->get_obj()['obj'];
  56. $req = new \OapiV2DepartmentListsubRequest;
  57. $req->setDeptId($dept_id);
  58. $resp = $c->execute($req, $this->get_obj()['access_token'], "https://oapi.dingtalk.com/topapi/v2/department/get");
  59. $name = '';
  60. if(isset($resp->result) && $resp->result){
  61. $name = $resp->result->name;
  62. }
  63. return $name;
  64. }
  65. //获取个部门用户信息
  66. public function get_member_list(){
  67. $list = Db::name('department')->column('dept_id');
  68. foreach ($list as $value){
  69. $list = $this->get_user_list($value);
  70. if($list){
  71. foreach ($list as $v){
  72. $id = Db::name('store_member')->where('unionid',$v->unionid)->value('id');
  73. if(empty($id)){
  74. $data = array(
  75. 'unionid' => $v->unionid,
  76. 'userid' => $v->userid,
  77. 'name' => $v->name,
  78. 'title' => isset($v->title)?$v->title:'',
  79. 'email' => isset($v->email)?$v->email:'',
  80. 'phone' => isset($v->mobile)?$v->mobile:'',
  81. 'department' => implode(',',array_diff($v->dept_id_list,array(1))),
  82. );
  83. if($v->avatar){
  84. $data['headimg'] = $v->avatar;
  85. }
  86. Db::name('store_member')->insert($data);
  87. }
  88. }
  89. }
  90. }
  91. }
  92. //获取部门用户详情
  93. public function get_user_list($dept_id){
  94. $c = $this->get_obj()['obj'];
  95. $req = new \OapiV2UserListRequest;
  96. $req->setDeptId($dept_id);
  97. $req->setCursor("0");
  98. $req->setSize("100");
  99. $resp = $c->execute($req, $this->get_obj()['access_token'], "https://oapi.dingtalk.com/topapi/v2/user/list");
  100. $user_list = array();
  101. if(isset($resp->result) && $resp->result && $resp->result->list){
  102. $user_list = $resp->result->list;
  103. }
  104. return $user_list;
  105. }
  106. //通过免登码获取用户信息
  107. public function get_user_info($code){
  108. $c = $this->get_obj()['obj'];
  109. $req = new \OapiV2UserGetuserinfoRequest;
  110. $req->setCode($code);
  111. $resp = $c->execute($req, $this->get_obj()['access_token'], "https://oapi.dingtalk.com/topapi/v2/user/getuserinfo");
  112. var_dump($resp);exit();
  113. $user_info = array();
  114. if(isset($resp->result) && $resp->result){
  115. $user_info = $resp->result->dept_id_list;
  116. }
  117. return $user_info;
  118. }
  119. public function get_obj(){
  120. $get_token_obj = new Dingtoken();
  121. $access_token = $get_token_obj->get_company_token();
  122. require_once env('root_path').'/vendor/dingapi/TopSdk.php';
  123. date_default_timezone_set('Asia/Shanghai');
  124. $c = new \DingTalkClient(\DingTalkConstant::$CALL_TYPE_OAPI, \DingTalkConstant::$METHOD_POST , \DingTalkConstant::$FORMAT_JSON);
  125. return array('obj'=>$c,'access_token'=>$access_token);
  126. }
  127. }