微信企业号通讯录递归查询父级部门
wchat 2018-03-23 16:02:54

 

PHP Code复制内容到剪贴板
  1. static $api_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=CORP_ID&corpsecret=SECRET_KEY';  
  2.   
  3. public $userinfo_url = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE';  
  4.   
  5. static $oauth_access_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code';  
  6.   
  7. // 根据ID获取部门列表  
  8. static $department_list_url = 'https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=ACCESS_TOKEN&id=ID';  
  9.   
  10. static $access_token = '';  
  11.   
  12. static $expired_at = 0;  
  13.   
  14.   
  15. /** 
  16.  * access_token获取,通讯录单独的secret。如果type=1时,变更secret获取方式 
  17.  * @param string $type 
  18.  * @return bool|mixed|string 
  19.  */  
  20. public function access($type = 0)  
  21. {  
  22.     $corpId = \Yii::$app->config->get('CorpID');  
  23.   
  24.     $secret = \Yii::$app->config->get('Secret');  
  25.     switch ($type){  
  26.         case 1:  
  27.             $secret = \Yii::$app->config->get('contacts_secret');  
  28.             break;  
  29.   
  30.         default;  
  31.             break;  
  32.     }  
  33.   
  34.     $path = \Yii::getAlias('@wechat') . '/runtime/cache/' . $secret . '.dat';  
  35.     if (file_exists($path)) {  
  36.         $data = json_decode(file_get_contents($path), true);  
  37.     } else {  
  38.         $data = [];  
  39.     }  
  40.   
  41.     if ($data['expire_time'] < time()) {  
  42.         $com_curl = new Curl();  
  43.         $api_url = str_replace(['CORP_ID''SECRET_KEY'], [$corpId$secret], self::$api_url);  
  44.         $result = $com_curl->get($api_url);  
  45.   
  46.         if ($result['errcode'] != 0) {  
  47.             return false;  
  48.         };  
  49.   
  50.         $data['access_token'] = self::$access_token = $result['access_token'];  
  51.         $data['expire_time'] = self::$expired_at = time() + $result['expires_in'];  
  52.         file_put_contents($path, json_encode($data));  
  53.     } else {  
  54.         self::$access_token = $data['access_token'];  
  55.         self::$expired_at = $data['expire_time'];  
  56.     }  
  57.     return self::$access_token;  
  58. }  
  59.   
  60. public function getUserInfo($code)  
  61. {  
  62.     $token = $this->access();  
  63.   
  64.     $url = str_replace(['ACCESS_TOKEN''CODE'], [$token$code], $this->userinfo_url);  
  65.     $curl = new Curl();  
  66.     $result = $curl->get($url);  
  67.   
  68.     return $result;  
  69. }  
  70.   
  71. public function getDepartment($id){  
  72.     $token = $this->access(1);  
  73.     $url = str_replace(['ACCESS_TOKEN''ID'], [$token$id], self::$department_list_url);  
  74.     $curl = new Curl();  
  75.     $result = $curl->get($url);  
  76.     if($result['errcode'] == 0){  
  77.         return $result['department'];  
  78.     }else{  
  79.         return false;  
  80.     }  
  81. }  
  82.   
  83. /** 
  84.  * @param int $id 
  85.  * @param int $parent 
  86.  * @return array|bool 
  87.  */  
  88. public function YL_Class_Trees($id = 0,$parent = 1){  
  89.   
  90.     $dep = $this->getDepartment($id);  
  91.   
  92.     if(!$dep){  
  93.       return false;  
  94.     }  
  95.     $dep = $dep[0];  
  96.   
  97.     if($dep['parentid'] != $parent){  
  98.   
  99.         // 判断父级与指定的父级是否一致,就继续递归  
  100.         $rdata[] = $this->YL_Class_Trees($dep['parentid'])[0];  
  101.   
  102.     }elseif($dep['parentid'] == $parent){  
  103.         //判断当前父级与指定的父级一致,返加入数组  
  104.   
  105.         $rdata[] = $dep;  
  106.     }  
  107.   
  108.     return $rdata;  
  109. }  
  110.   
  111.   
  112.   
  113. /** 
  114.  * 获取部门列表,部门ID以数组形式传参,不要更低级的部门,只要二级部门 
  115.  * @param array $ids 
  116.  * @param array $defaultName 
  117.  * @return array|bool 
  118.  */  
  119. public function getDepartmentsNotLowLevel($ids = array()){  
  120.   
  121.     if($ids){  
  122.         $res = [];  
  123.         foreach ($ids as $id){  
  124.             if($id == 1){  
  125.                 // 如果是根级,跳出循环;  
  126.                 continue;  
  127.             }  
  128.             $result = $this->YL_Class_Trees($id); // 递归查询指定的父级部门  
  129.             if($result){  
  130.                 $res[] = $result;  
  131.             }else{  
  132.                 return [  
  133.                     "code" => 400,  
  134.                     "msg" => "部门ID:" . $id ."获取信息失败!"  
  135.                 ];  
  136.             }  
  137.   
  138.         }  
  139.   
  140.         if($res){  
  141.             $result = [];  
  142.             foreach ($res as $k => $v){  
  143.                 $result[] = $v[0];  
  144.             }  
  145.             $res = $result;  
  146.         }  
  147.   
  148.         return [  
  149.             "code" => 200,  
  150.             "data" => $res  
  151.         ];  
  152.   
  153.     }else{  
  154.         return false;  
  155.     }  
  156.   
  157. }  

 

本文来自于:http://www.yoyo88.cn/study/wchat/288.html

Powered by yoyo苏ICP备15045725号