save
This commit is contained in:
parent
846be6eecc
commit
317d7b4fc8
@ -39,25 +39,29 @@ class Tree
|
||||
|
||||
/**
|
||||
* 获取子孙节点
|
||||
* @param int $id
|
||||
* @param array $include
|
||||
* @param bool $with_self
|
||||
* @return array
|
||||
*/
|
||||
public function getDescendants(int $id, bool $with_self = false): array
|
||||
public function getDescendants(array $include, bool $with_self = false): array
|
||||
{
|
||||
if (!isset($this->hashTree[$id])) {
|
||||
return [];
|
||||
}
|
||||
$items = [];
|
||||
if ($with_self) {
|
||||
$item = $this->hashTree[$id];
|
||||
unset($item['children']);
|
||||
$items[$item['id']] = $item;
|
||||
}
|
||||
foreach ($this->hashTree[$id]['children'] ?? [] as $item) {
|
||||
unset($item['children']);
|
||||
$items[$item['id']] = $item;
|
||||
$items = array_merge($items, $this->getDescendants($item['id']));
|
||||
foreach ($include as $id) {
|
||||
if (!isset($this->hashTree[$id])) {
|
||||
return [];
|
||||
}
|
||||
if ($with_self) {
|
||||
$item = $this->hashTree[$id];
|
||||
unset($item['children']);
|
||||
$items[$item['id']] = $item;
|
||||
}
|
||||
foreach ($this->hashTree[$id]['children'] ?? [] as $item) {
|
||||
unset($item['children']);
|
||||
$items[$item['id']] = $item;
|
||||
foreach ($this->getDescendants([$item['id']]) as $it) {
|
||||
$items[$it['id']] = $it;
|
||||
}
|
||||
}
|
||||
}
|
||||
return array_values($items);
|
||||
}
|
||||
@ -120,26 +124,36 @@ class Tree
|
||||
$formatted_items[] = $item;
|
||||
}
|
||||
}
|
||||
$formatted_items = array_values($formatted_items);
|
||||
foreach ($formatted_items as &$item) {
|
||||
$this->arrayValues($item);
|
||||
}
|
||||
return $formatted_items;
|
||||
|
||||
return static::arrayValues($formatted_items);
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归重建数组下标
|
||||
* @return void
|
||||
* @param $array
|
||||
* @return array
|
||||
*/
|
||||
protected function arrayValues(&$array)
|
||||
public static function arrayValues($array): array
|
||||
{
|
||||
if (!is_array($array) || !isset($array['children'])) {
|
||||
return;
|
||||
if (!$array) {
|
||||
return [];
|
||||
}
|
||||
if (!isset($array['children'])) {
|
||||
$current = current($array);
|
||||
if (!is_array($current) || !isset($current['children'])) {
|
||||
return $array;
|
||||
}
|
||||
$tree = array_values($array);
|
||||
foreach ($tree as $index => $item) {
|
||||
$tree[$index] = static::arrayValues($item);
|
||||
}
|
||||
return $tree;
|
||||
}
|
||||
$array['children'] = array_values($array['children']);
|
||||
foreach ($array['children'] as &$child) {
|
||||
$this->arrayValues($child);
|
||||
foreach ($array['children'] as $index => $child) {
|
||||
$array['children'][$index] = static::arrayValues($child);
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
}
|
@ -76,7 +76,6 @@ class AccountController extends Crud
|
||||
$admin = $admin->toArray();
|
||||
$session = $request->session();
|
||||
unset($admin['password']);
|
||||
$admin['roles'] = $admin['roles'] ? explode(',', $admin['roles']) : [];
|
||||
$session->set('admin', $admin);
|
||||
return $this->json(0, '登录成功', [
|
||||
'nickname' => $admin['nickname'],
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace plugin\admin\app\controller;
|
||||
|
||||
use plugin\admin\app\model\Admin;
|
||||
use plugin\admin\app\model\AdminRole;
|
||||
use support\exception\BusinessException;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
@ -45,6 +46,17 @@ class AdminController extends Crud
|
||||
public function insert(Request $request): Response
|
||||
{
|
||||
if ($request->method() === 'POST') {
|
||||
$data = $this->insertInput($request);
|
||||
$admin_id = $this->doInsert($data);
|
||||
$role_ids = $request->post('roles');
|
||||
$role_ids = $role_ids ? explode(',', $role_ids) : [];
|
||||
AdminRole::where('admin_id', $admin_id)->delete();
|
||||
foreach ($role_ids as $id) {
|
||||
$admin_role = new AdminRole;
|
||||
$admin_role->admin_id = $admin_id;
|
||||
$admin_role->role_id = $id;
|
||||
$admin_role->save();
|
||||
}
|
||||
return parent::insert($request);
|
||||
}
|
||||
return view('admin/insert');
|
||||
@ -59,6 +71,24 @@ class AdminController extends Crud
|
||||
public function update(Request $request): Response
|
||||
{
|
||||
if ($request->method() === 'POST') {
|
||||
$role_ids = $request->post('roles');
|
||||
$admin_id = $request->post('id');
|
||||
if (!$admin_id) {
|
||||
return $this->json(1, '缺少参数');
|
||||
}
|
||||
$role_ids = $role_ids ? explode(',', $role_ids) : [];
|
||||
$exist_role_ids = AdminRole::where('admin_id', $admin_id)->pluck('role_id')->toArray();
|
||||
// 删除
|
||||
$delete_ids = array_diff($exist_role_ids, $role_ids);
|
||||
AdminRole::whereIn('role_id', $delete_ids)->where('admin_id', $admin_id)->delete();
|
||||
// 添加
|
||||
$add_ids = array_diff($role_ids, $exist_role_ids);
|
||||
foreach ($add_ids as $id) {
|
||||
$admin_role = new AdminRole;
|
||||
$admin_role->admin_id = $admin_id;
|
||||
$admin_role->role_id = $id;
|
||||
$admin_role->save();
|
||||
}
|
||||
return parent::update($request);
|
||||
}
|
||||
return view('admin/update');
|
||||
@ -81,6 +111,7 @@ class AdminController extends Crud
|
||||
return $this->json(1, '不能删除自己');
|
||||
}
|
||||
$this->model->whereIn($primary_key, $ids)->delete();
|
||||
AdminRole::whereIn('admin_id', $ids)->delete();
|
||||
return $this->json(0);
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@ namespace plugin\admin\app\controller;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||
use Illuminate\Database\Query\Builder as QueryBuilder;
|
||||
use plugin\admin\app\common\Auth;
|
||||
use plugin\admin\app\common\Tree;
|
||||
use plugin\admin\app\common\Util;
|
||||
use support\exception\BusinessException;
|
||||
@ -105,8 +106,9 @@ class Crud extends Base
|
||||
// 按照数据限制字段返回数据
|
||||
if ($this->dataLimit === 'personal') {
|
||||
$where[$this->dataLimitField] = admin_id();
|
||||
} elseif ($this->dataLimit === 'auth') {
|
||||
$where[$this->dataLimitField] = ['in', Auth::getAdminIds()];
|
||||
}
|
||||
|
||||
return [$where, $format, $limit, $field, $order, $page];
|
||||
}
|
||||
|
||||
|
@ -64,9 +64,9 @@ class RuleController extends Crud
|
||||
function get(Request $request): Response
|
||||
{
|
||||
$rules = $this->getRules(admin('roles'));
|
||||
$items = Rule::orderBy('weight', 'desc')->get()->toArray();
|
||||
$types = $request->get('type', '0,1');
|
||||
$types = is_string($types) ? explode(',', $types) : [0, 1];
|
||||
$items = Rule::orderBy('weight', 'desc')->get()->toArray();
|
||||
|
||||
$formatted_items = [];
|
||||
foreach ($items as $item) {
|
||||
@ -79,13 +79,12 @@ class RuleController extends Crud
|
||||
|
||||
$tree = new Tree($formatted_items);
|
||||
$tree_items = $tree->getTree();
|
||||
|
||||
// 超级管理员权限为 *
|
||||
if (!in_array('*', $rules)) {
|
||||
$this->removeNotContain($tree_items, 'id', $rules);
|
||||
}
|
||||
$this->removeNotContain($tree_items, 'type', $types);
|
||||
return $this->json(0, 'ok', $tree_items);
|
||||
return $this->json(0, 'ok', Tree::arrayValues($tree_items));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -42,50 +42,6 @@ function admin($fields = null)
|
||||
return $admin[$fields] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前是否是超级管理员
|
||||
* @return bool
|
||||
*/
|
||||
function is_supper_admin(): bool
|
||||
{
|
||||
$roles = admin('roles');
|
||||
if (!$roles) {
|
||||
return false;
|
||||
}
|
||||
$rules = Role::whereIn('id', $roles)->pluck('rules');
|
||||
return $rules && in_array('*', $rules->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前管理员权限
|
||||
* @return array
|
||||
*/
|
||||
function admin_rules(): array
|
||||
{
|
||||
$roles = admin('roles');
|
||||
if (!$roles) {
|
||||
return [];
|
||||
}
|
||||
$rule_ids = Role::whereIn('id', $roles)->pluck('rules');
|
||||
if (!$rule_ids) {
|
||||
return [];
|
||||
}
|
||||
$rule_id_strings = $rule_ids->toArray();
|
||||
$rule_ids = [];
|
||||
foreach ($rule_id_strings as $id_string) {
|
||||
if (!$id_string) {
|
||||
continue;
|
||||
}
|
||||
$rule_ids = array_merge($rule_ids, explode(',', $id_string));
|
||||
}
|
||||
if (in_array('*', $rule_ids)) {
|
||||
$rules = Rule::pluck('key', 'id');
|
||||
} else {
|
||||
$rules = Rule::whereIn('id', $rule_ids)->pluck('key', 'id');
|
||||
}
|
||||
return $rules ? $rules->toArray() : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前登录用户id
|
||||
* @return integer|null
|
||||
|
@ -26,6 +26,6 @@ class AdminRole extends Base
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
}
|
||||
|
@ -166,7 +166,6 @@
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user