From 456256af118b7a8a00dbb4f4a2f358585a02d035 Mon Sep 17 00:00:00 2001 From: Henry Date: Sat, 2 Apr 2022 21:57:53 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= =?UTF-8?q?=EF=BC=8C=E9=80=9A=E8=BF=87=E6=8F=90=E5=89=8D=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E6=9D=A5=E9=81=BF=E5=85=8D=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84?= =?UTF-8?q?if=E5=88=86=E6=94=AF=E5=B5=8C=E5=A5=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/library/traits/Backend.php | 258 +++++++++++-------- 1 file changed, 144 insertions(+), 114 deletions(-) diff --git a/application/admin/library/traits/Backend.php b/application/admin/library/traits/Backend.php index 37b730855..b37f01af9 100755 --- a/application/admin/library/traits/Backend.php +++ b/application/admin/library/traits/Backend.php @@ -9,18 +9,24 @@ use PhpOffice\PhpSpreadsheet\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Reader\Xls; use PhpOffice\PhpSpreadsheet\Reader\Csv; use think\Db; +use think\db\exception\BindParamException; +use think\db\exception\DataNotFoundException; +use think\db\exception\ModelNotFoundException; +use think\exception\DbException; use think\exception\PDOException; use think\exception\ValidateException; +use think\response\Json; trait Backend { /** * 排除前台提交过来的字段 - * @param $params + * + * @param array $params * @return array */ - protected function preExcludeFields($params) + protected function preExcludeFields(array $params): array { if (is_array($this->excludeFields)) { foreach ($this->excludeFields as $field) { @@ -36,9 +42,12 @@ trait Backend return $params; } - /** * 查看 + * + * @return string|Json + * @throws DbException + * @throws \think\Exception */ public function index() { @@ -65,6 +74,9 @@ trait Backend /** * 回收站 + * + * @return string|Json + * @throws \think\Exception */ public function recyclebin() { @@ -88,47 +100,53 @@ trait Backend /** * 添加 + * + * @return string + * @throws \think\Exception */ - public function add() + public function add(): string { if ($this->request->isPost()) { $params = $this->request->post("row/a"); - if ($params) { - $params = $this->preExcludeFields($params); - - if ($this->dataLimit && $this->dataLimitFieldAutoFill) { - $params[$this->dataLimitField] = $this->auth->id; - } - $result = false; - Db::startTrans(); - try { - //是否采用模型验证 - if ($this->modelValidate) { - $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); - $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; - $this->model->validateFailException(true)->validate($validate); - } - $result = $this->model->allowField(true)->save($params); - Db::commit(); - } catch (ValidateException|PDOException|Exception $e) { - Db::rollback(); - $this->error($e->getMessage()); - } - if ($result !== false) { - $this->success(); - } else { - $this->error(__('No rows were inserted')); + if (empty($params)) { + $this->error(__('Parameter %s can not be empty', '')); + } + $params = $this->preExcludeFields($params); + if ($this->dataLimit && $this->dataLimitFieldAutoFill) { + $params[$this->dataLimitField] = $this->auth->id; + } + Db::startTrans(); + try { + //是否采用模型验证 + if ($this->modelValidate) { + $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); + $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; + $this->model->validateFailException(true)->validate($validate); } + $result = $this->model->allowField(true)->save($params); + Db::commit(); + } catch (ValidateException|PDOException|Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); } - $this->error(__('Parameter %s can not be empty', '')); + if (empty($result)) { + $this->error(__('No rows were inserted')); + + } + $this->success(); } return $this->view->fetch(); } /** * 编辑 + * + * @param $ids + * @return string + * @throws DbException + * @throws \think\Exception */ - public function edit($ids = null) + public function edit($ids = null): string { $row = $this->model->get($ids); if (!$row) { @@ -142,30 +160,28 @@ trait Backend } if ($this->request->isPost()) { $params = $this->request->post("row/a"); - if ($params) { - $params = $this->preExcludeFields($params); - $result = false; - Db::startTrans(); - try { - //是否采用模型验证 - if ($this->modelValidate) { - $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); - $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; - $row->validateFailException(true)->validate($validate); - } - $result = $row->allowField(true)->save($params); - Db::commit(); - } catch (ValidateException|PDOException|Exception $e) { - Db::rollback(); - $this->error($e->getMessage()); - } - if ($result !== false) { - $this->success(); - } else { - $this->error(__('No rows were updated')); + if (empty($params)) { + $this->error(__('Parameter %s can not be empty', '')); + } + $params = $this->preExcludeFields($params); + Db::startTrans(); + try { + //是否采用模型验证 + if ($this->modelValidate) { + $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); + $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; + $row->validateFailException(true)->validate($validate); } + $result = $row->allowField(true)->save($params); + Db::commit(); + } catch (ValidateException|PDOException|Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); + } + if (empty($result)) { + $this->error(__('No rows were updated')); } - $this->error(__('Parameter %s can not be empty', '')); + $this->success(); } $this->view->assign("row", $row); return $this->view->fetch(); @@ -173,45 +189,53 @@ trait Backend /** * 删除 + * + * @param int|string|array $ids + * @return void + * @throws DbException + * @throws DataNotFoundException + * @throws ModelNotFoundException */ - public function del($ids = "") + public function del($ids = ''): void { if (!$this->request->isPost()) { $this->error(__("Invalid parameters")); } $ids = $ids ? $ids : $this->request->post("ids"); - if ($ids) { - $pk = $this->model->getPk(); - $adminIds = $this->getDataLimitAdminIds(); - if (is_array($adminIds)) { - $this->model->where($this->dataLimitField, 'in', $adminIds); - } - $list = $this->model->where($pk, 'in', $ids)->select(); + if (empty($ids)) { + $this->error(__('Parameter %s can not be empty', 'ids')); + } + $pk = $this->model->getPk(); + $adminIds = $this->getDataLimitAdminIds(); + if (is_array($adminIds)) { + $this->model->where($this->dataLimitField, 'in', $adminIds); + } + $list = $this->model->where($pk, 'in', $ids)->select(); - $count = 0; - Db::startTrans(); - try { - foreach ($list as $k => $v) { - $count += $v->delete(); - } - Db::commit(); - } catch (PDOException|Exception $e) { - Db::rollback(); - $this->error($e->getMessage()); - } - if ($count) { - $this->success(); - } else { - $this->error(__('No rows were deleted')); + $count = 0; + Db::startTrans(); + try { + foreach ($list as $k => $v) { + $count += $v->delete(); } + Db::commit(); + } catch (PDOException|Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); } - $this->error(__('Parameter %s can not be empty', 'ids')); + if (0 === $count) { + $this->error(__('No rows were deleted')); + } + $this->success(); } /** * 真实删除 + * + * @param int|string|array $ids + * @return void */ - public function destroy($ids = "") + public function destroy($ids = ''): void { if (!$this->request->isPost()) { $this->error(__("Invalid parameters")); @@ -247,8 +271,11 @@ trait Backend /** * 还原 + * + * @param int|string|array $ids + * @return void */ - public function restore($ids = "") + public function restore($ids = ''): void { if (!$this->request->isPost()) { $this->error(__("Invalid parameters")); @@ -274,59 +301,62 @@ trait Backend Db::rollback(); $this->error($e->getMessage()); } - if ($count) { - $this->success(); + if (0 === $count) { + $this->error(__('No rows were updated')); } - $this->error(__('No rows were updated')); + $this->success(); } /** * 批量更新 + * + * @param int|string|array $ids + * @return void */ - public function multi($ids = "") + public function multi($ids = ''): void { if (!$this->request->isPost()) { $this->error(__("Invalid parameters")); } $ids = $ids ? $ids : $this->request->post("ids"); - if ($ids) { - if ($this->request->has('params')) { - parse_str($this->request->post("params"), $values); - $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields))); - if ($values) { - $adminIds = $this->getDataLimitAdminIds(); - if (is_array($adminIds)) { - $this->model->where($this->dataLimitField, 'in', $adminIds); - } - $count = 0; - Db::startTrans(); - try { - $list = $this->model->where($this->model->getPk(), 'in', $ids)->select(); - foreach ($list as $index => $item) { - $count += $item->allowField(true)->isUpdate(true)->save($values); - } - Db::commit(); - } catch (PDOException|Exception $e) { - Db::rollback(); - $this->error($e->getMessage()); - } - if ($count) { - $this->success(); - } else { - $this->error(__('No rows were updated')); - } - } else { - $this->error(__('You have no permission')); - } + if (empty($ids) && false === $this->request->has('params')) { + $this->error(__('Parameter %s can not be empty', 'ids')); + } + parse_str($this->request->post("params"), $values); + $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields))); + if (empty($values)) { + $this->error(__('You have no permission')); + } + $adminIds = $this->getDataLimitAdminIds(); + if (is_array($adminIds)) { + $this->model->where($this->dataLimitField, 'in', $adminIds); + } + $count = 0; + Db::startTrans(); + try { + $list = $this->model->where($this->model->getPk(), 'in', $ids)->select(); + foreach ($list as $index => $item) { + $count += $item->allowField(true)->isUpdate(true)->save($values); } + Db::commit(); + } catch (PDOException|Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); } - $this->error(__('Parameter %s can not be empty', 'ids')); + if (0 === $count) { + $this->error(__('No rows were updated')); + } + $this->success(); } /** * 导入 + * + * @return void + * @throws PDOException + * @throws BindParamException */ - protected function import() + protected function import(): void { $file = $this->request->request('file'); if (!$file) { -- Gitee From ed103bff985dba4ae2f785a2df0e2366e58022fe Mon Sep 17 00:00:00 2001 From: Henry Date: Sat, 2 Apr 2022 22:03:50 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=A7=A3=E5=86=B3destroy=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=9C=AA=E5=88=A4=E6=96=AD=EF=BC=8C=E5=B9=B6=E4=BD=BF=E7=94=A8?= =?UTF-8?q?PHP7=E8=AF=AD=E6=B3=95=E4=BC=98=E5=8C=96Backend=E7=B1=BB?= =?UTF-8?q?=E7=9A=84=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/library/traits/Backend.php | 47 ++++++++++---------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/application/admin/library/traits/Backend.php b/application/admin/library/traits/Backend.php index b37f01af9..997d0b9bb 100755 --- a/application/admin/library/traits/Backend.php +++ b/application/admin/library/traits/Backend.php @@ -65,7 +65,7 @@ trait Backend ->order($sort, $order) ->paginate($limit); - $result = array("total" => $list->total(), "rows" => $list->items()); + $result = ['total' => $list->total(), 'rows' => $list->items()]; return json($result); } @@ -91,7 +91,7 @@ trait Backend ->order($sort, $order) ->paginate($limit); - $result = array("total" => $list->total(), "rows" => $list->items()); + $result = ['total' => $list->total(), 'rows' => $list->items()]; return json($result); } @@ -107,7 +107,7 @@ trait Backend public function add(): string { if ($this->request->isPost()) { - $params = $this->request->post("row/a"); + $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } @@ -119,7 +119,7 @@ trait Backend try { //是否采用模型验证 if ($this->modelValidate) { - $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); + $name = str_replace('\\model\\', '\\validate\\', get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; $this->model->validateFailException(true)->validate($validate); } @@ -159,7 +159,7 @@ trait Backend } } if ($this->request->isPost()) { - $params = $this->request->post("row/a"); + $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } @@ -168,7 +168,7 @@ trait Backend try { //是否采用模型验证 if ($this->modelValidate) { - $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); + $name = str_replace('\\model\\', '\\validate\\', get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; $row->validateFailException(true)->validate($validate); } @@ -183,7 +183,7 @@ trait Backend } $this->success(); } - $this->view->assign("row", $row); + $this->view->assign('row', $row); return $this->view->fetch(); } @@ -199,9 +199,9 @@ trait Backend public function del($ids = ''): void { if (!$this->request->isPost()) { - $this->error(__("Invalid parameters")); + $this->error(__('Invalid parameters')); } - $ids = $ids ? $ids : $this->request->post("ids"); + $ids = $ids ?: $this->request->post('ids'); if (empty($ids)) { $this->error(__('Parameter %s can not be empty', 'ids')); } @@ -238,17 +238,18 @@ trait Backend public function destroy($ids = ''): void { if (!$this->request->isPost()) { - $this->error(__("Invalid parameters")); + $this->error(__('Invalid parameters')); + } + $ids = $ids ?: $this->request->post('ids'); + if (empty($ids)) { + $this->error(__('Parameter %s can not be empty', 'ids')); } - $ids = $ids ? $ids : $this->request->post("ids"); $pk = $this->model->getPk(); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { $this->model->where($this->dataLimitField, 'in', $adminIds); } - if ($ids) { - $this->model->where($pk, 'in', $ids); - } + $this->model->where($pk, 'in', $ids); $count = 0; Db::startTrans(); try { @@ -261,12 +262,10 @@ trait Backend Db::rollback(); $this->error($e->getMessage()); } - if ($count) { - $this->success(); - } else { + if (0 === $count) { $this->error(__('No rows were deleted')); } - $this->error(__('Parameter %s can not be empty', 'ids')); + $this->success(); } /** @@ -278,9 +277,9 @@ trait Backend public function restore($ids = ''): void { if (!$this->request->isPost()) { - $this->error(__("Invalid parameters")); + $this->error(__('Invalid parameters')); } - $ids = $ids ? $ids : $this->request->post("ids"); + $ids = $ids ?: $this->request->post('ids'); $pk = $this->model->getPk(); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { @@ -316,13 +315,13 @@ trait Backend public function multi($ids = ''): void { if (!$this->request->isPost()) { - $this->error(__("Invalid parameters")); + $this->error(__('Invalid parameters')); } - $ids = $ids ? $ids : $this->request->post("ids"); + $ids = $ids ?: $this->request->post('ids'); if (empty($ids) && false === $this->request->has('params')) { $this->error(__('Parameter %s can not be empty', 'ids')); } - parse_str($this->request->post("params"), $values); + parse_str($this->request->post('params'), $values); $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields))); if (empty($values)) { $this->error(__('You have no permission')); @@ -374,7 +373,7 @@ trait Backend if ($ext === 'csv') { $file = fopen($filePath, 'r'); $filePath = tempnam(sys_get_temp_dir(), 'import_csv'); - $fp = fopen($filePath, "w"); + $fp = fopen($filePath, 'w'); $n = 0; while ($line = fgets($file)) { $line = rtrim($line, "\n\r\0"); -- Gitee