study-api-v2/app/Services/System/SystemMenuService.php
hx a621de91ff feat(system): 添加系统菜单、角色菜单关联和用户角色关联功能- 新增系统菜单模型、控制器、请求验证和业务逻辑
- 新增角色菜单关联模型、控制器、请求验证和业务逻辑
- 新增用户角色关联模型、控制器、请求验证和业务逻辑
- 更新系统角色模型,增加与用户和菜单的关联
- 更新用户模型,增加与角色的关联和相关方法
- 在路由文件中添加新功能的路由定义
2025-07-15 01:14:15 +08:00

155 lines
4.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Services\System;
use App\Models\System\SystemMenu;
use App\Services\BaseService;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* 系统菜单服务类
*/
class SystemMenuService extends BaseService
{
protected string $modelClass = SystemMenu::class;
/**
* 获取系统菜单列表
*/
public function getList(array $params): LengthAwarePaginator
{
$query = SystemMenu::query();
// 搜索条件
if (!empty($params['keyword'])) {
$query->where(function ($q) use ($params) {
$q->where('name', 'like', '%' . $params['keyword'] . '%')
->orWhere('path', 'like', '%' . $params['keyword'] . '%')
->orWhere('component', 'like', '%' . $params['keyword'] . '%');
});
}
// 状态筛选
if (isset($params['status'])) {
$query->where('status', $params['status']);
}
// 菜单类型筛选
if (isset($params['type'])) {
$query->where('type', $params['type']);
}
// 父级菜单筛选
if (isset($params['parent_id'])) {
$query->where('parent_id', $params['parent_id']);
}
// 显示状态筛选
if (isset($params['visible'])) {
$query->where('visible', $params['visible']);
}
// 排序
$query->orderBy('sort', 'asc')
->orderBy('id', 'desc');
return $query->paginate($params['page_size'] ?? 15);
}
/**
* 获取简单列表(用于下拉选择等)
*/
public function getSimpleList(): array
{
return SystemMenu::select('id', 'name', 'parent_id')
->where('status', 1)
->orderBy('sort', 'asc')
->get()
->toArray();
}
/**
* 获取树形菜单结构
*/
public function getMenuTree(): array
{
$menus = SystemMenu::where('status', 1)
->orderBy('sort', 'asc')
->get()
->toArray();
return $this->buildTree($menus);
}
/**
* 获取父级菜单列表
*/
public function getParentMenus(): array
{
return SystemMenu::where('type', '!=', 3) // 排除按钮类型
->where('status', 1)
->select('id', 'name', 'parent_id')
->orderBy('sort', 'asc')
->get()
->toArray();
}
/**
* 构建树形结构
*/
private function buildTree(array $menus, int $parentId = 0): array
{
$tree = [];
foreach ($menus as $menu) {
if ($menu['parent_id'] == $parentId) {
$children = $this->buildTree($menus, $menu['id']);
if (!empty($children)) {
$menu['children'] = $children;
}
$tree[] = $menu;
}
}
return $tree;
}
/**
* 创建前的数据处理
*/
protected function beforeCreate(array &$data): void
{
// 如果没有设置排序,则设置为最大值+1
if (!isset($data['sort'])) {
$maxSort = SystemMenu::max('sort') ?? 0;
$data['sort'] = $maxSort + 1;
}
// 如果没有设置状态,默认为启用
if (!isset($data['status'])) {
$data['status'] = 1;
}
// 如果没有设置显示状态,默认为显示
if (!isset($data['visible'])) {
$data['visible'] = 0;
}
// 如果没有设置父级ID默认为0
if (!isset($data['parent_id'])) {
$data['parent_id'] = 0;
}
}
/**
* 更新前的数据处理
*/
protected function beforeUpdate(array &$data, $model): void
{
// 不能将自己设置为自己的父级
if (isset($data['parent_id']) && $data['parent_id'] == $model->id) {
throw new \App\Exceptions\BusinessException(
\App\Helpers\ResponseEnum::CLIENT_PARAMETER_ERROR,
'不能将自己设置为父级菜单'
);
}
}
}