webman-admin管理员数据限制的方法

2494911084@qq.com

需求:

每个账号只能管理自己的下级账号,admin账号可以管理所有账号的数据
功能模块进行数据限制仅添加人,以及添加人的上级账号可以看到。
截图

实现代码写的好垃圾,有没有大哥感兴趣顺便帮帮小弟优化一下。

实现

crudlimit 文件

<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\model\Option;
use plugin\admin\app\model\Admin;
use plugin\admin\app\Util;
use support\Db;
use support\Model;
use support\Request;

trait Crudlimit
{

    use Crud;

    /**
     * @var Model
     */
    protected $model = null;

    protected $sonids = [];

    /**
     * 查询
     *
     * @param Request $request
     * @return \support\Response
     */
    public function select(Request $request)
    {
        [$where, $format, $page_size, $field, $order] = $this->selectInput($request);

        $model = $this->model;

        // 数据限制
        $ids = $this->getAllSonAmdin(admin_id());
        $ids[] = admin_id();
        $model = $model->whereIn('admin_id', $ids);
        // 数据限制结束

        foreach ($where as $column => $value) {
            if (is_array($value)) {
                if (in_array($value[0], ['>', '=', '<', '<>'])) {
                    $model = $model->where($column, $value[0], $value[1]);
                } elseif ($value[0] == 'in') {
                    $model = $model->whereIn($column, $value[1]);
                } else {
                    $model = $model->whereBetween($column, $value);
                }
            } else {
                $model = $model->where($column, $value);
            }
        }
        $model = $model->orderBy($field, $order);
        if (in_array($format, ['select', 'tree', 'table_tree'])) {
            $items = $model->get();
            if ($format == 'select') {
                return $this->formatSelect($items);
            } elseif ($format == 'tree') {
                return $this->formatTree($items);
            }
            return $this->formatTableTree($items);
        }

        $paginator = $model->paginate($page_size);
        return $this->json(0, 'ok', [
            'items' => $paginator->items(),
            'total' => $paginator->total()
        ]);
    }

    // select下拉列表获取下级所有账户
    public function getAdminSelect()
    {
        $ids = $this->getAllSonAmdin(admin_id());
        $ids[] = admin_id();
        $model = new Admin();
        $items = $model->whereIn('id', $ids)->get();
        return $this->formatSelect($items);
    }

    // 查询所有下级账户ID
    public function getAllSonAmdin($id,$sonids = [])
    {
        $model = new Admin();
        $ids = $model->where('parent_id',$id)->where('id', '!=', $id)->pluck('id')->toArray();

        $sonids = array_merge($ids,$sonids);
        if ($ids) {
            foreach ($ids as $id) {
                $sonids = array_merge($sonids, $this->getAllSonAmdin($id));
            }
        } else {
            return $sonids;
        }

        return $sonids;
    }
}
1309 0 5
0个评论

年代过于久远,无法发表评论

2494911084@qq.com

-140
积分
0
获赞数
0
粉丝数
2022-08-17 加入
×
🔝