|
@@ -0,0 +1,103 @@
|
|
|
+<?php
|
|
|
+namespace app\api\controller;
|
|
|
+use AlibabaCloud\SDK\Dingtalk\Vcrm_1_0\Models\DescribeRelationMetaResponseBody\relationMetaDTOList\items\props\rule;
|
|
|
+use think\Db;
|
|
|
+use hg\apidoc\annotation as Apidoc;
|
|
|
+/**
|
|
|
+ * @Apidoc\Title("钉钉内部组织")
|
|
|
+ * @Apidoc\Group("api")
|
|
|
+ */
|
|
|
+class Dingtalk extends Base
|
|
|
+{
|
|
|
+
|
|
|
+ //获取部门信息
|
|
|
+ public function get_department(){
|
|
|
+ $c = $this->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_user_list(){
|
|
|
+ $c = $this->get_obj()['obj'];
|
|
|
+ $req = new \OapiV2UserListRequest;
|
|
|
+ $req->setDeptId("537635109");
|
|
|
+ $req->setCursor("0");
|
|
|
+ $req->setSize("100");
|
|
|
+ $resp = $c->execute($req, $this->get_obj()['access_token'], "https://oapi.dingtalk.com/topapi/v2/user/list");
|
|
|
+ if(isset($resp->result) && $resp->result && $resp->result->list){
|
|
|
+ var_dump($resp->result->list);exit();
|
|
|
+ foreach ($resp->result->list as $value){
|
|
|
+ var_dump($value);exit();
|
|
|
+ $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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public function get_obj(){
|
|
|
+ $get_token_obj = new GetUserToken();
|
|
|
+ $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);
|
|
|
+ }
|
|
|
+}
|