get_obj()['obj']; $req = new \OapiV2DepartmentListsubRequest; $resp = $c->execute($req, $this->get_obj()['access_token'], "https://oapi.dingtalk.com/topapi/v2/department/listsub"); if(isset($resp->result) && $resp->result){ foreach ($resp->result as $value){ $id = Db::name('department')->where('dept_id',$value->dept_id)->value('id'); if(empty($id)){ $data = array( 'dept_id' => $value->dept_id, 'name' => $value->name, 'parent_id' => $value->parent_id, ); Db::name('department')->insert($data); } //获取子部门信息 $dept_id_list = $this->get_son_department($value->dept_id); if($dept_id_list){ foreach ($dept_id_list as $v){ $id = Db::name('department')->where('dept_id',$v)->value('id'); if(empty($id)){ $data = array( 'dept_id' => $v, 'name' => $this->get_department_name($v), 'parent_id' => $value->dept_id, ); Db::name('department')->insert($data); } } } } } } //获取子部门列表 public function get_son_department($parent_id){ $c = $this->get_obj()['obj']; $req = new \OapiV2DepartmentListsubRequest; $req->setDeptId($parent_id); $resp = $c->execute($req, $this->get_obj()['access_token'], "https://oapi.dingtalk.com/topapi/v2/department/listsubid"); $dept_id_list = array(); if(isset($resp->result) && $resp->result){ $dept_id_list = $resp->result->dept_id_list; } return $dept_id_list; } //获取部门名称 public function get_department_name($dept_id){ $c = $this->get_obj()['obj']; $req = new \OapiV2DepartmentListsubRequest; $req->setDeptId($dept_id); $resp = $c->execute($req, $this->get_obj()['access_token'], "https://oapi.dingtalk.com/topapi/v2/department/get"); $name = ''; if(isset($resp->result) && $resp->result){ $name = $resp->result->name; } return $name; } //获取个部门用户信息 public function get_member_list(){ $list = Db::name('department')->column('dept_id'); foreach ($list as $value){ $list = $this->get_user_list($value); if($list){ foreach ($list as $v){ $id = Db::name('store_member')->where('unionid',$v->unionid)->value('id'); if(empty($id)){ $data = array( 'unionid' => $v->unionid, 'userid' => $v->userid, 'name' => $v->name, 'title' => isset($v->title)?$v->title:'', 'email' => isset($v->email)?$v->email:'', 'phone' => isset($v->mobile)?$v->mobile:'', 'department' => implode(',',array_diff($v->dept_id_list,array(1))), ); if($v->avatar){ $data['headimg'] = $v->avatar; } Db::name('store_member')->insert($data); } } } } } //获取部门用户详情 public function get_user_list($dept_id){ $c = $this->get_obj()['obj']; $req = new \OapiV2UserListRequest; $req->setDeptId($dept_id); $req->setCursor("0"); $req->setSize("100"); $resp = $c->execute($req, $this->get_obj()['access_token'], "https://oapi.dingtalk.com/topapi/v2/user/list"); $user_list = array(); if(isset($resp->result) && $resp->result && $resp->result->list){ $user_list = $resp->result->list; } return $user_list; } //通过免登码获取用户信息 public function get_user_info($code){ $c = $this->get_obj()['obj']; $req = new \OapiV2UserGetuserinfoRequest; $req->setCode($code); $resp = $c->execute($req, $this->get_obj()['access_token'], "https://oapi.dingtalk.com/topapi/v2/user/getuserinfo"); var_dump($resp);exit(); $user_info = array(); if(isset($resp->result) && $resp->result){ $user_info = $resp->result->dept_id_list; } return $user_info; } public function get_obj(){ $get_token_obj = new Dingtoken(); $access_token = $get_token_obj->get_company_token(); require_once env('root_path').'/vendor/dingapi/TopSdk.php'; date_default_timezone_set('Asia/Shanghai'); $c = new \DingTalkClient(\DingTalkConstant::$CALL_TYPE_OAPI, \DingTalkConstant::$METHOD_POST , \DingTalkConstant::$FORMAT_JSON); return array('obj'=>$c,'access_token'=>$access_token); } }