diff --git a/src/HttpController/Admin/PubTrait.php b/src/HttpController/Admin/PubTrait.php index 50fde1b..9c51709 100644 --- a/src/HttpController/Admin/PubTrait.php +++ b/src/HttpController/Admin/PubTrait.php @@ -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() @@ -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() diff --git a/src/Model/AdminModelTrait.php b/src/Model/AdminModelTrait.php index 670e809..5a301f3 100644 --- a/src/Model/AdminModelTrait.php +++ b/src/Model/AdminModelTrait.php @@ -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;