Ejemplo n.º 1
0
 public function loginAction()
 {
     $this->view->setVar('pageTitle', '登录');
     if ($this->request->isPost() == true) {
         if (!$this->validFlag) {
             echo json_encode(array('ret' => 0, 'msg' => $this->warnMsg));
             $this->view->disable();
             return;
         } else {
             $user = new Users();
             $res = $user->login($this->_sanReq['mobile'], $this->_sanReq['passwd']);
             if ($res == 1) {
                 $casTime = 0;
                 if ($this->_sanReq['autoLogin']) {
                     $time = $_SERVER['REQUEST_TIME'];
                     if ($user->updateUserLoginTime($this->_sanReq['mobile'], $time)) {
                         $val = base64_encode(substr(md5($this->_sanReq['mobile']), 8, 20) . ':' . base64_encode($this->session->get('uid')) . ':' . base64_encode($time) . ':' . ($time + 14 * 86400));
                         setcookie(substr(md5($this->di['sysconfig']['siteUrl']), 5, 15), $val, $time + 14 * 86400, '/');
                         $casTime = $time + 14 * 86400;
                     }
                 }
                 // cas  start
                 $cas = new CAServer();
                 if ($cas->casSave($this->_sanReq['siteId'], $casTime)) {
                     // 登录成功,保存用户资料至redis
                     $cas->setRedisUserInfo();
                     $backurl = $this->_sanReq['backurl'];
                     $backurl .= !empty($this->_sanReq['auth']) ? '?ticket=' . $st . '&backurl=' . urlencode('http://' . ltrim($backurl, 'http://')) : '';
                     echo json_encode(array('ret' => 1, 'backurl' => $backurl));
                 } else {
                     $this->session->destroy();
                     $key = substr(md5($this->di['sysconfig']['siteUrl']), 5, 15);
                     setcookie($key, '', $_SERVER['REQUEST_TIME'] - 3600, '/');
                     return;
                 }
             } elseif ($res == 10004) {
                 echo json_encode(array('ret' => 0, 'msg' => array('mobile' => array('msg' => $this->di['sysconfig']['flagMsg']['10004']))));
             } elseif ($res == 10010) {
                 echo json_encode(array('ret' => 0, 'msg' => array('passwd' => array('msg' => $this->di['sysconfig']['flagMsg']['10010']))));
             }
             exit;
         }
     } else {
         $viewVars = array();
         // cas  start
         if ($this->request->getQuery('siteid')) {
             $siteId = $this->request->getQuery('siteid');
             $backurl = $this->request->getQuery('backurl');
             $cas = new CAServer();
             $tgc = $cas->getCookieTGC();
             if ($tgc) {
                 // 存在tgc,重新生成st并存入redis
                 $st = $cas->getST($siteId);
                 $tgt = $tgc['tgt'];
                 $RedisLib = new \Ucenter\Utils\RedisLib($this->di);
                 $redis = $RedisLib::getRedis();
                 $resRedis = $redis->setex($st, 86400, $tgt);
                 if ($resRedis && $backurl) {
                     // 跳转
                     $urlParse = parse_url($backurl);
                     $com = isset($urlParse['query']) ? '&' : '?';
                     if (isset($urlParse['query'])) {
                         $fragment = isset($urlParse['fragment']) ? '#' . $urlParse['fragment'] : '';
                         $baseUrl = $urlParse['scheme'] . '://' . $urlParse['host'] . ':' . $urlParse['port'] . $urlParse['path'] . '?' . urlencode($urlParse['query'] . $fragment);
                         $com = '&';
                     } else {
                         $baseUrl = $backurl;
                         $com = '?';
                     }
                     $backurl = $baseUrl . $com . 'ticket=' . $st . '&backurl=' . urlencode($backurl);
                     $this->response->redirect($backurl);
                     return;
                 }
             } else {
                 // 处理存在uid,但cas tgc不存在
                 if ($this->session->get('uid')) {
                     $cas->casSave($siteId);
                 }
             }
             $viewVars['siteId'] = $siteId;
         }
         // cas end
         $viewVars['backurl'] = $this->request->getQuery('backurl') ?: '';
         $viewVars['auth'] = $this->request->getQuery('auth') ?: '';
         if (empty($this->session->get('uid'))) {
             $key = substr(md5($this->di['sysconfig']['siteUrl']), 5, 15);
             if ($this->cookies->has($key)) {
                 $val = explode(':', base64_decode($this->cookies->get($key)));
                 $uid = base64_decode($val[1]);
                 $lastTime = base64_decode($val[2]);
                 $keepTime = $val[3];
                 $nowTime = $_SERVER['REQUEST_TIME'];
                 $user = new Users();
                 $userInfo = $user->getUserInfo($uid);
                 if ($lastTime == $userInfo['u_last_logintime'] && $val[0] === substr(md5($userInfo['u_mobi']), 8, 20)) {
                     if ($user->updateUserLoginTime($userInfo['u_mobi'], $nowTime)) {
                         $val = base64_encode(substr(md5($userInfo['u_mobi']), 8, 20) . ':' . base64_encode($uid) . ':' . base64_encode($nowTime) . ':' . $keepTime);
                         setcookie(substr(md5($this->di['sysconfig']['siteUrl']), 5, 15), $val, $keepTime, '/');
                     }
                     $this->session->set('uid', $userInfo['u_id']);
                     $this->session->set('uinfo', array('name' => $userInfo['u_name'], 'mobile' => $userInfo['u_mobi']));
                     $this->response->redirect('user/index');
                 }
             }
         } else {
             $this->response->redirect('user/index');
         }
         $this->view->setVars($viewVars);
     }
 }