Skip to content

Commit 2777a07

Browse files
committed
refactor:优化多个后台控制器细节
1 parent 7c7d7ef commit 2777a07

File tree

10 files changed

+41
-30
lines changed

10 files changed

+41
-30
lines changed

app/admin/controller/auth/Admin.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,10 @@ public function add(): void
126126
* 编辑
127127
* @throws Throwable
128128
*/
129-
public function edit($id = null): void
129+
public function edit(): void
130130
{
131+
$pk = $this->model->getPk();
132+
$id = $this->request->param($pk);
131133
$row = $this->model->find($id);
132134
if (!$row) {
133135
$this->error(__('Record not found'));
@@ -253,7 +255,7 @@ public function del(): void
253255
* 检查分组权限
254256
* @throws Throwable
255257
*/
256-
public function checkGroupAuth(array $groups): void
258+
private function checkGroupAuth(array $groups): void
257259
{
258260
if ($this->auth->isSuperAdmin()) {
259261
return;

app/admin/controller/auth/Group.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ public function add(): void
132132

133133
/**
134134
* 编辑
135-
* @param string|null $id
136-
* @return void
137135
* @throws Throwable
138136
*/
139-
public function edit(string $id = null): void
137+
public function edit(): void
140138
{
139+
$pk = $this->model->getPk();
140+
$id = $this->request->param($pk);
141141
$row = $this->model->find($id);
142142
if (!$row) {
143143
$this->error(__('Record not found'));
@@ -187,7 +187,8 @@ public function edit(string $id = null): void
187187
$pidArr = AdminRule::field('pid')
188188
->distinct(true)
189189
->where('id', 'in', $row->rules)
190-
->select()->toArray();
190+
->select()
191+
->toArray();
191192
$rules = $row->rules ? explode(',', $row->rules) : [];
192193
foreach ($pidArr as $item) {
193194
$ruKey = array_search($item['pid'], $rules);
@@ -261,7 +262,7 @@ public function select(): void
261262
* 权限节点入库前处理
262263
* @throws Throwable
263264
*/
264-
public function handleRules(array &$data): array
265+
private function handleRules(array &$data): array
265266
{
266267
if (!empty($data['rules']) && is_array($data['rules'])) {
267268
$rules = AdminRule::select();
@@ -293,7 +294,7 @@ public function handleRules(array &$data): array
293294
* @return array
294295
* @throws Throwable
295296
*/
296-
public function getGroups(array $where = []): array
297+
private function getGroups(array $where = []): array
297298
{
298299
$pk = $this->model->getPk();
299300
$initKey = $this->request->get("initKey/s", $pk);
@@ -347,7 +348,7 @@ public function getGroups(array $where = []): array
347348
* @return void
348349
* @throws Throwable
349350
*/
350-
public function checkAuth($groupId): void
351+
private function checkAuth($groupId): void
351352
{
352353
$authGroups = $this->auth->getAllAuthGroups($this->authMethod);
353354
if (!$this->auth->isSuperAdmin() && !in_array($groupId, $authGroups)) {

app/admin/controller/routine/AdminInfo.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ public function index(): void
3333
]);
3434
}
3535

36-
public function edit($id = null): void
36+
public function edit(): void
3737
{
38+
$pk = $this->model->getPk();
39+
$id = $this->request->param($pk);
3840
$row = $this->model->find($id);
3941
if (!$row) {
4042
$this->error(__('Record not found'));
@@ -46,7 +48,7 @@ public function edit($id = null): void
4648
$this->error(__('Parameter %s can not be empty', ['']));
4749
}
4850

49-
if (isset($data['avatar']) && $data['avatar']) {
51+
if (!empty($data['avatar'])) {
5052
$row->avatar = $data['avatar'];
5153
if ($row->save()) {
5254
$this->success(__('Avatar modified successfully!'));
@@ -64,7 +66,7 @@ public function edit($id = null): void
6466
}
6567
}
6668

67-
if (isset($data['password']) && $data['password']) {
69+
if (!empty($data['password'])) {
6870
$this->model->resetPassword($this->auth->id, $data['password']);
6971
}
7072

app/admin/controller/security/DataRecycle.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ public function add(): void
7474

7575
/**
7676
* 编辑
77-
* @param int|string|null $id
7877
* @throws Throwable
7978
*/
80-
public function edit(int|string $id = null): void
79+
public function edit(): void
8180
{
81+
$pk = $this->model->getPk();
82+
$id = $this->request->param($pk);
8283
$row = $this->model->find($id);
8384
if (!$row) {
8485
$this->error(__('Record not found'));

app/admin/controller/security/DataRecycleLog.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public function initialize(): void
3131

3232
/**
3333
* 还原
34-
* @param array|null $ids
3534
* @throws Throwable
3635
*/
37-
public function restore(array $ids = null): void
36+
public function restore(): void
3837
{
38+
$ids = $this->request->param('ids/a', []);
3939
$data = $this->model->where('id', 'in', $ids)->select();
4040
if (!$data) {
4141
$this->error(__('Record not found'));
@@ -66,11 +66,12 @@ public function restore(array $ids = null): void
6666

6767
/**
6868
* 详情
69-
* @param string|int|null $id
7069
* @throws Throwable
7170
*/
72-
public function info(string|int $id = null): void
71+
public function info(): void
7372
{
73+
$pk = $this->model->getPk();
74+
$id = $this->request->param($pk);
7475
$row = $this->model
7576
->withJoin($this->withJoinTable, $this->withJoinType)
7677
->where('data_recycle_log.id', $id)

app/admin/controller/security/SensitiveData.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,12 @@ public function add(): void
117117

118118
/**
119119
* 编辑重写
120-
* @param string|int|null $id
121120
* @throws Throwable
122121
*/
123-
public function edit(string|int $id = null): void
122+
public function edit(): void
124123
{
124+
$pk = $this->model->getPk();
125+
$id = $this->request->param($pk);
125126
$row = $this->model->find($id);
126127
if (!$row) {
127128
$this->error(__('Record not found'));

app/admin/controller/security/SensitiveDataLog.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@ public function index(): void
6060

6161
/**
6262
* 详情
63-
* @param string|int|null $id
6463
* @throws Throwable
6564
*/
66-
public function info(string|int $id = null): void
65+
public function info(): void
6766
{
67+
$pk = $this->model->getPk();
68+
$id = $this->request->param($pk);
6869
$row = $this->model
6970
->withJoin($this->withJoinTable, $this->withJoinType)
7071
->where('sensitive_data_log.id', $id)
@@ -80,11 +81,11 @@ public function info(string|int $id = null): void
8081

8182
/**
8283
* 回滚
83-
* @param array|null $ids
8484
* @throws Throwable
8585
*/
86-
public function rollback(array $ids = null): void
86+
public function rollback(): void
8787
{
88+
$ids = $this->request->param('ids/a', []);
8889
$data = $this->model->where('id', 'in', $ids)->select();
8990
if (!$data) {
9091
$this->error(__('Record not found'));

app/admin/controller/user/Group.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,12 @@ public function add(): void
7070

7171
/**
7272
* 编辑
73-
* @param string|int|null $id
7473
* @throws Throwable
7574
*/
76-
public function edit(string|int $id = null): void
75+
public function edit(): void
7776
{
77+
$pk = $this->model->getPk();
78+
$id = $this->request->param($pk);
7879
$row = $this->model->find($id);
7980
if (!$row) {
8081
$this->error(__('Record not found'));
@@ -138,7 +139,7 @@ public function edit(string|int $id = null): void
138139
* @return array
139140
* @throws Throwable
140141
*/
141-
public function handleRules(array &$data): array
142+
private function handleRules(array &$data): array
142143
{
143144
if (is_array($data['rules']) && $data['rules']) {
144145
$rules = UserRule::select();

app/admin/controller/user/Rule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public function select(): void
231231
* 获取菜单规则
232232
* @throws Throwable
233233
*/
234-
public function getRules(array $where = []): array
234+
private function getRules(array $where = []): array
235235
{
236236
$pk = $this->model->getPk();
237237
$initKey = $this->request->get("initKey/s", $pk);

app/admin/controller/user/User.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ public function add(): void
102102

103103
/**
104104
* 编辑
105-
* @param string|int|null $id
106105
* @throws Throwable
107106
*/
108-
public function edit(string|int $id = null): void
107+
public function edit(): void
109108
{
109+
$pk = $this->model->getPk();
110+
$id = $this->request->param($pk);
110111
$row = $this->model->find($id);
111112
if (!$row) {
112113
$this->error(__('Record not found'));

0 commit comments

Comments
 (0)