每個(gè)賬號(hào)只能管理自己的下級(jí)賬號(hào),admin賬號(hào)可以管理所有賬號(hào)的數(shù)據(jù)
功能模塊進(jìn)行數(shù)據(jù)限制僅添加人,以及添加人的上級(jí)賬號(hào)可以看到。
實(shí)現(xiàn)代碼寫的好垃圾,有沒(méi)有大哥感興趣順便幫幫小弟優(yōu)化一下。
<?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;
// 數(shù)據(jù)限制
$ids = $this->getAllSonAmdin(admin_id());
$ids[] = admin_id();
$model = $model->whereIn('admin_id', $ids);
// 數(shù)據(jù)限制結(jié)束
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下拉列表獲取下級(jí)所有賬戶
public function getAdminSelect()
{
$ids = $this->getAllSonAmdin(admin_id());
$ids[] = admin_id();
$model = new Admin();
$items = $model->whereIn('id', $ids)->get();
return $this->formatSelect($items);
}
// 查詢所有下級(jí)賬戶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;
}
}