示例#1
0
 public function doAction()
 {
     $this->forceInstall();
     $session = new USession($this);
     $sessionData = $session->getSessionData();
     //判断用户是否已登录
     if ($session->getUid() != 0) {
         header('Location: /');
         return;
     }
     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
         $this->showLoginForm();
         return;
     }
     $postData = new DataMap($_POST);
     $user = new UserModel($this);
     $errArr = array();
     $username = $postData->get('username', '');
     $pass = $postData->get('pass', '');
     $rcodePost = $postData->get('rcode', '');
     $rcode = $sessionData->get('rcode', '');
     $sessionData->set('rcode', '');
     $urlPost = $postData->get('url', '');
     $loginType = 1;
     if ($user->isUsername($username)) {
         $loginType = 1;
     } elseif ($user->isEmail($username)) {
         $loginType = 2;
     } else {
         $errArr[] = '请输入正确的用户名或者邮箱地址';
     }
     if (!$user->isPass($pass)) {
         $errArr[] = $user->getErrMsg();
     }
     if ($rcode == '') {
         $errArr[] = '请打开验证码图片显示';
     }
     if (strcasecmp($rcodePost, $rcode) != 0) {
         $errArr[] = '验证码输入有误';
     }
     if (!empty($errArr)) {
         $this->showLoginForm('', $errArr);
         return;
     }
     // 判断用户名是否存在
     if ($loginType == 1 && !$user->isUsernameExists($username)) {
         $errArr[] = '用户名' . $username . '不存在';
     }
     if ($loginType == 2 && !$user->isEmailExists($username)) {
         $errArr[] = '邮箱' . $username . '不存在';
     }
     if (!empty($errArr)) {
         $this->showLoginForm('', $errArr);
         return;
     }
     $uid = $user->authPass($username, $pass, $loginType);
     if ($uid == -1) {
         if ($loginType == 1) {
             $errArr[] = '用户名或密码错误';
         } elseif ($loginType == 2) {
             $errArr[] = '邮箱或密码错误';
         }
         $this->showLoginForm('', $errArr);
     } else {
         $session->setUid($uid);
         $session->updateLifetime(30 * 24 * 3600);
         $url = $postData->get('url', '');
         if (empty($url)) {
             $urlHandler = $this->getApp()->getUrlHandler();
             $url = $urlHandler->createUrl('web/Index', 'index', array(), false);
         }
         header('Location: ' . $url);
     }
 }