更新完善字典相关功能

This commit is contained in:
2026-02-18 17:15:33 +08:00
parent 5450777bd7
commit 378b9bd71f
23 changed files with 1657 additions and 572 deletions
+1 -1
View File
@@ -210,7 +210,7 @@ private function buildMenuTree($permissions, $parentId = 0): array
'path' => $permission->path,
'name' => $permission->name,
'title' => $permission->title,
'meta' => $permission->meta ? json_decode($permission->meta, true) : [],
'meta' => $permission->meta ?: [],
];
// 添加组件路径
+44 -7
View File
@@ -43,6 +43,7 @@ public function getAll(): array
if ($dictionaries === null) {
$dictionaries = Dictionary::where('status', true)
->with(['activeItems'])
->orderBy('sort')
->get()
->toArray();
@@ -52,7 +53,7 @@ public function getAll(): array
return $dictionaries;
}
public function getById(int $id): ?Dictionary
public function getById(int $id): ?array
{
$cacheKey = 'system:dictionaries:' . $id;
$dictionary = Cache::get($cacheKey);
@@ -60,22 +61,26 @@ public function getById(int $id): ?Dictionary
if ($dictionary === null) {
$dictionary = Dictionary::with('items')->find($id);
if ($dictionary) {
Cache::put($cacheKey, $dictionary->toArray(), 3600);
$dictionary = $dictionary->toArray();
Cache::put($cacheKey, $dictionary, 3600);
}
}
return $dictionary ? $dictionary : null;
return $dictionary ?? null;
}
public function getByCode(string $code): ?Dictionary
public function getByCode(string $code): ?array
{
$cacheKey = 'system:dictionaries:code:' . $code;
$dictionary = Cache::get($cacheKey);
if ($dictionary === null) {
$dictionary = Dictionary::where('code', $code)->first();
if ($dictionary) {
Cache::put($cacheKey, $dictionary->toArray(), 3600);
$dictionaryModel = Dictionary::where('code', $code)->first();
if ($dictionaryModel) {
$dictionary = $dictionaryModel->toArray();
Cache::put($cacheKey, $dictionary, 3600);
} else {
$dictionary = null;
}
}
@@ -269,4 +274,36 @@ public function batchUpdateItemsStatus(array $ids, bool $status): bool
return true;
}
/**
* 获取所有字典项(按字典分类)
* @return array 按字典code分类的字典项数据
*/
public function getAllItems(): array
{
$cacheKey = 'system:dictionary-items:all';
$allItems = Cache::get($cacheKey);
if ($allItems === null) {
// 获取所有启用的字典
$dictionaries = Dictionary::where('status', true)
->orderBy('sort')
->get();
$result = [];
foreach ($dictionaries as $dictionary) {
$result[] = [
'code' => $dictionary->code,
'name' => $dictionary->name,
'description' => $dictionary->description,
'items' => $dictionary->activeItems->toArray()
];
}
$allItems = $result;
Cache::put($cacheKey, $allItems, 3600);
}
return $allItems;
}
}