SearchController.class.php 4.21 KB
<?php
/**
 * 选人组件-搜索接口
 * Created by PhpStorm.
 * User: 鲜彤
 * Date: 2016/8/26
 * Time: 15:00
 */
namespace Apicp\Controller\ChooseMem;

use VcySDK\Service;
use VcySDK\Member;
use Common\Common\Department;
use Common\Common\User;

class SearchController extends AbstractController
{

    /**
     * 成员类型
     */
    const TYPE_MEMBER = 1;

    /**
     * 部门类型
     */
    const TYPE_DEPARTMENT = 2;

    /**
     * 标签类型
     */
    const TYPE_TAG = 3;

    /**
     * 常用类型
     */
    const TYPE_COMMONLY_USED = 4;

    /**
     * 岗位类型
     */
    const TYPE_JOB = 5;

    /**
     * 角色类型
     */
    const TYPE_ROLE = 6;

    /**
     * 类型数字标识(UC用) => 类型字符串标识(前端用)
     */
    const TYPE_NUMBER_WITH_STRING = [
        self::TYPE_MEMBER => 'member',
        self::TYPE_DEPARTMENT => 'department',
        self::TYPE_TAG => 'tag',
        self::TYPE_COMMONLY_USED => 'commonly_used',
        self::TYPE_JOB => 'job',
        self::TYPE_ROLE => 'role',
    ];

    public function Index()
    {
        $role = $this->_login->role;
        $search = I("post.search");

        // 获取参数key
        $key = I('post.key', '', 'trim');
        $field = I('post.field', []);

        // 调用SDK获取列表
        $sdk = new Member(Service::instance());
        $result = $sdk->searchList(array('name' => $key, 'flags' => $field, 'pageSize' => 50));

        // 循环返回列表,更改类型字段值
        foreach ($result['list'] as $k => $v) {
            // 如果有需要查询某类 并且 返回值的类型不在内, 则剔除
            if (!empty($field) && !in_array($v['flag'], $field)) {
                unset($result['list'][$k]);
                continue;
            }

            $flag = $v['flag'];
            $type = self::TYPE_NUMBER_WITH_STRING[$flag];

            // 删掉UC返回的flag键
            unset($result['list'][$k]['flag']);

            // 添加type键
            $result['list'][$k]['type'] = $type;
        }

        // skip_role 用于跳过角色权限限制
        // 默认按管理员可见部门范围进行搜索
        if (empty(I('get.skip_role')) && empty($search) && !empty($role['readDpIdList'])) {
            $search = [];
            $dpServ = &Department::instance();

            // 获取管理员可见部门ID,包括子级部门ID
            $search['dpIds'] = $dpServ->list_childrens_by_cdid($role['readDpIdList'], true);
            $search['dpIds'] = array_values($search['dpIds']);

            // 获取管理员可见部门下的所有人员
            $userServ = &User::instance();
            $users = $userServ->listAll(['dpIdList' => $search['dpIds']]);

            if (!empty($users)) {
                $search['uids'] = array_column($users, 'memUid');
            }
        }

        // 特殊处理
        if (!empty($search)) {

            $result['list'] = $this->search($result['list'], $search);
            $result['total'] = count($result['list']);
        }

        $result['list'] = array_values($result['list']);
        $this->_result = $result;

        return true;
    }

    /**
     * 特殊查询处理
     * @param array $list 搜索后的数据列表
     * @param array $search
     *              +array dpIds  部门IDS
     *              +array uids  人员UIDS
     *              +array tagIds  标签IDS
     * @return array
     */
    private function search($list = array(), $search = array())
    {
        // 初始化返回值
        $data = array();

        // 遍历结果集
        foreach ($list as $key => $v) {

            // 如果是人员
            if ($v['type'] == 'member') {

                if (in_array($v['id'], $search['uids'])) {

                    $data[] = $v;
                }

            }

            // 如果是部门
            if ($v['type'] == 'department') {

                if (in_array($v['id'], $search['dpIds'])) {

                    $data[] = $v;
                }
            }

            // 如果是标签
            if ($v['type'] == 'tag') {

                if (in_array($v['id'], $search['tagIds'])) {

                    $data[] = $v;

                }
            }


        }
        return $data;
    }
}