From 273b2f1e0589cc6fc07363ba293e4766cb3aaa8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E6=B8=85=E7=88=BD?= Date: Thu, 17 Apr 2025 15:00:58 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B6=85=E7=BA=A7=E6=80=BB=E7=AE=A1=20-=20?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E8=BF=94=E5=B7=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/model/Administrator.php | 19 +++ .../application/superadmin/config/route.php | 2 +- .../controller/auth/AuthLoginController.php | 130 ++++++++++++++++++ .../company/CreateCompanyController.php | 7 +- 4 files changed, 153 insertions(+), 5 deletions(-) create mode 100644 Server/application/common/model/Administrator.php create mode 100644 Server/application/superadmin/controller/auth/AuthLoginController.php diff --git a/Server/application/common/model/Administrator.php b/Server/application/common/model/Administrator.php new file mode 100644 index 00000000..c6fb0618 --- /dev/null +++ b/Server/application/common/model/Administrator.php @@ -0,0 +1,19 @@ +id . '|' . $admin->account . 'cunkebao_admin_secret'); + } + + /** + * 数据验证 + * + * @param array $params + * @return $this + * @throws \Exception + */ + protected function dataValidate(array $params): self + { + $validate = Validate::make([ + 'account' => 'require|/\S+/', + 'password' => 'require|/\S+/', + ]); + + if (!$validate->check($params)) { + throw new \Exception($validate->getError(), 400); + } + + return $this; + } + + + /** + * @param array $params + * @return object|AdministratorModel + * @throws \Exception + */ + protected function getAdministrator(array $params): AdministratorModel + { + extract($params); + + $admin = AdministratorModel::where(['account' => $account])->find(); + + if (!$admin || + $admin->password !== $password || + $admin->deleteTime + ) { + throw new \Exception('账号不存在或密码错误', 404); + } + + if (!$admin->status) { + throw new \Exception('账号已禁用', 404); + } + + return $admin; + } + + /** + * 更新登录信息 + * + * @param AdministratorModel $admin + * @return void + */ + protected function saveLoginInfo(AdministratorModel $admin): void + { + $admin->lastLoginTime = time(); + $admin->lastLoginIp = $this->request->ip(); + + if (!$admin->save()) { + throw new \Exception('拒绝登录', 403); + } + } + + /** + * 设置登录Cookie,有效期24小时 + * + * @param AdministratorModel $admin + * @return void + */ + protected function setCookie(AdministratorModel $admin): void + { + cookie('admin_id', $admin->id, 86400); + cookie('admin_token', $this->createToken($admin), 86400); + } + + /** + * 管理员登录 + * + * @return \think\response\Json + */ + public function index() + { + try { + $params = $this->request->only(['account', 'password']); + + $admin = $this->dataValidate($params)->getAdministrator($params); + + $this->saveLoginInfo($admin); + $this->setCookie($admin); + + return json([ + 'code' => 200, + 'msg' => '登录成功', + 'data' => [ + 'id' => $admin->id, + 'name' => $admin->name, + 'account' => $admin->account, + 'token' => cookie('admin_token') + ] + ]); + } catch (\Exception $e) { + return json([ + 'code' => $e->getCode(), + 'msg' => $e->getMessage() + ]); + } + } +} \ No newline at end of file diff --git a/Server/application/superadmin/controller/company/CreateCompanyController.php b/Server/application/superadmin/controller/company/CreateCompanyController.php index 39a96d09..3b1931e6 100644 --- a/Server/application/superadmin/controller/company/CreateCompanyController.php +++ b/Server/application/superadmin/controller/company/CreateCompanyController.php @@ -74,10 +74,11 @@ class CreateCompanyController extends BaseController /** * 数据验证 * + * @param array $params * @return $this * @throws \Exception */ - protected function dataValidate(): self + protected function dataValidate(array $params): self { $validate = Validate::make([ 'name' => 'require|max:50|/\S+/', @@ -88,7 +89,7 @@ class CreateCompanyController extends BaseController 'description' => 'require|/\S+/', ]); - if (!$validate->check($this->request->post())) { + if (!$validate->check($params)) { throw new \Exception($validate->getError(), 400); } @@ -181,8 +182,6 @@ class CreateCompanyController extends BaseController try { $params = $this->request->only(['name', 'nickname', 'account', 'password', 'realName', 'description']); - var_dump($params); - die; $department = $this->dataValidate($params)->creatS2About($params); Db::startTrans();