From 9e784a5d72494c32dba3aebd224406d66c52d04f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=99=E7=A0=81=E7=94=9F=E8=8A=B1?= <18523774412@qq.com> Date: Mon, 29 Apr 2024 19:53:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=E4=BC=98=E5=8C=96=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=91=98=E7=AE=A1=E7=90=86=E5=92=8C=E8=8F=9C=E5=8D=95=E8=A7=84?= =?UTF-8?q?=E5=88=99=E7=AE=A1=E7=90=86=E7=9A=84=E6=8E=A7=E5=88=B6=E5=99=A8?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/auth/Admin.php | 9 ++++++--- app/admin/controller/auth/Rule.php | 26 +++----------------------- app/admin/controller/user/Rule.php | 26 +++----------------------- 3 files changed, 12 insertions(+), 49 deletions(-) diff --git a/app/admin/controller/auth/Admin.php b/app/admin/controller/auth/Admin.php index 2c49d263..c7fea36c 100644 --- a/app/admin/controller/auth/Admin.php +++ b/app/admin/controller/auth/Admin.php @@ -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) { diff --git a/app/admin/controller/auth/Rule.php b/app/admin/controller/auth/Rule.php index e697d350..a81433bc 100644 --- a/app/admin/controller/auth/Rule.php +++ b/app/admin/controller/auth/Rule.php @@ -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); } /** diff --git a/app/admin/controller/user/Rule.php b/app/admin/controller/user/Rule.php index ebc52f5c..3942ecfd 100644 --- a/app/admin/controller/user/Rule.php +++ b/app/admin/controller/user/Rule.php @@ -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); } /**