Login.php 6.9 KB
<?php

use Action\AbstractAction;
use LibModels\Wap\Passport\LoginData;
use LibModels\Wap\Passport\RegData;
use Plugin\Helpers;
use Plugin\Partner\Factory;

/**
 * 登录的控制器
 */
class LoginController extends AbstractAction
{
    /**
     * 登录页
     */
    public function indexAction()
    {
        $this->setTitle('登录');
        
        $data = array(
            'loginIndex' => true,
            'backUrl' => '/',
            'showHeaderImg' => true,
            'isPassportPage' => true,
            'registerUrl' => '/reg.html',
            'interationalUrl' => '/login.html',
            'phoneRetriveUrl' => '',
            'emailRetriveUrl' => '',
        );

        $this->_view->display('index', $data);
    }

    /**
     * 国际账号登录页
     */
    public function interationalAction()
    {
        $this->setTitle('国际账号登录');
        
        $data = array();
        $data['loginInterational'] = true;
        $data['backUrl'] = '/';
        $data['headerText'] = '登录';
        $data['isPassportPage'] = true;
        $data['countryCode'] = '+86';
        
        // 获取地区数据列表
        $area = RegData::getAreasData();
        // 有数据
        if (!empty($area['data'])) {
            $build = array();
            foreach ($area['data'] as $value) {
                $build = array();
                $build['areaCode'] = '+' . $value['area'];
                $build['selected'] = $value['area'] === '86';
                $build['name'] = $value['name'];
                $data['countrys'][] = $build;
            }
        } 
        // 没数据
        else {
            $data['countrys'][0] = array();
            $data['countrys'][0]['areaCode'] = '+86';
            $data['countrys'][0]['selected'] = true;
            $data['countrys'][0]['name'] = '中国';
        }

        $this->_view->display('interational', $data);
        
        $data = array();
        $area = array();
    }
    
    /**
     * 登录操作
     * 
     * @param string area 地区编号, 不需要+号
     * @param string profile 账号(邮箱或手机号)
     * @param string password 密码
     * @return json
     */
    public function authAction()
    {
        $data = array('code' => 400, 'message' => '账号或密码不正确', 'data' => '');
        
        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }
            
            /* 判断参数是否传递 */
            $area = $this->post('area', '86');
            $profile = $this->post('profile');
            $password = $this->post('password');
            if (!is_numeric($area) || empty($profile) || empty($password)) {
                break;
            }
            
            /* 判断参数是否有效 */
            $verifyEmail = Helpers::verifyEmail($profile);
            $verifyMobile = ($area === '86') ? Helpers::verifyMobile($profile) 
                                             : Helpers::verifyAreaMobile($profile);
            if (!$verifyEmail && !$verifyMobile) {
                break;
            }
            
            /* 调用登录接口进行登录 */
            $data = LoginData::signin($area, $profile, $password);
            
        } while (false);
        
        $this->echoJson($data);
    }

    /**
     * 支付宝账号登录:授权页面
     */
    public function alipayAction()
    {
        $redirect = $this->_request->getServer('HTTP_REFERER', '');
        if($redirect != '')
        {
            $this->setCookie('alipay_redirect', $redirect);
        }
        Factory::create('alipay')->getAuthorizeUrl();

        exit();
    }


    /**
     * QQ账号登录:授权页面
     */
    public function qqAction()
    {
        $redirect = $this->_request->getServer('HTTP_REFERER', '');
        if($redirect != '')
        {
            $this->setCookie('qq_redirect', $redirect);
        }
        Factory::create('qqconnect')->getAuthorizeUrl();

        exit();
    }


    /**
     * 新浪微博账号登录:授权页面
     */
    public function sinaAction()
    {
        $redirect = $this->_request->getServer('HTTP_REFERER', '');
        if($redirect != '')
        {
            $this->setCookie('sina_redirect', $redirect);
        }
        header('Location:' . Factory::create('sinaweibo')->getAuthorizeUrl());

        exit();
    }

    /**
     * 支付宝账号登录:回调方法
     */
    public function alipaycallbackAction()
    {
        $nickname = '';
        $alipay = Factory::create('alipay');
        $access = $alipay->getAccessToken();

        if (!isset($_GET['real_name']))
        {
            /* 获取支付宝用户的详细信息 */
            $userInfo = $alipay->getUserInfo($access);
            if ($userInfo && $userInfo['is_success'] === 'T' && isset($userInfo['response']['user_info']['user_name']))
            {
                $nickname = $userInfo['response']['user_info']['user_name'];
                $alipayEmail = $userInfo['response']['user_info']['email'];
            }
            var_dump($userInfo);
        }
        else
        {
            $nickname = $_GET['real_name'];
            $alipayEmail = isset($_GET['email']) ? $_GET['email'] : '';
        }
        var_dump($access);

        $result = LoginData::signinByOpenID($nickname, $access['user_id'], 'qq');

        if($result['code'] == 200)
        {
            $redirect = $this->_request->getCookie('alipay_redirect');
            $redirect && $this->redirect($redirect);
        }

        $this->redirect('/');
    }

    /**
     * QQ账号登录:回调方法
     */
    public function qqcallbackAction()
    {
        $qqconnect = Factory::create('qqconnect');
        $access = $qqconnect->getAccessToken();
        /* 获取QQ腾讯用户的详细信息 */
        $partnerInfo = $qqconnect->getUserInfo($access);

        if ($partnerInfo && is_array($partnerInfo))
        {
            $result = LoginData::signinByOpenID($partnerInfo['nickname'], $access['openid'], 'qq');

            if($result['code'] == 200)
            {
                $redirect = $this->_request->getCookie('qq_redirect');
                $redirect && $this->redirect($redirect);
            }
        }

        $this->redirect('/');
    }

    /**
     * 新浪微博账号登录:回调方法
     */
    public function sinacallbackAction()
    {
        $sina = Factory::create('sina');
        $access = $sina->getAccessToken();
        /* 获取QQ腾讯用户的详细信息 */
        $partnerInfo = $sina->getUserInfo($access);

        if ($partnerInfo && is_array($partnerInfo))
        {
            $result = LoginData::signinByOpenID($partnerInfo['screen_name'], $access['uid'], 'sina');

            if($result['code'] == 200)
            {
                $redirect = $this->_request->getCookie('sina_redirect');
                $redirect && $this->redirect($redirect);
            }
        } 

        $this->redirect('/');  
    }
}