Register.php 8.53 KB
<?php
use Action\WebAction;
use LibModels\Web\Passport\RegData;
use WebPlugin\Helpers;
use WebPlugin\Cache;
use Passport\PassportModel;
use LibModels\Wap\Passport\BindData;
class RegisterController extends WebAction
{    
    /**
     * 注册页
     */
    public function indexAction()
    {
        $refer = $this->get('refer', SITE_MAIN);
        $cover = PassportModel::getLeftBanner(PassportModel::REGISTER_LEFT_BANNER_CODE);
        $this->setSession('effective_time', time() + 1800);
        $data = array(
            'registerPage' => true,
            'simpleHeader' => PassportModel::getSimpleHeader(),
            'passport' => array(
                'actionUrl' => Helpers::url('/passport/register/mobileregister'),
                'region' => RegData::getAreasData(),
                'location' => '+86',
                'captchaUrl' => Helpers::url('/passport/images?t='.time()),
                'itemUrl' => Helpers::url('/help/?category_id=9'),
                'referUrl' => $refer,
                'loginUrl' => Helpers::url('/signin.html?refer='.$refer),
                'coverHref' => $cover['url'],
                'coverImg' => $cover['img'],
                'regBtnText' => '立即注册'
            )
        );
        $this->setCookie('refer', $refer);
        $this->_view->display('index', $data);
    }


    /**
     * 检测手机号
     * 
     * @return json
     */
    public function checkmobileAction()
    {
        $mobile = trim($this->post('mobile'), ' ');
        $area = intval(trim($this->post('area'), ' '));
        $ip = Helpers::getClientIp();
        $data = array('code' => 400, 'message' => '', 'data' => '');
        $ipKey = md5('ip_checkmobile_' . $ip);
        $ipTimeKey = md5('ip_checkmobile_time_' . $ip);
        PassportModel::increment($ipKey,$ipTimeKey,1, 0, 3600);
        $ipTimes = Cache::get($ipKey);
        do{
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }
            /* 判断参数是否合法 */
            if (!is_numeric($mobile) || !is_numeric($area)) {
                $data['message'] = '手机号码格式不正确';
                break;
            }
            if ($ipTimes > 500) {
                $data['message'] = '由于你IP受限无法注册';
                break;
            }
            $user = PassportModel::getUserInfoByMobile($area, $mobile);
            if(!empty($user)) {
               $data['message'] = '手机号码已经存在';
               break;
            }
            $data['code'] = 200;
        } 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(){
        $mobile = trim($this->post('mobile'), ' ');
        $area = intval(trim($this->post('area'), ' '));
        $verifyCode = strtolower(trim($this->post('verifyCode'),' '));
        $data = array('code' => 400, 'message' => '', 'data' => '');
        do
        {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }
            /* 判断参数是否合法 */
            if (!is_numeric($mobile) || !is_numeric($area)) {
                $data['code'] = 400;
                $data['message'] = '手机号码格式不正确';
                break;
            }
            //检测验证码不正确
            if(!PassportModel::verifyCode($verifyCode)) {
                $data['code'] = 400;
                $data['message'] = '图形验证码不正确';
                break;
            }
            //发送代码
            $sendCodeKey = md5('send_code_' . $area . '_' . $mobile);
            $sendCodeTimeKey = md5('send_code_time_' . $area . '_' . $mobile);
            PassportModel::increment($sendCodeKey,$sendCodeTimeKey, 1, 0, 3600);
            $sendCodeTimes = Cache::get($sendCodeKey);
            if ($sendCodeTimes > 50) {
                $data['message'] = '发送验证码太多';
                break;
            }
            /* 向手机发送注册验证码 */
            $data = RegData::sendCodeToMobile($area, $mobile);
            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()
    {
        $mobile = trim($this->post('mobile'), ' ');//mobile
        $area = intval(trim($this->post('area'), ' '));//area
        $verifyCode = strtolower(trim($this->post('verifyCode'),' '));//图形验证码
        $code = trim($this->post('code'),'');//手机验证码
        $password = $this->post('password');
        $data = array('code' => 400, 'message' => '', 'data' => '');
        do
        {
            /* 判断参数是否合法 */
            if (!is_numeric($mobile) || !is_numeric($area)) {
                $data['message'] = '手机号码格式不正确';
                break;
            }
            //检测验证码不正确
            if(!PassportModel::verifyCode($verifyCode)) {
                $data['message'] = '验证码不正确';
                break;
            }
            /* 判断密码是否符合规则 */
            if (!Helpers::verifyPassword($password)) {
                $data['message'] = '密码不正确';
                break;
            }
            $effective_time = $this->getSession('effective_time');
            if($effective_time < time() || empty($effective_time))
            {
                $data['message'] = '注册超时';
                break;
            }
            /* 验证注册的标识码是否有效 */
            $data = RegData::validMobileCode($area, $mobile, $code);
            if (!isset($data['code']) || $data['code'] != 200) {
                $data['message'] = '验证码错误';
                break;
            }
            /*手机注册*/
            $data = RegData::regMobile($area, $mobile, $password);
            if (!isset($data['code']) || $data['code'] != 200) {
                break;
            }
            /* 返回跳转到来源页面 */
            $refer = $this->getCookie('refer');
            if (empty($refer)) {
                $refer = SITE_MAIN;
            } else {
                $refer = rawurldecode($refer);
            }
            $data['code'] = 200;
            $data['data']['href'] = $refer;
            $data['data']['session'] = Helpers::syncUserSession($data['data']['uid']);
            $token = Helpers::makeToken($data['data']['uid']);
            $this->setCookie('_TOKEN', $token);
            $this->setSession('_TOKEN', $token);
            $this->setSession('_LOGIN_UID', $data['data']['uid']);
        }while(false);
        echo $this->echoJson($data);
    }
    
}