微信企业号通讯录递归查询父级部门
PHP Code复制内容到剪贴板
- static $api_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=CORP_ID&corpsecret=SECRET_KEY';
- public $userinfo_url = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE';
- static $oauth_access_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code';
- // 根据ID获取部门列表
- static $department_list_url = 'https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=ACCESS_TOKEN&id=ID';
- static $access_token = '';
- static $expired_at = 0;
- /**
- * access_token获取,通讯录单独的secret。如果type=1时,变更secret获取方式
- * @param string $type
- * @return bool|mixed|string
- */
- public function access($type = 0)
- {
- $corpId = \Yii::$app->config->get('CorpID');
- $secret = \Yii::$app->config->get('Secret');
- switch ($type){
- case 1:
- $secret = \Yii::$app->config->get('contacts_secret');
- break;
- default;
- break;
- }
- $path = \Yii::getAlias('@wechat') . '/runtime/cache/' . $secret . '.dat';
- if (file_exists($path)) {
- $data = json_decode(file_get_contents($path), true);
- } else {
- $data = [];
- }
- if ($data['expire_time'] < time()) {
- $com_curl = new Curl();
- $api_url = str_replace(['CORP_ID', 'SECRET_KEY'], [$corpId, $secret], self::$api_url);
- $result = $com_curl->get($api_url);
- if ($result['errcode'] != 0) {
- return false;
- };
- $data['access_token'] = self::$access_token = $result['access_token'];
- $data['expire_time'] = self::$expired_at = time() + $result['expires_in'];
- file_put_contents($path, json_encode($data));
- } else {
- self::$access_token = $data['access_token'];
- self::$expired_at = $data['expire_time'];
- }
- return self::$access_token;
- }
- public function getUserInfo($code)
- {
- $token = $this->access();
- $url = str_replace(['ACCESS_TOKEN', 'CODE'], [$token, $code], $this->userinfo_url);
- $curl = new Curl();
- $result = $curl->get($url);
- return $result;
- }
- public function getDepartment($id){
- $token = $this->access(1);
- $url = str_replace(['ACCESS_TOKEN', 'ID'], [$token, $id], self::$department_list_url);
- $curl = new Curl();
- $result = $curl->get($url);
- if($result['errcode'] == 0){
- return $result['department'];
- }else{
- return false;
- }
- }
- /**
- * @param int $id
- * @param int $parent
- * @return array|bool
- */
- public function YL_Class_Trees($id = 0,$parent = 1){
- $dep = $this->getDepartment($id);
- if(!$dep){
- return false;
- }
- $dep = $dep[0];
- if($dep['parentid'] != $parent){
- // 判断父级与指定的父级是否一致,就继续递归
- $rdata[] = $this->YL_Class_Trees($dep['parentid'])[0];
- }elseif($dep['parentid'] == $parent){
- //判断当前父级与指定的父级一致,返加入数组
- $rdata[] = $dep;
- }
- return $rdata;
- }
- /**
- * 获取部门列表,部门ID以数组形式传参,不要更低级的部门,只要二级部门
- * @param array $ids
- * @param array $defaultName
- * @return array|bool
- */
- public function getDepartmentsNotLowLevel($ids = array()){
- if($ids){
- $res = [];
- foreach ($ids as $id){
- if($id == 1){
- // 如果是根级,跳出循环;
- continue;
- }
- $result = $this->YL_Class_Trees($id); // 递归查询指定的父级部门
- if($result){
- $res[] = $result;
- }else{
- return [
- "code" => 400,
- "msg" => "部门ID:" . $id ."获取信息失败!"
- ];
- }
- }
- if($res){
- $result = [];
- foreach ($res as $k => $v){
- $result[] = $v[0];
- }
- $res = $result;
- }
- return [
- "code" => 200,
- "data" => $res
- ];
- }else{
- return false;
- }
- }