更新完善字典相关功能
This commit is contained in:
@@ -140,6 +140,16 @@ public function getItemsList(Request $request)
|
||||
]);
|
||||
}
|
||||
|
||||
public function getAllItems()
|
||||
{
|
||||
$items = $this->dictionaryService->getAllItems();
|
||||
return response()->json([
|
||||
'code' => 200,
|
||||
'message' => 'success',
|
||||
'data' => $items
|
||||
]);
|
||||
}
|
||||
|
||||
public function storeItem(Request $request)
|
||||
{
|
||||
try {
|
||||
|
||||
@@ -25,6 +25,28 @@ public function index()
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有字典数据(包含字典项)
|
||||
* 用于前端登录后缓存所有字典数据
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
$dictionaries = $this->dictionaryService->getAll();
|
||||
|
||||
// 为每个字典添加 items 字段
|
||||
$result = array_map(function($dictionary) {
|
||||
$items = $this->dictionaryService->getItemsByCode($dictionary['code']);
|
||||
$dictionary['items'] = $items;
|
||||
return $dictionary;
|
||||
}, $dictionaries);
|
||||
|
||||
return response()->json([
|
||||
'code' => 200,
|
||||
'message' => 'success',
|
||||
'data' => $result
|
||||
]);
|
||||
}
|
||||
|
||||
public function getByCode(Request $request)
|
||||
{
|
||||
$code = $request->input('code');
|
||||
|
||||
@@ -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 ?: [],
|
||||
];
|
||||
|
||||
// 添加组件路径
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user