Register.php 11.1 KB
<?php

use Action\WebAction;
use LibModels\Web\Passport\RegData;
use WebPlugin\Helpers;
use WebPlugin\Cache;
use Passport\PassportModel;
use LibModels\Web\Passport\BindData;

/**
 * 注册
 */
class RegisterController extends WebAction
{

    /**
     * 注册页
     */
    public function indexAction()
    {
        $this->setTitle('新用户注册', true, ' | ');
        $this->setSimpleHeader();
        $this->setSession('effective_time', time() + 1800);

        $referUrl = $this->get('refer', '');
        $referCookie = $this->getCookie('refer', '');
        $refer = (!$referUrl) ? $referCookie : $referUrl;
        $refer = empty($refer) ? SITE_MAIN : rawurldecode($refer);

        $cover = PassportModel::getLeftBanner(PassportModel::REGISTER_LEFT_BANNER_CODE);
        $data = array(
            'registerPage' => true,
            'passport' => array(
                'actionUrl' => Helpers::url('/passport/register/mobileregister'),
                'region' => RegData::getAreasData(),
                'location' => '+86',
                'captchaUrl' => Helpers::url('/passport/images', array('t' => time())),
                'itemUrl' => Helpers::url('/help/', array('category_id' => 9)),
                'referUrl' => Helpers::getUrlSafe($refer),
                'loginUrl' => Helpers::url('/signin.html', array('refer' => rawurlencode($refer))),
                'coverHref' => Helpers::getUrlSafe($cover['url']),
                'coverImg' => Helpers::getUrlSafe($cover['img']),
                'regBtnText' => '立即注册'
            )
        );

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

    /**
     * 注册成功页
     */
    public function successAction()
    {
        $this->setTitle('注册成功', true, ' | ');
        $this->setSimpleHeader();
        $goUrl = $this->get('next', SITE_MAIN);
        $goShoppingUrl = $this->get('goShoppingUrl',SITE_MAIN);
        $cover = PassportModel::getLeftBanner(PassportModel::REGISTER_LEFT_BANNER_CODE);
        $data = array(
            'registerPage' => true,
            'passport' => array(
                'goUrl' => $goUrl,
                'goShoppong' => $goShoppingUrl,
                'coverHref' => $cover['url'],
                'coverImg' => $cover['img'],
            ),
            'uid' => $this->getUid(false),
        );
        $this->_view->display('success', $data);
    }

    /**
     * 检测手机号
     * 
     * @return json
     */
    public function checkmobileAction()
    {
        $data = array('code' => 400, 'message' => '', 'data' => '');

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

            /* 判断参数是否合法 */
            $mobile = trim($this->post('mobile'));
            $area = trim($this->post('area'));
            if (!is_numeric($mobile) || !is_numeric($area)) {
                $data['message'] = '手机号码格式不正确';
                break;
            }

            /* 判断手机号是否检查超过指定次数 */
            $makeMobile = Helpers::makeMobile($area, $mobile);
            $key = 'checkmobilenum_' . $makeMobile;
            $checkNum = intval(Cache::get($key));
            if ($checkNum > 500) {
                $data['message'] = '检查次数太多';
                break;
            }
            Cache::set($key, ++$checkNum);
            /* 判断用户是否存在 */
            $area = intval($area);
            $user = PassportModel::getUserInfoByMobile($area, $mobile);
            if (!empty($user)) {
                $data['message'] = '手机号码已经存在';
                break;
            }

            $data['code'] = 200;
            $this->setSession('checkmobile_' . $makeMobile, $makeMobile);
        }
        while (false);

        $this->echoJson($data);
    }

    /**
     * 校验图形验证码
     * 
     * @return json
     */
    public function piccaptchaAction()
    {
        $data = array('code' => 400, 'message' => '', 'data' => '');

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

            $verifyCode = trim($this->post('verifyCode'));
            $picFlag = PassportModel::verifyCode($verifyCode);
            if ($picFlag) {
                $data = array('code' => 200, 'message' => '验证码正确', 'data' => '');
            }
            else {
                $data = array('code' => 400, 'message' => '验证码错误', 'data' => '');
            }
        }
        while (false);

        $this->echoJson($data);
    }

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

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

            $mobile = trim($this->post('mobile'));
            $area = trim($this->post('area'));
            // 判断参数是否合法
            if (!is_numeric($mobile) || !is_numeric($area)) {
                $data['code'] = 400;
                $data['message'] = '手机号码格式不正确';
                break;
            }

            //校验是否发送过多
            $sendCodeKey = 'send_code_' . $area . '_' . $mobile;
            $sendCodeTimes = Cache::get($sendCodeKey);
            if (!$sendCodeTimes) {
                $sendCodeTimes = 0;
            }
            else {
                $sendCodeTimes = intval($sendCodeTimes);
            }

            if ($sendCodeTimes >= 10) {
                $data['message'] = '您已多次提交验证码,请尽快联系客服解决';
                break;
            }

            //检测验证码不正确
            $verifyCode = strtolower(trim($this->post('verifyCode')));
            if (!PassportModel::verifyCode($verifyCode)) {
                $data['code'] = 400;
                $data['message'] = '图形验证码不正确';
                break;
            }

            //检查是否检查过
            $makeMobile = Helpers::makeMobile($area, $mobile);
            if ($this->getSession('checkmobile_' . $makeMobile) != $makeMobile) {
                $data['code'] = 400;
                $data['message'] = '发送失败';
                break;
            }
            if ($sendCodeTimes >= 5) {
                $data['message'] = '您收到的验证码短信已超过本日限定最多次数,请您耐心等待';
            }
            /* 向手机发送注册验证码 */
            $data = RegData::sendCodeToMobile($area, $mobile);
            Cache::set($sendCodeKey, $sendCodeTimes + 1, 3600);
            if (!isset($data['code'])) {
                break;
            }
        }
        while (false);

        $this->echoJson($data);
    }

    /**
     * 验证短信码是否正确
     */
    public function msgcaptchaAction()
    {
        $data = array('code' => 400, 'message' => '', 'data' => '');

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

            $phoneNum = trim($this->post('mobile'));
            $areaCode = trim($this->post('area'));
            $code = trim($this->post('code')); //短信验证码
            //校验手机号格式
            if (!is_numeric($phoneNum) || !is_numeric($areaCode)) {
                $data['code'] = 401;
                $data['message'] = '手机号码格式不正确';
                break;
            }

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

        $this->echoJson($data);
    }

    /**
     * 手机注册
     */
    public function mobileregisterAction()
    {
        $data = array('code' => 400, 'message' => '', 'data' => '');

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

            /* 判断参数是否合法 */
            $mobile = trim($this->post('mobile'));
            $area = trim($this->post('area'));
            if (!is_numeric($mobile) || !is_numeric($area)) {
                $data['message'] = '手机号码格式不正确';
                break;
            }

            /* 判断是否是有效的注册方式,防注册机刷 */
            $effectiveTime = $this->getSession('effective_time');
            if ($effectiveTime < time() || empty($effectiveTime)) {
                $data['message'] = '注册超时';
                break;
            }

            //检测验证码不正确
            $verifyCode = strtolower(trim($this->post('verifyCode'))); //图形验证码
            if (!PassportModel::verifyCode($verifyCode)) {
                $data['message'] = '验证码不正确';
                break;
            }

            /* 判断密码是否符合规则 */
            $code = trim($this->post('code')); //手机验证码
            $password = $this->post('password');
            if (!Helpers::verifyPassword($password)) {
                $data['message'] = '密码不正确';
                break;
            }

            /* IP仅允许点击注册500次/时 */
            $ip = Helpers::getClientIp();
            $ipKey = 'ip_register_' . $ip;
            $ipTimes = Cache::get($ipKey);
            if (!$ipTimes) {
                $ipTimes = 0;
            }
            else {
                $ipTimes = intval($ipTimes);
            }
            if ($ipTimes >= 500) {
                $data['message'] = '由于你IP受限无法注册';
                break;
            }

            /* 验证注册的标识码是否有效 */
            $data = RegData::validMobileCode($area, $mobile, $code);
            if (!isset($data['code']) || $data['code'] != 200) {
                $data['message'] = '验证码错误';
                break;
            }

            /* 手机注册: 调用注册接口,ip限制计数 */
            $data = RegData::regMobile($area, $mobile, $password);
            Cache::set($ipKey, $ipTimes + 1, 3600);
            if (!isset($data['code']) || $data['code'] != 200) {
                break;
            }

            /* 返回跳转到来源页面,passport跳转至首页 */
            $refer = rawurldecode($this->getCookie('refer'));
            $isRedirectHome = PassportModel::redirectHome($refer);
            if (empty($refer) || $isRedirectHome) {
                $refer = SITE_MAIN;
            }
            $data['code'] = 200;
            $this->syncUserSession($data['data']['uid']);
            $data['data']['href'] = Helpers::url('/passport/register/success', array('next' => $refer, 'goShoppingUrl' => SITE_MAIN));
        }
        while (false);

        $this->echoJson($data);
    }

}