Back.php 12.3 KB
<?php

use Action\WebAction;
use LibModels\Web\Passport\RegData;
use Passport\PassportModel;
use WebPlugin\Helpers;
use LibModels\Web\Passport\BackData;
use WebPlugin\AuthCode;

/**
 * 找回密码
 */
class BackController extends WebAction
{

    /**
     * 找回密码
     */
    public function indexAction()
    {
        $this->setTitle('找回密码', true, ' | ');
        $this->setSimpleHeader();
        
        $banner = PassportModel::getLeftBanner(PassportModel::BACK_LFFT_BANNER_CODE);
        $data = array(
            'backPage' => true,
            'back' => array(
                'coverHref' => Helpers::getUrlSafe($banner ['url']),
                'coverImg' => Helpers::getUrlSafe($banner ['img']),
                'countryCode' => '86',
                'countryName' => '中国',
                'captchaUrl' => Helpers::url('/passport/images', array('t' => time())),
                'countryList' => RegData::getAreasData()
            ),
        );
        
        $this->_view->display('index', $data);
    }

    /**
     * 校验验证码
     */
    public function authcodeAction()
    {
        $data = array('code' => 400,'message' => '验证失败');
        
        do {
            // 判断是不是AJAX请求
            if (!$this->isAjax()) {
                break;
            }
            // 判断验证码是否有效
            $verifyCode = $this->post('verifyCode', '');
            if (!PassportModel::verifyCode($verifyCode)) {
                break;
            }
            $phoneNum = $this->post('phoneNum', '');
            if (Helpers::verifyEmail($phoneNum)) {
                $ret = PassportModel::getUserInfoByEmail($phoneNum);
                if (empty($ret)) {
                    $data['message'] = '您输入的邮件账户尚未注册!';
                    $data['code'] = 402;
                    break;
                }
            } 
            else {
                $area = intval($this->post('area', '86'));
                if (Helpers::verifyAreaMobile(Helpers::makeMobile($area, $phoneNum))) {
                    $ret = PassportModel::getUserInfoByMobile($area, $phoneNum);
                    if (empty($ret)) {
                        $data['message'] = '您输入的手机号码尚未注册!';
                        $data['code'] = 402;
                        break;
                    }
                } 
                else {
                    break; //其他方式   
                }
            }
            $data['code'] = 200;
            $data['message'] = '验证成功';
        }
        while (false);
        
        $this->echoJson($data);
    }

    /**
     * 邮箱
     */
    public function emailAction()
    {
        $verifyCode = $this->post('verifyCode', ''); //图形验证码
        
        if (PassportModel::verifyCode($verifyCode)) {
            $phoneNum = $this->post('phoneNum', '');
            $area = intval($this->post('area', '86'));
            if (Helpers::verifyEmail($phoneNum)) { // 验证邮箱
                $email = $phoneNum;
                $data = BackData::sendCodeToEmail($email);
                if (isset($data['code']) && $data['code'] == 200) {
                    $this->setSession('email', $email);
                    $this->go(Helpers::url('/passport/back/sendemail'));
                }
            } elseif (Helpers::verifyAreaMobile(Helpers::makeMobile($area, $phoneNum))) { // 验证手机号
                $mobile = $phoneNum;
                $data = BackData::sendCodeToMobile($mobile, $area);
                if (isset($data['code']) && $data['code'] == 200) {
                    $this->setSession('mobile', $mobile);
                    $this->setSession('area', $area);
                    $this->setSession('verifyCode', $verifyCode);
                    $this->go(Helpers::url('/passport/back/verification'));
                }
            }
        }
        
        //跳转
        $this->go(Helpers::url('/passport/back/index'));
    }

    /**
     * 发送找回手机号短信
     * 
     * @return json
     */
    public function sendbackmobileAction()
    {
        $data = array('code' => 400, 'message' => '验证失败');
                
        $mobile = $this->post('mobile', '');
        $area = intval($this->post('area', '86'));
        $verifyCode = $this->post('verifyCode', ''); //图形验证码
        // 验证手机号
        if (Helpers::verifyAreaMobile(Helpers::makeMobile($area, $mobile)) && PassportModel::verifyCode($verifyCode)) { 
            $data = BackData::sendCodeToMobile($mobile, $area);
        }
        
        $this->echoJson($data);
    }

    /**
     * 发送邮件页面
     */
    public function sendemailAction()
    {
        $email = $this->getSession('email');
        if (empty($email)) {
            $this->go(Helpers::url('/passport/back/index'));
        }
        $banner = PassportModel::getLeftBanner(PassportModel::BACK_LFFT_BANNER_CODE);
        $emailArr = explode('@', $email);
        $emailUrl = ''; //邮箱地址
        $emailUrls = array('yoho.cn' => 'http://smail.yoho.cn');
        if (in_array($emailArr[1], $emailUrls)) {
            $emailUrl = $emailUrls[$emailArr[1]]; //邮箱后缀
        } else {
            $emailUrl = 'http://mail.' . $emailArr[1];
        }
        $this->setSimpleHeader();
        $data = array(
            'sendEmail' => array(
                'coverHref' => $banner['url'],
                'coverImg' => $banner['img'],
                'countrys' => array(),
                'email' => $emailUrl
            )
        );
        $this->_view->display('send-email', $data);
    }

    /**
     * 重置密码页面
     */
    public function backcodeAction()
    {
        $code = $this->get('code'); //加密code兼容以前参数
        $info = $this->checkCode($code);
        $verifyState = false; //验证状态
        $ret = array();
        if (!empty($info)) {
            $verifyState = true;
            if (isset($info['uid'])) { //老版修改邮箱密码不处理
                $verifyState = false; //重新找回密码
            }
        } else {
            $ret = BackData::checkEmailCode($code);
            if (isset($ret['data']) && $ret['code'] == 200) {
                $verifyState = true;
            }
        }
        //验证失败
        if (!$verifyState) {
            $this->go(Helpers::url('/passport/back/index'));
        }
        
        $this->setTitle('重置密码');
        $this->setSimpleHeader();
        
        $banner = PassportModel::getLeftBanner(PassportModel::BACK_LFFT_BANNER_CODE);
        $data = array(
            'resetPage' => true,
            'resetPwd' => array(
                'coverHref' => $banner ['url'],
                'coverImg' => $banner ['img'],
                'countrys' => array(),
                'code' => $code
            )
        );
        $this->_view->display('reset-pwd', $data);
    }

    /**
     * 更新密码接口
     */
    public function updateAction()
    {
        $code = $this->post('code'); //加密code兼容以前参数
        $password = $this->post('pwd');
        $info = $this->checkCode($code);
        $ret = false;
        $successType = '';
        if (Helpers::verifyPassword($password)) {
            if (!empty($info)) { //手机号找回
                // 修改密码
                if (isset($info ['mobile'])) { // 手机号修改密码
                    $mobile = $info ['mobile'];
                    $token = $info ['token'];
                    $area = $info ['area'];
                    $data = BackData::modifyPasswordByMobile($mobile, $token, $password, $area);
                    if (isset($data['code']) && $data ['code'] == 200) {
                        $successType = 'mobile';
                        $ret = true;
                    }
                } else if (isset($info ['uid'])) { //其他方式修改密码,跳到找回密码首页
                    $ret = false;
                }
            } else { //新版邮箱修改接口
                $data = BackData::modifyPasswordByEmailCode($code, $password);
                if ($data ['code'] == 200) {
                    $successType = 'email';
                    $ret = true;
                }
            }
        }
        if ($ret) { //跳转到成功页
            $this->setSession('successType', $successType);
            $this->go(Helpers::url('/passport/back/resetsuccess'));
        } else {
            // 跳转错误页面
            $this->go(Helpers::url('/passport/back/index'));
        }
    }

    /**
     * 重置密码成功
     */
    public function resetsuccessAction()
    {
        $successType = $this->getSession('successType');
        //成功状态标识
        if (empty($successType)) {
            $this->go(Helpers::url('/passport/back/index'));
        }
        
        $this->setTitle('重置密码成功');
        $this->setSimpleHeader();
        $banner = PassportModel::getLeftBanner(PassportModel::BACK_LFFT_BANNER_CODE);
        $data = array(
            'resetSuccess' => array(
                'coverHref' => Helpers::getUrlSafe($banner ['url']),
                'coverImg' => Helpers::getUrlSafe($banner ['img']),
                'countrys' => array()
            )
        );
        //清除标识
        $this->setSession('successType', '');
        $this->_view->display('reset-success', $data);
    }

    /**
     * 手机验证页面
     */
    public function verificationAction()
    {
        $mobile = $this->getSession('mobile');
        $verifyCode = $this->getSession('verifyCode'); //图形验证码
        if (empty($mobile) || !PassportModel::verifyCode($verifyCode)) {
            $this->go(Helpers::url('/passport/back/index'));
        }
        $area = $this->getSession('area');
        
        $this->setTitle('手机验证');
        $this->setSimpleHeader();
        $banner = PassportModel::getLeftBanner(PassportModel::BACK_LFFT_BANNER_CODE);
        $data = array(
            'vertificationPage' => true,
            'verification' => array(
                'coverHref' => Helpers::getUrlSafe($banner ['url']),
                'coverImg' => Helpers::getUrlSafe($banner ['img']),
                'mobile' => $mobile,
                'area' => $area,
                'verifyCode' => $verifyCode,
                'countrys' => array()
            )
        );
        $this->_view->display('verification', $data);
    }

    /**
     * 手机找回密码验证
     */
    public function backmobileAction()
    {
        $data = array('code' => 400, 'message' => '验证码错误!', 'data' => Helpers::url('/passport/back/index'));
                
        $mobile = $this->post('mobile');
        $area = $this->post('area');
        $verifyCode = $this->post('verifyCode'); //图形验证码
        $code = $this->post('code'); //手机验证码
        if (!empty($code) && PassportModel::verifyCode($verifyCode) && $this->getSession('mobile') == $mobile && $this->getSession('area') == $area) {
            $result = BackData::validateMobileCode($mobile, $code, $area);
            if (isset($result['code']) && $result['code'] == 200) {
                $str = json_encode(array(
                    'mobile' => $mobile,
                    'area' => $area,
                    'token' => $result ['data'] ['token'],
                    'create_time' => time()
                ));
                $code = AuthCode::encode($str, PassportModel::BACK_FIND_SECRET_KEY);
                $data ['code'] = 200;
                $data['message'] = '验证成功';
                $data['data'] = Helpers::url('/passport/back/backcode', array('code' => base64_encode($code)));
            }
        }
        
        $this->echoJson($data);
    }

    /**
     * 检查code
     *
     * @param string $code            
     * @return boolean
     */
    private function checkCode($code)
    {
        $code = base64_decode($code);
        $info = json_decode(AuthCode::decode($code, PassportModel::BACK_FIND_SECRET_KEY), true);
        if ($info ['create_time'] < 1 || (time() - $info ['create_time']) > 86400) {
            return array();
        }
        return $info;
    }

}