diff --git a/application/admin/library/traits/Backend.php b/application/admin/library/traits/Backend.php index f81f62fee840c33d300fd432d4af641b5e5e9094..b91a5629adca30819679a38e2d64d4ce2fbcd617 100755 --- a/application/admin/library/traits/Backend.php +++ b/application/admin/library/traits/Backend.php @@ -9,12 +9,16 @@ 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 @@ -24,115 +28,114 @@ trait Backend { if (is_array($this->excludeFields)) { foreach ($this->excludeFields as $field) { - if (key_exists($field, $params)) { + if (array_key_exists($field, $params)) { unset($params[$field]); } } - } else { - if (key_exists($this->excludeFields, $params)) { - unset($params[$this->excludeFields]); - } + } else if (array_key_exists($this->excludeFields, $params)) { + unset($params[$this->excludeFields]); } return $params; } - /** * 查看 + * + * @return string|Json + * @throws \think\Exception + * @throws DbException */ public function index() { //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); - if ($this->request->isAjax()) { - //如果发送的来源是Selectpage,则转发到Selectpage - if ($this->request->request('keyField')) { - return $this->selectpage(); - } - list($where, $sort, $order, $offset, $limit) = $this->buildparams(); - - $list = $this->model - ->where($where) - ->order($sort, $order) - ->paginate($limit); - - $result = array("total" => $list->total(), "rows" => $list->items()); - - return json($result); + if (false === $this->request->isAjax()) { + return $this->view->fetch(); + } + //如果发送的来源是 Selectpage,则转发到 Selectpage + if ($this->request->request('keyField')) { + return $this->selectpage(); } - return $this->view->fetch(); + [$where, $sort, $order, $offset, $limit] = $this->buildparams(); + $list = $this->model + ->where($where) + ->order($sort, $order) + ->paginate($limit); + $result = ['total' => $list->total(), 'rows' => $list->items()]; + return json($result); } /** * 回收站 + * + * @return string|Json + * @throws \think\Exception */ public function recyclebin() { //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); - if ($this->request->isAjax()) { - list($where, $sort, $order, $offset, $limit) = $this->buildparams(); - - $list = $this->model - ->onlyTrashed() - ->where($where) - ->order($sort, $order) - ->paginate($limit); - - $result = array("total" => $list->total(), "rows" => $list->items()); - - return json($result); + if (false === $this->request->isAjax()) { + return $this->view->fetch(); } - return $this->view->fetch(); + [$where, $sort, $order, $offset, $limit] = $this->buildparams(); + $list = $this->model + ->onlyTrashed() + ->where($where) + ->order($sort, $order) + ->paginate($limit); + $result = ['total' => $list->total(), 'rows' => $list->items()]; + return json($result); } /** * 添加 + * + * @return string + * @throws \think\Exception */ public function add() { - if ($this->request->isPost()) { - $params = $this->request->post("row/a"); - if ($params) { - $params = $this->preExcludeFields($params); + if (false === $this->request->isPost()) { + return $this->view->fetch(); + } + $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; - } - $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 $e) { - Db::rollback(); - $this->error($e->getMessage()); - } catch (PDOException $e) { - Db::rollback(); - $this->error($e->getMessage()); - } catch (Exception $e) { - Db::rollback(); - $this->error($e->getMessage()); - } - if ($result !== false) { - $this->success(); - } else { - $this->error(__('No rows were inserted')); - } + 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()->validate($validate); } - $this->error(__('Parameter %s can not be empty', '')); + $result = $this->model->allowField(true)->save($params); + Db::commit(); + } catch (ValidateException|PDOException|Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); } - return $this->view->fetch(); + if ($result === false) { + $this->error(__('No rows were inserted')); + } + $this->success(); } /** * 编辑 + * + * @param $ids + * @return string + * @throws DbException + * @throws \think\Exception */ public function edit($ids = null) { @@ -141,137 +144,132 @@ trait Backend $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); - if (is_array($adminIds)) { - if (!in_array($row[$this->dataLimitField], $adminIds)) { - $this->error(__('You have no permission')); - } + if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) { + $this->error(__('You have no permission')); } - 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 $e) { - Db::rollback(); - $this->error($e->getMessage()); - } catch (PDOException $e) { - Db::rollback(); - $this->error($e->getMessage()); - } catch (Exception $e) { - Db::rollback(); - $this->error($e->getMessage()); - } - if ($result !== false) { - $this->success(); - } else { - $this->error(__('No rows were updated')); - } - } + if (false === $this->request->isPost()) { + $this->view->assign('row', $row); + return $this->view->fetch(); + } + $params = $this->request->post('row/a'); + if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } - $this->view->assign("row", $row); - return $this->view->fetch(); + $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()->validate($validate); + } + $result = $row->allowField(true)->save($params); + Db::commit(); + } catch (ValidateException|PDOException|Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); + } + if (false === $result) { + $this->error(__('No rows were updated')); + } + $this->success(); } /** * 删除 + * + * @param $ids + * @return void + * @throws DbException + * @throws DataNotFoundException + * @throws ModelNotFoundException */ - public function del($ids = "") + public function del($ids = null) { - if (!$this->request->isPost()) { + if (false === $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(); + $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 $e) { - Db::rollback(); - $this->error($e->getMessage()); - } catch (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 $item) { + $count += $item->delete(); } + Db::commit(); + } catch (PDOException|Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); + } + if ($count) { + $this->success(); } - $this->error(__('Parameter %s can not be empty', 'ids')); + $this->error(__('No rows were deleted')); } /** * 真实删除 + * + * @param $ids + * @return void */ - public function destroy($ids = "") + public function destroy($ids = null) { - if (!$this->request->isPost()) { + if (false === $this->request->isPost()) { $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')); + } $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 { $list = $this->model->onlyTrashed()->select(); - foreach ($list as $k => $v) { - $count += $v->delete(true); + foreach ($list as $item) { + $count += $item->delete(true); } Db::commit(); - } catch (PDOException $e) { - Db::rollback(); - $this->error($e->getMessage()); - } catch (Exception $e) { + } catch (PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($count) { $this->success(); - } else { - $this->error(__('No rows were deleted')); } - $this->error(__('Parameter %s can not be empty', 'ids')); + $this->error(__('No rows were deleted')); } /** * 还原 + * + * @param $ids + * @return void */ - public function restore($ids = "") + public function restore($ids = null) { - if (!$this->request->isPost()) { - $this->error(__("Invalid parameters")); + if (false === $this->request->isPost()) { + $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)) { @@ -284,14 +282,11 @@ trait Backend Db::startTrans(); try { $list = $this->model->onlyTrashed()->select(); - foreach ($list as $index => $item) { + foreach ($list as $item) { $count += $item->restore(); } Db::commit(); - } catch (PDOException $e) { - Db::rollback(); - $this->error($e->getMessage()); - } catch (Exception $e) { + } catch (PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } @@ -303,52 +298,56 @@ trait Backend /** * 批量更新 + * + * @param $ids + * @return void */ - public function multi($ids = "") + public function multi($ids = null) { - if (!$this->request->isPost()) { - $this->error(__("Invalid parameters")); + if (false === $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 $e) { - Db::rollback(); - $this->error($e->getMessage()); - } catch (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)) { + $this->error(__('Parameter %s can not be empty', 'ids')); + } + + if (false === $this->request->has('params')) { + $this->error(__('No rows were updated')); + } + 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 $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(); } - $this->error(__('Parameter %s can not be empty', 'ids')); + $this->error(__('No rows were updated')); } /** * 导入 + * + * @return void + * @throws PDOException + * @throws BindParamException */ protected function import() { @@ -368,12 +367,12 @@ 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"); $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']); - if ($encoding != 'utf-8') { + if ($encoding !== 'utf-8') { $line = mb_convert_encoding($line, 'utf-8', $encoding); } if ($n == 0 || preg_match('/^".*"$/', $line)) { diff --git a/application/api/lang/zh-cn/user.php b/application/api/lang/zh-cn/user.php index eb40897fd690de4e212c091fad3546ae49b5f716..e98e4cd050ecd766c783a4b3d96b8a06d7b25b53 100644 --- a/application/api/lang/zh-cn/user.php +++ b/application/api/lang/zh-cn/user.php @@ -7,7 +7,7 @@ return [ 'Sign up successful' => '注册成功', 'Username can not be empty' => '用户名不能为空', 'Username must be 3 to 30 characters' => '用户名必须3-30个字符', - 'Username must be 6 to 30 characters' => '用户名必须3-30个字符', + 'Username must be 6 to 30 characters' => '用户名必须6-30个字符', 'Password can not be empty' => '密码不能为空', 'Password must be 6 to 30 characters' => '密码必须6-30个字符', 'Mobile is incorrect' => '手机格式不正确', diff --git a/application/index/view/common/captcha.html b/application/index/view/common/captcha.html index 8db2df91337abdc670244509b98477d047df0026..d424e70fc29506116993e67ac655bc6a15356338 100644 --- a/application/index/view/common/captcha.html +++ b/application/index/view/common/captcha.html @@ -1,11 +1,11 @@ {if "[type]" == 'email'} - + 发送验证码 {elseif "[type]" == 'mobile'/} - + 发送验证码 diff --git a/application/index/view/user/login.html b/application/index/view/user/login.html index eb3ecfb7dc5563077aeb28a26e0d5f9a735f08bd..f75237b83e75f83f21bae4f50998d168952d8574 100755 --- a/application/index/view/user/login.html +++ b/application/index/view/user/login.html @@ -69,7 +69,7 @@