当前位置: 首页 > 工具软件 > SelectPage > 使用案例 >

fastadmin中selectpage条件wherein添加

欧阳博超
2023-12-01
public function selectpage()
{
    //设置过滤方法
    $this->request->filter(['strip_tags', 'htmlspecialchars']);

    //搜索关键词,客户端输入以空格分开,这里接收为数组
    $word = (array)$this->request->request("q_word/a");
    //当前页
    $page = $this->request->request("pageNumber");
    //分页大小
    $pagesize = $this->request->request("pageSize");
    //搜索条件
    $andor = $this->request->request("andOr", "and", "strtoupper");
    //排序方式
    $orderby = (array)$this->request->request("orderBy/a");
    //显示的字段
    $field = $this->request->request("showField");
    $extraShowField = $this->request->request("extraShowField/s"); //需要额外显示的字段

    //主键
    $primarykey = $this->request->request("keyField");
    //主键值
    $primaryvalue = $this->request->request("keyValue");

    //搜索字段
    $searchfield = (array)$this->request->request("searchField/a");

    //在返回数据时,额外返回的字段值
    $returnField = $this->request->request("returnField/s");
    //自定义搜索条件
    $custom = (array)$this->request->request("custom/a");

    //自定义搜索条件 多数值 
    $wherein = (array)$this->request->request("wherein/a");
    $order = [];
    foreach ($orderby as $k => $v) {
        $order[$v[0]] = $v[1];
    }
    $field = $field ? $field : 'name';

    //如果有primaryvalue,说明当前是初始化传值
    if ($primaryvalue !== null) {
        $where = [$primarykey => ['in', $primaryvalue]];
    } else {
        $where = function ($query) use ($word, $andor, $field, $searchfield, $custom, $wherein) {
            $logic = $andor == 'AND' ? '&' : '|';
            $searchfield = is_array($searchfield) ? implode($logic, $searchfield) : $searchfield;
            foreach ($word as $k => $v) {
                $query->where(str_replace(',', $logic, $searchfield), "like", "%{$v}%");
            }
            if ($custom && is_array($custom)) {
                foreach ($custom as $k => $v) {
                    $query->where($k, '=', $v);
                }
            }
            if ($wherein && is_array($wherein)) {
                foreach ($wherein as $k => $v) {
                    $query->where($k, 'in', $v);
                }
            }

        };
    }
    $adminIds = $this->getDataLimitAdminIds();
    if (is_array($adminIds)) {
        $this->model->where($this->dataLimitField, 'in', $adminIds);
    }
    $list = [];
    $total = $this->model->where($where)->count();
    if ($total > 0) {
        if (is_array($adminIds)) {
            $this->model->where($this->dataLimitField, 'in', $adminIds);
        }
        $qFields = $primarykey . "," . $field;
        if ($returnField) {
            $returnField = explode(",", $returnField);
        } else {
            $returnField = [];
        }

        if ($extraShowField) {
            $extraShowField = explode(',', $extraShowField);
        } else {
            $extraShowField = [];
        }

        $f = array_unique(array_merge($extraShowField, $returnField));
        $f && $qFields .= ',' . implode(',', $f);

        $datalist = $this->model->where($where)
            ->order($order)
            ->page($page, $pagesize)
            ->field($qFields)
            ->select();

        foreach ($datalist as $index => $item) {
            unset($item['password'], $item['salt']);
            $fieldValue = isset($item[$field]) ? $item[$field] : '';
            $row = [
                $primarykey => isset($item[$primarykey]) ? $item[$primarykey] : '',

            ];
            if ($extraShowField) {
                $fieldValue .= '(';
                foreach ($extraShowField as $f) {
                    $row[$f] = @(string)$item[$f];
                    $fieldValue .= $row[$f] . '--';
                }
                $fieldValue = trim($fieldValue, '-') . ')';
            }
            if($returnField){
                foreach ($returnField as $f) {
                    $row[$f] = @(string)$item[$f];
                }
            }

            $row[$field]=$fieldValue;
            $list[] = $row;
        }
    }
    //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
    return json(['list' => $list, 'total' => $total]);
}
 类似资料: