管理员登录

This commit is contained in:
2025-03-21 11:42:40 +08:00
parent 1933a616a5
commit 89958a3f7d
3 changed files with 64 additions and 19 deletions

View File

@ -5,6 +5,7 @@ namespace plugin\inspection\controller\api;
use plugin\inspection\model\InspectionStaff; use plugin\inspection\model\InspectionStaff;
use plugin\inspection\model\InspectionStaffToken; use plugin\inspection\model\InspectionStaffToken;
use think\admin\Controller; use think\admin\Controller;
use think\admin\model\SystemUser;
use think\exception\HttpResponseException; use think\exception\HttpResponseException;
class Auth extends Controller class Auth extends Controller
@ -27,12 +28,22 @@ class Auth extends Controller
} }
if (empty($token)) $this->error('需要登录授权', [], 401); 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)) { if (empty($tokenInfo)) {
$this->error('无效的登录令牌', [], 401); $this->error('无效的登录令牌', [], 401);
} }
$this->tokenInfo = $tokenInfo; $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) { } catch (HttpResponseException $exception) {
throw $exception; throw $exception;
} catch (\Exception $exception) { } catch (\Exception $exception) {

View File

@ -3,7 +3,9 @@
namespace plugin\inspection\controller\api; namespace plugin\inspection\controller\api;
use plugin\inspection\model\InspectionStaff; use plugin\inspection\model\InspectionStaff;
use plugin\inspection\model\InspectionStaffToken;
use think\admin\Controller; use think\admin\Controller;
use think\admin\model\SystemUser;
class Login extends Controller class Login extends Controller
{ {
@ -14,27 +16,50 @@ class Login extends Controller
$where = $this->_vali([ $where = $this->_vali([
'phone.require' => '手机号码不能为空', 'phone.require' => '手机号码不能为空',
]); ]);
$this->staff = InspectionStaff::mk()->where($where)->find();
} else { } else {
$where = $this->_vali([ $where = $this->_vali([
'account.require' => '登录账号不能为空', '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" => $password] = $this->_vali([
'password.require' => '登录密码不能为空', 'password.require' => '登录密码不能为空',
]); ]);
if ($this->staff->password !== $password) { if ($staff->isEmpty()) {
$this->error('密码错误'); // 可能是后台用户登录
$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(),
]);
} }
} }

View File

@ -10,10 +10,19 @@ class Staff extends Auth
if (!$this->tokenInfo) { if (!$this->tokenInfo) {
$this->error('请重新登录', [], 401); $this->error('请重新登录', [], 401);
} }
$this->success('登录成功', [ if ($this->tokenInfo->is_admin != 1) {
'token' => $this->tokenInfo->token, $this->success('登录成功', [
'user' => $this->staff '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() { public function logout() {