Skip to content

Commit

Permalink
登录日志
Browse files Browse the repository at this point in the history
  • Loading branch information
Joyboo committed Apr 11, 2022
1 parent a51d5d2 commit 4099a5f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
47 changes: 21 additions & 26 deletions src/HttpController/Admin/PubTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
namespace WonderGame\EsUtility\HttpController\Admin;

use WonderGame\EsUtility\Common\Classes\CtxRequest;
use WonderGame\EsUtility\Common\Exception\HttpParamException;
use WonderGame\EsUtility\Common\Http\Code;
use WonderGame\EsUtility\Common\Languages\Dictionary;

trait PubTrait
{
protected function setBaseTraitProptected()
{
$this->modelName = null;
$this->modelName = 'Admin';
}

public function index()
Expand All @@ -24,39 +24,34 @@ public function login()
$array = $this->post;
if (!isset($array['username']))
{
throw new HttpParamException(Dictionary::ADMIN_PUBTRAIT_1);
return $this->error(Code::ERROR_OTHER, Dictionary::ADMIN_PUBTRAIT_1);
}

$model = model('Admin');
// 查询记录
$data = $model->where('username', $array['username'])->get();
$data = $this->Model->where('username', $array['username'])->get();

if ($data && password_verify($array['password'], $data['password']))
if (empty($data) || !password_verify($array['password'], $data['password']))
{
$data = $data->toArray();
return $this->error(Code::ERROR_OTHER, Dictionary::ADMIN_PUBTRAIT_4);
}

// 被锁定
if (empty($data['status']) && (!isSuper($data['rid'])))
{
throw new HttpParamException(Dictionary::ADMIN_PUBTRAIT_2);
}
$data = $data->toArray();

$request = CtxRequest::getInstance()->request;
// 被锁定
if (empty($data['status']) && (!isSuper($data['rid'])))
{
return $this->error(Code::ERROR_OTHER, Dictionary::ADMIN_PUBTRAIT_2);
}

// 记录登录日志
/** @var AdminLog $AdminLog */
$AdminLog = model('AdminLog');
$AdminLog->data([
'uid' => $data['id'],
'name' => $data['realname'] ?: $data['username'],
'ip' => ip($request),
])->save();
$request = CtxRequest::getInstance()->request;
$this->Model->signInLog([
'uid' => $data['id'],
'name' => $data['realname'] ?: $data['username'],
'ip' => ip($request),
]);

$token = get_login_token($data['id']);
$this->success(['token' => $token], Dictionary::ADMIN_PUBTRAIT_3);
} else {
$this->error(Code::ERROR_OTHER, Dictionary::ADMIN_PUBTRAIT_4);
}
$token = get_login_token($data['id']);
$this->success(['token' => $token], Dictionary::ADMIN_PUBTRAIT_3);
}

public function logout()
Expand Down
8 changes: 8 additions & 0 deletions src/Model/AdminModelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

trait AdminModelTrait
{
/**
* 保存登录日志,新项目将log表统一命名规则,自己实现记录日志的操作
* 实现示例: $model->data($data)->save();
* @param $data
* @return mixed
*/
abstract public function signInLog($data = []);

protected function setBaseTraitProptected()
{
$this->autoTimeStamp = true;
Expand Down

0 comments on commit 4099a5f

Please sign in to comment.