Files
nl-mall-api/app/Service/MenuService.php
2025-05-12 16:13:46 +08:00

146 lines
4.0 KiB
PHP
Executable File

<?php
namespace App\Service;
use App\BaseApp\BaseService;
use App\Models\MenuModel;
use Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
* 角色服务类
*/
class MenuService extends BaseService
{
public function __construct()
{
parent::__construct();
$this->model = MenuModel::class;
$this->selectField = [
'id', 'pid', 'title', 'icon', 'name', 'affix_tab', 'path', 'component', 'redirect', 'keep_alive', 'hide_in_menu', 'badge', 'badge_type', 'badge_variants', 'iframe_src', 'sort', 'query', 'created_at'
];
$this->queryField = [ 'title' => 'like', 'pid' => '=' ];
$this->optionField = ['id', 'title'];
$this->orderBy = [
'name' => 'sort',
'sort' => 'asc'
];
}
/**
* 获取列表
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function list()
{
// /order/product-order
$this->where[] = ['pid', '=', 0];
$list = $this->getPageList();
$ids = array_column($list['items'], 'id');
$items = $this->model::whereIn('pid', $ids)->get($this->selectField);
$childrenIds = [];
foreach ($items as $v) {
$list['items'][] = $v;
$childrenIds[] = $v['id'];
}
$items = $this->model::whereIn('pid', $childrenIds)->get($this->selectField);
foreach ($items as $v) {
$list['items'][] = $v;
}
return $list;
}
/**
* 获取树形结构的菜单下拉
* @param bool $isSelect
* @return array
*/
public function getTreeOption(bool $isSelect = false): array
{
$this->optionField = ['id', 'title', 'icon', 'pid'];
$list = $this->model::get($this->optionField);
$result = $this->utils->tree($list);
if ($isSelect == 'true') {
array_unshift($result, [ 'id' => 0, 'title' => '根目录', 'pid' => 0, 'icon' => '' ]);
}
return $result;
}
/**
* 获取下拉列表
* @return mixed
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public function option(): mixed
{
$option = $this->getOption();
if (request()->get('is_select', false)) {
$result = [
[ 'id' => 0, 'title' => '根目录' ]
];
foreach ($option as $item) {
$result[] = [ 'id' => $item['id'], 'title' => $item['title'] ];
}
return $result;
}
return $option;
}
/**
* 获取数据详情
* @param $id
* @return mixed
* @throws Exception
*/
public function detail($id): mixed
{
return $this->getDetail($id);
}
/**
* 新增数据
* @param $params
* @return mixed
*/
public function create($params): mixed
{
$params['badge_type'] = !empty($params['badge_type']) ? $params['badge_type']: 0;
$params['badge_variants'] = !empty($params['badge_variants']) ? $params['badge_variants']: 0;
$params['created_at'] = time();
return $this->insert($params);
}
/**
* 编辑数据
* @param $id
* @param $params
* @return mixed
* @throws Exception
*/
public function update($id, $params): mixed
{
$params['badge_type'] = !empty($params['badge_type']) ? $params['badge_type']: 0;
$params['badge_variants'] = !empty($params['badge_variants']) ? $params['badge_variants']: 0;
$params['updated_at'] = time();
return $this->save($id, $params);
}
/**
* 删除数据
* @param $id
* @return mixed|true
* @throws Exception
*/
public function delete($id): mixed
{
if (in_array($id, [1, 2])) {
$this->utils->errorThrow('管理员角色禁止删除');
}
return $this->del($id);
}
}