save
This commit is contained in:
parent
5576a94f59
commit
5bf2eabd0f
@ -4,6 +4,7 @@ namespace plugin\admin\app\controller;
|
|||||||
|
|
||||||
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||||
use Illuminate\Database\Query\Builder as QueryBuilder;
|
use Illuminate\Database\Query\Builder as QueryBuilder;
|
||||||
|
use plugin\admin\app\common\Tree;
|
||||||
use plugin\admin\app\common\Util;
|
use plugin\admin\app\common\Util;
|
||||||
use support\exception\BusinessException;
|
use support\exception\BusinessException;
|
||||||
use support\Model;
|
use support\Model;
|
||||||
@ -305,24 +306,14 @@ class Crud extends Base
|
|||||||
{
|
{
|
||||||
$items_map = [];
|
$items_map = [];
|
||||||
foreach ($items as $item) {
|
foreach ($items as $item) {
|
||||||
$items_map[$item->id] = [
|
$items_map[] = [
|
||||||
'name' => $item->title ?? $item->name ?? $item->id,
|
'name' => $item->title ?? $item->name ?? $item->id,
|
||||||
'value' => (string)$item->id,
|
'value' => (string)$item->id,
|
||||||
'pid' => $item->pid,
|
'pid' => $item->pid,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
$formatted_items = [];
|
$tree = new Tree($items_map);
|
||||||
foreach ($items_map as $index => $item) {
|
return $this->json(0, 'ok', $tree->getTree());
|
||||||
if ($item['pid'] && isset($items_map[$item['pid']])) {
|
|
||||||
$items_map[$item['pid']]['children'][] = &$items_map[$index];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
foreach ($items_map as $item) {
|
|
||||||
if (!$item['pid']) {
|
|
||||||
$formatted_items[] = $item;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $this->json(0, 'ok', $formatted_items);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -332,22 +323,8 @@ class Crud extends Base
|
|||||||
*/
|
*/
|
||||||
protected function formatTableTree($items): Response
|
protected function formatTableTree($items): Response
|
||||||
{
|
{
|
||||||
$items_map = [];
|
$tree = new Tree($items);
|
||||||
foreach ($items as $item) {
|
return $this->json(0, 'ok', $tree->getTree());
|
||||||
$items_map[$item->id] = $item->toArray();
|
|
||||||
}
|
|
||||||
$formatted_items = [];
|
|
||||||
foreach ($items_map as $index => $item) {
|
|
||||||
if ($item['pid'] && isset($items_map[$item['pid']])) {
|
|
||||||
$items_map[$item['pid']]['children'][] = &$items_map[$index];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
foreach ($items_map as $item) {
|
|
||||||
if (!$item['pid']) {
|
|
||||||
$formatted_items[] = $item;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $this->json(0, 'ok', $formatted_items);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
namespace plugin\admin\app\controller;
|
namespace plugin\admin\app\controller;
|
||||||
|
|
||||||
|
use plugin\admin\app\common\Tree;
|
||||||
use plugin\admin\app\model\Role;
|
use plugin\admin\app\model\Role;
|
||||||
|
use plugin\admin\app\model\Rule;
|
||||||
use support\exception\BusinessException;
|
use support\exception\BusinessException;
|
||||||
use support\Request;
|
use support\Request;
|
||||||
use support\Response;
|
use support\Response;
|
||||||
@ -100,4 +102,29 @@ class RoleController extends Crud
|
|||||||
return $this->json(0);
|
return $this->json(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取角色权限
|
||||||
|
* @param Request $request
|
||||||
|
* @return Response
|
||||||
|
*/
|
||||||
|
public function rules(Request $request): Response
|
||||||
|
{
|
||||||
|
$role_id = $request->get('role');
|
||||||
|
if (empty($role_id)) {
|
||||||
|
return $this->json(0, 'ok', []);
|
||||||
|
}
|
||||||
|
$rule_id_string = Role::where('id', $role_id)->value('rules');
|
||||||
|
if ($rule_id_string === '') {
|
||||||
|
return $this->json(0, 'ok', []);
|
||||||
|
}
|
||||||
|
$rules = Rule::get();
|
||||||
|
$ids = [];
|
||||||
|
if ($rule_id_string !== '*') {
|
||||||
|
$ids = explode(',', $rule_id_string);
|
||||||
|
}
|
||||||
|
$tree = new Tree($rules);
|
||||||
|
return $this->json(0, 'ok', $tree->getTree($ids));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace plugin\admin\app\controller;
|
namespace plugin\admin\app\controller;
|
||||||
|
|
||||||
|
use plugin\admin\app\common\Tree;
|
||||||
use plugin\admin\app\common\Util;
|
use plugin\admin\app\common\Util;
|
||||||
use plugin\admin\app\model\Role;
|
use plugin\admin\app\model\Role;
|
||||||
use plugin\admin\app\model\Rule;
|
use plugin\admin\app\model\Rule;
|
||||||
@ -59,7 +60,6 @@ class RuleController extends Crud
|
|||||||
* 获取菜单
|
* 获取菜单
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @return Response
|
* @return Response
|
||||||
* @throws BusinessException
|
|
||||||
*/
|
*/
|
||||||
function get(Request $request): Response
|
function get(Request $request): Response
|
||||||
{
|
{
|
||||||
@ -67,44 +67,31 @@ class RuleController extends Crud
|
|||||||
$items = Rule::orderBy('weight', 'desc')->get()->toArray();
|
$items = Rule::orderBy('weight', 'desc')->get()->toArray();
|
||||||
$types = $request->get('type', '0,1');
|
$types = $request->get('type', '0,1');
|
||||||
$types = is_string($types) ? explode(',', $types) : [0, 1];
|
$types = is_string($types) ? explode(',', $types) : [0, 1];
|
||||||
$items_map = [];
|
|
||||||
|
$formatted_items = [];
|
||||||
foreach ($items as $item) {
|
foreach ($items as $item) {
|
||||||
$item['pid'] = (int)$item['pid'];
|
$item['pid'] = (int)$item['pid'];
|
||||||
$item['name'] = $item['title'];
|
$item['name'] = $item['title'];
|
||||||
$item['value'] = $item['id'];
|
$item['value'] = $item['id'];
|
||||||
$items_map[$item['id']] = $item;
|
$item['icon'] = $item['icon'] ? "layui-icon {$item['icon']}" : '';
|
||||||
}
|
$formatted_items[] = $item;
|
||||||
$formatted_items = [];
|
|
||||||
foreach ($items_map as $index => $item) {
|
|
||||||
//$items_map[$index]['type'] = $items_map[$index]['href'] ? 1 : 0;
|
|
||||||
$items_map[$index]['icon'] = $items_map[$index]['icon'] ? "layui-icon {$items_map[$index]['icon']}" : '';
|
|
||||||
if ($item['pid'] && isset($items_map[$item['pid']])) {
|
|
||||||
$items_map[$item['pid']]['children'][] = &$items_map[$index];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
foreach ($items_map as $item) {
|
|
||||||
if (!$item['pid']) {
|
|
||||||
$formatted_items[] = $item;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$tree = new Tree($formatted_items);
|
||||||
|
$tree_items = $tree->getTree();
|
||||||
|
|
||||||
// 超级管理员权限为 *
|
// 超级管理员权限为 *
|
||||||
if (!in_array('*', $rules)) {
|
if (!in_array('*', $rules)) {
|
||||||
$this->removeUncontain($formatted_items, 'id', $rules);
|
$this->removeNotContain($tree_items, 'id', $rules);
|
||||||
}
|
}
|
||||||
$this->removeUncontain($formatted_items, 'type', $types);
|
$this->removeNotContain($tree_items, 'type', $types);
|
||||||
$formatted_items = array_values($formatted_items);
|
return $this->json(0, 'ok', $tree_items);
|
||||||
foreach ($formatted_items as &$item) {
|
|
||||||
$this->arrayValues($item);
|
|
||||||
}
|
|
||||||
return $this->json(0, 'ok', $formatted_items);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取控制器详细权限
|
* 获取控制器详细权限
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @return Response
|
* @return Response
|
||||||
* @throws BusinessException
|
|
||||||
*/
|
*/
|
||||||
public function permissionCodes(Request $request): Response
|
public function permissionCodes(Request $request): Response
|
||||||
{
|
{
|
||||||
@ -279,7 +266,7 @@ class RuleController extends Crud
|
|||||||
* @param $values
|
* @param $values
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function removeUncontain(&$array, $key, $values)
|
protected function removeNotContain(&$array, $key, $values)
|
||||||
{
|
{
|
||||||
foreach ($array as $k => &$item) {
|
foreach ($array as $k => &$item) {
|
||||||
if (!is_array($item)) {
|
if (!is_array($item)) {
|
||||||
@ -291,7 +278,7 @@ class RuleController extends Crud
|
|||||||
if (!isset($item['children'])) {
|
if (!isset($item['children'])) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$this->removeUncontain($item['children'], $key, $values);
|
$this->removeNotContain($item['children'], $key, $values);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -359,20 +346,4 @@ class RuleController extends Crud
|
|||||||
return $rules;
|
return $rules;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 递归重建数组下标
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
protected function arrayValues(&$array)
|
|
||||||
{
|
|
||||||
if (!is_array($array) || !isset($array['children'])) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$array['children'] = array_values($array['children']);
|
|
||||||
|
|
||||||
foreach ($array['children'] as &$child) {
|
|
||||||
$this->arrayValues($child);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
use app\model\User;
|
use app\model\User;
|
||||||
use plugin\admin\app\model\Admin;
|
use plugin\admin\app\model\Admin;
|
||||||
|
use plugin\admin\app\model\AdminRole;
|
||||||
use plugin\admin\app\model\Role;
|
use plugin\admin\app\model\Role;
|
||||||
use plugin\admin\app\model\Rule;
|
use plugin\admin\app\model\Rule;
|
||||||
|
|
||||||
@ -143,7 +144,7 @@ function refresh_admin_session(bool $force = false)
|
|||||||
}
|
}
|
||||||
$admin = $admin->toArray();
|
$admin = $admin->toArray();
|
||||||
unset($admin['password']);
|
unset($admin['password']);
|
||||||
$admin['roles'] = $admin['roles'] ? explode(',', $admin['roles']) : [];
|
$admin['roles'] = AdminRole::where('admin_id', $admin_id)->pluck('role_id')->toArray();
|
||||||
$admin['session_last_update_time'] = $time_now;
|
$admin['session_last_update_time'] = $time_now;
|
||||||
$session->set('admin', $admin);
|
$session->set('admin', $admin);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user