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 (
0 =>
array (
'id' => 72,
'id' => 82,
'permission_name' => '域名管理',
'parent_id' => 0,
'level' => '',
@ -47,50 +47,53 @@ class DomainMenusSeed extends Seeder
'hidden' => 1,
'sort' => 1,
'created_at' => 1601105834,
'updated_at' => 1601105834,
'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,
'updated_at' => 1612754299,
'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);
$permissions = $params['permissions'];
if (!empty($permissions)) {
$this->role->attachPermissions(array_unique($permissions));
// 分配权限
if (count($params['permissions'])) {
$this->role->attachPermissions(array_unique($params['permissions']));
}
if (!empty($params['departments'])) {
// 分配部门
if (count($params['departments'])) {
$this->role->attachDepartments($params['departments']);
}
// 添加角色
@ -168,24 +169,24 @@ class Role extends CatchController
{
$parentRoleHasPermissionIds = [];
if ($request->param('parent_id')) {
$permissions = $this->role->findBy($request->param('parent_id'))->getPermissions();
foreach ($permissions as $_permission) {
$parentRoleHasPermissionIds[] = $_permission->pivot->permission_id;
}
$this->role->findBy($request->param('parent_id'))
->getPermissions()
->each(function ($permission) use (&$parentRoleHasPermissionIds){
$parentRoleHasPermissionIds[] = $permission->pivot->permission_id;
});
}
$permissions = Permissions::whereIn('id', $parentRoleHasPermissionIds)->select()->toTree();
$permissionIds = [];
if ($request->param('role_id')) {
$roleHasPermissions = $this->role->findBy($request->param('role_id'))->getPermissions();
foreach ($roleHasPermissions as $_permission) {
$permissionIds[] = $_permission->pivot->permission_id;
}
$this->role->findBy($request->param('role_id'))
->getPermissions()
->each(function ($permission) use (&$roleHasPermissions){
$permissionIds[] = $permission->pivot->permission_id;
});
}
return CatchResponse::success([
'permissions' => $permissions,
'permissions' => Permissions::whereIn('id', $parentRoleHasPermissionIds)->select()->toTree(),
'hasPermissions' => $permissionIds,
]);
}

View File

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