Bind.php 10.2 KB
<?php

use Action\AbstractAction;
use LibModels\Wap\Passport\BindData;
use LibModels\Wap\Passport\RegData;
use Plugin\Helpers;

/**
 * 登录的控制器
 */
class BindController extends AbstractAction
{

    /**
     * 登录到bind页面
     */
    public function indexAction()
    {
        $refer = $this->get('refer');
        if (!empty($refer)) {
            $this->setCookie('refer', $refer);
        }

        $this->setTitle('绑定手机号');

        $openId = $this->get('openId');
        $sourceType = $this->get('sourceType');
        $nickname = $this->get('nickname');
        $data = array(
            'bindIndex' => true, //js标识
            'backUrl' => Helpers::url('/signin.html'), // 返回的URL链接
            'showHeaderImg' => true, // 控制显示头部图片
            'isPassportPage' => true, // 模板中模块标识
            'sourceType' => $sourceType, // 第三方登录来源
            'platform' => $sourceType,
            'openId' => $openId, // openId
            'areaCode' => '+86', //默认区号
            'countrys' => RegData::getAreasData(), //国别码
            'nickname' => $nickname, //昵称
        );

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

    /**
     * 手机验证码页面
     */
    public function codeAction()
    {
        $this->setTitle('验证手机');

        $openId = $this->get('openId');
        $sourceType = $this->get('sourceType');
        $nickname = $this->get('nickname');
        $areaCode = $this->get('areaCode', '86');
        $isReg = $this->get('isReg');
        $phoneNum = $this->get('phoneNum');

        $data = array(
            'bindCode' => true, //js标识
            'backUrl' => Helpers::url('/signin.html'), // 返回的URL链接
            'showHeaderImg' => true, // 控制显示头部图片
            'isPassportPage' => true, // 模板中模块标识
            'sourceType' => $sourceType, // 第三方登录来源
            'openId' => $openId, // openId
            'nickname' => $nickname, //昵称
            'isReg' => $isReg, //是否是已注册过的手机号
            'areaCode' => $areaCode, //国别码
            'phoneNum' => $phoneNum, //手机号码
        );

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

    /**
     * 设置登录密码页面
     */
    public function passwordAction()
    {
        $this->setTitle('重新设置登录密码');
        $openId = $this->get('openId');
        $sourceType = $this->get('sourceType');
        $nickname = $this->get('nickname');
        $areaCode = $this->get('areaCode', '86');
        $phoneNum = $this->get('phoneNum');
        $data = array(
            'bindPwd' => true, //js标识
            'backUrl' => Helpers::url('/signin.html'), // 返回的URL链接
            'showHeaderImg' => true, // 控制显示头部图片
            'isPassportPage' => true, // 模板中模块标识
            'sourceType' => $sourceType, // 第三方登录来源
            'openId' => $openId, // openId
            'nickname' => $nickname, //昵称
            'areaCode' => $areaCode, //国别码
            'phoneNum' => $phoneNum   //国别码
        );

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

    /**
     * 绑定成功页面
     */
    public function successAction()
    {
        $this->setTitle('重新设置登录密码');
        $data = array(
            'isPassportPage' => true, // 模板中模块标识
            'successTip' => '恭喜,您的手机号码已经关联成功。<br/>该手机号码不能用来登陆此账户,您可以选择继续使用微信登录', //提示文字
            'goUrl' => Helpers::url('/'), // 返回的到关联流程前的浏览页面
        );

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

    /**
     * 绑定前手机号校验
     */
    public function bindCheckAction()
    {
        $data = array('code' => 400, 'message' => '', 'data' => '');

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

            $phoneNum = $this->post('phoneNum');
            $openId = $this->post('openId');
            $areaCode = $this->post('areaCode', '86');
            $sourceType = $this->post('sourceType');
            $nickname = $this->post('nickname');

            if (!is_numeric($phoneNum) || !$openId || !$areaCode || !$sourceType) {
                break;
            }

            $res = BindData::bindCheck($phoneNum, $openId, $sourceType, $areaCode);
            if (!isset($res['code'])) {
                break;
            }

            if ($res['code'] == 200) {
                $next = Helpers::url('/passport/bind/code', array(
                    'isReg' => $res['data']['is_register'],
                    'openId' => $openId,
                    'sourceType' => $sourceType,
                    'areaCode' => $areaCode,
                    'phoneNum' => $phoneNum,
                    'nickname' => $nickname,
                ));
                $data = array('code' => $res['code'], 'message' => $res['message'], 'data' => array('isReg' => $res['data']['is_register'], 'next' => $next));
            } else {
                $data = array('code' => $res['code'], 'message' => $res['message'], 'data' => isset($res['data']) ? $res['data'] : '');
            }
        } while (false);

        $this->echoJson($data);
    }

    /**
     * 发送验证码
     */
    public function sendBindMsgAction()
    {
        $data = array('code' => 400, 'message' => '', 'data' => '');

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

            $phoneNum = $this->post('phoneNum');
            $areaCode = $this->post('areaCode');

            if (!is_numeric($phoneNum) || !$areaCode) {
                break;
            }

            $data = BindData::sendBindMsg($areaCode, $phoneNum);
            if (!isset($data['code'])) {
                break;
            }
        } while (false);

        $this->echoJson($data);
    }

    /**
     * 校验短信验证码
     */
    public function checkBindMsgAction()
    {
        $data = array('code' => 400, 'message' => '', 'data' => '');

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

            $phoneNum = $this->post('phoneNum');
            $code = $this->post('code');
            $areaCode = $this->post('areaCode');

            if (!is_numeric($phoneNum) || !$code || !$areaCode) {
                break;
            }

            $data = BindData::checkBindCode($areaCode, $phoneNum, $code);
            if (!isset($data['code'])) {
                break;
            }
        } while (false);

        $this->echoJson($data);
    }

    /**
     * 绑定手机号
     */
    public function bindMobileAction()
    {
        $data = array('code' => 400, 'message' => '', 'data' => '');

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

            $phoneNum = $this->post('phoneNum');
            $openId = $this->post('openId');
            $areaCode = $this->post('areaCode', '86');
            $sourceType = $this->post('sourceType');
//            $nickname = $this->post('nickname');//nickname不同步信息
            $password = $this->post('password');
            $password = !empty($password) ? $password : '';

            if (!is_numeric($phoneNum) || !$openId || !$sourceType || !$areaCode) {
                break;
            }

            $res = BindData::bindMobile($openId, $sourceType, $phoneNum, $areaCode, $password);
            if (!isset($res['code'])) {
                break;
            }

            //绑定成功,跳转页面
            $refer = $this->getCookie('refer');
            if (empty($refer)) {
                $refer = SITE_MAIN . '/?go=1';
            } else {
                $refer = rawurldecode($refer);
            }

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

        $this->echoJson($data);
    }

    /**
     * 换绑check
     */
    public function changeCheckAction()
    {
        $data = array('code' => 400, 'message' => '', 'data' => '');

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

            $phoneNum = $this->post('phoneNum');
            $areaCode = $this->post('areaCode');

            if (!is_numeric($phoneNum) || !$areaCode) {
                break;
            }

            $data = BindData::changeCheck($phoneNum, $areaCode);
            if (!isset($data['code'])) {
                break;
            }
        } while (false);

        $this->echoJson($data);
    }

    /**
     * 换绑mobile
     */
    public function changeMobileAction()
    {
        $data = array('code' => 400, 'message' => '', 'data' => '');

        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }
            $uid = $this->getUid(true);
            $phoneNum = $this->post('phoneNum');
            $areaCode = $this->post('areaCode');
            $code = $this->post('code');

            if (!is_numeric($phoneNum) || !$areaCode || !$code || !$uid) {
                break;
            }

            $data = BindData::changeMobile($uid, $phoneNum, $areaCode, $code);
            if (!isset($data['code'])) {
                break;
            }
        } while (false);

        $this->echoJson($data);
    }

}