refactor:优化管理员管理和菜单规则管理的控制器代码

This commit is contained in:
妙码生花 2024-04-29 19:53:16 +08:00
parent 5a8cd3422c
commit 9e784a5d72
3 changed files with 12 additions and 49 deletions

View File

@ -221,14 +221,17 @@ class Admin extends Backend
$this->error(__('Parameter error'));
}
$where = [];
$dataLimitAdminIds = $this->getDataLimitAdminIds();
if ($dataLimitAdminIds) {
$this->model->where($this->dataLimitField, 'in', $dataLimitAdminIds);
$where[] = [$this->dataLimitField, 'in', $dataLimitAdminIds];
}
$pk = $this->model->getPk();
$data = $this->model->where($pk, 'in', $ids)->select();
$pk = $this->model->getPk();
$where[] = [$pk, 'in', $ids];
$count = 0;
$data = $this->model->where($where)->select();
$this->model->startTrans();
try {
foreach ($data as $v) {

View File

@ -207,35 +207,15 @@ class Rule extends Backend
$this->error(__('Parameter error'));
}
$dataLimitAdminIds = $this->getDataLimitAdminIds();
if ($dataLimitAdminIds) {
$this->model->where($this->dataLimitField, 'in', $dataLimitAdminIds);
}
$pk = $this->model->getPk();
$data = $this->model->where($pk, 'in', $ids)->select();
// 子级元素检查
$subData = $this->model->where('pid', 'in', $ids)->column('pid', 'id');
foreach ($subData as $key => $subDatum) {
if (!in_array($key, $ids)) {
$this->error(__('Please delete the child element first, or use batch deletion'));
}
}
$count = 0;
$this->model->startTrans();
try {
foreach ($data as $v) {
$count += $v->delete();
}
$this->model->commit();
} catch (Throwable $e) {
$this->model->rollback();
$this->error($e->getMessage());
}
if ($count) {
$this->success(__('Deleted successfully'));
} else {
$this->error(__('No rows were deleted'));
}
parent::del($ids);
}
/**

View File

@ -203,35 +203,15 @@ class Rule extends Backend
$this->error(__('Parameter error'));
}
$dataLimitAdminIds = $this->getDataLimitAdminIds();
if ($dataLimitAdminIds) {
$this->model->where($this->dataLimitField, 'in', $dataLimitAdminIds);
}
$pk = $this->model->getPk();
$data = $this->model->where($pk, 'in', $ids)->select();
// 子级元素检查
$subData = $this->model->where('pid', 'in', $ids)->column('pid', 'id');
foreach ($subData as $key => $subDatum) {
if (!in_array($key, $ids)) {
$this->error(__('Please delete the child element first, or use batch deletion'));
}
}
$count = 0;
$this->model->startTrans();
try {
foreach ($data as $v) {
$count += $v->delete();
}
$this->model->commit();
} catch (Throwable $e) {
$this->model->rollback();
$this->error($e->getMessage());
}
if ($count) {
$this->success(__('Deleted successfully'));
} else {
$this->error(__('No rows were deleted'));
}
parent::del($ids);
}
/**