update:优化部分代码

This commit is contained in:
JaguarJack 2021-02-08 11:26:54 +08:00
parent 61ecd0629f
commit 706410480b
3 changed files with 67 additions and 63 deletions

View File

@ -31,7 +31,7 @@ class DomainMenusSeed extends Seeder
return array ( return array (
0 => 0 =>
array ( array (
'id' => 72, 'id' => 82,
'permission_name' => '域名管理', 'permission_name' => '域名管理',
'parent_id' => 0, 'parent_id' => 0,
'level' => '', 'level' => '',
@ -47,50 +47,53 @@ class DomainMenusSeed extends Seeder
'hidden' => 1, 'hidden' => 1,
'sort' => 1, 'sort' => 1,
'created_at' => 1601105834, 'created_at' => 1601105834,
'updated_at' => 1601105834, 'updated_at' => 1612754299,
'deleted_at' => 0,
),
1 =>
array (
'id' => 73,
'permission_name' => '域名设置',
'parent_id' => 72,
'level' => '72',
'route' => '/domain/index',
'icon' => 'el-icon-setting',
'module' => 'domain',
'creator_id' => 1,
'permission_mark' => 'domain',
'component' => 'domain',
'redirect' => '',
'keepalive' => 1,
'type' => 1,
'hidden' => 1,
'sort' => 8,
'created_at' => 1601105879,
'updated_at' => 1601112604,
'deleted_at' => 0,
),
2 =>
array (
'id' => 84,
'permission_name' => '域名记录',
'parent_id' => 72,
'level' => '72',
'route' => '/domain/record/:domain',
'icon' => 'el-icon-document',
'module' => 'domain',
'creator_id' => 1,
'permission_mark' => 'domainRecord',
'component' => 'domainRecord',
'redirect' => '',
'keepalive' => 1,
'type' => 1,
'hidden' => 2,
'sort' => 1,
'created_at' => 1601112569,
'updated_at' => 1601112606,
'deleted_at' => 0, 'deleted_at' => 0,
'children' =>
array (
0 =>
array (
'id' => 83,
'permission_name' => '域名设置',
'parent_id' => 82,
'level' => '82',
'route' => '/domain/index',
'icon' => 'el-icon-setting',
'module' => 'domain',
'creator_id' => 1,
'permission_mark' => 'domain',
'component' => 'domain',
'redirect' => '',
'keepalive' => 1,
'type' => 1,
'hidden' => 1,
'sort' => 8,
'created_at' => 1601105879,
'updated_at' => 1612754299,
'deleted_at' => 0,
),
1 =>
array (
'id' => 84,
'permission_name' => '域名记录',
'parent_id' => 82,
'level' => '82',
'route' => '/domain/record/:domain',
'icon' => 'el-icon-document',
'module' => 'domain',
'creator_id' => 1,
'permission_mark' => 'domainRecord',
'component' => 'domainRecord',
'redirect' => '',
'keepalive' => 1,
'type' => 1,
'hidden' => 2,
'sort' => 1,
'created_at' => 1601112569,
'updated_at' => 1612754299,
'deleted_at' => 0,
),
),
), ),
); );
} }

View File

@ -46,11 +46,12 @@ class Role extends CatchController
} }
$this->role->storeBy($params); $this->role->storeBy($params);
$permissions = $params['permissions']; // 分配权限
if (!empty($permissions)) { if (count($params['permissions'])) {
$this->role->attachPermissions(array_unique($permissions)); $this->role->attachPermissions(array_unique($params['permissions']));
} }
if (!empty($params['departments'])) { // 分配部门
if (count($params['departments'])) {
$this->role->attachDepartments($params['departments']); $this->role->attachDepartments($params['departments']);
} }
// 添加角色 // 添加角色
@ -168,24 +169,24 @@ class Role extends CatchController
{ {
$parentRoleHasPermissionIds = []; $parentRoleHasPermissionIds = [];
if ($request->param('parent_id')) { if ($request->param('parent_id')) {
$permissions = $this->role->findBy($request->param('parent_id'))->getPermissions(); $this->role->findBy($request->param('parent_id'))
foreach ($permissions as $_permission) { ->getPermissions()
$parentRoleHasPermissionIds[] = $_permission->pivot->permission_id; ->each(function ($permission) use (&$parentRoleHasPermissionIds){
} $parentRoleHasPermissionIds[] = $permission->pivot->permission_id;
});
} }
$permissions = Permissions::whereIn('id', $parentRoleHasPermissionIds)->select()->toTree();
$permissionIds = []; $permissionIds = [];
if ($request->param('role_id')) { if ($request->param('role_id')) {
$roleHasPermissions = $this->role->findBy($request->param('role_id'))->getPermissions(); $this->role->findBy($request->param('role_id'))
foreach ($roleHasPermissions as $_permission) { ->getPermissions()
$permissionIds[] = $_permission->pivot->permission_id; ->each(function ($permission) use (&$roleHasPermissions){
} $permissionIds[] = $permission->pivot->permission_id;
});
} }
return CatchResponse::success([ return CatchResponse::success([
'permissions' => $permissions, 'permissions' => Permissions::whereIn('id', $parentRoleHasPermissionIds)->select()->toTree(),
'hasPermissions' => $permissionIds, 'hasPermissions' => $permissionIds,
]); ]);
} }

View File

@ -17,7 +17,7 @@ class Module extends CatchController
* *
* @return Json * @return Json
*/ */
public function index() public function index(): Json
{ {
$modules = []; $modules = [];
@ -51,7 +51,7 @@ class Module extends CatchController
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
*/ */
public function disOrEnable(string $module) public function disOrEnable(string $module): Json
{ {
$moduleInfo = CatchAdmin::getModuleInfo(CatchAdmin::directory() . $module); $moduleInfo = CatchAdmin::getModuleInfo(CatchAdmin::directory() . $module);
@ -71,7 +71,7 @@ class Module extends CatchController
* @time 2020年09月21日 * @time 2020年09月21日
* @return Json * @return Json
*/ */
public function cache() public function cache(): Json
{ {
return CatchResponse::success(CatchAdmin::cacheServices()); return CatchResponse::success(CatchAdmin::cacheServices());
} }
@ -82,7 +82,7 @@ class Module extends CatchController
* @time 2020年09月21日 * @time 2020年09月21日
* @return Json * @return Json
*/ */
public function clear() public function clear(): Json
{ {
return !file_exists(CatchAdmin::getCacheServicesFile()) ? return !file_exists(CatchAdmin::getCacheServicesFile()) ?
CatchResponse::fail('模块没有缓存') : CatchResponse::fail('模块没有缓存') :