Skip to content

Commit d431f1a

Browse files
zds-sShaBaoFa
andauthored
Release v2.0.2 (#304)
* Add App Store .gitignore Entry and Include install.lock in Repository * fix [BUG] 后台菜单的level字段出错 #244 * Apply cs-fix --------- Co-authored-by: wlfpanda <[email protected]>
1 parent d129ec7 commit d431f1a

File tree

6 files changed

+56
-51
lines changed

6 files changed

+56
-51
lines changed

CHANGELOG-2.0.md

-28
This file was deleted.

CHANGELOG-2.0.zh_CN.md

-22
This file was deleted.

app/System/Mapper/SystemMenuMapper.php

+28
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,21 @@ public function update(mixed $id, array $data): bool
158158
return parent::update($id, $data);
159159
}
160160

161+
/**
162+
* 批量更新菜单.
163+
*/
164+
#[DeleteCache('loginInfo:*'), Transaction]
165+
public function batchUpdate(array $update): bool
166+
{
167+
foreach ($update as $item) {
168+
$result = parent::update($item['id'], $item['data']);
169+
if (! $result) {
170+
return false;
171+
}
172+
}
173+
return true;
174+
}
175+
161176
/**
162177
* 逻辑删除菜单.
163178
*/
@@ -189,6 +204,15 @@ public function checkChildrenExists(int $id): bool
189204
return $this->model::withTrashed()->where('parent_id', $id)->exists();
190205
}
191206

207+
/**
208+
* 获取子孙menus.
209+
*/
210+
public function getDescendantsMenus(int $parentId): array
211+
{
212+
$params = ['level' => $parentId];
213+
return $this->handleSearch($this->model::query(), $params)->get()->toArray();
214+
}
215+
192216
/**
193217
* 搜索处理器.
194218
*/
@@ -198,6 +222,10 @@ public function handleSearch(Builder $query, array $params): Builder
198222
$query->where('status', $params['status']);
199223
}
200224

225+
if (isset($params['level']) && filled($params['level'])) {
226+
$query->where('level', 'like', '%' . $params['level'] . '%');
227+
}
228+
201229
if (isset($params['name']) && filled($params['name'])) {
202230
$query->where('name', 'like', '%' . $params['name'] . '%');
203231
}

app/System/Service/SystemMenuService.php

+26-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,23 @@ public function genButtonMenu(SystemMenu $model): bool
121121
*/
122122
public function update(mixed $id, array $data): bool
123123
{
124-
return $this->mapper->update($id, $this->handleData($data));
124+
$handleData = $this->handleData($data);
125+
if (! $this->checkChildrenExists($id)) {
126+
return $this->mapper->update($id, $handleData);
127+
}
128+
$update[] = [
129+
'id' => $id,
130+
'data' => $handleData,
131+
];
132+
$descendants = $this->mapper->getDescendantsMenus((int) $id);
133+
foreach ($descendants as $descendant) {
134+
$handleDescendantMenuLevelData = $this->handleDescendantMenuLevels($descendant['level'], $handleData['level'], $id);
135+
$update[] = [
136+
'id' => $descendant['id'],
137+
'data' => ['level' => $handleDescendantMenuLevelData],
138+
];
139+
}
140+
return $this->mapper->batchUpdate($update);
125141
}
126142

127143
/**
@@ -166,4 +182,13 @@ protected function handleData(array $data): array
166182
}
167183
return $data;
168184
}
185+
186+
protected function handleDescendantMenuLevels(string $descendantLevel, string $handleDataLevel, int $id): string
187+
{
188+
$descendantLevelArr = explode(',', $descendantLevel);
189+
$handleDataLevelArr = explode(',', $handleDataLevel);
190+
$position = array_search($id, $descendantLevelArr);
191+
array_splice($descendantLevelArr, 0, $position, $handleDataLevelArr);
192+
return implode(',', $descendantLevelArr);
193+
}
169194
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!install.lock
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1

0 commit comments

Comments
 (0)