You've already forked guangan
管理员登录
This commit is contained in:
@ -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;
|
||||
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) {
|
||||
|
@ -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) {
|
||||
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(),
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -10,10 +10,19 @@ class Staff extends Auth
|
||||
if (!$this->tokenInfo) {
|
||||
$this->error('请重新登录', [], 401);
|
||||
}
|
||||
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() {
|
||||
|
Reference in New Issue
Block a user