- 新增角色菜单关联模型、控制器、请求验证和业务逻辑 - 新增用户角色关联模型、控制器、请求验证和业务逻辑 - 更新系统角色模型,增加与用户和菜单的关联 - 更新用户模型,增加与角色的关联和相关方法 - 在路由文件中添加新功能的路由定义
168 lines
4.3 KiB
PHP
168 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\System;
|
|
|
|
use App\Models\System\SystemRoleMenu;
|
|
use App\Models\System\SystemRole;
|
|
use App\Models\System\SystemMenu;
|
|
use App\Services\BaseService;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* 系统角色菜单关联服务类
|
|
*/
|
|
class SystemRoleMenuService extends BaseService
|
|
{
|
|
protected string $modelClass = SystemRoleMenu::class;
|
|
|
|
/**
|
|
* 获取角色菜单关联列表
|
|
*/
|
|
public function getList(array $params): LengthAwarePaginator
|
|
{
|
|
$query = SystemRoleMenu::query()
|
|
->with(['role:id,name', 'menu:id,name,type']);
|
|
|
|
// 角色筛选
|
|
if (isset($params['role_id'])) {
|
|
$query->where('role_id', $params['role_id']);
|
|
}
|
|
|
|
// 菜单筛选
|
|
if (isset($params['menu_id'])) {
|
|
$query->where('menu_id', $params['menu_id']);
|
|
}
|
|
|
|
// 排序
|
|
$query->orderBy('role_id', 'asc')
|
|
->orderBy('menu_id', 'asc');
|
|
|
|
return $query->paginate($params['page_size'] ?? 15);
|
|
}
|
|
|
|
/**
|
|
* 获取角色的菜单列表
|
|
*/
|
|
public function getRoleMenus(int $roleId): array
|
|
{
|
|
return SystemRoleMenu::where('role_id', $roleId)
|
|
->pluck('menu_id')
|
|
->toArray();
|
|
}
|
|
|
|
/**
|
|
* 获取菜单的角色列表
|
|
*/
|
|
public function getMenuRoles(int $menuId): array
|
|
{
|
|
return SystemRoleMenu::where('menu_id', $menuId)
|
|
->pluck('role_id')
|
|
->toArray();
|
|
}
|
|
|
|
/**
|
|
* 设置角色菜单权限
|
|
*/
|
|
public function setRoleMenus(int $roleId, array $menuIds): bool
|
|
{
|
|
return DB::transaction(function () use ($roleId, $menuIds) {
|
|
// 删除现有关联
|
|
SystemRoleMenu::where('role_id', $roleId)->delete();
|
|
|
|
// 批量插入新关联
|
|
if (!empty($menuIds)) {
|
|
$data = [];
|
|
foreach ($menuIds as $menuId) {
|
|
$data[] = [
|
|
'role_id' => $roleId,
|
|
'menu_id' => $menuId,
|
|
'creator' => auth('admin')->id() ?? 0,
|
|
'create_time' => now(),
|
|
'tenant_id' => $this->getCurrentTenantId() ?? 0,
|
|
'deleted' => 0,
|
|
];
|
|
}
|
|
SystemRoleMenu::insert($data);
|
|
}
|
|
|
|
return true;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 获取角色的菜单树
|
|
*/
|
|
public function getRoleMenuTree(int $roleId): array
|
|
{
|
|
$menuIds = $this->getRoleMenus($roleId);
|
|
|
|
if (empty($menuIds)) {
|
|
return [];
|
|
}
|
|
|
|
$menus = SystemMenu::whereIn('id', $menuIds)
|
|
->where('status', 1)
|
|
->orderBy('sort', 'asc')
|
|
->get()
|
|
->toArray();
|
|
|
|
return $this->buildMenuTree($menus);
|
|
}
|
|
|
|
/**
|
|
* 构建菜单树
|
|
*/
|
|
private function buildMenuTree(array $menus, int $parentId = 0): array
|
|
{
|
|
$tree = [];
|
|
foreach ($menus as $menu) {
|
|
if ($menu['parent_id'] == $parentId) {
|
|
$children = $this->buildMenuTree($menus, $menu['id']);
|
|
if (!empty($children)) {
|
|
$menu['children'] = $children;
|
|
}
|
|
$tree[] = $menu;
|
|
}
|
|
}
|
|
return $tree;
|
|
}
|
|
|
|
/**
|
|
* 批量设置多个角色的菜单权限
|
|
*/
|
|
public function batchSetRoleMenus(array $roleMenuData): bool
|
|
{
|
|
return DB::transaction(function () use ($roleMenuData) {
|
|
foreach ($roleMenuData as $data) {
|
|
$this->setRoleMenus($data['role_id'], $data['menu_ids'] ?? []);
|
|
}
|
|
return true;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 删除角色的所有菜单权限
|
|
*/
|
|
public function clearRoleMenus(int $roleId): bool
|
|
{
|
|
return SystemRoleMenu::where('role_id', $roleId)->delete();
|
|
}
|
|
|
|
/**
|
|
* 删除菜单的所有角色关联
|
|
*/
|
|
public function clearMenuRoles(int $menuId): bool
|
|
{
|
|
return SystemRoleMenu::where('menu_id', $menuId)->delete();
|
|
}
|
|
|
|
/**
|
|
* 获取当前租户ID
|
|
*/
|
|
protected function getCurrentTenantId(): ?int
|
|
{
|
|
$user = auth('admin')->user();
|
|
return $user ? $user->tenant_id : null;
|
|
}
|
|
}
|