超级总管 - 登录返工
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace app\superadmin\controller\auth;
|
||||
|
||||
use app\superadmin\controller\AdministratorController;
|
||||
use app\common\model\Administrator as AdministratorModel;
|
||||
use think\Controller;
|
||||
use think\Db;
|
||||
use think\Validate;
|
||||
|
||||
class AuthLoginController extends Controller
|
||||
{
|
||||
/**
|
||||
* 创建登录令牌
|
||||
* @param AdministratorController $admin
|
||||
* @return string
|
||||
*/
|
||||
protected function createToken($admin): string
|
||||
{
|
||||
return md5($admin->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()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user