From 89958a3f7d29a31558b718ba772cc892cdc68ea6 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Fri, 21 Mar 2025 11:42:40 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=91=98=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/controller/api/Auth.php | 15 +++++- .../src/controller/api/Login.php | 51 ++++++++++++++----- .../src/controller/api/auth/Staff.php | 17 +++++-- 3 files changed, 64 insertions(+), 19 deletions(-) diff --git a/plugs/think-plugs-inspection/src/controller/api/Auth.php b/plugs/think-plugs-inspection/src/controller/api/Auth.php index 5ad55e5..790d7f0 100644 --- a/plugs/think-plugs-inspection/src/controller/api/Auth.php +++ b/plugs/think-plugs-inspection/src/controller/api/Auth.php @@ -5,6 +5,7 @@ namespace plugin\inspection\controller\api; use plugin\inspection\model\InspectionStaff; use plugin\inspection\model\InspectionStaffToken; use think\admin\Controller; +use think\admin\model\SystemUser; use think\exception\HttpResponseException; class Auth extends Controller @@ -27,12 +28,22 @@ class Auth extends Controller } if (empty($token)) $this->error('需要登录授权', [], 401); // 读取用户账号数据 - $tokenInfo = InspectionStaffToken::query()->where('token', "=", $token)->with("staff")->find(); + $tokenInfo = InspectionStaffToken::query()->where('token', "=", $token)->find(); if (empty($tokenInfo)) { $this->error('无效的登录令牌', [], 401); } $this->tokenInfo = $tokenInfo; - $this->staff = $tokenInfo->staff; + if ($tokenInfo->is_admin != 1) { + $this->staff = $tokenInfo->staff; + if (!$this->staff || $this->staff->isEmpty()) { + $this->error('无效的登录令牌', [], 401); + } + } else { + $this->user = SystemUser::query()->findOrEmpty($tokenInfo->staff_id); + if ($this->user->isEmpty()) { + $this->error('无效的登录令牌', [], 401); + } + } } catch (HttpResponseException $exception) { throw $exception; } catch (\Exception $exception) { diff --git a/plugs/think-plugs-inspection/src/controller/api/Login.php b/plugs/think-plugs-inspection/src/controller/api/Login.php index eec6018..b511833 100644 --- a/plugs/think-plugs-inspection/src/controller/api/Login.php +++ b/plugs/think-plugs-inspection/src/controller/api/Login.php @@ -3,7 +3,9 @@ namespace plugin\inspection\controller\api; use plugin\inspection\model\InspectionStaff; +use plugin\inspection\model\InspectionStaffToken; use think\admin\Controller; +use think\admin\model\SystemUser; class Login extends Controller { @@ -14,27 +16,50 @@ class Login extends Controller $where = $this->_vali([ 'phone.require' => '手机号码不能为空', ]); - $this->staff = InspectionStaff::mk()->where($where)->find(); } else { $where = $this->_vali([ 'account.require' => '登录账号不能为空', ]); - $this->staff = InspectionStaff::mk()->where($where)->find(); - } - if (empty($this->staff)) { - $this->error('用户不存在'); } + $staff = InspectionStaff::mk()->where($where)->findOrEmpty(); ["password" => $password] = $this->_vali([ 'password.require' => '登录密码不能为空', ]); - if ($this->staff->password !== $password) { - $this->error('密码错误'); + if ($staff->isEmpty()) { + // 可能是后台用户登录 + $map = ['username' => $where['phone'] ?? $where['account'], 'is_deleted' => 0]; + $user = SystemUser::mk()->where($map)->findOrEmpty(); + if ($user->isEmpty()) { + $this->error('用户不存在'); + } else { + if ($user->password !== md5($password)) { + $this->error('密码错误'); + } + $token = md5(uniqid()); + InspectionStaffToken::query()->where('is_admin', '=', 1)->where('staff_id', '=', $user->id)->delete(); + InspectionStaffToken::mk([ + 'staff_id' => $user->id, + 'token' => $token, + "is_admin" => 1, + ])->save(); + $this->success('登录成功', [ + 'is_admin'=> true, + 'token' => $token, + 'user' => $user->toArray(), + ]); + } + } else { + if ($staff->password !== $password) { + $this->error('密码错误'); + } + $this->staff->save(['last_login_at' => date('Y-m-d H:i:s')]); + $this->staff->tokens()->where('token', '<>', '')->delete(); + $this->success('登录成功', [ + 'is_admin' => false, + 'token' => $this->staff->tokens()->save(['token' => md5(uniqid())])->token, + 'user' => $this->staff->toArray(), + ]); } - $this->staff->save(['last_login_at' => date('Y-m-d H:i:s')]); - $this->staff->tokens()->where('token', '<>', '')->delete(); - $this->success('登录成功', [ - 'token' => $this->staff->tokens()->save(['token' => md5(uniqid())])->token, - 'user' => $this->staff->toArray(), - ]); + } } \ No newline at end of file diff --git a/plugs/think-plugs-inspection/src/controller/api/auth/Staff.php b/plugs/think-plugs-inspection/src/controller/api/auth/Staff.php index 5a18d73..99d1500 100644 --- a/plugs/think-plugs-inspection/src/controller/api/auth/Staff.php +++ b/plugs/think-plugs-inspection/src/controller/api/auth/Staff.php @@ -10,10 +10,19 @@ class Staff extends Auth if (!$this->tokenInfo) { $this->error('请重新登录', [], 401); } - $this->success('登录成功', [ - 'token' => $this->tokenInfo->token, - 'user' => $this->staff - ]); + if ($this->tokenInfo->is_admin != 1) { + $this->success('登录成功', [ + 'is_admin' => false, + 'token' => $this->tokenInfo->token, + 'user' => $this->staff + ]); + } else { + $this->success('登录成功', [ + 'is_admin' => true, + 'token' => $this->tokenInfo->token, + 'user' => $this->user + ]); + } } public function logout() {