public function loginAction()
{
if ($this->request->isPost()) {
$user_name = $this->request->get('user_name');
$password = $this->request->get('password');
$code = $this->request->get('code');
$this->captcha->verify($code);
if ($this->request->has('remember_me')) {
$this->cookies->set('user_name', $user_name, strtotime('2 year'));
} else {
$this->cookies->delete('user_name');
}
$admin = Admin::findFirstByAdminName($user_name);
if (!$admin || !$this->password->verify($password, $admin->password, $admin->salt)) {
return $this->response->setJsonContent(['code' => __LINE__, 'error' => 'account or password is wrong.']);
}
$udid = $this->cookies->has('udid') ? $this->cookies->get('udid') : '';
if (strlen($udid) !== 16) {
$udid = $this->random->getBase(16);
$this->cookies->set('udid', $udid, strtotime('5 year'), '/');
}
$adminLogin = new AdminLogin();
$adminLogin->admin_id = $admin->admin_id;
$adminLogin->ip = $this->request->getClientAddress();
$adminLogin->udid = $udid;
$adminLogin->user_agent = $this->request->getUserAgent();
$adminLogin->login_time = time();
$adminLogin->logout_time = 0;
$adminLogin->create();
$this->session->set('admin_auth', ['userId' => $admin->admin_id, 'userName' => $admin->admin_name]);
$this->session->set('login_id', $adminLogin->login_id);
return $this->response->setJsonContent(['code' => 0, 'error' => '']);
} else {
$this->view->setVar('redirect', $this->request->get('redirect', null, '/'));
$this->view->setVar('user_name', $this->cookies->has('user_name') ? $this->cookies->get('user_name') : '');
}
}