AI代码生成工具

This commit is contained in:
李琦
2026-08-10 15:51:00 +08:00
parent d0018a0173
commit bd226c260f
31 changed files with 5468 additions and 31 deletions

View File

@@ -9,7 +9,7 @@ use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
* 角色服务类
* 菜单服务:列表树、搜索(含子菜单)、下拉
*/
class MenuService extends BaseService
{
@@ -29,29 +29,122 @@ class MenuService extends BaseService
}
/**
* 获取列表
* 获取菜单列表(扁平树,前端 transform
* title 搜索时:命中节点 + 祖先 + 子孙,保证子菜单可搜到并完整展示路径
* 无搜索时:仍按根菜单分页,再挂两级子节点
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function list()
{
// /order/product-order
$title = trim((string) request()->get('title', ''));
if ($title !== '') {
return $this->listByTitleSearch($title);
}
$this->where[] = ['pid', '=', 0];
$list = $this->getPageList();
$ids = array_column($list['items'], 'id');
$items = $this->model::whereIn('pid', $ids)->get($this->selectField);
if (empty($ids)) {
return $list;
}
$items = $this->model::where('deleted_at', 0)->whereIn('pid', $ids)->orderBy('sort')->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;
if (!empty($childrenIds)) {
$items = $this->model::where('deleted_at', 0)->whereIn('pid', $childrenIds)->orderBy('sort')->get($this->selectField);
foreach ($items as $v) {
$list['items'][] = $v;
}
}
return $list;
}
/**
* 按标题模糊搜索:返回命中项及其祖先、子孙,供树表正确拼装
* 为什么要带祖先:仅返回子节点时 pid 找不到父级,树会丢节点或挂到根外
*/
private function listByTitleSearch(string $title): array
{
$all = $this->model::where('deleted_at', 0)
->orderBy('sort')
->get($this->selectField)
->keyBy('id');
if ($all->isEmpty()) {
return $this->emptyPageList();
}
$matchedIds = [];
foreach ($all as $id => $row) {
$rowTitle = (string) ($row['title'] ?? '');
if ($rowTitle !== '' && mb_stripos($rowTitle, $title) !== false) {
$matchedIds[] = (int) $id;
}
}
if (empty($matchedIds)) {
return $this->emptyPageList();
}
// 子节点按 pid 分组,便于向下展开子孙
$childrenMap = [];
foreach ($all as $id => $row) {
$pid = (int) ($row['pid'] ?? 0);
$childrenMap[$pid][] = (int) $id;
}
$needIds = [];
foreach ($matchedIds as $mid) {
// 向上:自身 + 祖先
$cur = $mid;
while ($cur > 0 && $all->has($cur) && !isset($needIds[$cur])) {
$needIds[$cur] = true;
$cur = (int) ($all[$cur]['pid'] ?? 0);
}
// 向下:命中节点的全部子孙(搜父级时仍能看到子树)
$stack = [$mid];
while (!empty($stack)) {
$nodeId = array_pop($stack);
foreach ($childrenMap[$nodeId] ?? [] as $cid) {
if (!isset($needIds[$cid])) {
$needIds[$cid] = true;
$stack[] = $cid;
}
}
}
}
$items = [];
foreach ($all as $id => $row) {
if (isset($needIds[(int) $id])) {
$items[] = $row->toArray();
}
}
return [
'items' => $items,
'page' => 1,
'size' => count($items),
'total' => count($items),
];
}
/**
* 空分页结构(与 getPageList 对齐)
*/
private function emptyPageList(): array
{
return [
'items' => [],
'page' => 1,
'size' => 0,
'total' => 0,
];
}
/**
* 获取树形结构的菜单下拉
* @param bool $isSelect
@@ -119,7 +212,6 @@ class MenuService extends BaseService
* @param $id
* @param $params
* @return mixed
* @throws Exception
*/
public function update($id, $params): mixed
{
@@ -131,15 +223,11 @@ class MenuService extends BaseService
/**
* 删除数据
* @param $id
* @return mixed|true
* @throws Exception
* @param $ids
* @return mixed
*/
public function delete($id): mixed
public function delete($ids): mixed
{
if (in_array($id, [1, 2])) {
$this->utils->errorThrow('管理员角色禁止删除');
}
return $this->del($id);
return $this->del($ids);
}
}