diff --git a/application/admin/library/traits/Backend.php b/application/admin/library/traits/Backend.php index 37b730855768e950cd1f8409ca1ac483432477e4..997d0b9bb835c7c1b4209849e29aba49205e035f 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() { @@ -56,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); } @@ -65,6 +74,9 @@ trait Backend /** * 回收站 + * + * @return string|Json + * @throws \think\Exception */ public function recyclebin() { @@ -79,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); } @@ -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')); + $params = $this->request->post('row/a'); + 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()); + } + if (empty($result)) { + $this->error(__('No rows were inserted')); + } - $this->error(__('Parameter %s can not be empty', '')); + $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) { @@ -141,90 +159,97 @@ 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')); + $params = $this->request->post('row/a'); + 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()); } - $this->error(__('Parameter %s can not be empty', '')); + if (empty($result)) { + $this->error(__('No rows were updated')); + } + $this->success(); } - $this->view->assign("row", $row); + $this->view->assign('row', $row); return $this->view->fetch(); } /** * 删除 + * + * @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")); + $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(); + $ids = $ids ?: $this->request->post('ids'); + 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()); + } + if (0 === $count) { + $this->error(__('No rows were deleted')); } - $this->error(__('Parameter %s can not be empty', 'ids')); + $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")); + $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 { @@ -237,23 +262,24 @@ 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(); } /** * 还原 + * + * @param int|string|array $ids + * @return void */ - public function restore($ids = "") + 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)) { @@ -274,59 +300,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")); + $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')); - } + $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); + $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) { @@ -344,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");