SearchController.class.php 2.02 KB
<?php
/**
 * Created by PhpStorm.
 * User: liyifei2012it
 * Date: 16/9/17
 * Time: 23:20
 */

namespace Apicp\Controller\Department;

use Common\Common\AdminerRole;
use VcySDK\Service;
use VcySDK\Department;

class SearchController extends AbstractController
{

    /**
     * 【通讯录】搜索部门
     * @author liyifei
     * @time 2016-09-19 14:50:22
     */
    public function Index_post()
    {

        // 接收参数
        $keyword = I('post.keyword');

        // 调用架构接口,搜索部门列表
        $departmentServ = new Department(Service::instance());
        $conds = [];
        if (!empty($keyword)) {
            $conds = [
                'dpName' => $keyword,
            ];
        }
        $result = $departmentServ->listAll($conds, 1, 99999);
        // 处理扩展属性
        $result['list'] = array_combine_by_key($result['list'], 'dpId');
        \Common\Common\Department::parseExt($result['list'], $result['extList']);
        $list = [];
        $adminerRoleUtil = new AdminerRole();
        foreach ($result['list'] as $v) {
            // 获取部门管理员权限
            if (false === ($auth = $adminerRoleUtil->childrenDepInAuth($this->_login->role, $v['dpId']))) {
                // 部门 以及 部门所有子部门 没有权限, 直接不显示
                continue;
            };
            $v['write'] = $auth[0];
            $v['read'] = $auth[1];

            $this->changeField($v, [
                'departmentMemberCount' => 'user_total',
                'dpLevel' => 'dept_level',
            ]);
            $list[] = $v;
        }

        $this->_result = [
            'list' => $list
        ];
    }

    /**
     * 变更数组字段名
     * @param $depData
     * @param $fieldChange
     * @return bool
     */
    private function changeField(&$depData, $fieldChange)
    {
        foreach ($fieldChange as $fieldName => $changeTo) {
            $depData[$changeTo] = $depData[$fieldName];
            unset($depData[$fieldName]);
        }

        return true;
    }
}