Login.php 10.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()
    {
        // 设置登录有效时间30分钟, 防机器刷,cache不稳定,改为cookie
        $this->setCookie('LE' . md5('_LOGIN_EXPIRE'), time() + 1800);
//        $this->setSession('_LOGIN_EXPIRE', time() + 1800);
        // 清除客户端
        $this->setCookie('_UID', '');
        $this->setCookie('_TOKEN', '');

        $refer = $this->get('refer');
        if (!empty($refer)) {
            $this->setCookie('refer', $refer);
        }

        $this->setTitle('登录');

        $data = array(
            'loginIndex' => true, // 模板中使用JS的标识
            'backUrl' => 'javascript:history.go(-1)', // 返回的URL链接
            'showHeaderImg' => true, // 控制显示头部图片
            'isPassportPage' => true, // 模板中模块标识
            'registerUrl' => '/reg.html', // 注册的URL链接
            'aliLoginUrl' => '/passport/login/alipay', // 支付宝快捷登录的URL链接
            'weiboLoginUrl' => '/passport/login/sina', // 微博登录的URL链接
            'qqLoginUrl' => '/passport/login/qq', // 腾讯QQ登录的URL链接
            'internationalUrl' => '/login.html', // 国际号登录的URL链接
            'phoneRetriveUrl' => '/passport/back/mobile', // 通过手机号找回密码的URL链接
            'emailRetriveUrl' => '/passport/back/email', // 通过邮箱找回密码的URL链接
        );

        // 渲染模板
        $this->_view->display('index', $data);
    }

    /**
     * 国际账号登录页
     */
    public function internationalAction()
    {
        // 设置登录有效时间30分钟, 防机器刷,cache不稳定,改为cookie
        $this->setCookie('LE' . md5('_LOGIN_EXPIRE'), time() + 1800);
//        $this->setSession('_LOGIN_EXPIRE', time() + 1800);

        $refer = $this->get('refer');
        if (!empty($refer)) {
            $this->setCookie('refer', $refer);
        }

        $this->setTitle('国际账号登录');

        $data = array();
        $data['loginInternational'] = true; // 模板中使用JS的标识
        $data['backUrl'] = 'javascript:history.go(-1)'; // 返回的URL链接
        $data['headerText'] = '登录'; // 头部信息
        $data['isPassportPage'] = true; // 模板中模块标识
        $data['areaCode'] = '+86'; // 默认区号
        $data['countrys'] = RegData::getAreasData(); // 地区信息列表
        // 渲染模板
        $this->_view->display('international', $data);
    }

    /**
     * 退出
     */
    public function outAction()
    {
        // 清除服务端会话
        $this->setSession('_TOKEN', '');
        $this->setSession('_LOGIN_UID', '');

        // 清除客户端
        $this->setCookie('_UID', '');
        $this->setCookie('_TOKEN', '');
        $this->setCookie('_SPK', '');

        $refer = $this->server('HTTP_REFERER', SITE_MAIN);
        $token = $this->get('token');
        if (!empty($token)) {
            $this->go(Helpers::logoutSession($token, $refer));
        }

        $this->go($refer);
    }

    /**
     * 登录操作
     * 
     * @param string areaCode 地区编号, 不需要+号
     * @param string account 账号(邮箱或手机号)
     * @param string password 密码
     * @return json
     */
    public function authAction()
    {
        $data = array('code' => 400, 'message' => '账号或密码不正确', 'data' => '');

        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }

            /* 判断参数是否传递 */
            $area = $this->post('areaCode', '86');
            $profile = $this->post('account');
            $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, $area);
            if (!$verifyEmail && !$verifyMobile) {
                break;
            }

            /* 设置登录有效时间30分钟, 防机器刷 */
            $expire = $this->getCookie('LE' . md5('_LOGIN_EXPIRE'));
//            $expire = $this->getSession('_LOGIN_EXPIRE');
            if (empty($expire) || $expire < time()) {
                $data = array('code' => 400, 'message' => '页面停留时间过长,请刷新页面', 'data' => '');
                break;
            }

            /* 调用登录接口进行登录 */
            // 获取未登录时的唯一识别码
            $shoppingKey = Helpers::getShoppingKeyByCookie();
            $data = LoginData::signin($area, $profile, $password, $shoppingKey);
            if (!isset($data['code']) || $data['code'] != 200 || !isset($data['data']['uid'])) {
                break;
            }

            $refer = $this->getCookie('refer');
            if (empty($refer)) {
                $refer = SITE_MAIN . '/?go=1';
            }
            else {
                $refer = rawurldecode($refer);
            }
            $data['data']['session'] = Helpers::syncUserSession($data['data']['uid']);
            $data['data']['href'] = $refer;

            $token = Helpers::makeToken($data['data']['uid']);
            $this->setCookie('_TOKEN', $token);
            $this->setSession('_TOKEN', $token);
            $this->setSession('_LOGIN_UID', $data['data']['uid']);
        }
        while (false);

        $this->echoJson($data);
    }

    /**
     * 支付宝账号登录:授权页面
     */
    public function alipayAction()
    {
        $this->setSession('_TOKEN', '');

        Factory::create('alipay')->getAuthorizeUrl();

        exit();
    }

    /**
     * QQ账号登录:授权页面
     */
    public function qqAction()
    {
        $this->setSession('_TOKEN', '');

        Factory::create('qqconnect')->getAuthorizeUrl();

        exit();
    }

    /**
     * 新浪微博账号登录:授权页面
     */
    public function sinaAction()
    {
        $this->setSession('_TOKEN', '');

        $this->go(Factory::create('sinaweibo')->getAuthorizeUrl());
    }

    /**
     * 支付宝账号登录:回调方法
     */
    public function alipaycallbackAction()
    {
        $realName = $this->_request->get('real_name');
        $email = $this->_request->get('email');
        $userId = $this->_request->get('user_id');

        $result = array();
        if (isset($realName, $email, $userId)) {
            $shoppingKey = Helpers::getShoppingKeyByCookie();
            $result = LoginData::signinByOpenID($realName, $userId, 'alipay', $shoppingKey);
        }

        //判定是否需要绑定手机号
        if (isset($result['data']['is_bind']) && $result['data']['is_bind'] == 'N') {
            $this->go(Helpers::url('/passport/bind/index', array('openId' => $userId, 'sourceType' => 'alipay'))); //'nickname' => $realName
        }

        $refer = $this->getCookie('refer');
        if (empty($refer)) {
            $refer = SITE_MAIN . '/?go=1';
        }
        else {
            $refer = rawurldecode($refer);
        }

        if (isset($result['code']) && $result['code'] == 200 && !empty($result['data']['uid'])) {
            $token = Helpers::makeToken($result['data']['uid']);
            $this->setSession('_TOKEN', $token);
            $this->setSession('_LOGIN_UID', $result['data']['uid']);
            $this->setCookie('_TOKEN', $token);
            $this->go(Helpers::syncUserSession($result['data']['uid'], $refer));
        }
        else {
            $this->go($refer);
        }
    }

    /**
     * QQ账号登录:回调方法
     */
    public function qqcallbackAction()
    {
        $qqconnect = Factory::create('qqconnect');
        $access = $qqconnect->getAccessToken();
        /* 获取QQ腾讯用户的详细信息 */
        $partnerInfo = $qqconnect->getUserInfo($access);
        $result = array();
        if (!empty($partnerInfo) && isset($partnerInfo['nickname'])) {
            $shoppingKey = Helpers::getShoppingKeyByCookie();
            $result = LoginData::signinByOpenID($partnerInfo['nickname'], $access['openid'], 'qq', $shoppingKey);
        }

        //判定是否需要绑定手机号
        if (isset($result['data']['is_bind']) && $result['data']['is_bind'] == 'N') {
            $this->go(Helpers::url('/passport/bind/index', array('openId' => $access['openid'], 'sourceType' => 'qq'))); //'nickname' => $partnerInfo['nickname']
        }

        $refer = $this->getCookie('refer');
        if (empty($refer)) {
            $refer = SITE_MAIN . '/?go=1';
        }
        else {
            $refer = rawurldecode($refer);
        }

        if (isset($result['code']) && $result['code'] == 200 && !empty($result['data']['uid'])) {
            $token = Helpers::makeToken($result['data']['uid']);
            $this->setSession('_TOKEN', $token);
            $this->setSession('_LOGIN_UID', $result['data']['uid']);
            $this->setCookie('_TOKEN', $token);
            $this->go(Helpers::syncUserSession($result['data']['uid'], $refer));
        }
        else {
            $this->go($refer);
        }
    }

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

        $result = array();
        if ($partnerInfo && is_array($partnerInfo)) {
            $shoppingKey = Helpers::getShoppingKeyByCookie();
            $result = LoginData::signinByOpenID($partnerInfo['screen_name'], $access['uid'], 'sina', $shoppingKey);
        }
        //判定是否需要绑定手机号
        if (isset($result['data']['is_bind']) && $result['data']['is_bind'] == 'N') {
            $this->go(Helpers::url('/passport/bind/index', array('openId' => $access['uid'], 'sourceType' => 'sina'))); //'nickname' => $partnerInfo['screen_name']
        }

        $refer = $this->getCookie('refer');
        if (empty($refer)) {
            $refer = SITE_MAIN . '/?go=1';
        }
        else {
            $refer = rawurldecode($refer);
        }

        if (isset($result['code']) && $result['code'] == 200 && !empty($result['data']['uid'])) {
            $token = Helpers::makeToken($result['data']['uid']);
            $this->setSession('_TOKEN', $token);
            $this->setSession('_LOGIN_UID', $result['data']['uid']);
            $this->setCookie('_TOKEN', $token);
            $this->go(Helpers::syncUserSession($result['data']['uid'], $refer));
        }
        else {
            $this->go($refer);
        }
    }

}